From 553fdb261ee5000b526b426d7a9cb9da8765f3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 10 Aug 2014 01:59:18 +0200 Subject: [PATCH] Added Scene2D actors + colors + jumping --- .../actors/{DemoActor.java => CRActor.java} | 28 ++-- .../colorrunner/actors/CRGravityActor.java | 37 +++++ .../samdev/colorrunner/actors/CRPlayer.java | 46 ++++++ .../samdev/colorrunner/assets/CRAssets.java | 42 +++++ .../input/CRGameInputProcessor.java | 156 ++++++++++++++++++ .../colorrunner/input/GameInputListener.java | 6 + .../colorrunner/input/SwipeDirection.java | 8 + .../screens/gameScreen/GameScreen.java | 51 ++++-- 8 files changed, 342 insertions(+), 32 deletions(-) rename core/src/de/samdev/colorrunner/actors/{DemoActor.java => CRActor.java} (51%) create mode 100644 core/src/de/samdev/colorrunner/actors/CRGravityActor.java create mode 100644 core/src/de/samdev/colorrunner/actors/CRPlayer.java create mode 100644 core/src/de/samdev/colorrunner/assets/CRAssets.java create mode 100644 core/src/de/samdev/colorrunner/input/CRGameInputProcessor.java create mode 100644 core/src/de/samdev/colorrunner/input/GameInputListener.java create mode 100644 core/src/de/samdev/colorrunner/input/SwipeDirection.java diff --git a/core/src/de/samdev/colorrunner/actors/DemoActor.java b/core/src/de/samdev/colorrunner/actors/CRActor.java similarity index 51% rename from core/src/de/samdev/colorrunner/actors/DemoActor.java rename to core/src/de/samdev/colorrunner/actors/CRActor.java index e91dfd9..9bf4e92 100644 --- a/core/src/de/samdev/colorrunner/actors/DemoActor.java +++ b/core/src/de/samdev/colorrunner/actors/CRActor.java @@ -7,17 +7,18 @@ import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.InputListener; -public class DemoActor extends Actor { - Texture texture = new Texture(Gdx.files.internal("data/jet.png")); - public boolean started = false; - - public DemoActor() { - setBounds(getX(), getY(), texture.getWidth(), texture.getHeight()); +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"); + Gdx.app.log("CRGame", "touch my one more time"); return true; } }); @@ -25,18 +26,15 @@ public class DemoActor extends Actor { @Override public void draw(Batch batch, float alpha) { - batch.draw(texture, 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); + 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; + } } diff --git a/core/src/de/samdev/colorrunner/actors/CRGravityActor.java b/core/src/de/samdev/colorrunner/actors/CRGravityActor.java new file mode 100644 index 0000000..06a377c --- /dev/null +++ b/core/src/de/samdev/colorrunner/actors/CRGravityActor.java @@ -0,0 +1,37 @@ +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; + } +} diff --git a/core/src/de/samdev/colorrunner/actors/CRPlayer.java b/core/src/de/samdev/colorrunner/actors/CRPlayer.java new file mode 100644 index 0000000..fffa860 --- /dev/null +++ b/core/src/de/samdev/colorrunner/actors/CRPlayer.java @@ -0,0 +1,46 @@ +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); + } +} diff --git a/core/src/de/samdev/colorrunner/assets/CRAssets.java b/core/src/de/samdev/colorrunner/assets/CRAssets.java new file mode 100644 index 0000000..8991e89 --- /dev/null +++ b/core/src/de/samdev/colorrunner/assets/CRAssets.java @@ -0,0 +1,42 @@ +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); + } + } +} diff --git a/core/src/de/samdev/colorrunner/input/CRGameInputProcessor.java b/core/src/de/samdev/colorrunner/input/CRGameInputProcessor.java new file mode 100644 index 0000000..89b685d --- /dev/null +++ b/core/src/de/samdev/colorrunner/input/CRGameInputProcessor.java @@ -0,0 +1,156 @@ +package de.samdev.colorrunner.input; + +import com.badlogic.gdx.Input; +import com.badlogic.gdx.InputProcessor; +import com.badlogic.gdx.input.GestureDetector.GestureListener; +import com.badlogic.gdx.math.Vector2; + +public class CRGameInputProcessor implements InputProcessor, GestureListener { + private GameInputListener owner; + + public CRGameInputProcessor(GameInputListener gsc) { + this.owner = gsc; + } + + @Override + public boolean keyDown(int keycode) { + if (keycode == Input.Keys.UP) { + owner.switchColor(SwipeDirection.UP); + return true; + } + + if (keycode == Input.Keys.RIGHT) { + owner.switchColor(SwipeDirection.RIGHT); + return true; + } + + if (keycode == Input.Keys.DOWN) { + owner.switchColor(SwipeDirection.DOWN); + return true; + } + + if (keycode == Input.Keys.LEFT) { + owner.switchColor(SwipeDirection.LEFT); + return true; + } + + if (keycode == Input.Keys.SPACE) { + owner.doJump(); + return true; + } + + return false; + } + + @Override + public boolean keyUp(int keycode) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean keyTyped(char character) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean touchDown(int screenX, int screenY, int pointer, int button) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean touchUp(int screenX, int screenY, int pointer, int button) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean touchDragged(int screenX, int screenY, int pointer) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean mouseMoved(int screenX, int screenY) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean scrolled(int amount) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean touchDown(float x, float y, int pointer, int button) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean tap(float x, float y, int count, int button) { + owner.doJump(); + return false; + } + + @Override + public boolean longPress(float x, float y) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean fling(float velocityX, float velocityY, int button) { + if (velocityX == velocityY) return false; + + if (velocityY > Math.abs(velocityX)) { + owner.switchColor(SwipeDirection.UP); + return true; + } + + if (Math.abs(velocityY) < velocityX) { + owner.switchColor(SwipeDirection.LEFT); + return true; + } + + if (velocityY < Math.abs(velocityX)) { + owner.switchColor(SwipeDirection.DOWN); + return true; + } + + if (Math.abs(velocityY) > velocityX) { + owner.switchColor(SwipeDirection.RIGHT); + return true; + } + + return false; + } + + @Override + public boolean pan(float x, float y, float deltaX, float deltaY) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean panStop(float x, float y, int pointer, int button) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean zoom(float initialDistance, float distance) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) { + // TODO Auto-generated method stub + return false; + } + +} diff --git a/core/src/de/samdev/colorrunner/input/GameInputListener.java b/core/src/de/samdev/colorrunner/input/GameInputListener.java new file mode 100644 index 0000000..c6b5e55 --- /dev/null +++ b/core/src/de/samdev/colorrunner/input/GameInputListener.java @@ -0,0 +1,6 @@ +package de.samdev.colorrunner.input; + +public interface GameInputListener { + public void doJump(); + public void switchColor(SwipeDirection sd); +} diff --git a/core/src/de/samdev/colorrunner/input/SwipeDirection.java b/core/src/de/samdev/colorrunner/input/SwipeDirection.java new file mode 100644 index 0000000..dc1f5bd --- /dev/null +++ b/core/src/de/samdev/colorrunner/input/SwipeDirection.java @@ -0,0 +1,8 @@ +package de.samdev.colorrunner.input; + +public enum SwipeDirection { + UP, + RIGHT, + DOWN, + LEFT; +} diff --git a/core/src/de/samdev/colorrunner/screens/gameScreen/GameScreen.java b/core/src/de/samdev/colorrunner/screens/gameScreen/GameScreen.java index b26ccb2..1e7bbb3 100644 --- a/core/src/de/samdev/colorrunner/screens/gameScreen/GameScreen.java +++ b/core/src/de/samdev/colorrunner/screens/gameScreen/GameScreen.java @@ -1,30 +1,28 @@ package de.samdev.colorrunner.screens.gameScreen; import com.badlogic.gdx.Gdx; +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.scenes.scene2d.Touchable; -import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction; import com.badlogic.gdx.utils.viewport.ExtendViewport; -import de.samdev.colorrunner.actors.DemoActor; +import de.samdev.colorrunner.actors.CRPlayer; +import de.samdev.colorrunner.input.CRGameInputProcessor; +import de.samdev.colorrunner.input.GameInputListener; +import de.samdev.colorrunner.input.SwipeDirection; -public class GameScreen implements Screen { +public class GameScreen implements Screen, GameInputListener { private Stage stage; + private CRPlayer player; + public GameScreen() { - stage = new Stage(new ExtendViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight())); //TODO fix w/h ??? + stage = new Stage(new ExtendViewport(967, 544)); - DemoActor myActor = new DemoActor(); - myActor.setTouchable(Touchable.enabled); - stage.addActor(myActor); - - - MoveToAction moveAction = new MoveToAction(); - moveAction.setPosition(300f, 0f); - moveAction.setDuration(10f); - myActor.addAction(moveAction); + player = new CRPlayer(); + stage.addActor(player); } @Override @@ -33,22 +31,27 @@ public class GameScreen implements Screen { stage.act(delta); stage.draw(); + +// stage.getViewport().getCamera().position.x = player.getX(); } @Override public void resize(int width, int height) { - // TODO Auto-generated method stub - + Gdx.app.log("GameScreen", "resize called"); } @Override public void show() { - Gdx.input.setInputProcessor(stage); + CRGameInputProcessor ip = new CRGameInputProcessor(this); + Gdx.input.setInputProcessor(new InputMultiplexer(stage, ip, new GestureDetector(ip))); + Gdx.app.log("GameScreen", "show called"); } @Override public void hide() { + Gdx.input.setInputProcessor(null); + Gdx.app.log("GameScreen", "hide called"); } @@ -67,4 +70,18 @@ public class GameScreen implements Screen { Gdx.app.log("GameScreen", "dispose called"); } + @Override + public void doJump() { + player.jump(); + + Gdx.app.log("GameScreen", "[DO] Jump"); + } + + @Override + public void switchColor(SwipeDirection sd) { + player.switchColorState(sd); + + Gdx.app.log("GameScreen", "[DO] Switch + " + sd.toString()); + } + }