From 4282c0fdfda84907067fa0b6f3a8b7b38df1f159 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sun, 3 Dec 2017 16:59:55 +0100 Subject: [PATCH] Changed system of moving Obstacles --- .../assets/debug/levelpacks/Grasslands.xml | 460 +++++++++--------- app/src/main/assets/levelpacks/Grasslands.xml | 460 +++++++++--------- .../main/assets/levelpacks/Icy Mountains.xml | 55 +++ .../frajul/endlessroll/entities/Obstacle.java | 69 +-- .../frajul/endlessroll/entities/Player.java | 7 + .../endlessroll/levels/MoveComponent.java | 57 ++- .../frajul/endlessroll/main/game/Scene.java | 2 +- 7 files changed, 581 insertions(+), 529 deletions(-) diff --git a/app/build/intermediates/assets/debug/levelpacks/Grasslands.xml b/app/build/intermediates/assets/debug/levelpacks/Grasslands.xml index 8267f19..064ce26 100644 --- a/app/build/intermediates/assets/debug/levelpacks/Grasslands.xml +++ b/app/build/intermediates/assets/debug/levelpacks/Grasslands.xml @@ -11,7 +11,7 @@ - + @@ -33,34 +33,34 @@ - + - + - + - + - + - + - + - + - + - + @@ -82,40 +82,40 @@ - + - + - + - + - + - + - + - + - + - + - + - + @@ -141,31 +141,31 @@ - + - + - + - + - + - + - + - + - + @@ -187,19 +187,19 @@ - + - + - + - + - + @@ -220,16 +220,16 @@ - + - + - + - + @@ -251,31 +251,31 @@ - + - + - + - + - + - + - + - + - + @@ -297,58 +297,58 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -371,55 +371,55 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -441,94 +441,94 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -549,31 +549,31 @@ - + - + - + - + - + - + - + - + - + @@ -599,76 +599,76 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -690,70 +690,70 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -780,88 +780,88 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -883,37 +883,37 @@ - + - + - + - + - + - + - + - + - + - + - + @@ -938,61 +938,61 @@ - + - + - + - + - + - + - + - + - + - + - + - - + + - + - - + + - + - + - + - + - + diff --git a/app/src/main/assets/levelpacks/Grasslands.xml b/app/src/main/assets/levelpacks/Grasslands.xml index 8267f19..064ce26 100644 --- a/app/src/main/assets/levelpacks/Grasslands.xml +++ b/app/src/main/assets/levelpacks/Grasslands.xml @@ -11,7 +11,7 @@ - + @@ -33,34 +33,34 @@ - + - + - + - + - + - + - + - + - + - + @@ -82,40 +82,40 @@ - + - + - + - + - + - + - + - + - + - + - + - + @@ -141,31 +141,31 @@ - + - + - + - + - + - + - + - + - + @@ -187,19 +187,19 @@ - + - + - + - + - + @@ -220,16 +220,16 @@ - + - + - + - + @@ -251,31 +251,31 @@ - + - + - + - + - + - + - + - + - + @@ -297,58 +297,58 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -371,55 +371,55 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -441,94 +441,94 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -549,31 +549,31 @@ - + - + - + - + - + - + - + - + - + @@ -599,76 +599,76 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -690,70 +690,70 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -780,88 +780,88 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -883,37 +883,37 @@ - + - + - + - + - + - + - + - + - + - + - + @@ -938,61 +938,61 @@ - + - + - + - + - + - + - + - + - + - + - + - - + + - + - - + + - + - + - + - + - + diff --git a/app/src/main/assets/levelpacks/Icy Mountains.xml b/app/src/main/assets/levelpacks/Icy Mountains.xml index 4fdba71..1c077de 100644 --- a/app/src/main/assets/levelpacks/Icy Mountains.xml +++ b/app/src/main/assets/levelpacks/Icy Mountains.xml @@ -33,5 +33,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/de/frajul/endlessroll/entities/Obstacle.java b/app/src/main/java/de/frajul/endlessroll/entities/Obstacle.java index e56711a..d30e13d 100644 --- a/app/src/main/java/de/frajul/endlessroll/entities/Obstacle.java +++ b/app/src/main/java/de/frajul/endlessroll/entities/Obstacle.java @@ -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; } diff --git a/app/src/main/java/de/frajul/endlessroll/entities/Player.java b/app/src/main/java/de/frajul/endlessroll/entities/Player.java index 8ce4840..cae57fd 100644 --- a/app/src/main/java/de/frajul/endlessroll/entities/Player.java +++ b/app/src/main/java/de/frajul/endlessroll/entities/Player.java @@ -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); diff --git a/app/src/main/java/de/frajul/endlessroll/levels/MoveComponent.java b/app/src/main/java/de/frajul/endlessroll/levels/MoveComponent.java index 9a79808..9bd3ef3 100644 --- a/app/src/main/java/de/frajul/endlessroll/levels/MoveComponent.java +++ b/app/src/main/java/de/frajul/endlessroll/levels/MoveComponent.java @@ -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); } } diff --git a/app/src/main/java/de/frajul/endlessroll/main/game/Scene.java b/app/src/main/java/de/frajul/endlessroll/main/game/Scene.java index 2ee76de..516828d 100644 --- a/app/src/main/java/de/frajul/endlessroll/main/game/Scene.java +++ b/app/src/main/java/de/frajul/endlessroll/main/game/Scene.java @@ -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);