From 4186bfcacb5d975c8a8058fa8cf6cbf96cced96e Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 1 May 2022 18:27:27 -0400 Subject: [PATCH] Add unit actions execution This also adds a win/lose condition, although those aren't fleshed out yet. --- core/build.gradle | 2 +- .../redstrate/watersymbol/AttackAction.java | 12 +++ .../redstrate/watersymbol/TradeAction.java | 5 ++ core/src/com/redstrate/watersymbol/Unit.java | 2 + .../com/redstrate/watersymbol/UnitAction.java | 2 + .../com/redstrate/watersymbol/WaitAction.java | 6 ++ .../screens/GameOverMenuScreen.java | 75 +++++++++++++++++++ .../watersymbol/screens/GameScreen.java | 33 +++++++- 8 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 core/src/com/redstrate/watersymbol/screens/GameOverMenuScreen.java diff --git a/core/build.gradle b/core/build.gradle index d192d04..0cad2ec 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -1,4 +1,4 @@ -sourceCompatibility = 1.7 +sourceCompatibility = 1.8 [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' sourceSets.main.java.srcDirs = [ "src/" ] diff --git a/core/src/com/redstrate/watersymbol/AttackAction.java b/core/src/com/redstrate/watersymbol/AttackAction.java index c9d6944..f9a61d3 100644 --- a/core/src/com/redstrate/watersymbol/AttackAction.java +++ b/core/src/com/redstrate/watersymbol/AttackAction.java @@ -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; + } } diff --git a/core/src/com/redstrate/watersymbol/TradeAction.java b/core/src/com/redstrate/watersymbol/TradeAction.java index 08e03fd..9154bfe 100644 --- a/core/src/com/redstrate/watersymbol/TradeAction.java +++ b/core/src/com/redstrate/watersymbol/TradeAction.java @@ -6,4 +6,9 @@ public class TradeAction implements UnitAction { public String getName() { return "Trade"; } + + @Override + public boolean execute() { + return true; + } } diff --git a/core/src/com/redstrate/watersymbol/Unit.java b/core/src/com/redstrate/watersymbol/Unit.java index 70b8e8f..ab48af5 100644 --- a/core/src/com/redstrate/watersymbol/Unit.java +++ b/core/src/com/redstrate/watersymbol/Unit.java @@ -1,6 +1,8 @@ package com.redstrate.watersymbol; public class Unit { + public int health = 10; + public int positionX; public int positionY; diff --git a/core/src/com/redstrate/watersymbol/UnitAction.java b/core/src/com/redstrate/watersymbol/UnitAction.java index bec92dd..5cc5f5a 100644 --- a/core/src/com/redstrate/watersymbol/UnitAction.java +++ b/core/src/com/redstrate/watersymbol/UnitAction.java @@ -2,5 +2,7 @@ package com.redstrate.watersymbol; public interface UnitAction { public String getName(); + + public boolean execute(); } diff --git a/core/src/com/redstrate/watersymbol/WaitAction.java b/core/src/com/redstrate/watersymbol/WaitAction.java index 59579fa..1a66aa0 100644 --- a/core/src/com/redstrate/watersymbol/WaitAction.java +++ b/core/src/com/redstrate/watersymbol/WaitAction.java @@ -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; + } } diff --git a/core/src/com/redstrate/watersymbol/screens/GameOverMenuScreen.java b/core/src/com/redstrate/watersymbol/screens/GameOverMenuScreen.java new file mode 100644 index 0000000..549b3b9 --- /dev/null +++ b/core/src/com/redstrate/watersymbol/screens/GameOverMenuScreen.java @@ -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; +} diff --git a/core/src/com/redstrate/watersymbol/screens/GameScreen.java b/core/src/com/redstrate/watersymbol/screens/GameScreen.java index 1ab476d..c6ed2b4 100644 --- a/core/src/com/redstrate/watersymbol/screens/GameScreen.java +++ b/core/src/com/redstrate/watersymbol/screens/GameScreen.java @@ -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