Custom Entity system

This commit is contained in:
Mike Schwörer 2014-08-10 17:19:40 +02:00
parent 553fdb261e
commit d6f8f324ad
15 changed files with 294 additions and 198 deletions

View File

@ -1,40 +0,0 @@
package de.samdev.colorrunner.actors;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
public abstract class CRActor extends Actor {
private Texture texture = null;
public CRActor(Texture _tex) {
texture = _tex;
setBounds(getX(), getY(), _tex.getWidth(), _tex.getHeight());
addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("CRGame", "touch my one more time");
return true;
}
});
}
@Override
public void draw(Batch batch, float alpha) {
batch.draw(getTexture(), this.getX(), this.getY(), this.getOriginX(), this.getOriginY(), this.getWidth(), this.getHeight(), this.getScaleX(), this.getScaleY(), this.getRotation(), 0, 0, texture.getWidth(), texture.getHeight(), false, false);
}
@Override
public void act(float delta) {
super.act(delta);
}
public Texture getTexture() {
return texture;
}
}

View File

@ -1,37 +0,0 @@
package de.samdev.colorrunner.actors;
import com.badlogic.gdx.graphics.Texture;
public abstract class CRGravityActor extends CRActor{
public static float GRAVITY_FORCE = 1.5f;
public static float TERMINAL_VELOCITY = 150;
private float velocity_Y = 0;
public CRGravityActor(Texture _tex) {
super(_tex);
}
@Override
public void act(float delta) {
super.act(delta);
doGravity();
}
private void doGravity() {
velocity_Y -= GRAVITY_FORCE;
if (-velocity_Y > TERMINAL_VELOCITY) {
velocity_Y = -TERMINAL_VELOCITY;
}
if (getY() <= 0 && velocity_Y < 0) velocity_Y = 0;
moveBy(0, velocity_Y);
}
protected void doGravitationalJump(float force) {
velocity_Y += force;
}
}

View File

@ -1,46 +0,0 @@
package de.samdev.colorrunner.actors;
import com.badlogic.gdx.graphics.Texture;
import de.samdev.colorrunner.assets.CRAssets;
import de.samdev.colorrunner.input.SwipeDirection;
public class CRPlayer extends CRGravityActor {
private SwipeDirection colorState = SwipeDirection.UP;
private static float FORWARD_SPEED = 100;
private static float JUMP_FORCE = 25;
public CRPlayer() {
super(CRAssets.TEX_PLAYER_UP);
// setTouchable(Touchable.enabled);
}
@Override
public void act(float delta) {
super.act(delta);
moveBy(delta * FORWARD_SPEED, 0);
}
@Override
public Texture getTexture() {
switch (colorState) {
case UP: return CRAssets.TEX_PLAYER_UP;
case RIGHT: return CRAssets.TEX_PLAYER_RIGHT;
case DOWN: return CRAssets.TEX_PLAYER_DOWN;
case LEFT: return CRAssets.TEX_PLAYER_LEFT;
default: return null;
}
}
public void switchColorState(SwipeDirection sd) {
colorState = sd;
}
public void jump() {
doGravitationalJump(JUMP_FORCE);
}
}

View File

@ -1,42 +0,0 @@
package de.samdev.colorrunner.assets;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Pixmap.Format;
public class CRAssets {
public static Texture TEX_PLAYER_UP;
public static Texture TEX_PLAYER_RIGHT;
public static Texture TEX_PLAYER_LEFT;
public static Texture TEX_PLAYER_DOWN;
static {
{
Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
pixmap.setColor(1, 0, 0, 1);
pixmap.fill();
TEX_PLAYER_UP = new Texture(pixmap);
}
{
Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
pixmap.setColor(0, 1, 0, 1);
pixmap.fill();
TEX_PLAYER_RIGHT = new Texture(pixmap);
}
{
Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
pixmap.setColor(0, 0, 1, 1);
pixmap.fill();
TEX_PLAYER_DOWN = new Texture(pixmap);
}
{
Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
pixmap.setColor(1, 1, 0, 1);
pixmap.fill();
TEX_PLAYER_LEFT = new Texture(pixmap);
}
}
}

View File

@ -0,0 +1,68 @@
package de.samdev.colorrunner.game.renderer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
public abstract class AbstractGameRenderer {
private final static float GAME_HEIGHT = 512;
private final static float MIN_GAME_WIDTH = 672; // ~ 512 * (4/3)
private final static float MAX_GAME_WIDTH = 1024; // ~ 512 * (2/1) // ~~ 16:9
private OrthographicCamera cam;
protected ShapeRenderer shapeRenderer;
protected BitmapFont font = new BitmapFont();
protected SpriteBatch spriteBatch = new SpriteBatch();
public AbstractGameRenderer(float width, float height) {
cam = new OrthographicCamera();
shapeRenderer = new ShapeRenderer();
resize(width, height);
}
public void render() {
Gdx.gl.glClearColor(1, 0, 1, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// #################
shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(0.22f, 0.22f, 0.22f, 1);
shapeRenderer.rect(0, 0, MIN_GAME_WIDTH, GAME_HEIGHT);
shapeRenderer.end();
doRender();
}
public void resize(float width, float height) {
float cam_width = (width / height) * GAME_HEIGHT;
if (cam_width < MIN_GAME_WIDTH)
Gdx.app.error("SIZE", "Minimal aspect ratio is 4:3");
if (cam_width > MAX_GAME_WIDTH)
Gdx.app.error("SIZE", "Maximal aspect ratio is 2:1");
cam.setToOrtho(true, cam_width, GAME_HEIGHT);
shapeRenderer.setProjectionMatrix(cam.combined);
}
protected void renderDebug(String s) {
spriteBatch.begin();
font.setColor(0, 1, 0, 1);
font.draw(spriteBatch, s, 0, Gdx.graphics.getHeight());
spriteBatch.end();
}
public abstract void doRender();
}

View File

@ -0,0 +1,30 @@
package de.samdev.colorrunner.game.renderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import de.samdev.colorrunner.game.world.CRGameWorld;
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
public class CRGameRenderer extends AbstractGameRenderer {
private CRGameWorld gameworld;
public CRGameRenderer(CRGameWorld _world, float width, float height) {
super(width, height);
gameworld = _world;
}
@Override
public void doRender() {
renderDebug("FPS: " + (int)gameworld.fps.getFPS());
shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(1, 0, 0, 1);
for (CRGameEntity e : gameworld.entities) {
shapeRenderer.rect(e.bounds.x, e.bounds.y, e.bounds.width, e.bounds.height);
}
shapeRenderer.end();
}
}

View File

@ -0,0 +1,31 @@
package de.samdev.colorrunner.game.world;
import java.util.ArrayList;
import java.util.List;
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.PlayerEntity;
public class CRGameWorld {
public List<CRGameEntity> entities = new ArrayList<CRGameEntity>();
public FPSCounter fps = new FPSCounter();
public CRGameWorld() {
addEntity(new PlayerEntity());
}
public void update(float delta) {
fps.Inc();
for (CRGameEntity ent : entities) {
ent.update(delta);
}
// Gdx.app.log("GameWorld", "update FPS[" + (int)fps.getFPS() + "] DELTA:[" + delta + "]");
}
public void addEntity(CRGameEntity ent) {
entities.add(ent);
}
}

View File

@ -0,0 +1,35 @@
package de.samdev.colorrunner.game.world;
import com.badlogic.gdx.utils.TimeUtils;
public class FPSCounter {
private static final int UPDATE_TIME = 500;
private double fps;
private long lastUpdate;
private int count;
public FPSCounter() {
fps = 1;
lastUpdate = 0;
count = 0;
}
public void Inc() {
count++;
calc();
}
private void calc() {
long delta = TimeUtils.millis() - lastUpdate;
if (delta > UPDATE_TIME) {
fps = Math.max(1, (count * 1d) / (delta / 1000.0));
count = 0;
lastUpdate = TimeUtils.millis();
}
}
public double getFPS() {
return fps;
}
}

View File

@ -0,0 +1,25 @@
package de.samdev.colorrunner.game.world.entities;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public abstract class CRGameEntity {
public Rectangle bounds = new Rectangle();
protected Vector2 tmp_pos = new Vector2();
public CRGameEntity(float width, float height) {
bounds = new Rectangle(0, 0, width, height);
}
public Vector2 getPosition() {
return bounds.getPosition(tmp_pos);
}
protected void moveBy(float bx, float by) {
bounds.x += bx;
bounds.y += by;
}
public abstract void update(float delta);
}

View File

@ -0,0 +1,17 @@
package de.samdev.colorrunner.game.world.entities;
public abstract class GravityEntity extends MovingEntity {
public GravityEntity(float width, float height) {
super(width, height);
// TODO Auto-generated constructor stub
}
@Override
public void update(float delta) {
super.update(delta);
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,16 @@
package de.samdev.colorrunner.game.world.entities;
public abstract class MovingEntity extends CRGameEntity {
public MovingEntity(float width, float height) {
super(width, height);
// TODO Auto-generated constructor stub
}
@Override
public void update(float delta) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,20 @@
package de.samdev.colorrunner.game.world.entities.gameentities;
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
public class FloorTileEntity extends CRGameEntity {
public final static float FLOORTILE_WIDTH = 32;
public final static float FLOORTILE_HEIGHT = 32;
public FloorTileEntity() {
super(FLOORTILE_WIDTH, FLOORTILE_HEIGHT);
// TODO Auto-generated constructor stub
}
@Override
public void update(float delta) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,19 @@
package de.samdev.colorrunner.game.world.entities.gameentities;
import de.samdev.colorrunner.game.world.entities.GravityEntity;
public class PlayerEntity extends GravityEntity {
public final static float PLAYER_WIDTH = 32;
public final static float PLAYER_HEIGHT = 32;
public PlayerEntity() {
super(PLAYER_WIDTH, PLAYER_HEIGHT);
}
@Override
public void update(float delta) {
super.update(delta);
moveBy(50 * delta, 50 * delta);
}
}

View File

@ -5,83 +5,83 @@ import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import de.samdev.colorrunner.actors.CRPlayer;
import de.samdev.colorrunner.game.renderer.CRGameRenderer;
import de.samdev.colorrunner.game.world.CRGameWorld;
import de.samdev.colorrunner.input.CRGameInputProcessor;
import de.samdev.colorrunner.input.GameInputListener;
import de.samdev.colorrunner.input.SwipeDirection;
public class GameScreen implements Screen, GameInputListener {
private Stage stage;
private CRPlayer player;
private final static float GAME_WIDTH = 500;
private CRGameWorld world;
private CRGameRenderer renderer;
public GameScreen() {
stage = new Stage(new ExtendViewport(967, 544));
player = new CRPlayer();
stage.addActor(player);
world = new CRGameWorld(); // initialize world
renderer = new CRGameRenderer(world, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); // initialize renderer
}
@Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
// stage.getViewport().getCamera().position.x = player.getX();
world.update(delta);
renderer.render();
}
@Override
public void resize(int width, int height) {
Gdx.app.log("GameScreen", "resize called");
Gdx.app.log("GameScreen", "resize called");
float gameHeight = height / (width / GAME_WIDTH);
float gameX = 0;
float gameY = (height - gameHeight) / 2;
renderer.resize(width, height);
}
@Override
public void show() {
CRGameInputProcessor ip = new CRGameInputProcessor(this);
Gdx.input.setInputProcessor(new InputMultiplexer(stage, ip, new GestureDetector(ip)));
Gdx.app.log("GameScreen", "show called");
Gdx.input.setInputProcessor(new InputMultiplexer(ip, new GestureDetector(ip)));
Gdx.app.log("GameScreen", "show called");
}
@Override
public void hide() {
Gdx.input.setInputProcessor(null);
Gdx.app.log("GameScreen", "hide called");
Gdx.app.log("GameScreen", "hide called");
}
@Override
public void pause() {
Gdx.app.log("GameScreen", "pause called");
Gdx.app.log("GameScreen", "pause called");
}
@Override
public void resume() {
Gdx.app.log("GameScreen", "resume called");
Gdx.app.log("GameScreen", "resume called");
}
@Override
public void dispose() {
Gdx.app.log("GameScreen", "dispose called");
Gdx.app.log("GameScreen", "dispose called");
}
@Override
public void doJump() {
player.jump();
Gdx.app.log("GameScreen", "[DO] Jump");
Gdx.app.log("GameScreen", "[DO] Jump");
}
@Override
public void switchColor(SwipeDirection sd) {
player.switchColorState(sd);
Gdx.app.log("GameScreen", "[DO] Switch + " + sd.toString());
Gdx.app.log("GameScreen", "[DO] Switch + " + sd.toString());
}
}

View File

@ -9,8 +9,8 @@ public class DesktopLauncher {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = "ColorRunner";
config.width = 967;
config.height = 544;
config.width = 1000;
config.height = 563;
new LwjglApplication(new CRGame(), config);
}