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

177 lines
5.7 KiB
Java
Raw Normal View History

2014-08-10 17:19:40 +02:00
package de.samdev.colorrunner.game.world.entities.gameentities;
2017-08-02 22:04:56 +02:00
import com.badlogic.gdx.Game;
2017-04-13 23:30:38 +02:00
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
2014-08-10 20:59:50 +02:00
import com.badlogic.gdx.graphics.Texture;
2014-08-10 19:41:06 +02:00
import de.samdev.colorrunner.CRGame;
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;
2017-04-30 19:53:36 +02:00
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.GravityPlayerController;
2017-04-30 21:21:53 +02:00
import de.samdev.colorrunner.game.world.entities.gameentities.controller.RunBottomPlayerController;
import de.samdev.colorrunner.game.world.entities.gameentities.controller.FlyPlayerController;
import de.samdev.colorrunner.game.world.entities.gameentities.controller.RunTopPlayerController;
2014-08-11 18:23:07 +02:00
import de.samdev.colorrunner.game.world.entities.gameentities.floor.FloorTileEntity;
import de.samdev.colorrunner.game.world.map.CRMapStorage;
import de.samdev.colorrunner.game.world.map.provider.StaticMapProvider;
2017-04-25 18:44:34 +02:00
import de.samdev.colorrunner.game.world.map.provider.TriggerType;
import de.samdev.colorrunner.screens.gameScreen.GameScreen;
2017-08-02 22:04:56 +02:00
import de.samdev.colorrunner.screens.menu.MainMenu;
2017-04-25 18:44:34 +02:00
2017-04-30 19:53:36 +02:00
public class PlayerEntity extends MovingEntity {
2017-04-30 18:06:50 +02:00
public final static float PLAYER_WIDTH = 31.9f;
public final static float PLAYER_HEIGHT = 31.9f;
2014-08-10 19:41:06 +02:00
2014-08-10 20:59:50 +02:00
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-30 19:53:36 +02:00
private AbstractPlayerController controller;
2014-08-11 15:36:03 +02:00
private Preferences ingameprefs = Gdx.app.getPreferences("ingamepreferences");
2014-08-10 19:41:06 +02:00
public PlayerEntity(CRGameWorld _owner, float x, float y) {
super(_owner, x, y, PLAYER_WIDTH, PLAYER_HEIGHT);
2017-04-30 19:53:36 +02:00
2017-04-30 21:21:53 +02:00
controller = new RunBottomPlayerController(this);
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
2014-08-11 18:23:07 +02:00
updateRotation(delta);
2017-05-17 21:02:23 +02:00
TriggerType trigger = getTrigger();
2017-04-30 18:06:50 +02:00
switch (trigger){
2017-04-25 18:44:34 +02:00
case RUNTOP:
2017-04-30 21:21:53 +02:00
if (controller.getControllerType() != ControllingType.RUNTOP) controller = new RunTopPlayerController(this);
2017-04-25 18:44:34 +02:00
break;
case RUNBOTTOM:
2017-04-30 21:21:53 +02:00
if (controller.getControllerType() != ControllingType.RUNBOTTOM) controller = new RunBottomPlayerController(this);
2017-04-25 18:44:34 +02:00
break;
case FLY:
2017-04-30 21:21:53 +02:00
if (controller.getControllerType() != ControllingType.FLY) controller = new FlyPlayerController(this);
2017-04-25 18:44:34 +02:00
break;
case GRAVITY:
if (controller.getControllerType() != ControllingType.GRAVITY) controller = new GravityPlayerController(this);
break;
2017-08-02 22:04:56 +02:00
case END:
//((Game)Gdx.app.getApplicationListener()).setScreen(new MainMenu());
int currantlvl = ingameprefs.getInteger("currantlvl", 1);
currantlvl++;
int highestlevel = ingameprefs.getInteger("highestlevel", 1);
if(currantlvl > highestlevel)
highestlevel = currantlvl;
ingameprefs.putInteger("highestlevel", highestlevel);
ingameprefs.putInteger("currantlvl", currantlvl);
ingameprefs.flush();
2017-11-22 17:20:52 +01:00
int maximumlvl = 4;
if(currantlvl > maximumlvl)
((Game)Gdx.app.getApplicationListener()).setScreen(new MainMenu());
else
CRGame.Inst().setScreen(new GameScreen(new StaticMapProvider(CRMapStorage.map_map.get(currantlvl))));
2017-08-02 22:04:56 +02:00
world.music.stop();
break;
2017-04-25 18:44:34 +02:00
}
2017-04-30 19:53:36 +02:00
controller.update(delta);
2014-08-11 18:23:07 +02:00
super.update(delta);
}
private void updateRotation(float delta) {
2017-04-30 18:06:50 +02:00
boolean touch = isTouching_ANY();
boolean aligned = (visualRotation % 90 == 0);
if (!touch)
{
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 if (touch && aligned)
{
// all ok - move along
}
else if (touch && !aligned)
{
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);
}
else
{
Gdx.app.error("PlayerEntity", "updateRotation::WTF");
2014-08-11 15:36:03 +02:00
}
2014-08-10 19:41:06 +02:00
}
2017-04-30 19:53:36 +02:00
public void jumpPressed() {
controller.jumpPressed();
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
}
2014-08-10 20:59:50 +02:00
@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
}