Removed the usage of Box2D

This commit is contained in:
Allexit 2015-05-08 16:49:42 +03:00
parent e985102e80
commit 5975288d04
9 changed files with 37 additions and 150 deletions

View File

@ -8,17 +8,9 @@ import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.components.CRenderedObject;
import com.saltosion.gladiator.input.InputHandler;
import com.saltosion.gladiator.systems.MovementSystem;
import com.saltosion.gladiator.systems.RenderingSystem;
import com.saltosion.gladiator.util.AppUtil;
import com.saltosion.gladiator.util.Name;
@ -28,7 +20,6 @@ import com.saltosion.gladiator.util.SpriteSequence;
public class GladiatorBrawler extends ApplicationAdapter {
private Engine engine;
private World world;
private InputHandler inputHandler;
private float physics_accumulator = 0f;
@ -37,16 +28,12 @@ public class GladiatorBrawler extends ApplicationAdapter {
@Override
public void create () {
// Initializing Physics
world = new World(new Vector2(0, -10), true);
// Initialize the Engine
engine = new Engine();
engine.addSystem(new RenderingSystem(world));
engine.addSystem(new MovementSystem());
engine.addSystem(new RenderingSystem());
engine.addEntityListener(Family.getFor(),
new EntityListener() {
@ -54,16 +41,12 @@ public class GladiatorBrawler extends ApplicationAdapter {
public void entityRemoved(Entity entity) {
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
rs.updateEntities(engine);
MovementSystem ms = engine.getSystem(MovementSystem.class);
ms.updateEntities(engine);
}
@Override
public void entityAdded(Entity entity) {
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
rs.updateEntities(engine);
MovementSystem ms = engine.getSystem(MovementSystem.class);
ms.updateEntities(engine);
}
});
@ -72,7 +55,6 @@ public class GladiatorBrawler extends ApplicationAdapter {
// Initialize stuff in the world
initializePlayer();
initializeTerrain();
// Initialize input
@ -83,45 +65,9 @@ public class GladiatorBrawler extends ApplicationAdapter {
@Override
public void render () {
engine.update(Gdx.graphics.getDeltaTime());
physicsStep(Gdx.graphics.getDeltaTime());
}
private void physicsStep(float deltaTime) {
float frameTime = Math.max(deltaTime, 0.25f);
physics_accumulator += frameTime;
if (physics_accumulator >= AppUtil.PHYSICS_TIMESTEP) {
world.step(AppUtil.PHYSICS_TIMESTEP, 6, 2);
physics_accumulator -= AppUtil.PHYSICS_TIMESTEP;
}
}
public void initializePlayer() {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(5, 5);
bodyDef.fixedRotation = true;
Body body = world.createBody(bodyDef);
PolygonShape bottomBox = new PolygonShape();
bottomBox.setAsBox(1.95f, .5f, new Vector2(0, -1.5f), 0);
FixtureDef bottomDef = new FixtureDef();
bottomDef.shape = bottomBox;
bottomDef.density = 75f;
bottomDef.friction = 7f;
Fixture bottom = body.createFixture(bottomDef);
PolygonShape topBox = new PolygonShape();
topBox.setAsBox(2, 1.5f, new Vector2(0, .5f), 0);
FixtureDef topDef = new FixtureDef();
topDef.shape = topBox;
topDef.density = 75f;
topDef.friction = 0f;
Fixture top = body.createFixture(topDef);
bottomBox.dispose();
topBox.dispose();
player = new Entity();
@ -133,22 +79,12 @@ public class GladiatorBrawler extends ApplicationAdapter {
renderedObject.playAnimation("Idle");
player.add(renderedObject);
player.add(new CPhysics());
player.getComponent(CPhysics.class).body = body;
engine.addEntity(player);
AppUtil.player = player;
}
public void initializeTerrain() {
BodyDef terrain = new BodyDef();
Body terrainBody = world.createBody(terrain);
PolygonShape terrainBox = new PolygonShape();
terrainBox.setAsBox(20, 2);
terrainBody.createFixture(terrainBox, 0);
terrainBox.dispose();
}
@Override
public void resize(int width, int height) {
super.resize(width, height);

View File

@ -2,14 +2,13 @@ package com.saltosion.gladiator.components;
import com.badlogic.ashley.core.Component;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
public class CPhysics extends Component {
public Body body;
public Vector2 position = new Vector2(0, 0);
public Vector2 velocity = new Vector2(0, 0);
public boolean movingLeft = false;
public boolean movingRight = false;
public boolean jumping = false;
public boolean grounded = true;
}

View File

@ -0,0 +1,23 @@
package com.saltosion.gladiator.input;
import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.util.AppUtil;
public class IRJump implements InputReceiver {
@Override
public boolean pressed() {
CPhysics physics = AppUtil.player.getComponent(CPhysics.class);
physics.jumping = true;
return true;
}
@Override
public boolean released() {
CPhysics physics = AppUtil.player.getComponent(CPhysics.class);
physics.jumping = false;
physics.grounded = true;
return true;
}
}

View File

@ -13,6 +13,7 @@ public class InputHandler implements InputProcessor {
public InputHandler() {
keys.put(Keys.A, Name.MOVE_LEFT);
keys.put(Keys.D, Name.MOVE_RIGHT);
keys.put(Keys.SPACE, Name.JUMP);
}
@Override

View File

@ -10,6 +10,7 @@ public class InputReceivers {
static {
inputreceivers.put(Name.MOVE_LEFT, new IRMoveLeft());
inputreceivers.put(Name.MOVE_RIGHT, new IRMoveRight());
inputreceivers.put(Name.JUMP, new IRJump());
}
public static InputReceiver getReceiver(String key) {

View File

@ -1,60 +0,0 @@
package com.saltosion.gladiator.systems;
import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.EntitySystem;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.math.Vector2;
import com.saltosion.gladiator.components.CPhysics;
public class MovementSystem extends EntitySystem {
private ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
private ImmutableArray<Entity> entities;
private final Vector2 RIGHT = new Vector2(1, 0);
private final float MOVEMENT_SPEED = 850;
private final float MAX_MOVEMENT_SPEED = 10;
private Vector2 newVel = new Vector2(0, 0);
private Vector2 bodyVel = new Vector2(0, 0);
@Override
public void addedToEngine(Engine engine) {
updateEntities(engine);
}
@Override
public void update(float deltaTime) {
for (int i=0; i<entities.size(); i++) {
Entity e = entities.get(i);
CPhysics physics = pm.get(e);
newVel.setZero();
if (physics.movingRight) {
newVel.add(RIGHT);
}
if (physics.movingLeft) {
newVel.add(RIGHT).scl(-1);
}
newVel.scl(MOVEMENT_SPEED);
bodyVel.set(physics.body.getLinearVelocity());
bodyVel.y = 0;
newVel.sub(bodyVel.scl(50));
System.out.println(newVel);
physics.body.applyLinearImpulse(newVel, physics.body.getPosition(), true);
/**
if (Math.abs(bodyVel.x) < MAX_MOVEMENT_SPEED) {
physics.body.applyLinearImpulse(newVel, physics.body.getPosition(), true);
}
*/
}
}
public void updateEntities(Engine engine) {
entities = engine.getEntitiesFor(Family.getFor(CPhysics.class));
}
}

View File

@ -9,15 +9,11 @@ import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.components.CRenderedObject;
import com.saltosion.gladiator.util.AppUtil;
import com.saltosion.gladiator.util.SpriteLoader;
import com.saltosion.gladiator.util.SpriteSequence;
public class RenderingSystem extends EntitySystem {
@ -29,16 +25,8 @@ public class RenderingSystem extends EntitySystem {
private SpriteBatch batch;
private OrthographicCamera camera;
private Box2DDebugRenderer debugRenderer;
private World world;
public RenderingSystem(World world) {
this.world = world;
}
@Override
public void addedToEngine(Engine engine) {
debugRenderer = new Box2DDebugRenderer();
updateEntities(engine);
@ -54,7 +42,7 @@ public class RenderingSystem extends EntitySystem {
@Override
public void update(float deltaTime) {
CPhysics phys = pm.get(AppUtil.player);
camera.position.set(phys.body.getPosition().x, phys.body.getPosition().y, 0);
camera.position.set(phys.position.x, phys.position.y, 0);
camera.update();
Gdx.gl.glClearColor(0, 0, 0, 0);
@ -72,17 +60,14 @@ public class RenderingSystem extends EntitySystem {
int spriteHeight = currSprite.getRegionHeight();
int spriteWidth = currSprite.getRegionWidth();
currSprite.setPosition(physics.body.getPosition().x-spriteWidth/2,
physics.body.getPosition().y-spriteHeight/2);
currSprite.setRotation(physics.body.getAngle());
currSprite.setPosition(physics.position.x-spriteWidth/2,
physics.position.y-spriteHeight/2);
currSprite.draw(batch);
float nextFrame = renderedObject.getCurrentFrame() + deltaTime*currSequence.getPlayspeed();
renderedObject.setCurrentFrame(nextFrame%currSequence.frameCount());
}
batch.end();
debugRenderer.render(world, camera.combined);
}
public void updateEntities(Engine engine) {

View File

@ -1,12 +1,13 @@
package com.saltosion.gladiator.util;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.math.Vector2;
public class AppUtil {
public static Entity player;
public static final int VPHEIGHT_CONST = 14;
public static final float PHYSICS_TIMESTEP = 1/45f;
public static final Vector2 JUMP_FORCE = new Vector2(0, 12000);
}

View File

@ -9,5 +9,6 @@ public class Name {
public static final String MOVE_LEFT = "MOVE_LEFT";
public static final String MOVE_RIGHT = "MOVE_RIGHT";
public static final String JUMP = "JUMP";
}