Moving Obstacles can move with player-speed

This commit is contained in:
=
2018-01-02 11:59:44 +01:00
parent e5ce11150c
commit 0ab80bd6ad
6 changed files with 687 additions and 479 deletions

View File

@ -39,8 +39,10 @@ public class Obstacle extends Entity {
gridSize = calcGridSize();
}
public boolean isInSightDistance(Camera camera) {
return super.getLeftEdge() <= camera.getX() + 2;
public boolean isMovePathInSightDistance(Camera camera) {
if (moveComponent == null)
return false;
return moveComponent.getLeftmostPosition() - super.getWidth() / 2 <= camera.getX() + 2;
}
private Vector calcGridSize() {
@ -51,14 +53,17 @@ public class Obstacle extends Entity {
return new Vector(gridWidth, gridHeight);
}
public void moveWithMoveComponent(float frameTime) {
float distance = frameTime * moveComponent.getSpeed() * 0.001f;
public void moveWithMoveComponent(float frameTime, Player player) {
if(moveComponent.isHasPlayerXSpeed()){
moveComponent.calcSpeedForPlayerSpeed(player);
}
float distance = frameTime * moveComponent.getSpeed();
moveProgress += moveDirection * distance / moveComponent.getLength();
if(moveProgress < 0) {
if (moveProgress < 0) {
moveProgress *= -1;
moveDirection *= -1;
}
if(moveProgress > 1) {
if (moveProgress > 1) {
moveProgress = 2 - moveProgress;
moveDirection *= -1;
}

View File

@ -1,10 +1,10 @@
package de.frajul.endlessroll.levels;
import de.frajul.endlessroll.data.Vector;
import de.frajul.endlessroll.entities.Vertex;
import org.simpleframework.xml.Attribute;
import de.frajul.endlessroll.data.Vector;
import de.frajul.endlessroll.entities.Player;
public class MoveComponent {
@Attribute
@ -17,12 +17,15 @@ public class MoveComponent {
private float direction;
@Attribute
private float speed;
@Attribute
private boolean hasPlayerXSpeed;
private final float TRANSITION_VALUE = 0.001f;
private Vector position;
private float triangleWidth;
private float triangleHeight;
public void init(Vector obstaclePosition){
public void init(Vector obstaclePosition) {
double rotationRadians = Math.toRadians(getRotation());
double invertRotationRadians = Math.toRadians(90 - getRotation());
@ -34,6 +37,8 @@ public class MoveComponent {
x -= startOffset * triangleWidth;
y -= startOffset * triangleHeight;
this.position = new Vector(x, y);
speed *= TRANSITION_VALUE;
}
public float getLength() {
@ -56,8 +61,24 @@ public class MoveComponent {
return speed;
}
public Vector getPositionForProgress(float progress){
return new Vector(position.x + triangleWidth * progress, position.y + triangleHeight * progress);
public boolean isHasPlayerXSpeed() {
return hasPlayerXSpeed;
}
public void calcSpeedForPlayerSpeed(Player player) {
float xSpeed = player.getSpeed();
double invertRotationRadians = Math.toRadians(90 - getRotation());
float ySpeed = (float) (xSpeed * Math.sin(invertRotationRadians));
speed = (float) Math.sqrt(xSpeed * xSpeed + ySpeed * ySpeed);
}
public float getLeftmostPosition() {
return triangleWidth >= 0 ? position.x : position.x + triangleWidth;
}
public Vector getPositionForProgress(float progress) {
return new Vector(position.x + triangleWidth * progress,
position.y + triangleHeight * progress);
}
}

View File

@ -9,11 +9,9 @@ import de.frajul.endlessroll.data.Vector;
import de.frajul.endlessroll.entities.AnimatedEntity;
import de.frajul.endlessroll.entities.Background;
import de.frajul.endlessroll.entities.collectables.Collectables;
import de.frajul.endlessroll.entities.collectables.Energy;
import de.frajul.endlessroll.entities.Entity;
import de.frajul.endlessroll.entities.Obstacle;
import de.frajul.endlessroll.entities.Player;
import de.frajul.endlessroll.entities.collectables.Star;
import de.frajul.endlessroll.entities.particles.ParticleSystem;
import de.frajul.endlessroll.entities.textures.TexturePack;
import de.frajul.endlessroll.entities.tileLists.Ceiling;
@ -92,8 +90,8 @@ public abstract class Scene {
Entity entity = iterator.next();
if(entity instanceof Obstacle){
Obstacle obstacle = (Obstacle) entity;
if (obstacle.isMoving() && obstacle.isInSightDistance(camera))
obstacle.moveWithMoveComponent(timer.getFrameTimeSeconds());
if (obstacle.isMoving() && obstacle.isMovePathInSightDistance(camera))
obstacle.moveWithMoveComponent(timer.getFrameTimeSeconds(), player);
}
boolean remove = updateEntity(entity, timer);
if (remove)