Physics done, jumping WIP.
This commit is contained in:
parent
5975288d04
commit
47eb5f5702
1
.gitignore
vendored
1
.gitignore
vendored
@ -62,3 +62,4 @@ build/
|
|||||||
|
|
||||||
## OS Specific
|
## OS Specific
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
/.nb-gradle/private/
|
Binary file not shown.
Before Width: | Height: | Size: 67 KiB |
BIN
core/assets/sprites/ground.png
Normal file
BIN
core/assets/sprites/ground.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 20 KiB |
BIN
core/assets/sprites/wall.png
Normal file
BIN
core/assets/sprites/wall.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.0 KiB |
@ -3,7 +3,6 @@ package com.saltosion.gladiator;
|
|||||||
import com.badlogic.ashley.core.Engine;
|
import com.badlogic.ashley.core.Engine;
|
||||||
import com.badlogic.ashley.core.Entity;
|
import com.badlogic.ashley.core.Entity;
|
||||||
import com.badlogic.ashley.core.EntityListener;
|
import com.badlogic.ashley.core.EntityListener;
|
||||||
import com.badlogic.ashley.core.Family;
|
|
||||||
import com.badlogic.gdx.ApplicationAdapter;
|
import com.badlogic.gdx.ApplicationAdapter;
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.graphics.g2d.Sprite;
|
import com.badlogic.gdx.graphics.g2d.Sprite;
|
||||||
@ -11,8 +10,10 @@ import com.badlogic.gdx.math.Vector2;
|
|||||||
import com.saltosion.gladiator.components.CPhysics;
|
import com.saltosion.gladiator.components.CPhysics;
|
||||||
import com.saltosion.gladiator.components.CRenderedObject;
|
import com.saltosion.gladiator.components.CRenderedObject;
|
||||||
import com.saltosion.gladiator.input.InputHandler;
|
import com.saltosion.gladiator.input.InputHandler;
|
||||||
|
import com.saltosion.gladiator.systems.PhysicsSystem;
|
||||||
import com.saltosion.gladiator.systems.RenderingSystem;
|
import com.saltosion.gladiator.systems.RenderingSystem;
|
||||||
import com.saltosion.gladiator.util.AppUtil;
|
import com.saltosion.gladiator.util.AppUtil;
|
||||||
|
import com.saltosion.gladiator.util.Global;
|
||||||
import com.saltosion.gladiator.util.Name;
|
import com.saltosion.gladiator.util.Name;
|
||||||
import com.saltosion.gladiator.util.SpriteLoader;
|
import com.saltosion.gladiator.util.SpriteLoader;
|
||||||
import com.saltosion.gladiator.util.SpriteSequence;
|
import com.saltosion.gladiator.util.SpriteSequence;
|
||||||
@ -22,21 +23,31 @@ public class GladiatorBrawler extends ApplicationAdapter {
|
|||||||
private Engine engine;
|
private Engine engine;
|
||||||
private InputHandler inputHandler;
|
private InputHandler inputHandler;
|
||||||
|
|
||||||
private float physics_accumulator = 0f;
|
|
||||||
|
|
||||||
private Entity player;
|
private Entity player;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void create() {
|
public void create() {
|
||||||
|
|
||||||
// Initialize the Engine
|
// Initialize the Engine
|
||||||
|
|
||||||
engine = new Engine();
|
engine = new Engine();
|
||||||
|
|
||||||
engine.addSystem(new RenderingSystem());
|
engine.addSystem(new PhysicsSystem());
|
||||||
|
engine.addEntityListener(new EntityListener() {
|
||||||
|
@Override
|
||||||
|
public void entityAdded(Entity entity) {
|
||||||
|
PhysicsSystem ps = engine.getSystem(PhysicsSystem.class);
|
||||||
|
ps.updateEntities(engine);
|
||||||
|
}
|
||||||
|
|
||||||
engine.addEntityListener(Family.getFor(),
|
@Override
|
||||||
new EntityListener() {
|
public void entityRemoved(Entity entity) {
|
||||||
|
PhysicsSystem ps = engine.getSystem(PhysicsSystem.class);
|
||||||
|
ps.updateEntities(engine);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
engine.addSystem(new RenderingSystem());
|
||||||
|
engine.addEntityListener(new EntityListener() {
|
||||||
@Override
|
@Override
|
||||||
public void entityRemoved(Entity entity) {
|
public void entityRemoved(Entity entity) {
|
||||||
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
|
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
|
||||||
@ -47,17 +58,14 @@ public class GladiatorBrawler extends ApplicationAdapter {
|
|||||||
public void entityAdded(Entity entity) {
|
public void entityAdded(Entity entity) {
|
||||||
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
|
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
|
||||||
rs.updateEntities(engine);
|
rs.updateEntities(engine);
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Initialize stuff in the world
|
// Initialize stuff in the world
|
||||||
|
|
||||||
initializePlayer();
|
initializePlayer();
|
||||||
|
initializeLevel();
|
||||||
|
|
||||||
// Initialize input
|
// Initialize input
|
||||||
|
|
||||||
inputHandler = new InputHandler();
|
inputHandler = new InputHandler();
|
||||||
Gdx.input.setInputProcessor(inputHandler);
|
Gdx.input.setInputProcessor(inputHandler);
|
||||||
}
|
}
|
||||||
@ -78,13 +86,50 @@ public class GladiatorBrawler extends ApplicationAdapter {
|
|||||||
renderedObject.addSequence("Idle", sequence);
|
renderedObject.addSequence("Idle", sequence);
|
||||||
renderedObject.playAnimation("Idle");
|
renderedObject.playAnimation("Idle");
|
||||||
player.add(renderedObject);
|
player.add(renderedObject);
|
||||||
player.add(new CPhysics());
|
player.add(new CPhysics().setSize(player1.getRegionWidth() * Global.SPRITE_SCALE, player1.getRegionHeight() * Global.SPRITE_SCALE));
|
||||||
|
|
||||||
engine.addEntity(player);
|
engine.addEntity(player);
|
||||||
|
|
||||||
AppUtil.player = player;
|
AppUtil.player = player;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void initializeLevel() {
|
||||||
|
Entity ground = new Entity();
|
||||||
|
|
||||||
|
Sprite groundSprite = SpriteLoader.loadSprite(Name.GROUNDIMG, 0, 0, 256, 64);
|
||||||
|
CRenderedObject renderedObject = new CRenderedObject(groundSprite);
|
||||||
|
ground.add(renderedObject);
|
||||||
|
CPhysics physics = new CPhysics().setMovable(false).setGravityApplied(false)
|
||||||
|
.setSize(groundSprite.getRegionWidth() * Global.SPRITE_SCALE,
|
||||||
|
groundSprite.getRegionHeight() * Global.SPRITE_SCALE);
|
||||||
|
physics.position.set(new Vector2(-2.5f, -5));
|
||||||
|
ground.add(physics);
|
||||||
|
|
||||||
|
Sprite wallSprite = SpriteLoader.loadSprite(Name.WALLIMG, 0, 0, 64, 64);
|
||||||
|
|
||||||
|
Entity wall0 = new Entity();
|
||||||
|
CRenderedObject wall0RenderedObject = new CRenderedObject(wallSprite);
|
||||||
|
CPhysics wall0Physics = new CPhysics().setMovable(false).setGravityApplied(false)
|
||||||
|
.setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE,
|
||||||
|
wallSprite.getRegionHeight() * Global.SPRITE_SCALE);
|
||||||
|
wall0Physics.position.set(new Vector2(5, 0));
|
||||||
|
wall0.add(wall0RenderedObject);
|
||||||
|
wall0.add(wall0Physics);
|
||||||
|
|
||||||
|
Entity wall1 = new Entity();
|
||||||
|
CRenderedObject wall1RenderedObject = new CRenderedObject(wallSprite);
|
||||||
|
CPhysics wall1Physics = new CPhysics().setMovable(false).setGravityApplied(false)
|
||||||
|
.setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE,
|
||||||
|
wallSprite.getRegionHeight() * Global.SPRITE_SCALE);
|
||||||
|
wall1Physics.position.set(new Vector2(-5, 0));
|
||||||
|
wall1.add(wall1RenderedObject);
|
||||||
|
wall1.add(wall1Physics);
|
||||||
|
|
||||||
|
engine.addEntity(ground);
|
||||||
|
engine.addEntity(wall0);
|
||||||
|
engine.addEntity(wall1);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void resize(int width, int height) {
|
public void resize(int width, int height) {
|
||||||
super.resize(width, height);
|
super.resize(width, height);
|
||||||
|
@ -5,10 +5,42 @@ import com.badlogic.gdx.math.Vector2;
|
|||||||
|
|
||||||
public class CPhysics extends Component {
|
public class CPhysics extends Component {
|
||||||
|
|
||||||
public Vector2 position = new Vector2(0, 0);
|
public Vector2 position = new Vector2();
|
||||||
public Vector2 velocity = new Vector2(0, 0);
|
public Vector2 velocity = new Vector2();
|
||||||
|
public Vector2 size = new Vector2();
|
||||||
|
public float movespeed = 5f, jumpForce = 5f, gravity = 1f;
|
||||||
|
public boolean grounded = true;
|
||||||
|
|
||||||
|
// Movable toggles if the entity can move by itself
|
||||||
|
public boolean movable = true;
|
||||||
|
// GravityApplied toggles if the entity is affected by gravity
|
||||||
|
public boolean gravityApplied = true;
|
||||||
|
// Dynamic toggles if the entity processes collisions
|
||||||
|
public boolean dynamic = true;
|
||||||
|
|
||||||
|
// Movement (/input) vars
|
||||||
public boolean movingLeft = false;
|
public boolean movingLeft = false;
|
||||||
public boolean movingRight = false;
|
public boolean movingRight = false;
|
||||||
public boolean jumping = false;
|
public boolean jumping = false;
|
||||||
public boolean grounded = true;
|
|
||||||
|
public CPhysics setMovable(boolean movable) {
|
||||||
|
this.movable = movable;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CPhysics setGravityApplied(boolean gravityApplied) {
|
||||||
|
this.gravityApplied = gravityApplied;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CPhysics setDynamic(boolean dynamic) {
|
||||||
|
this.dynamic = dynamic;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CPhysics setSize(float w, float h) {
|
||||||
|
this.size.set(w, h);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ public class IRJump implements InputReceiver {
|
|||||||
public boolean released() {
|
public boolean released() {
|
||||||
CPhysics physics = AppUtil.player.getComponent(CPhysics.class);
|
CPhysics physics = AppUtil.player.getComponent(CPhysics.class);
|
||||||
physics.jumping = false;
|
physics.jumping = false;
|
||||||
physics.grounded = true;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
119
core/src/com/saltosion/gladiator/systems/PhysicsSystem.java
Normal file
119
core/src/com/saltosion/gladiator/systems/PhysicsSystem.java
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
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.saltosion.gladiator.components.CPhysics;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Jens "Jeasonfire" Pitkänen <jeasonfire@gmail.com>
|
||||||
|
*/
|
||||||
|
public class PhysicsSystem extends EntitySystem {
|
||||||
|
|
||||||
|
private ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
|
||||||
|
private ImmutableArray<Entity> entities;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addedToEngine(Engine engine) {
|
||||||
|
updateEntities(engine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(float deltaTime) {
|
||||||
|
for (int i = 0; i < entities.size(); i++) {
|
||||||
|
CPhysics obj = pm.get(entities.get(i));
|
||||||
|
|
||||||
|
// Movement
|
||||||
|
if (obj.movable) {
|
||||||
|
float move = 0;
|
||||||
|
if (obj.movingLeft) {
|
||||||
|
move--;
|
||||||
|
}
|
||||||
|
if (obj.movingRight) {
|
||||||
|
move++;
|
||||||
|
}
|
||||||
|
obj.velocity.x = move * obj.movespeed * deltaTime;
|
||||||
|
if (obj.jumping) {
|
||||||
|
obj.velocity.y = obj.jumpForce;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gravity
|
||||||
|
if (obj.gravityApplied) {
|
||||||
|
obj.velocity.y -= obj.gravity * deltaTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collisions
|
||||||
|
if (obj.dynamic) {
|
||||||
|
for (int j = 0; j < entities.size(); j++) {
|
||||||
|
if (i == j) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
CPhysics other = pm.get(entities.get(j));
|
||||||
|
collision(obj, other);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply movement
|
||||||
|
obj.position.add(obj.velocity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void collision(CPhysics cp0, CPhysics cp1) {
|
||||||
|
float x00 = cp0.position.x - cp0.size.x / 2;
|
||||||
|
float x01 = cp0.position.x + cp0.size.x / 2;
|
||||||
|
float x10 = cp1.position.x - cp1.size.x / 2;
|
||||||
|
float x11 = cp1.position.x + cp1.size.x / 2;
|
||||||
|
float y00 = cp0.position.y - cp0.size.y / 2;
|
||||||
|
float y01 = cp0.position.y + cp0.size.y / 2;
|
||||||
|
float y10 = cp1.position.y - cp1.size.y / 2;
|
||||||
|
float y11 = cp1.position.y + cp1.size.y / 2;
|
||||||
|
cp0.grounded = false;
|
||||||
|
cp1.grounded = false;
|
||||||
|
|
||||||
|
boolean colliding = (x00 < x11) && (x01 > x10) && (y00 < y11) && (y01 > y10);
|
||||||
|
|
||||||
|
if (!colliding) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x00 <= x11 && Math.abs(x00 - x11) < (cp0.size.x + cp1.size.x) / 4) {
|
||||||
|
// cp0's left side is colliding with cp1's right side
|
||||||
|
if (cp0.velocity.x < 0) {
|
||||||
|
// cp0 is going left, stop
|
||||||
|
cp0.velocity.x = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (x01 > x10 && Math.abs(x01 - x10) < (cp0.size.x + cp1.size.x) / 4) {
|
||||||
|
// cp0's right side is colliding with cp1's left side
|
||||||
|
if (cp0.velocity.x > 0) {
|
||||||
|
// cp0 is going right, stop
|
||||||
|
cp0.velocity.x = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (y00 <= y11 && Math.abs(y00 - y11) < (cp0.size.y + cp1.size.y) / 4) {
|
||||||
|
// cp0's bottom side is colliding with cp1's top side
|
||||||
|
if (cp0.velocity.y < 0) {
|
||||||
|
// cp0 is going down, stop
|
||||||
|
cp0.velocity.y = 0;
|
||||||
|
}
|
||||||
|
cp0.grounded = true;
|
||||||
|
}
|
||||||
|
if (y01 > y10 && Math.abs(y01 - y10) < (cp0.size.y + cp1.size.y) / 4) {
|
||||||
|
// cp0's top side is colliding with cp1's bottom side
|
||||||
|
if (cp0.velocity.y > 0) {
|
||||||
|
// cp0 is going up, stop
|
||||||
|
cp0.velocity.y = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateEntities(Engine engine) {
|
||||||
|
entities = engine.getEntitiesFor(Family.getFor(CPhysics.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -8,6 +8,6 @@ public class Global {
|
|||||||
public static final String GAME_NAME = "Gladiator Brawl";
|
public static final String GAME_NAME = "Gladiator Brawl";
|
||||||
|
|
||||||
public static final int VPHEIGHT_CONST = 14;
|
public static final int VPHEIGHT_CONST = 14;
|
||||||
public static final float PHYSICS_TIMESTEP = 1/45f;
|
public static final float SPRITE_SCALE = 1 / 16f;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@ public class Name {
|
|||||||
|
|
||||||
public static final String STATICPLAYER = "STATICPLAYER";
|
public static final String STATICPLAYER = "STATICPLAYER";
|
||||||
public static final String PLAYERIMG = "PLAYERIMG";
|
public static final String PLAYERIMG = "PLAYERIMG";
|
||||||
|
public static final String GROUNDIMG = "GROUNDIMG";
|
||||||
|
public static final String WALLIMG = "WALLIMG";
|
||||||
|
|
||||||
public static final String MOVE_LEFT = "MOVE_LEFT";
|
public static final String MOVE_LEFT = "MOVE_LEFT";
|
||||||
public static final String MOVE_RIGHT = "MOVE_RIGHT";
|
public static final String MOVE_RIGHT = "MOVE_RIGHT";
|
||||||
|
@ -14,10 +14,13 @@ public class SpriteLoader {
|
|||||||
static {
|
static {
|
||||||
loadTexture(Name.STATICPLAYER, "sprites/staticplayer.png");
|
loadTexture(Name.STATICPLAYER, "sprites/staticplayer.png");
|
||||||
loadTexture(Name.PLAYERIMG, "sprites/player/player.png");
|
loadTexture(Name.PLAYERIMG, "sprites/player/player.png");
|
||||||
|
loadTexture(Name.GROUNDIMG, "sprites/ground.png");
|
||||||
|
loadTexture(Name.WALLIMG, "sprites/wall.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a sprite, which is a piece from texture.
|
* Returns a sprite, which is a piece from texture.
|
||||||
|
*
|
||||||
* @param texKey
|
* @param texKey
|
||||||
* @param x
|
* @param x
|
||||||
* @param y
|
* @param y
|
||||||
@ -28,12 +31,13 @@ public class SpriteLoader {
|
|||||||
public static Sprite loadSprite(String texKey, int x, int y, int width, int height) {
|
public static Sprite loadSprite(String texKey, int x, int y, int width, int height) {
|
||||||
TextureRegion tr = new TextureRegion(textures.get(texKey), x * width, y * height, width, height);
|
TextureRegion tr = new TextureRegion(textures.get(texKey), x * width, y * height, width, height);
|
||||||
Sprite s = new Sprite(tr);
|
Sprite s = new Sprite(tr);
|
||||||
s.setScale(1/16f);
|
s.setScale(Global.SPRITE_SCALE);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a sprite, which is originally a texture.
|
* Returns a sprite, which is originally a texture.
|
||||||
|
*
|
||||||
* @param texKey
|
* @param texKey
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@ -43,6 +47,7 @@ public class SpriteLoader {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Load texture from path.
|
* Load texture from path.
|
||||||
|
*
|
||||||
* @param filePath
|
* @param filePath
|
||||||
*/
|
*/
|
||||||
public static Texture loadTexture(String key, String filePath) {
|
public static Texture loadTexture(String key, String filePath) {
|
||||||
|
@ -6,11 +6,12 @@ import com.badlogic.gdx.graphics.g2d.Sprite;
|
|||||||
|
|
||||||
public class SpriteSequence {
|
public class SpriteSequence {
|
||||||
|
|
||||||
private ArrayList<Sprite> sprites = new ArrayList<Sprite>();
|
private final ArrayList<Sprite> sprites = new ArrayList<Sprite>();
|
||||||
private float defaultPlayspeed = 1;
|
private float defaultPlayspeed = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A static single image.
|
* A static single image.
|
||||||
|
*
|
||||||
* @param sprite
|
* @param sprite
|
||||||
*/
|
*/
|
||||||
public SpriteSequence(Sprite sprite) {
|
public SpriteSequence(Sprite sprite) {
|
||||||
@ -20,9 +21,6 @@ public class SpriteSequence {
|
|||||||
|
|
||||||
public SpriteSequence(float playspeed) {
|
public SpriteSequence(float playspeed) {
|
||||||
this.defaultPlayspeed = playspeed;
|
this.defaultPlayspeed = playspeed;
|
||||||
if (sprites != null) {
|
|
||||||
this.sprites = sprites;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpriteSequence addSprite(Sprite s) {
|
public SpriteSequence addSprite(Sprite s) {
|
||||||
|
Loading…
Reference in New Issue
Block a user