ColorRunner/core/src/de/samdev/colorrunner/game/world/entities/gameentities/PlayerEntity.java

163 lines
5.1 KiB
Java
Raw Normal View History

2014-08-10 17:19:40 +02:00
package de.samdev.colorrunner.game.world.entities.gameentities;
2017-04-13 23:30:38 +02:00
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
2014-08-10 20:59:50 +02:00
import com.badlogic.gdx.graphics.Texture;
2014-08-10 19:41:06 +02:00
2014-08-10 20:59:50 +02:00
import de.samdev.colorrunner.game.renderer.CRAssets;
2014-08-10 19:41:06 +02:00
import de.samdev.colorrunner.game.world.CRGameWorld;
2014-08-11 00:24:55 +02:00
import de.samdev.colorrunner.game.world.SwipeDirection;
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
2014-08-10 17:19:40 +02:00
import de.samdev.colorrunner.game.world.entities.GravityEntity;
2014-08-11 18:23:07 +02:00
import de.samdev.colorrunner.game.world.entities.gameentities.floor.FloorTileEntity;
2017-04-25 18:44:34 +02:00
import de.samdev.colorrunner.game.world.map.provider.TriggerType;
2017-04-13 23:30:38 +02:00
import de.samdev.colorrunner.screens.menu.MainMenu;
2014-08-10 17:19:40 +02:00
import static de.samdev.colorrunner.game.world.map.provider.TriggerType.RUNBOTTOM;
2017-04-25 18:44:34 +02:00
import static de.samdev.colorrunner.game.world.map.provider.TriggerType.RUNTOP;
2014-08-10 17:19:40 +02:00
public class PlayerEntity extends GravityEntity {
public final static float PLAYER_WIDTH = 32;
public final static float PLAYER_HEIGHT = 32;
2014-08-10 19:41:06 +02:00
2014-08-10 20:59:50 +02:00
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;
2014-08-11 15:36:03 +02:00
public final static float PLAYER_ROTATION_SPEED = -200f;
2014-08-10 20:59:50 +02:00
private SwipeDirection phase = SwipeDirection.UP;
2014-08-11 15:36:03 +02:00
2017-04-25 18:44:34 +02:00
2014-08-10 20:59:50 +02:00
private boolean doFly = false;
2017-04-25 18:44:34 +02:00
private TriggerType triggerType;
2014-08-11 15:36:03 +02:00
2014-08-10 19:41:06 +02:00
public PlayerEntity(CRGameWorld _owner, float x, float y) {
super(_owner, x, y, PLAYER_WIDTH, PLAYER_HEIGHT);
2014-08-10 17:19:40 +02:00
}
@Override
2014-08-11 15:36:03 +02:00
public void update(float delta) {
2014-08-10 20:59:50 +02:00
if (velocity.x < PLAYER_TERMINAL_SPEED)
velocity.x += PLAYER_SPEED_FORCE * delta;
2014-08-11 15:36:03 +02:00
if (doFly) {
if ( getType == GetTriggerType.RUNBOTTOM)
velocity.y += PLAYER_FLY_FORCE * delta;
else if(getType == GetTriggerType.RUNTOP)
velocity.y += -PLAYER_FLY_FORCE * delta;
}
2014-08-10 20:59:50 +02:00
doFly = false;
2014-08-11 15:36:03 +02:00
2014-08-11 18:23:07 +02:00
updateRotation(delta);
2017-04-25 18:44:34 +02:00
triggerType = world.mapprovider.getTrigger((int)(bounds.x / FloorTileEntity.FLOORTILE_WIDTH), (int)(bounds.y / FloorTileEntity.FLOORTILE_HEIGHT));
switch (triggerType){
case RUNTOP:
getType = GetTriggerType.RUNTOP;
break;
case RUNBOTTOM:
getType = GetTriggerType.RUNBOTTOM;
break;
case FLY:
getType = GetTriggerType.FLY;
break;
}
2014-08-11 18:23:07 +02:00
super.update(delta);
}
private void updateRotation(float delta) {
if (((!isTouching_BOTTOM() && getType == GetTriggerType.RUNBOTTOM)||(!isTouching_TOP() && getType == GetTriggerType.RUNTOP) )|| visualRotation % 90 != 0) {
if (! isTouching_BOTTOM() && getType == GetTriggerType.RUNBOTTOM) {
2014-08-11 15:36:03 +02:00
visualRotation = visualRotation + PLAYER_ROTATION_SPEED * delta;
visualRotation = (visualRotation + 720f) % 360f;
2014-08-11 15:36:03 +02:00
float p = (float) ((Math.max(Math.abs(Math.cos(Math.toRadians(visualRotation + 45))), Math.abs(Math.sin(Math.toRadians(visualRotation + 45)))) * Math.sqrt(PLAYER_WIDTH * PLAYER_WIDTH + PLAYER_HEIGHT * PLAYER_HEIGHT)));
updateHitBox(p, p);
}
else if (!isTouching_TOP() && getType == GetTriggerType.RUNTOP){
visualRotation = visualRotation + PLAYER_ROTATION_SPEED * delta;
visualRotation = (visualRotation + 720f) % 360f;
float p = (float) ((Math.max(Math.abs(Math.cos(Math.toRadians(visualRotation + 45))), Math.abs(Math.sin(Math.toRadians(visualRotation + 45)))) * Math.sqrt(PLAYER_WIDTH * PLAYER_WIDTH + PLAYER_HEIGHT * PLAYER_HEIGHT)));
updateHitBox(p, p);
}
else {
2014-08-11 15:36:03 +02:00
visualRotation = (visualRotation + 720f) % 90f;
if (visualRotation < 45)
visualRotation = visualRotation + PLAYER_ROTATION_SPEED * delta;
else
visualRotation = visualRotation - PLAYER_ROTATION_SPEED * delta;
if (visualRotation >= 90f || visualRotation <= 0f)
visualRotation = 0f;
visualRotation = (visualRotation + 720f) % 90f;
float p = (float) ((Math.max(Math.abs(Math.cos(Math.toRadians(visualRotation + 45))), Math.abs(Math.sin(Math.toRadians(visualRotation + 45)))) * Math.sqrt(PLAYER_WIDTH * PLAYER_WIDTH + PLAYER_HEIGHT * PLAYER_HEIGHT)));
updateHitBox(p, p);
}
}
2014-08-10 19:41:06 +02:00
}
public void jump() {
if (isTouching_BOTTOM() && getType == GetTriggerType.RUNBOTTOM)
2014-08-10 19:41:06 +02:00
velocity.y = PLAYER_JUMP_FORCE;
else if(isTouching_TOP() && getType == GetTriggerType.RUNTOP)
velocity.y = -PLAYER_JUMP_FORCE;
2014-08-10 19:41:06 +02:00
}
public void switchPhase(SwipeDirection sd) {
2014-08-11 18:23:07 +02:00
if (phase == sd) return;
2014-08-10 20:59:50 +02:00
phase = sd;
2014-08-11 18:23:07 +02:00
for (CRGameEntity ent : world.entities) {
if (ent == this) continue;
if (ent.bounds.overlaps(bounds) && ent.canCollide(false, this) && ent instanceof FloorTileEntity)
ent.remove();
}
2014-08-10 19:41:06 +02:00
}
public void fly() {
if (!isTouching_BOTTOM() && getType == GetTriggerType.RUNBOTTOM)
doFly = true;
else if(isTouching_TOP() && getType == GetTriggerType.RUNTOP)
2014-08-10 20:59:50 +02:00
doFly = true;
}
@Override
public Texture getTexture() {
switch (phase) {
2014-08-11 15:36:03 +02:00
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;
2014-08-10 20:59:50 +02:00
}
2014-08-10 17:19:40 +02:00
}
2014-08-11 00:24:55 +02:00
@Override
public boolean canCollide(boolean actualCollision, CRGameEntity collider) {
return collider.canCollide(actualCollision, this);
}
public SwipeDirection getPhase() {
return phase;
}
2014-08-10 17:19:40 +02:00
}