Added Scene2D actors + colors + jumping

This commit is contained in:
Mike Schwörer 2014-08-10 01:59:18 +02:00
parent bc201739b8
commit 553fdb261e
8 changed files with 342 additions and 32 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,6 @@
package de.samdev.colorrunner.input;
public interface GameInputListener {
public void doJump();
public void switchColor(SwipeDirection sd);
}

View File

@ -0,0 +1,8 @@
package de.samdev.colorrunner.input;
public enum SwipeDirection {
UP,
RIGHT,
DOWN,
LEFT;
}

View File

@ -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());
}
}