Changed system of moving Obstacles
This commit is contained in:
@ -1,12 +1,10 @@
|
||||
package de.frajul.endlessroll.entities;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import de.frajul.endlessroll.data.Vector;
|
||||
import de.frajul.endlessroll.entities.textures.Texture;
|
||||
import de.frajul.endlessroll.levels.MoveComponent;
|
||||
import de.frajul.endlessroll.levels.ObstacleData;
|
||||
import de.frajul.endlessroll.levels.worlds.World;
|
||||
import de.frajul.endlessroll.main.game.Camera;
|
||||
|
||||
/**
|
||||
* Created by Julian on 20.11.2015.
|
||||
@ -20,28 +18,32 @@ public class Obstacle extends Entity {
|
||||
private Vector gridSize;
|
||||
private MoveComponent moveComponent;
|
||||
|
||||
private Vertex zeroPoint;
|
||||
private boolean clockwise;
|
||||
private float totalMoveDistance;
|
||||
private float moveProgress;
|
||||
private float moveDirection;
|
||||
|
||||
public Obstacle(World world, ObstacleData data, float terrainEdge) {
|
||||
super(world.getObstacleTexture(), new Vector(data.getX(), data.getY()), data.getWidth(), data.getHeight());
|
||||
super(world.getObstacleTexture(), new Vector(data.getX(), data.getY()), data.getWidth(),
|
||||
data.getHeight());
|
||||
this.deadly = data.isDeadly();
|
||||
this.floating = data.isFloating();
|
||||
this.moving = data.isMoving();
|
||||
this.moveComponent = data.getMoveComponent();
|
||||
if (moveComponent != null)
|
||||
moveComponent.shrink(new Vector(super.getWidth(), super.getHeight()));
|
||||
if (moveComponent != null && moving) {
|
||||
moveComponent.init(super.getPosition());
|
||||
moveProgress = moveComponent.getStartOffset();
|
||||
moveDirection = moveComponent.getDirection();
|
||||
}
|
||||
|
||||
if (!floating)
|
||||
super.setToTerrain(terrainEdge);
|
||||
if (moving)
|
||||
randomMovementData();
|
||||
gridSize = calcGridSize();
|
||||
}
|
||||
|
||||
private Vector calcGridSize(){
|
||||
public boolean isInSightDistance(Camera camera) {
|
||||
return super.getLeftEdge() <= camera.getX() + 2;
|
||||
}
|
||||
|
||||
private Vector calcGridSize() {
|
||||
int gridWidth = (int) (width / GRID_SQUARE_SIZE);
|
||||
int gridHeight = (int) (height / GRID_SQUARE_SIZE);
|
||||
gridWidth = Math.max(gridWidth, 2);
|
||||
@ -49,37 +51,18 @@ public class Obstacle extends Entity {
|
||||
return new Vector(gridWidth, gridHeight);
|
||||
}
|
||||
|
||||
public void moveWithMoveComponent(float value) {
|
||||
float distance = value * moveComponent.getSpeed() * 0.001f;
|
||||
moveProgress += distance / totalMoveDistance;
|
||||
moveProgress %= 1.0f;
|
||||
setPositionForMoveProgress();
|
||||
}
|
||||
|
||||
private void setPositionForMoveProgress() {
|
||||
float distance = 0;
|
||||
Vertex lastVertex = zeroPoint;
|
||||
while (true) {
|
||||
Vertex nextVertex = lastVertex.getNext(clockwise);
|
||||
float nextDistance = distance + (Math.abs(lastVertex.getX() - nextVertex.getX()) * 0.5f * moveComponent.getWidth()) + (Math.abs(lastVertex.getY() - nextVertex.getY()) * 0.5f * moveComponent.getHeight());
|
||||
if (nextDistance >= moveProgress * totalMoveDistance) {
|
||||
float distanceLeft = moveProgress * totalMoveDistance - distance;
|
||||
Vector direction = new Vector(nextVertex.getX(), nextVertex.getY()).translate(new Vector(lastVertex.getX(), lastVertex.getY()).negate());
|
||||
direction = direction.mul(0.5f * distanceLeft);
|
||||
super.setPosition(moveComponent.getPositionOfVertex(lastVertex).translate(direction));
|
||||
return;
|
||||
}
|
||||
distance = nextDistance;
|
||||
lastVertex = nextVertex;
|
||||
public void moveWithMoveComponent(float frameTime) {
|
||||
float distance = frameTime * moveComponent.getSpeed() * 0.001f;
|
||||
moveProgress += moveDirection * distance / moveComponent.getLength();
|
||||
if(moveProgress < 0) {
|
||||
moveProgress *= -1;
|
||||
moveDirection *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
private void randomMovementData() {
|
||||
Random random = new Random();
|
||||
int index = random.nextInt(4);
|
||||
zeroPoint = Vertex.values()[index];
|
||||
clockwise = random.nextBoolean();
|
||||
totalMoveDistance = moveComponent.getWidth() * 2 + moveComponent.getHeight() * 2;
|
||||
if(moveProgress > 1) {
|
||||
moveProgress = 2 - moveProgress;
|
||||
moveDirection *= -1;
|
||||
}
|
||||
super.setPosition(moveComponent.getPositionForProgress(moveProgress));
|
||||
}
|
||||
|
||||
public boolean isMoving() {
|
||||
@ -94,7 +77,7 @@ public class Obstacle extends Entity {
|
||||
return floating;
|
||||
}
|
||||
|
||||
public Vector getGridSize(){
|
||||
public Vector getGridSize() {
|
||||
return gridSize;
|
||||
}
|
||||
|
||||
|
@ -39,6 +39,10 @@ public class Player extends Entity {
|
||||
super.setHeight(RADIUS * 2);
|
||||
}
|
||||
|
||||
public void reset(){
|
||||
|
||||
}
|
||||
|
||||
public void startSuperPower(long duration) {
|
||||
this.superPowerDuration = duration;
|
||||
currentSuperPowerDuration = 0;
|
||||
@ -53,6 +57,9 @@ public class Player extends Entity {
|
||||
super.setToTerrain(terrainEdge);
|
||||
super.getPosition().x = START_X;
|
||||
super.setMovement(new Vector(speed, 0));
|
||||
gravityForce = 0;
|
||||
hasSuperPower = false;
|
||||
forces.clear();
|
||||
this.startSpeed = startSpeed;
|
||||
this.endSpeed = endSpeed;
|
||||
setSpeedByProgress(0);
|
||||
|
@ -8,49 +8,56 @@ import org.simpleframework.xml.Attribute;
|
||||
public class MoveComponent {
|
||||
|
||||
@Attribute
|
||||
private float width;
|
||||
private float length;
|
||||
@Attribute
|
||||
private float height;
|
||||
private float rotation;
|
||||
@Attribute
|
||||
private float x;
|
||||
private float startOffset;
|
||||
@Attribute
|
||||
private float y;
|
||||
private float direction;
|
||||
@Attribute
|
||||
private float speed;
|
||||
|
||||
public float getWidth() {
|
||||
return width;
|
||||
private Vector position;
|
||||
private float triangleWidth;
|
||||
private float triangleHeight;
|
||||
|
||||
public void init(Vector obstaclePosition){
|
||||
double rotationRadians = Math.toRadians(getRotation());
|
||||
double invertRotationRadians = Math.toRadians(90 - getRotation());
|
||||
|
||||
this.triangleWidth = (float) (length * Math.sin(rotationRadians));
|
||||
this.triangleHeight = (float) (length * Math.sin(invertRotationRadians));
|
||||
|
||||
float x = obstaclePosition.getX();
|
||||
float y = obstaclePosition.getY();
|
||||
x -= startOffset * triangleWidth;
|
||||
y -= startOffset * triangleHeight;
|
||||
this.position = new Vector(x, y);
|
||||
}
|
||||
|
||||
public float getHeight() {
|
||||
return height;
|
||||
public float getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public float getX() {
|
||||
return x;
|
||||
public float getRotation() {
|
||||
return rotation;
|
||||
}
|
||||
|
||||
public float getY() {
|
||||
return y;
|
||||
public float getStartOffset() {
|
||||
return startOffset;
|
||||
}
|
||||
|
||||
public float getDirection() {
|
||||
return direction;
|
||||
}
|
||||
|
||||
public float getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
public Vector getPositionOfVertex(Vertex vertex) {
|
||||
float x = this.x + (width / 2f) * vertex.getX();
|
||||
float y = this.y + (height / 2f) * vertex.getY();
|
||||
return new Vector(x, y);
|
||||
}
|
||||
|
||||
public void shrink(Vector value) {
|
||||
this.width -= value.getX();
|
||||
this.height -= value.getY();
|
||||
if (width < 0)
|
||||
width = 0;
|
||||
if (height < 0)
|
||||
height = 0;
|
||||
public Vector getPositionForProgress(float progress){
|
||||
return new Vector(position.x + triangleWidth * progress, position.y + triangleHeight * progress);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -92,7 +92,7 @@ public abstract class Scene {
|
||||
Entity entity = iterator.next();
|
||||
if(entity instanceof Obstacle){
|
||||
Obstacle obstacle = (Obstacle) entity;
|
||||
if (obstacle.isMoving())
|
||||
if (obstacle.isMoving() && obstacle.isInSightDistance(camera))
|
||||
obstacle.moveWithMoveComponent(timer.getFrameTimeSeconds());
|
||||
}
|
||||
boolean remove = updateEntity(entity, timer);
|
||||
|
Reference in New Issue
Block a user