Skip to content

Commit 51869b1

Browse files
committed
Finish very basic battle implementation
Add battle message Add combat Add TesseractStrings for getting global strings
1 parent 77a9a1e commit 51869b1

20 files changed

+495
-27
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package uk.org.ulcompsoc.tesseract;
2+
3+
/**
4+
* @author Ashley Davis (SgtCoDFish)
5+
*/
6+
public enum AttackType {
7+
MELEE, JUMP;
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package uk.org.ulcompsoc.tesseract;
2+
3+
import uk.org.ulcompsoc.tesseract.components.Stats;
4+
5+
import com.badlogic.ashley.core.Entity;
6+
7+
/**
8+
* @author Ashley Davis (SgtCoDFish)
9+
*/
10+
public class BattleAttack {
11+
public Entity attacker = null;
12+
public Entity target = null;
13+
public AttackType attackType;
14+
15+
public BattleAttack(Entity attacker, Entity target, AttackType attackType) {
16+
this.attacker = attacker;
17+
this.target = target;
18+
this.attackType = attackType;
19+
}
20+
21+
public static int resolveDamage(Stats attacker, Stats defender) {
22+
return (int) Math.floor(attacker.attack - defender.defence);
23+
}
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package uk.org.ulcompsoc.tesseract;
2+
3+
import com.badlogic.gdx.graphics.Color;
4+
5+
/**
6+
* @author Ashley Davis (SgtCoDFish)
7+
*/
8+
public class BattleMessage {
9+
public String message;
10+
public float time;
11+
12+
public Color bgColor = Color.NAVY;
13+
public Color textColor = Color.WHITE;
14+
15+
public BattleMessage(String message) {
16+
this.message = message;
17+
this.time = guessTime(message);
18+
}
19+
20+
public BattleMessage(String message, float time) {
21+
this.message = message;
22+
this.time = time;
23+
}
24+
25+
public static float guessTime(String message) {
26+
return 0.05f * (float) message.length();
27+
}
28+
}
Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,95 @@
11
package uk.org.ulcompsoc.tesseract;
22

3+
import uk.org.ulcompsoc.tesseract.components.Enemy;
4+
import uk.org.ulcompsoc.tesseract.components.MouseClickListener;
5+
import uk.org.ulcompsoc.tesseract.components.Position;
6+
import uk.org.ulcompsoc.tesseract.components.TargetMarker;
7+
import uk.org.ulcompsoc.tesseract.systems.BattleAttackSystem;
8+
import uk.org.ulcompsoc.tesseract.systems.BattleMessageSystem;
9+
10+
import com.badlogic.ashley.core.ComponentMapper;
11+
import com.badlogic.ashley.core.Engine;
312
import com.badlogic.ashley.core.Entity;
13+
import com.badlogic.ashley.core.Family;
14+
import com.badlogic.ashley.utils.ImmutableArray;
415
import com.badlogic.gdx.Gdx;
16+
import com.badlogic.gdx.math.GridPoint2;
17+
import com.badlogic.gdx.math.Rectangle;
518

619
/**
720
* @author Ashley Davis (SgtCoDFish)
821
*/
922
public class BattlePerformers {
1023
public static class AttackPerformer implements MouseClickPerformer {
24+
private ComponentMapper<Position> posMapper = ComponentMapper.getFor(Position.class);
25+
26+
@SuppressWarnings("unchecked")
1127
@Override
12-
public void perform(Entity invoker) {
28+
public void perform(Entity invoker, Engine engine) {
1329
Gdx.app.debug("PERFORM_ATTACK", "Performing an attack.");
30+
31+
ImmutableArray<Entity> enemies = engine.getEntitiesFor(Family.getFor(Enemy.class));
32+
33+
if (enemies != null) {
34+
for (int i = 0; i < enemies.size(); i++) {
35+
Entity e = enemies.get(i);
36+
GridPoint2 pos = posMapper.get(e).position;
37+
38+
e.add(new TargetMarker());
39+
e.add(new MouseClickListener(new Rectangle(pos.x * WorldConstants.TILE_WIDTH, pos.y
40+
* WorldConstants.TILE_HEIGHT, WorldConstants.TILE_WIDTH, WorldConstants.TILE_HEIGHT),
41+
enemyTargetPerformer));
42+
battleMessageSystem.addMessage(TesseractStrings.getChooseTargetMessage());
43+
}
44+
}
1445
}
1546
}
1647

1748
public static class JumpPerformer implements MouseClickPerformer {
1849
@Override
19-
public void perform(Entity invoker) {
50+
public void perform(Entity invoker, Engine engine) {
2051
Gdx.app.debug("PERFORM_JUMP", "Performing a jump.");
52+
battleMessageSystem.addMessage(TesseractStrings.getJumpMessage());
2153
}
2254
}
2355

2456
public static class FleePerformer implements MouseClickPerformer {
2557
@Override
26-
public void perform(Entity invoker) {
58+
public void perform(Entity invoker, Engine engine) {
2759
Gdx.app.debug("PERFORM_FLEE", "Performing a flee.");
60+
battleMessageSystem.addMessage(TesseractStrings.getFleeMessage());
2861
}
2962
}
3063

31-
public final static AttackPerformer attackPerformer = new AttackPerformer();
32-
public final static JumpPerformer jumpPerformer = new JumpPerformer();
33-
public final static FleePerformer fleePerformer = new FleePerformer();
34-
public final static MouseClickPerformer[] performers = { attackPerformer, jumpPerformer, fleePerformer };
64+
public static class EnemyTargetPerformer implements MouseClickPerformer {
65+
@Override
66+
public void perform(Entity invoker, Engine engine) {
67+
Gdx.app.debug("PERFORM_TARGET", "Attack was targetted.");
68+
69+
removeTargets(engine);
70+
battleAttackSystem.addAttack(new BattleAttack(TesseractMain.playerEntity, invoker, AttackType.MELEE));
71+
}
72+
}
73+
74+
public static BattleAttackSystem battleAttackSystem = null;
75+
public static BattleMessageSystem battleMessageSystem = null;
76+
77+
public final static AttackPerformer attackPerformer = new AttackPerformer();
78+
public final static JumpPerformer jumpPerformer = new JumpPerformer();
79+
public final static FleePerformer fleePerformer = new FleePerformer();
80+
public final static EnemyTargetPerformer enemyTargetPerformer = new EnemyTargetPerformer();
81+
public final static MouseClickPerformer[] performers = { attackPerformer, jumpPerformer,
82+
fleePerformer };
83+
84+
@SuppressWarnings("unchecked")
85+
public static void removeTargets(Engine engine) {
86+
ImmutableArray<Entity> targets = engine.getEntitiesFor(Family.getFor(TargetMarker.class));
87+
while (targets.size() > 0) {
88+
Entity e = targets.get(0);
89+
e.remove(TargetMarker.class);
90+
e.remove(MouseClickListener.class);
91+
92+
targets = engine.getEntitiesFor(Family.getFor(TargetMarker.class));
93+
}
94+
}
3595
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package uk.org.ulcompsoc.tesseract;
22

3+
import com.badlogic.ashley.core.Engine;
34
import com.badlogic.ashley.core.Entity;
45

56
/**
67
* @author Ashley Davis (SgtCoDFish)
78
*/
89
public interface MouseClickPerformer {
9-
void perform(Entity invoker);
10+
void perform(Entity invoker, Engine engine);
1011
}

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import uk.org.ulcompsoc.tesseract.components.BattleDialog;
44
import uk.org.ulcompsoc.tesseract.components.Enemy;
55
import uk.org.ulcompsoc.tesseract.components.MouseClickListener;
6+
import uk.org.ulcompsoc.tesseract.components.Named;
67
import uk.org.ulcompsoc.tesseract.components.Player;
78
import uk.org.ulcompsoc.tesseract.components.Position;
89
import uk.org.ulcompsoc.tesseract.components.RelativePosition;
910
import uk.org.ulcompsoc.tesseract.components.Renderable;
1011
import uk.org.ulcompsoc.tesseract.components.Stats;
1112
import uk.org.ulcompsoc.tesseract.components.Text;
13+
import uk.org.ulcompsoc.tesseract.systems.BattleAttackSystem;
1214
import uk.org.ulcompsoc.tesseract.systems.BattleDialogRenderSystem;
1315
import uk.org.ulcompsoc.tesseract.systems.BattleInputSystem;
16+
import uk.org.ulcompsoc.tesseract.systems.BattleMessageSystem;
1417
import uk.org.ulcompsoc.tesseract.systems.RenderSystem;
1518
import uk.org.ulcompsoc.tesseract.systems.TextRenderSystem;
1619

@@ -37,8 +40,8 @@ public class TesseractMain extends ApplicationAdapter {
3740

3841
private Engine engine = null;
3942

43+
public static Entity playerEntity = null;
4044
private Texture playerTexture = null;
41-
private Entity playerEntity = null;
4245

4346
private Texture slimeTexture = null;
4447
private Entity slimeEntity = null;
@@ -53,6 +56,7 @@ public class TesseractMain extends ApplicationAdapter {
5356
private BitmapFont font = null;
5457
private BitmapFont bigFont = null;
5558

59+
@SuppressWarnings("unused")
5660
private GameState gameState = null;
5761

5862
public TesseractMain() {
@@ -95,16 +99,23 @@ public void create() {
9599

96100
playerEntity.add(new Position(17, yTile));
97101
playerEntity.add(new Renderable(playerRegions[1], playerRegions[0], playerRegions[3], playerRegions[2]));
98-
playerEntity.add(new Stats(100, 4, 4));
99-
playerEntity.add(new Player());
102+
playerEntity.add(new Stats(100, 25, 4));
103+
104+
Player playerComp = new Player();
105+
playerEntity.add(playerComp);
106+
playerEntity.add(new Named(playerComp.name));
100107

101108
slimeTexture = new Texture(Gdx.files.local("monsters/greenSlime.png"));
102109
TextureRegion slimeRegion = TextureRegion.split(slimeTexture, WorldConstants.TILE_WIDTH,
103110
WorldConstants.TILE_HEIGHT)[0][0];
111+
104112
slimeEntity = new Entity();
105113
slimeEntity.add(new Position(3, yTile)).add(new Renderable(slimeRegion));
106114
slimeEntity.add(new Stats(50, 2, 2));
107-
slimeEntity.add(new Enemy());
115+
116+
Enemy slime1 = new Enemy("Green Ooze");
117+
slimeEntity.add(slime1);
118+
slimeEntity.add(new Named(slime1.speciesName + " 1"));
108119

109120
Rectangle screenRect = new Rectangle(0.0f, 0.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
110121

@@ -148,7 +159,6 @@ public void create() {
148159

149160
rageText = new Entity();
150161
Text rageTextComponent = new Text("Rage Level:\nReally mad.");
151-
Gdx.app.debug("rage_size", "Rage w: " + Text.getTextWidth(rageTextComponent, font) + ".");
152162
rageText.add(RelativePosition.makeCentredX(Text.getTextRectangle(0.0f, 0.5f, rageTextComponent, font),
153163
statusDialog));
154164
rageText.add(rageTextComponent);
@@ -163,7 +173,14 @@ public void create() {
163173
engine.addEntity(hpText);
164174
engine.addEntity(rageText);
165175

176+
BattleMessageSystem battleMessageSystem = new BattleMessageSystem(bigFont, camera, screenRect, 300);
177+
BattleAttackSystem battleAttackSystem = new BattleAttackSystem(battleMessageSystem, 200);
178+
BattlePerformers.battleAttackSystem = battleAttackSystem;
179+
BattlePerformers.battleMessageSystem = battleMessageSystem;
180+
166181
engine.addSystem(new BattleInputSystem(camera, 100));
182+
engine.addSystem(battleAttackSystem);
183+
engine.addSystem(battleMessageSystem);
167184
engine.addSystem(new RenderSystem(batch, camera, 1000));
168185
engine.addSystem(new BattleDialogRenderSystem(camera, 2000));
169186
engine.addSystem(new TextRenderSystem(batch, font, 3000));
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package uk.org.ulcompsoc.tesseract;
2+
3+
/**
4+
* @author Ashley Davis (SgtCoDFish)
5+
*/
6+
public class TesseractStrings {
7+
private static BattleMessage chooseTarget = new BattleMessage("Choose a target!", 1.5f);
8+
9+
public static BattleMessage getChooseTargetMessage() {
10+
return chooseTarget;
11+
}
12+
13+
private static BattleMessage killed = new BattleMessage(" was killed!", 3.0f);
14+
15+
public static BattleMessage getKilledMessage(String victim) {
16+
killed.message = victim + killed.message;
17+
return killed;
18+
}
19+
20+
private static BattleMessage jumpMessage = new BattleMessage(
21+
"You jump into the air!\n...what did you think that would do?",
22+
2.5f);
23+
24+
public static BattleMessage getJumpMessage() {
25+
return jumpMessage;
26+
}
27+
28+
private static BattleMessage fleeMessage = new BattleMessage(
29+
"You attempt to flee!\nOh, it's just going to chase you...",
30+
2.5f);
31+
32+
public static BattleMessage getFleeMessage() {
33+
return fleeMessage;
34+
}
35+
}

core/src/uk/org/ulcompsoc/tesseract/components/Enemy.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,15 @@
66
* @author Ashley Davis (SgtCoDFish)
77
*/
88
public class Enemy extends Component {
9+
public static final String DEFAULT_SPECIES_NAME = "Unidentified Scoundrel";
910

11+
public String speciesName = null;
12+
13+
public Enemy() {
14+
this(DEFAULT_SPECIES_NAME);
15+
}
16+
17+
public Enemy(String speciesName) {
18+
this.speciesName = speciesName;
19+
}
1020
}

core/src/uk/org/ulcompsoc/tesseract/components/MouseClickListener.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import uk.org.ulcompsoc.tesseract.MouseClickPerformer;
44

55
import com.badlogic.ashley.core.Component;
6+
import com.badlogic.ashley.core.Engine;
67
import com.badlogic.ashley.core.Entity;
78
import com.badlogic.gdx.math.Rectangle;
89

910
/**
1011
* @author Ashley Davis (SgtCoDFish)
1112
*/
12-
public class MouseClickListener extends Component {
13+
public class MouseClickListener extends Component implements MouseClickPerformer {
1314
public final Rectangle rect;
1415
public final MouseClickPerformer performer;
1516

@@ -25,7 +26,8 @@ public MouseClickListener(Rectangle rect, MouseClickPerformer performer) {
2526
* The entity that was being iterated over when this invocation
2627
* happened.
2728
*/
28-
public void perform(Entity invoker) {
29-
performer.perform(invoker);
29+
@Override
30+
public void perform(Entity invoker, Engine engine) {
31+
performer.perform(invoker, engine);
3032
}
3133
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package uk.org.ulcompsoc.tesseract.components;
2+
3+
import com.badlogic.ashley.core.Component;
4+
5+
/**
6+
* @author Ashley Davis (SgtCoDFish)
7+
*/
8+
public class Named extends Component {
9+
public static final String DEFAULT_NAME = "Unnamed Combatant";
10+
public String name = null;
11+
12+
public Named() {
13+
this(DEFAULT_NAME);
14+
}
15+
16+
public Named(String name) {
17+
this.name = name;
18+
}
19+
}

0 commit comments

Comments
 (0)