2015-04-08 15:59:01 +02:00
|
|
|
package com.saltosion.gladiator;
|
|
|
|
|
2015-04-08 18:19:14 +02:00
|
|
|
import com.badlogic.ashley.core.Engine;
|
2015-04-08 19:15:14 +02:00
|
|
|
import com.badlogic.ashley.core.Entity;
|
|
|
|
import com.badlogic.ashley.core.EntityListener;
|
|
|
|
import com.badlogic.ashley.core.Family;
|
2015-04-08 15:59:01 +02:00
|
|
|
import com.badlogic.gdx.ApplicationAdapter;
|
|
|
|
import com.badlogic.gdx.Gdx;
|
2015-04-08 18:19:14 +02:00
|
|
|
import com.badlogic.gdx.graphics.g2d.Sprite;
|
2015-04-09 20:16:49 +02:00
|
|
|
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;
|
2015-04-09 00:14:07 +02:00
|
|
|
import com.saltosion.gladiator.components.CPhysics;
|
2015-04-08 19:15:14 +02:00
|
|
|
import com.saltosion.gladiator.components.CRenderedObject;
|
|
|
|
import com.saltosion.gladiator.systems.RenderingSystem;
|
2015-04-09 20:16:49 +02:00
|
|
|
import com.saltosion.gladiator.util.Global;
|
2015-04-08 18:19:14 +02:00
|
|
|
import com.saltosion.gladiator.util.SpriteLoader;
|
2015-04-08 15:59:01 +02:00
|
|
|
|
|
|
|
public class GladiatorBrawler extends ApplicationAdapter {
|
2015-04-08 18:19:14 +02:00
|
|
|
|
|
|
|
private Engine engine;
|
2015-04-09 20:16:49 +02:00
|
|
|
private World world;
|
|
|
|
private float physics_accumulator = 0f;
|
|
|
|
|
2015-04-08 15:59:01 +02:00
|
|
|
|
2015-04-08 19:15:14 +02:00
|
|
|
private Entity player;
|
|
|
|
|
2015-04-08 15:59:01 +02:00
|
|
|
@Override
|
|
|
|
public void create () {
|
2015-04-09 20:16:49 +02:00
|
|
|
// Initializing Physics
|
|
|
|
|
|
|
|
world = new World(new Vector2(0, -10), true);
|
|
|
|
|
2015-04-08 19:15:14 +02:00
|
|
|
// Initialize the Engine
|
|
|
|
|
2015-04-08 18:19:14 +02:00
|
|
|
engine = new Engine();
|
|
|
|
|
2015-04-09 20:16:49 +02:00
|
|
|
engine.addSystem(new RenderingSystem(world));
|
2015-04-08 19:15:14 +02:00
|
|
|
|
2015-04-09 00:14:07 +02:00
|
|
|
engine.addEntityListener(Family.getFor(CRenderedObject.class, CPhysics.class),
|
2015-04-08 19:15:14 +02:00
|
|
|
new EntityListener() {
|
|
|
|
@Override
|
|
|
|
public void entityRemoved(Entity entity) {
|
|
|
|
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
|
|
|
|
rs.updateEntities(engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void entityAdded(Entity entity) {
|
|
|
|
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
|
|
|
|
rs.updateEntities(engine);
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-04-09 20:16:49 +02:00
|
|
|
|
2015-04-08 19:15:14 +02:00
|
|
|
// Initialize player
|
|
|
|
|
|
|
|
initializePlayer();
|
|
|
|
|
2015-04-08 15:59:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void render () {
|
2015-04-08 19:15:14 +02:00
|
|
|
engine.update(Gdx.graphics.getDeltaTime());
|
2015-04-09 20:16:49 +02:00
|
|
|
physicsStep(Gdx.graphics.getDeltaTime());
|
2015-04-08 19:15:14 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 20:16:49 +02:00
|
|
|
private void physicsStep(float deltaTime) {
|
|
|
|
float frameTime = Math.max(deltaTime, 0.25f);
|
|
|
|
physics_accumulator += frameTime;
|
|
|
|
if (physics_accumulator >= Global.PHYSICS_TIMESTEP) {
|
|
|
|
world.step(Global.PHYSICS_TIMESTEP, 6, 2);
|
|
|
|
physics_accumulator -= Global.PHYSICS_TIMESTEP;
|
|
|
|
}
|
2015-04-08 23:12:07 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:15:14 +02:00
|
|
|
public void initializePlayer() {
|
2015-04-09 20:16:49 +02:00
|
|
|
BodyDef bodyDef = new BodyDef();
|
|
|
|
bodyDef.type = BodyType.DynamicBody;
|
|
|
|
bodyDef.position.set(5, 5);
|
|
|
|
Body body = world.createBody(bodyDef);
|
|
|
|
|
|
|
|
PolygonShape box = new PolygonShape();
|
|
|
|
box.setAsBox(2, 2);
|
|
|
|
|
|
|
|
FixtureDef fixtureDef = new FixtureDef();
|
|
|
|
fixtureDef.shape = box;
|
|
|
|
fixtureDef.density = 0.5f;
|
|
|
|
fixtureDef.friction = 0.4f;
|
|
|
|
fixtureDef.restitution = 0.6f;
|
|
|
|
Fixture fixture = body.createFixture(fixtureDef);
|
|
|
|
|
|
|
|
box.dispose();
|
|
|
|
|
2015-04-08 19:15:14 +02:00
|
|
|
player = new Entity();
|
|
|
|
|
|
|
|
CRenderedObject renderedObject = new CRenderedObject();
|
2015-04-09 20:16:49 +02:00
|
|
|
Sprite player1 = SpriteLoader.loadSprite(Global.PLAYERIMG, 0, 0, 64, 64);
|
|
|
|
Sprite player2 = SpriteLoader.loadSprite(Global.PLAYERIMG, 1, 0, 64, 64);
|
2015-04-08 19:50:13 +02:00
|
|
|
SpriteSequence sequence = new SpriteSequence(1).addSprite(player1).addSprite(player2);
|
|
|
|
renderedObject.addSequence("Idle", sequence);
|
|
|
|
renderedObject.setCurrentSequence("Idle");
|
2015-04-08 19:15:14 +02:00
|
|
|
player.add(renderedObject);
|
2015-04-09 00:14:07 +02:00
|
|
|
player.add(new CPhysics());
|
2015-04-09 20:32:36 +02:00
|
|
|
player.getComponent(CPhysics.class).body = body;
|
2015-04-08 19:15:14 +02:00
|
|
|
|
|
|
|
engine.addEntity(player);
|
2015-04-08 15:59:01 +02:00
|
|
|
}
|
2015-04-09 20:16:49 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void resize(int width, int height) {
|
|
|
|
super.resize(width, height);
|
|
|
|
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
|
|
|
|
float aspectratio = ((float)width)/((float)height);
|
|
|
|
rs.setViewport((int)(Global.VPHEIGHT_CONST*aspectratio), Global.VPHEIGHT_CONST);
|
|
|
|
}
|
2015-04-08 15:59:01 +02:00
|
|
|
}
|