1
Fork 0

Add unit actions execution

This also adds a win/lose condition, although those aren't fleshed out
yet.
This commit is contained in:
Joshua Goins 2022-05-01 18:27:27 -04:00
parent 5ab8a6c388
commit 4186bfcacb
8 changed files with 134 additions and 3 deletions

View file

@ -1,4 +1,4 @@
sourceCompatibility = 1.7
sourceCompatibility = 1.8
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = [ "src/" ]

View file

@ -1,9 +1,21 @@
package com.redstrate.watersymbol;
public class AttackAction implements UnitAction {
private final Unit against;
private final Unit attacker;
public AttackAction(Unit attacker, Unit against) {
this.against = against;
this.attacker = attacker;
}
@Override
public String getName() {
return "Attack";
}
@Override
public boolean execute() {
against.health -= 5;
return true;
}
}

View file

@ -6,4 +6,9 @@ public class TradeAction implements UnitAction {
public String getName() {
return "Trade";
}
@Override
public boolean execute() {
return true;
}
}

View file

@ -1,6 +1,8 @@
package com.redstrate.watersymbol;
public class Unit {
public int health = 10;
public int positionX;
public int positionY;

View file

@ -2,5 +2,7 @@ package com.redstrate.watersymbol;
public interface UnitAction {
public String getName();
public boolean execute();
}

View file

@ -6,4 +6,10 @@ public class WaitAction implements UnitAction {
public String getName() {
return "Wait";
}
@Override
public boolean execute() {
// waiting of course, does nothing.
return true;
}
}

View file

@ -0,0 +1,75 @@
package com.redstrate.watersymbol.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.utils.ScreenUtils;
import com.redstrate.watersymbol.WaterSymbol;
public class GameOverMenuScreen implements Screen {
OrthographicCamera camera;
GlyphLayout titleLayout;
public GameOverMenuScreen(WaterSymbol game) {
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
titleLayout = new GlyphLayout(game.font, "GAME OVER");
}
@Override
public void show() {
}
@Override
public void render(float delta) {
ScreenUtils.clear(0, 0, 0.2f, 1);
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.font.draw(game.batch, titleLayout,
(camera.viewportWidth / 2) - (titleLayout.width / 2),
camera.viewportHeight / 2);
game.batch.end();
if(Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) {
game.setScreen(new MainMenuScreen(game));
dispose();
}
}
@Override
public void resize(int width, int height) {
camera.setToOrtho(false, width, height);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
private final WaterSymbol game;
}

View file

@ -99,7 +99,34 @@ public class GameScreen implements Screen {
getNextUnit();
}
void updateUnits() {
units.removeIf(u -> u.health <= 0);
// then check if theres a win or lose condition
long numberOfPlayers = units.stream().filter(u -> u.team == Unit.Team.Player).count();
long numberOfEnemies = units.stream().filter(u -> u.team == Unit.Team.Enemy).count();
if(numberOfPlayers == 0)
gameOver();
if(numberOfEnemies == 0)
gameWin();
}
void gameOver() {
game.setScreen(new GameOverMenuScreen(game));
dispose();
}
void gameWin() {
// right now let's just return to the main menu
game.setScreen(new MainMenuScreen(game));
dispose();
}
void advanceTurn() {
updateUnits();
currentUnitIndex++;
getNextUnit();
@ -135,7 +162,7 @@ public class GameScreen implements Screen {
for(Unit unit : units) {
if(unit.positionX == x && unit.positionY == y) {
if(unit.team != currentUnit.team) {
actions.add(new AttackAction());
actions.add(new AttackAction(currentUnit, unit));
} else {
// you can't trade with yourself!
if(unit != currentUnit) {
@ -162,7 +189,9 @@ public class GameScreen implements Screen {
}
void executeAction(UnitAction action) {
advanceTurn();
if(action.execute()) {
advanceTurn();
}
}
@Override