Added CCombat and made CPhysics a bit cleaner

This commit is contained in:
Allexit 2015-05-10 18:30:57 +03:00
parent ed2b09fa57
commit be0669a754
5 changed files with 173 additions and 60 deletions

View File

@ -90,7 +90,7 @@ public class GladiatorBrawler extends ApplicationAdapter {
CPhysics physics = new CPhysics().setMovable(false).setGravityApplied(false).setDynamic(false) CPhysics physics = new CPhysics().setMovable(false).setGravityApplied(false).setDynamic(false)
.setSize(groundSprite.getRegionWidth() * Global.SPRITE_SCALE, .setSize(groundSprite.getRegionWidth() * Global.SPRITE_SCALE,
groundSprite.getRegionHeight() * Global.SPRITE_SCALE); groundSprite.getRegionHeight() * Global.SPRITE_SCALE);
physics.position.set(new Vector2(0, -4)); physics.getPosition().set(new Vector2(0, -4));
ground.add(physics); ground.add(physics);
Sprite wallSprite = SpriteLoader.loadSprite(Name.WALLIMG); Sprite wallSprite = SpriteLoader.loadSprite(Name.WALLIMG);
@ -100,7 +100,7 @@ public class GladiatorBrawler extends ApplicationAdapter {
CPhysics wall0Physics = new CPhysics().setMovable(false).setGravityApplied(false).setDynamic(false) CPhysics wall0Physics = new CPhysics().setMovable(false).setGravityApplied(false).setDynamic(false)
.setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE, .setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE,
wallSprite.getRegionHeight() * Global.SPRITE_SCALE); wallSprite.getRegionHeight() * Global.SPRITE_SCALE);
wall0Physics.position.set(new Vector2(6, 0)); wall0Physics.getPosition().set(new Vector2(6, 0));
wall0.add(wall0RenderedObject); wall0.add(wall0RenderedObject);
wall0.add(wall0Physics); wall0.add(wall0Physics);
@ -109,7 +109,7 @@ public class GladiatorBrawler extends ApplicationAdapter {
CPhysics wall1Physics = new CPhysics().setMovable(false).setGravityApplied(false).setDynamic(false) CPhysics wall1Physics = new CPhysics().setMovable(false).setGravityApplied(false).setDynamic(false)
.setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE, .setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE,
wallSprite.getRegionHeight() * Global.SPRITE_SCALE); wallSprite.getRegionHeight() * Global.SPRITE_SCALE);
wall1Physics.position.set(new Vector2(-6, 0)); wall1Physics.getPosition().set(new Vector2(-6, 0));
wall1.add(wall1RenderedObject); wall1.add(wall1RenderedObject);
wall1.add(wall1Physics); wall1.add(wall1Physics);

View File

@ -0,0 +1,60 @@
package com.saltosion.gladiator.components;
import com.badlogic.ashley.core.Component;
public class CCombat extends Component {
private int health = 0;
private int maxHealth = 0;
private int damage = 0;
public boolean swinging = false;
private float swingCdCounter = 0;
private float swingCd = 0;
public int getHealth() {
return this.health;
}
public int getMaxHealth() {
return this.maxHealth;
}
public int getDamage() {
float minDmg = damage*0.9f;
float maxDmg = damage*1.1f;
int randomdamage = (int) (Math.random()*(maxDmg-minDmg)+minDmg);
if (Math.random() > 0.7) {
randomdamage *= 1.5;
}
return randomdamage;
}
public float getSwingCDCounter() {
return this.swingCdCounter;
}
public float getSwingCD() {
return this.swingCd;
}
public CCombat setBaseDamage(int basedmg) {
this.damage = basedmg;
return this;
}
public CCombat setCurrHealth(int health) {
this.health = health;
return this;
}
public CCombat setMaxHealth(int maxHealth) {
this.maxHealth = maxHealth;
return this;
}
public CCombat setSwingCD(float cd) {
this.swingCd = cd;
return this;
}
}

View File

@ -6,17 +6,17 @@ import com.saltosion.gladiator.physics.CollisionListener;
public class CPhysics extends Component { public class CPhysics extends Component {
public Vector2 position = new Vector2(); private Vector2 position = new Vector2();
public Vector2 velocity = new Vector2(); private Vector2 velocity = new Vector2();
public Vector2 size = new Vector2(); private Vector2 size = new Vector2();
public float movespeed = 5f, jumpForce = 0.5f, gravity = 1f; private float movespeed = 5f, jumpForce = 0.5f, gravity = 1f;
public boolean grounded = true; private CollisionListener collisionListener = null;
public CollisionListener collisionListener = null;
public boolean movable = true; private boolean movable = true;
public boolean gravityApplied = true; private boolean gravityApplied = true;
public boolean dynamic = true; private boolean dynamic = true;
public boolean ghost = false; private boolean ghost = false;
private boolean grounded = true;
// Movement (/input) vars // Movement (/input) vars
public boolean movingLeft = false; public boolean movingLeft = false;
@ -75,4 +75,57 @@ public class CPhysics extends Component {
return this; return this;
} }
public CPhysics setGrounded(boolean grounded) {
this.grounded = grounded;
return this;
}
public Vector2 getPosition() {
return this.position;
}
public Vector2 getVelocity() {
return this.velocity;
}
public Vector2 getSize() {
return this.size;
}
public float getMovespeed() {
return this.movespeed;
}
public float getJumpForce() {
return this.jumpForce;
}
public float getGravity() {
return this.gravity;
}
public CollisionListener getCollisionListener() {
return this.collisionListener;
}
public boolean isMovable() {
return this.movable;
}
public boolean isGravityApplied() {
return this.gravityApplied;
}
public boolean isDynamic() {
return this.dynamic;
}
public boolean isGhost() {
return this.ghost;
}
public boolean isGrounded() {
return this.grounded;
}
} }

View File

@ -31,10 +31,10 @@ public class PhysicsSystem extends EntitySystem {
CPhysics obj = pm.get(entities.get(i)); CPhysics obj = pm.get(entities.get(i));
// Apply movement // Apply movement
obj.position.add(obj.velocity); obj.getPosition().add(obj.getVelocity());
// Movement // Movement
if (obj.movable) { if (obj.isMovable()) {
float move = 0; float move = 0;
if (obj.movingLeft) { if (obj.movingLeft) {
move--; move--;
@ -42,22 +42,22 @@ public class PhysicsSystem extends EntitySystem {
if (obj.movingRight) { if (obj.movingRight) {
move++; move++;
} }
obj.velocity.x = move * obj.movespeed * deltaTime; obj.getVelocity().x = move * obj.getMovespeed() * deltaTime;
if (obj.jumping && obj.grounded) { if (obj.jumping && obj.isGrounded()) {
obj.grounded = false; obj.setGrounded(false);
obj.velocity.y = obj.jumpForce; obj.getVelocity().y = obj.getJumpForce();
} }
} }
// Gravity // Gravity
if (obj.gravityApplied) { if (obj.isGravityApplied()) {
obj.velocity.y -= obj.gravity * deltaTime; obj.getVelocity().y -= obj.getGravity() * deltaTime;
} }
obj.velocity.y = Math.max(Math.min(obj.velocity.y, MAX_VEL), -MAX_VEL); obj.getVelocity().y = Math.max(Math.min(obj.getVelocity().y, MAX_VEL), -MAX_VEL);
// Collisions // Collisions
if (obj.dynamic) { if (obj.isDynamic()) {
for (int j = 0; j < entities.size(); j++) { for (int j = 0; j < entities.size(); j++) {
if (i == j) { if (i == j) {
continue; continue;
@ -72,14 +72,14 @@ public class PhysicsSystem extends EntitySystem {
CPhysics cp0 = pm.get(entity0); CPhysics cp0 = pm.get(entity0);
CPhysics cp1 = pm.get(entity1); CPhysics cp1 = pm.get(entity1);
float x00 = cp0.position.x - cp0.size.x / 2; float x00 = cp0.getPosition().x - cp0.getSize().x / 2;
float x01 = cp0.position.x + cp0.size.x / 2; float x01 = cp0.getPosition().x + cp0.getSize().x / 2;
float x10 = cp1.position.x - cp1.size.x / 2; float x10 = cp1.getPosition().x - cp1.getSize().x / 2;
float x11 = cp1.position.x + cp1.size.x / 2; float x11 = cp1.getPosition().x + cp1.getSize().x / 2;
float y00 = cp0.position.y - cp0.size.y / 2; float y00 = cp0.getPosition().y - cp0.getSize().y / 2;
float y01 = cp0.position.y + cp0.size.y / 2; float y01 = cp0.getPosition().y + cp0.getSize().y / 2;
float y10 = cp1.position.y - cp1.size.y / 2; float y10 = cp1.getPosition().y - cp1.getSize().y / 2;
float y11 = cp1.position.y + cp1.size.y / 2; float y11 = cp1.getPosition().y + cp1.getSize().y / 2;
boolean colliding = (x00 < x11) && (x01 > x10) && (y00 < y11) && (y01 > y10); boolean colliding = (x00 < x11) && (x01 > x10) && (y00 < y11) && (y01 > y10);
@ -87,58 +87,58 @@ public class PhysicsSystem extends EntitySystem {
return; return;
} }
if (x00 <= x11 && Math.abs(x00 - x11) < (cp0.size.x + cp1.size.x) / 16) { if (x00 <= x11 && Math.abs(x00 - x11) < (cp0.getSize().x + cp1.getSize().x) / 16) {
// cp0's left side is colliding with cp1's right side // cp0's left side is colliding with cp1's right side
if (!cp0.ghost && !cp1.ghost) { if (!cp0.isGhost() && !cp1.isGhost()) {
if (cp0.velocity.x < 0) { if (cp0.getVelocity().x < 0) {
// cp0 is going left, stop // cp0 is going left, stop
cp0.velocity.x = 0; cp0.getVelocity().x = 0;
} }
} }
if (cp0.collisionListener != null) { if (cp0.getCollisionListener() != null) {
cp0.collisionListener.collision(CollisionSide.LEFT, entity0, entity1); cp0.getCollisionListener().collision(CollisionSide.LEFT, entity0, entity1);
} }
} }
if (x01 > x10 && Math.abs(x01 - x10) < (cp0.size.x + cp1.size.x) / 16) { if (x01 > x10 && Math.abs(x01 - x10) < (cp0.getSize().x + cp1.getSize().x) / 16) {
// cp0's right side is colliding with cp1's left side // cp0's right side is colliding with cp1's left side
if (!cp0.ghost && !cp1.ghost) { if (!cp0.isGhost() && !cp1.isGhost()) {
if (cp0.velocity.x > 0) { if (cp0.getVelocity().x > 0) {
// cp0 is going right, stop // cp0 is going right, stop
cp0.velocity.x = 0; cp0.getVelocity().x = 0;
} }
} }
if (cp0.collisionListener != null) { if (cp0.getCollisionListener() != null) {
cp0.collisionListener.collision(CollisionSide.RIGHT, entity0, entity1); cp0.getCollisionListener().collision(CollisionSide.RIGHT, entity0, entity1);
} }
} }
if (y00 <= y11 && Math.abs(y00 - y11) < (cp0.size.y + cp1.size.y) / 16) { if (y00 <= y11 && Math.abs(y00 - y11) < (cp0.getSize().y + cp1.getSize().y) / 16) {
// cp0's bottom side is colliding with cp1's top side // cp0's bottom side is colliding with cp1's top side
if (!cp0.ghost && !cp1.ghost) { if (!cp0.isGhost() && !cp1.isGhost()) {
if (cp0.velocity.y < 0) { if (cp0.getVelocity().y < 0) {
// cp0 is going down, stop // cp0 is going down, stop
cp0.velocity.y = 0; cp0.getVelocity().y = 0;
} }
cp0.grounded = true; cp0.setGrounded(true);
cp0.position.y += y11 - y00; cp0.getPosition().y += y11 - y00;
} }
if (cp0.collisionListener != null) { if (cp0.getCollisionListener() != null) {
cp0.collisionListener.collision(CollisionSide.BOTTOM, entity0, entity1); cp0.getCollisionListener().collision(CollisionSide.BOTTOM, entity0, entity1);
} }
} }
if (y01 > y10 && Math.abs(y01 - y10) < (cp0.size.y + cp1.size.y) / 16) { if (y01 > y10 && Math.abs(y01 - y10) < (cp0.getSize().y + cp1.getSize().y) / 16) {
// cp0's top side is colliding with cp1's bottom side // cp0's top side is colliding with cp1's bottom side
if (!cp0.ghost && !cp1.ghost) { if (!cp0.isGhost() && !cp1.isGhost()) {
if (cp0.velocity.y > 0) { if (cp0.getVelocity().y > 0) {
// cp0 is going up, stop // cp0 is going up, stop
cp0.velocity.y = 0; cp0.getVelocity().y = 0;
} }
} }
if (cp0.collisionListener != null) { if (cp0.getCollisionListener() != null) {
cp0.collisionListener.collision(CollisionSide.TOP, entity0, entity1); cp0.getCollisionListener().collision(CollisionSide.TOP, entity0, entity1);
} }
} }
} }

View File

@ -42,7 +42,7 @@ public class RenderingSystem extends EntitySystem {
@Override @Override
public void update(float deltaTime) { public void update(float deltaTime) {
CPhysics phys = pm.get(AppUtil.player); CPhysics phys = pm.get(AppUtil.player);
camera.position.set(phys.position.x, phys.position.y, 0); camera.position.set(phys.getPosition().x, phys.getPosition().y, 0);
camera.update(); camera.update();
Gdx.gl.glClearColor(0, 0, 0, 0); Gdx.gl.glClearColor(0, 0, 0, 0);
@ -60,8 +60,8 @@ public class RenderingSystem extends EntitySystem {
int spriteHeight = currSprite.getRegionHeight(); int spriteHeight = currSprite.getRegionHeight();
int spriteWidth = currSprite.getRegionWidth(); int spriteWidth = currSprite.getRegionWidth();
currSprite.setPosition(physics.position.x-spriteWidth/2, currSprite.setPosition(physics.getPosition().x-spriteWidth/2,
physics.position.y-spriteHeight/2); physics.getPosition().y-spriteHeight/2);
currSprite.draw(batch); currSprite.draw(batch);
float nextFrame = renderedObject.getCurrentFrame() + deltaTime*currSequence.getPlayspeed(); float nextFrame = renderedObject.getCurrentFrame() + deltaTime*currSequence.getPlayspeed();