PlayerController

~MIke
This commit is contained in:
Armin Benz 2017-04-30 19:53:36 +02:00
parent 1bac4713ed
commit d3491b1c7d
11 changed files with 205 additions and 120 deletions

View File

@ -4051,9 +4051,9 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -4553,7 +4553,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -5053,7 +5053,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -5553,7 +5553,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -6053,7 +6053,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -6553,7 +6553,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -7053,7 +7053,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -7553,7 +7553,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -8053,7 +8053,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -8553,7 +8553,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -9051,9 +9051,9 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -9546,14 +9546,14 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="23"/>
<tile gid="23"/>
<tile gid="23"/>
<tile gid="23"/>
<tile gid="23"/>
<tile gid="23"/>
<tile gid="23"/>
<tile gid="23"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -14056,7 +14056,6 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="17"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -14091,6 +14090,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="18"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -14590,7 +14590,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="18"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -15090,7 +15090,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="18"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -15590,7 +15590,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="18"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -16090,7 +16090,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="18"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -16590,7 +16590,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="18"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -17090,7 +17090,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="18"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -17590,7 +17590,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="18"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -18090,7 +18090,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="18"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -18590,7 +18590,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="18"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -19056,7 +19056,6 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="16"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -19091,6 +19090,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="18"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
@ -19590,7 +19590,7 @@
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="18"/>
<tile gid="0"/>
<tile gid="0"/>
<tile gid="0"/>

View File

@ -13,7 +13,6 @@ import java.util.List;
import de.samdev.colorrunner.CRGame;
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.PlayerEntity;
import de.samdev.colorrunner.game.world.map.provider.EndlessMapProvider;
import de.samdev.colorrunner.game.world.map.provider.MapProvider;
import de.samdev.colorrunner.input.GameInputListener;
import de.samdev.colorrunner.screens.gameScreen.GameScreen;
@ -101,7 +100,7 @@ public class CRGameWorld implements GameInputListener {
@Override
public void doJump() {
player.jump();
player.jumpPressed();
}
@Override
@ -109,11 +108,6 @@ public class CRGameWorld implements GameInputListener {
player.switchPhase(sd);
}
@Override
public void doFly() {
player.fly();
}
@Override
public void reset() {
reinitialize();

View File

@ -1,45 +0,0 @@
package de.samdev.colorrunner.game.world.entities;
import de.samdev.colorrunner.game.world.CRGameWorld;
import de.samdev.colorrunner.game.world.entities.gameentities.ControllingType;
public abstract class GravityEntity extends MovingEntity {
public float GRAVITY_FORCE = 700f;
public final static float TERMINAL_VELOCITY = 900f;
public ControllingType ctrlType = ControllingType.RUNBOTTOM;
public GravityEntity(CRGameWorld _owner, float width, float height) {
this(_owner, 0, 0, width, height);
}
public GravityEntity(CRGameWorld _owner, float x, float y, float width, float height) {
super(_owner, x, y, width, height);
}
@Override
public void update(float delta) {
switch (ctrlType){
case RUNTOP:
GRAVITY_FORCE = -700;
break;
case RUNBOTTOM:
GRAVITY_FORCE = 700;
break;
case FLY:
GRAVITY_FORCE = 400;
break;
}
velocity.y -= GRAVITY_FORCE * delta;
if (velocity.y < -TERMINAL_VELOCITY) {
velocity.y = -TERMINAL_VELOCITY;
}
super.update(delta);
if (isTouching_BOTTOM() && velocity.y < 0)
velocity.y = 0;
}
}

View File

@ -8,7 +8,7 @@ import de.samdev.colorrunner.CRGame;
import de.samdev.colorrunner.game.world.CRGameWorld;
public abstract class MovingEntity extends CRGameEntity {
protected Vector2 velocity = new Vector2();
public Vector2 velocity = new Vector2();
public final static float EPSILON = 1e-4f;
public final static float EPSILON_TOUCH = 5e-4f;

View File

@ -7,17 +7,18 @@ import de.samdev.colorrunner.game.renderer.CRAssets;
import de.samdev.colorrunner.game.world.CRGameWorld;
import de.samdev.colorrunner.game.world.SwipeDirection;
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
import de.samdev.colorrunner.game.world.entities.GravityEntity;
import de.samdev.colorrunner.game.world.entities.MovingEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.controller.AbstractPlayerController;
import de.samdev.colorrunner.game.world.entities.gameentities.controller.DefaultPlayerController;
import de.samdev.colorrunner.game.world.entities.gameentities.controller.FlyingPlayerController;
import de.samdev.colorrunner.game.world.entities.gameentities.controller.InvertedPlayerController;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.FloorTileEntity;
import de.samdev.colorrunner.game.world.map.provider.TriggerType;
public class PlayerEntity extends GravityEntity {
public class PlayerEntity extends MovingEntity {
public final static float PLAYER_WIDTH = 31.9f;
public final static float PLAYER_HEIGHT = 31.9f;
public final static float PLAYER_JUMP_FORCE = 356f;
public final static float PLAYER_FLY_FORCE = 250f;
public final static float PLAYER_SPEED_FORCE = 166f;
public final static float PLAYER_TERMINAL_SPEED = 320f;
@ -25,10 +26,12 @@ public class PlayerEntity extends GravityEntity {
private SwipeDirection phase = SwipeDirection.UP;
private boolean doFly = false;
private AbstractPlayerController controller;
public PlayerEntity(CRGameWorld _owner, float x, float y) {
super(_owner, x, y, PLAYER_WIDTH, PLAYER_HEIGHT);
controller = new DefaultPlayerController(this);
}
@Override
@ -36,29 +39,23 @@ public class PlayerEntity extends GravityEntity {
if (velocity.x < PLAYER_TERMINAL_SPEED)
velocity.x += PLAYER_SPEED_FORCE * delta;
if (doFly) {
if ( ctrlType == ControllingType.RUNBOTTOM)
velocity.y += PLAYER_FLY_FORCE * delta;
else if(ctrlType == ControllingType.RUNTOP)
velocity.y += -PLAYER_FLY_FORCE * delta;
}
doFly = false;
updateRotation(delta);
TriggerType trigger = world.mapprovider.getTrigger((int)((bounds.x + bounds.width/2f) / FloorTileEntity.FLOORTILE_WIDTH), (int)((bounds.y + bounds.height/2f) / FloorTileEntity.FLOORTILE_HEIGHT));
switch (trigger){
case RUNTOP:
ctrlType = ControllingType.RUNTOP;
if (controller.getControllerType() != ControllingType.RUNTOP) controller = new InvertedPlayerController(this);
break;
case RUNBOTTOM:
ctrlType = ControllingType.RUNBOTTOM;
if (controller.getControllerType() != ControllingType.RUNBOTTOM) controller = new DefaultPlayerController(this);
break;
case FLY:
ctrlType = ControllingType.FLY;
if (controller.getControllerType() != ControllingType.FLY) controller = new FlyingPlayerController(this);
break;
}
controller.update(delta);
super.update(delta);
}
@ -98,11 +95,8 @@ public class PlayerEntity extends GravityEntity {
}
}
public void jump() {
if (isTouching_BOTTOM() && ctrlType == ControllingType.RUNBOTTOM)
velocity.y = PLAYER_JUMP_FORCE;
else if(isTouching_TOP() && ctrlType == ControllingType.RUNTOP)
velocity.y = -PLAYER_JUMP_FORCE;
public void jumpPressed() {
controller.jumpPressed();
}
public void switchPhase(SwipeDirection sd) {
@ -118,13 +112,6 @@ public class PlayerEntity extends GravityEntity {
}
}
public void fly() {
if (!isTouching_BOTTOM() && ctrlType == ControllingType.RUNBOTTOM)
doFly = true;
else if(isTouching_TOP() && ctrlType == ControllingType.RUNTOP)
doFly = true;
}
@Override
public Texture getTexture() {
switch (phase) {

View File

@ -0,0 +1,23 @@
package de.samdev.colorrunner.game.world.entities.gameentities.controller;
import de.samdev.colorrunner.game.world.entities.gameentities.ControllingType;
import de.samdev.colorrunner.game.world.entities.gameentities.PlayerEntity;
public abstract class AbstractPlayerController {
public float GRAVITY_FORCE = 700f;
public final static float TERMINAL_VELOCITY = 900f;
public final static float PLAYER_JUMP_FORCE = 356f;
public final static float PLAYER_FLY_UP_FORCE = 555f;
protected final PlayerEntity Player;
public AbstractPlayerController(PlayerEntity e) {
Player = e;
}
public abstract void update(float delta);
public abstract ControllingType getControllerType();
public abstract void jumpPressed();
}

View File

@ -0,0 +1,49 @@
package de.samdev.colorrunner.game.world.entities.gameentities.controller;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import de.samdev.colorrunner.game.world.entities.gameentities.ControllingType;
import de.samdev.colorrunner.game.world.entities.gameentities.PlayerEntity;
public class DefaultPlayerController extends AbstractPlayerController {
private boolean isJumping = false;
public DefaultPlayerController(PlayerEntity e) {
super(e);
}
@Override
public void update(float delta) {
boolean down = Gdx.input.isKeyPressed(Input.Keys.SPACE) || Gdx.input.isTouched();
if (!down && isJumping) {
if (Player.velocity.y > 0) Player.velocity.y = 0;
isJumping = false;
}
if (! down) isJumping = false;
Player.velocity.y -= GRAVITY_FORCE * delta;
if (Player.velocity.y < -TERMINAL_VELOCITY) {
Player.velocity.y = -TERMINAL_VELOCITY;
}
if (Player.isTouching_BOTTOM() && Player.velocity.y < 0)
Player.velocity.y = 0;
}
@Override
public ControllingType getControllerType() {
return ControllingType.RUNBOTTOM;
}
@Override
public void jumpPressed() {
if (Player.isTouching_BOTTOM()) {
Player.velocity.y = PLAYER_JUMP_FORCE;
isJumping = true;
}
}
}

View File

@ -0,0 +1,32 @@
package de.samdev.colorrunner.game.world.entities.gameentities.controller;
import de.samdev.colorrunner.game.world.entities.gameentities.ControllingType;
import de.samdev.colorrunner.game.world.entities.gameentities.PlayerEntity;
public class FlyingPlayerController extends AbstractPlayerController {
public FlyingPlayerController(PlayerEntity e) {
super(e);
}
@Override
public void update(float delta) {
Player.velocity.y -= GRAVITY_FORCE * delta;
if (Player.velocity.y < -TERMINAL_VELOCITY) {
Player.velocity.y = -TERMINAL_VELOCITY;
}
if (Player.isTouching_BOTTOM() && Player.velocity.y < 0) Player.velocity.y = 0;
if (Player.isTouching_TOP() && Player.velocity.y > 0) Player.velocity.y = 0;
}
@Override
public ControllingType getControllerType() {
return ControllingType.FLY;
}
@Override
public void jumpPressed() {
Player.velocity.y += PLAYER_FLY_UP_FORCE;
}
}

View File

@ -0,0 +1,49 @@
package de.samdev.colorrunner.game.world.entities.gameentities.controller;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import de.samdev.colorrunner.game.world.entities.gameentities.ControllingType;
import de.samdev.colorrunner.game.world.entities.gameentities.PlayerEntity;
public class InvertedPlayerController extends AbstractPlayerController {
private boolean isJumping = false;
public InvertedPlayerController(PlayerEntity e) {
super(e);
}
@Override
public void update(float delta) {
boolean down = Gdx.input.isKeyPressed(Input.Keys.SPACE) || Gdx.input.isTouched();
if (!down && isJumping) {
if (Player.velocity.y < 0) Player.velocity.y = 0;
isJumping = false;
}
if (! down) isJumping = false;
Player.velocity.y += GRAVITY_FORCE * delta;
if (Player.velocity.y > TERMINAL_VELOCITY) {
Player.velocity.y = TERMINAL_VELOCITY;
}
if (Player.isTouching_TOP() && Player.velocity.y > 0)
Player.velocity.y = 0;
}
@Override
public ControllingType getControllerType() {
return ControllingType.RUNTOP;
}
@Override
public void jumpPressed() {
if (Player.isTouching_TOP()) {
Player.velocity.y = -PLAYER_JUMP_FORCE;
isJumping = true;
}
}
}

View File

@ -1,6 +1,5 @@
package de.samdev.colorrunner.input;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
@ -17,9 +16,7 @@ public class CRGameInputProcessor implements InputProcessor, GestureListener {
}
public void update() {
if (Gdx.input.isKeyPressed(Input.Keys.SPACE) || Gdx.input.isTouched()) {
owner.doFly();
}
}
@Override

View File

@ -4,7 +4,6 @@ import de.samdev.colorrunner.game.world.SwipeDirection;
public interface GameInputListener {
public void doJump();
public void doFly();
public void switchColor(SwipeDirection sd);
public void reset();
}