Skip to content

Commit 19cf524

Browse files
committed
Add world keyboard input with movement and nice tweening
Change map render to keep colour with map for nicer background colour
1 parent 8769a38 commit 19cf524

16 files changed

+554
-68
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package uk.org.ulcompsoc.tesseract;
2+
3+
import com.badlogic.ashley.core.Entity;
4+
import com.badlogic.gdx.math.Vector2;
5+
6+
/**
7+
* @author Ashley Davis (SgtCoDFish)
8+
*/
9+
public class Move {
10+
public enum Type {
11+
TILE, ABSOLUTE;
12+
}
13+
14+
public final Entity mover;
15+
16+
public final Vector2 start;
17+
18+
public final float nx;
19+
public final float ny;
20+
public final Type type;
21+
22+
public final int stepCount;
23+
public final float timePerStep;
24+
25+
public int currentStep;
26+
27+
public float durationRemaining;
28+
public float stepTime;
29+
30+
public final boolean isSolid;
31+
32+
// alternates between x and y coords; 0 = x, 1 = y, 2 = x, etc.
33+
public final float[] steps;
34+
35+
public Move(Entity mover, Type type, Vector2 start, float nx, float ny, float duration, boolean isSolid) {
36+
this.mover = mover;
37+
38+
this.type = type;
39+
40+
this.start = start;
41+
42+
this.nx = nx;
43+
this.ny = ny;
44+
45+
this.currentStep = 0;
46+
this.stepCount = calcStepCount();
47+
48+
this.durationRemaining = duration;
49+
this.stepTime = 0.0f;
50+
this.timePerStep = durationRemaining / (float) this.stepCount;
51+
52+
this.isSolid = isSolid;
53+
54+
float[] stepsTemp = new float[2 * stepCount];
55+
56+
final float xDiff = nx - start.x;
57+
final float yDiff = ny - start.y;
58+
59+
final float xStepSize = xDiff / stepCount;
60+
final float yStepSize = yDiff / stepCount;
61+
62+
for (int i = 0; i < 2 * stepCount; i += 2) {
63+
if (i == 0) {
64+
stepsTemp[i] = start.x;
65+
stepsTemp[i + 1] = start.y;
66+
} else if (i == (2 * stepCount) - 2) {
67+
stepsTemp[i] = nx;
68+
stepsTemp[i + 1] = ny;
69+
} else {
70+
stepsTemp[i] = start.x + xStepSize * (i / 2);
71+
stepsTemp[i + 1] = start.y + yStepSize * (i / 2);
72+
}
73+
}
74+
75+
steps = stepsTemp;
76+
}
77+
78+
public boolean update(float deltaTime) {
79+
durationRemaining -= deltaTime;
80+
stepTime += deltaTime;
81+
82+
final int stepsThisTick = (int) (stepTime / timePerStep);
83+
84+
if (stepsThisTick > 0) {
85+
stepTime = stepTime % timePerStep;
86+
currentStep += stepsThisTick;
87+
88+
if (currentStep >= stepCount) {
89+
currentStep = stepCount - 1;
90+
}
91+
}
92+
93+
return isDone();
94+
}
95+
96+
public boolean isDone() {
97+
return durationRemaining <= 0.0f;
98+
}
99+
100+
public float getCurrentX() {
101+
return steps[currentStep * 2];
102+
}
103+
104+
public float getCurrentY() {
105+
return steps[currentStep * 2 + 1];
106+
}
107+
108+
// only meaningful at constructor, returns min of 1
109+
private int calcStepCount() {
110+
float dist = (float) Math.sqrt(Math.pow(ny - start.y, 2) + Math.pow(nx - start.x, 2));
111+
int stepCount = (int) Math.ceil(dist / WorldConstants.MOVE_STEP_LENGTH);
112+
113+
return (stepCount == 0 ? 1 : stepCount);
114+
}
115+
}

core/src/uk/org/ulcompsoc/tesseract/TesseractMain.java

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,27 @@
1010
import uk.org.ulcompsoc.tesseract.components.Position;
1111
import uk.org.ulcompsoc.tesseract.components.RelativePosition;
1212
import uk.org.ulcompsoc.tesseract.components.Renderable;
13+
import uk.org.ulcompsoc.tesseract.components.Renderable.Facing;
1314
import uk.org.ulcompsoc.tesseract.components.Stats;
1415
import uk.org.ulcompsoc.tesseract.components.Text;
16+
import uk.org.ulcompsoc.tesseract.components.WorldPlayerInputListener;
1517
import uk.org.ulcompsoc.tesseract.systems.BattleAttackSystem;
1618
import uk.org.ulcompsoc.tesseract.systems.BattleDialogRenderSystem;
1719
import uk.org.ulcompsoc.tesseract.systems.BattleInputSystem;
1820
import uk.org.ulcompsoc.tesseract.systems.BattleMessageSystem;
1921
import uk.org.ulcompsoc.tesseract.systems.FocusTakingSystem;
22+
import uk.org.ulcompsoc.tesseract.systems.MovementSystem;
2023
import uk.org.ulcompsoc.tesseract.systems.RenderSystem;
2124
import uk.org.ulcompsoc.tesseract.systems.TextRenderSystem;
25+
import uk.org.ulcompsoc.tesseract.systems.WorldPlayerInputSystem;
2226
import uk.org.ulcompsoc.tesseract.tiled.TesseractMap;
2327

2428
import com.badlogic.ashley.core.Engine;
2529
import com.badlogic.ashley.core.Entity;
2630
import com.badlogic.gdx.Application;
2731
import com.badlogic.gdx.ApplicationAdapter;
2832
import com.badlogic.gdx.Gdx;
33+
import com.badlogic.gdx.Input.Keys;
2934
import com.badlogic.gdx.graphics.Camera;
3035
import com.badlogic.gdx.graphics.Color;
3136
import com.badlogic.gdx.graphics.GL20;
@@ -40,6 +45,8 @@
4045
import com.badlogic.gdx.math.Rectangle;
4146

4247
public class TesseractMain extends ApplicationAdapter {
48+
public static final String PLAYER_NAME = "Valiant Hero™";
49+
4350
private SpriteBatch batch = null;
4451
private Camera camera = null;
4552

@@ -68,8 +75,10 @@ public class TesseractMain extends ApplicationAdapter {
6875
private Entity rageText = null;
6976

7077
public static final String[] mapNames = { "world1/world1.tmx" };
71-
private TesseractMap[] maps = null;
72-
public static TesseractMap currentMap = null;
78+
public static final Color[] mapColors = { new Color(80.0f / 255.0f, 172.0f / 255.0f, 61.0f / 255.0f,
79+
1.0f) };
80+
private static TesseractMap[] maps = null;
81+
public static int currentMapIndex = 0;
7382

7483
@SuppressWarnings("unused")
7584
private GameState gameState = null;
@@ -95,27 +104,11 @@ public void create() {
95104

96105
batch = new SpriteBatch();
97106
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
98-
((OrthographicCamera) camera).setToOrtho(false, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
99107

100-
// if (Gdx.app.getType() == ApplicationType.WebGL) {
101108
font = new BitmapFont(Gdx.files.internal("fonts/robotobm16.fnt"), Gdx.files.internal("fonts/robotobm16.png"),
102109
false);
103110
bigFont = new BitmapFont(Gdx.files.internal("fonts/robotobm24.fnt"),
104111
Gdx.files.internal("fonts/robotobm24.png"), false);
105-
// } else {
106-
// com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator
107-
// fontGenerator = new
108-
// com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator(
109-
// Gdx.files.internal("fonts/RobotoRegular.ttf"));
110-
// com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter
111-
// parameter = new
112-
// com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter();
113-
// parameter.size = 16;
114-
// font = fontGenerator.generateFont(parameter);
115-
// parameter.size = 24;
116-
// bigFont = fontGenerator.generateFont(parameter);
117-
// fontGenerator.dispose();
118-
// }
119112

120113
playerTexture = new Texture(Gdx.files.internal("player/basicPlayer.png"));
121114
playerRegions = TextureRegion.split(playerTexture, WorldConstants.TILE_WIDTH, WorldConstants.TILE_HEIGHT)[0];
@@ -133,19 +126,40 @@ public void create() {
133126
initBattleEngine(battleEngine);
134127
initWorldEngine(worldEngine);
135128

136-
currentEngine = worldEngine;
129+
changeToWorld();
137130
}
138131

139132
@Override
140133
public void render() {
141134
float deltaTime = Gdx.graphics.getDeltaTime();
142135

143-
Gdx.gl.glClearColor(0.0f, 0.8f, 0.0f, 0.0f);
136+
Gdx.gl.glClearColor(mapColors[currentMapIndex].r, mapColors[currentMapIndex].g, mapColors[currentMapIndex].b,
137+
mapColors[currentMapIndex].a);
144138
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
145139

140+
if (Gdx.input.isKeyJustPressed(Keys.F10)) {
141+
if (currentEngine.equals(battleEngine)) {
142+
changeToWorld();
143+
} else {
144+
changeToBattle();
145+
}
146+
}
146147
currentEngine.update(deltaTime);
147148
}
148149

150+
public void changeToBattle() {
151+
this.currentEngine = battleEngine;
152+
153+
((OrthographicCamera) camera).setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
154+
camera.update();
155+
}
156+
157+
public void changeToWorld() {
158+
this.currentEngine = worldEngine;
159+
((OrthographicCamera) camera).setToOrtho(false, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
160+
camera.update();
161+
}
162+
149163
public void initWorldEngine(Engine engine) {
150164
maps = new TesseractMap[mapNames.length];
151165
TmxMapLoader mapLoader = new TmxMapLoader();
@@ -163,39 +177,44 @@ public void initWorldEngine(Engine engine) {
163177
engine.addEntity(maps[i].zLayerEntity);
164178
}
165179

166-
currentMap = maps[0];
180+
currentMapIndex = 0;
167181

168182
worldPlayerEntity = new Entity();
169-
worldPlayerEntity.add(new Renderable(playerRegions[1], playerRegions[0], playerRegions[3], playerRegions[2])
170-
.setPrioritity(50));
171-
worldPlayerEntity.add(currentMap.findPlayerPosition());
183+
worldPlayerEntity.add(new Renderable(Facing.DOWN, playerRegions[1], playerRegions[0], playerRegions[3],
184+
playerRegions[2]).setPrioritity(50));
185+
worldPlayerEntity.add(getCurrentMap().findPlayerPosition());
172186
worldPlayerEntity.add(new FocusTaker(camera));
187+
worldPlayerEntity.add(new Player(PLAYER_NAME));
188+
worldPlayerEntity.add(new WorldPlayerInputListener());
173189

174190
engine.addEntity(worldPlayerEntity);
175191

192+
engine.addSystem(new WorldPlayerInputSystem(50));
193+
engine.addSystem(new MovementSystem(getCurrentMap(), 75));
194+
engine.addSystem(new FocusTakingSystem(100));
176195
engine.addSystem(new RenderSystem(batch, camera, 1000));
177-
engine.addSystem(new FocusTakingSystem(10));
178196
}
179197

180198
public void initBattleEngine(Engine engine) {
181-
final int yTile = 12;
199+
final float yTile = 12 * WorldConstants.TILE_HEIGHT;
182200

183201
battlePlayerEntity = new Entity();
184202

185-
battlePlayerEntity.add(new Position(17, yTile));
186-
battlePlayerEntity.add(new Renderable(playerRegions[1], playerRegions[0], playerRegions[3], playerRegions[2])
187-
.setPrioritity(50));
203+
battlePlayerEntity.add(new Position(17 * WorldConstants.TILE_WIDTH, yTile));
204+
battlePlayerEntity.add(new Renderable(Facing.DOWN, playerRegions[1], playerRegions[0], playerRegions[3],
205+
playerRegions[2]).setPrioritity(50));
188206
battlePlayerEntity.add(new Stats(100, 25, 4));
189207

190-
Player playerComp = new Player();
208+
Player playerComp = new Player(PLAYER_NAME);
191209
battlePlayerEntity.add(playerComp);
192210
battlePlayerEntity.add(new Named(playerComp.name));
193211

194212
TextureRegion slimeRegion = TextureRegion.split(slimeTexture, WorldConstants.TILE_WIDTH,
195213
WorldConstants.TILE_HEIGHT)[0][0];
196214

197215
slimeEntity = new Entity();
198-
slimeEntity.add(new Position(3, yTile)).add(new Renderable(slimeRegion).setPrioritity(50));
216+
slimeEntity.add(new Position(3 * WorldConstants.TILE_WIDTH, yTile)).add(
217+
new Renderable(slimeRegion).setPrioritity(50));
199218
slimeEntity.add(new Stats(50, 2, 2));
200219

201220
Enemy slime1 = new Enemy("Green Ooze");
@@ -271,6 +290,10 @@ public void initBattleEngine(Engine engine) {
271290
engine.addSystem(new TextRenderSystem(batch, font, 3000));
272291
}
273292

293+
public static TesseractMap getCurrentMap() {
294+
return maps[currentMapIndex];
295+
}
296+
274297
@Override
275298
public void dispose() {
276299
super.dispose();

core/src/uk/org/ulcompsoc/tesseract/WorldConstants.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
* @author Ashley Davis (SgtCoDFish)
55
*/
66
public class WorldConstants {
7-
public static boolean DEBUG = false;
8-
public static Difficulty DIFFICULTY = Difficulty.EASY;
7+
public static boolean DEBUG = false;
8+
public static Difficulty DIFFICULTY = Difficulty.EASY;
99

10-
public static final int TILE_WIDTH = 32;
11-
public static final int TILE_HEIGHT = 32;
10+
public static final int TILE_WIDTH = 32;
11+
public static final int TILE_HEIGHT = 32;
12+
13+
public static final float TILE_DIAG = (float) Math.sqrt(TILE_WIDTH * TILE_WIDTH + TILE_HEIGHT
14+
* TILE_HEIGHT);
15+
public static final float STEPS_PER_TILE_DIAG = 8.0f;
16+
public static final float MOVE_STEP_LENGTH = TILE_DIAG / STEPS_PER_TILE_DIAG;
1217
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package uk.org.ulcompsoc.tesseract.animations;
2+
3+
import com.badlogic.gdx.graphics.g2d.Animation;
4+
import com.badlogic.gdx.graphics.g2d.TextureRegion;
5+
6+
/**
7+
* @author Ashley Davis (SgtCoDFish)
8+
*/
9+
public abstract class AnimationFrameResolver {
10+
public float animTime = 0.0f;
11+
12+
public abstract float resolveTime(float deltaTime);
13+
14+
public TextureRegion resolveFrame(Animation anim, float deltaTime) {
15+
return anim.getKeyFrame(resolveTime(deltaTime));
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package uk.org.ulcompsoc.tesseract.animations;
2+
3+
/**
4+
* @author Ashley Davis (SgtCoDFish)
5+
*/
6+
public class PingPongFrameResolver extends AnimationFrameResolver {
7+
8+
public PingPongFrameResolver() {
9+
this(0.0f);
10+
}
11+
12+
public PingPongFrameResolver(float startTime) {
13+
this.animTime = startTime;
14+
}
15+
16+
@Override
17+
public float resolveTime(float deltaTime) {
18+
animTime += deltaTime;
19+
return animTime;
20+
}
21+
22+
}

core/src/uk/org/ulcompsoc/tesseract/battle/BattlePerformers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void perform(Entity invoker, Engine engine) {
3737
if (enemies != null) {
3838
for (int i = 0; i < enemies.size(); i++) {
3939
Entity e = enemies.get(i);
40-
GridPoint2 pos = posMapper.get(e).position;
40+
GridPoint2 pos = posMapper.get(e).getGridPosition();
4141

4242
e.add(new TargetMarker());
4343
e.add(new MouseClickListener(new Rectangle(pos.x * WorldConstants.TILE_WIDTH, pos.y
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package uk.org.ulcompsoc.tesseract.components;
2+
3+
import uk.org.ulcompsoc.tesseract.Move;
4+
5+
import com.badlogic.ashley.core.Component;
6+
7+
/**
8+
* @author Ashley Davis (SgtCoDFish)
9+
*/
10+
public class Moving extends Component {
11+
public Move move = null;
12+
13+
public Moving(Move move) {
14+
this.move = move;
15+
}
16+
17+
}

0 commit comments

Comments
 (0)