From 7251086910912202b31a2a2c0318e7869bc1654d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 17 Sep 2014 00:04:21 +0200 Subject: merge HexMapFactory and UnitFactory => GameFactory --- core/src/ch/asynk/tankontank/game/GameFactory.java | 147 +++++++++++++++++++++ .../ch/asynk/tankontank/game/HexMapFactory.java | 56 -------- core/src/ch/asynk/tankontank/game/UnitFactory.java | 97 -------------- .../ch/asynk/tankontank/screens/GameScreen.java | 14 +- 4 files changed, 154 insertions(+), 160 deletions(-) create mode 100644 core/src/ch/asynk/tankontank/game/GameFactory.java delete mode 100644 core/src/ch/asynk/tankontank/game/HexMapFactory.java delete mode 100644 core/src/ch/asynk/tankontank/game/UnitFactory.java diff --git a/core/src/ch/asynk/tankontank/game/GameFactory.java b/core/src/ch/asynk/tankontank/game/GameFactory.java new file mode 100644 index 0000000..9dfdd6d --- /dev/null +++ b/core/src/ch/asynk/tankontank/game/GameFactory.java @@ -0,0 +1,147 @@ +package ch.asynk.tankontank.game; + +import com.badlogic.gdx.assets.AssetManager; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.TextureAtlas; +import com.badlogic.gdx.graphics.g2d.TextureRegion; + +public class GameFactory +{ + private static TextureAtlas usAtlas; + private static TextureAtlas geAtlas; + + public static void init(AssetManager manager) + { + usAtlas = manager.get("images/us.pack", TextureAtlas.class); + geAtlas = manager.get("images/ge.pack", TextureAtlas.class); + } + + public static void dispose() + { + usAtlas.dispose(); + geAtlas.dispose(); + } + + public enum UnitType + { + GE_AT_GUN, + GE_INFANTRY, + GE_KINGTIGER, + GE_PANZER_IV, + GE_PANZER_IV_HQ, + GE_TIGER, + GE_WESPE, + + US_AT_GUN, + US_INFANTRY, + US_PERSHING, + US_PERSHING_HQ, + US_PRIEST, + US_SHERMAN, + US_SHERMAN_HQ, + US_WOLVERINE + } + + public static Unit getUnit(UnitType t) + { + Unit u = null; + switch(t) { + case GE_AT_GUN: + u = new Unit(Army.GE, false, 3, 8, 9, 1, geAtlas.findRegion("at-gun")); + break; + case GE_INFANTRY: + u = new Unit(Army.GE, false, 1, 7, 10, 1, geAtlas.findRegion("infantry")); + break; + case GE_KINGTIGER: + u = new Unit(Army.GE, false, 3, 12, 1, geAtlas.findRegion("kingtiger")); + break; + case GE_PANZER_IV: + u = new Unit(Army.GE, false, 2, 9, 2, geAtlas.findRegion("panzer-iv")); + break; + case GE_PANZER_IV_HQ: + u = new Unit(Army.GE, true, 2, 9, 2, geAtlas.findRegion("panzer-iv-hq")); + break; + case GE_TIGER: + u = new Unit(Army.GE, false, 3, 11, 1, geAtlas.findRegion("tiger")); + break; + case GE_WESPE: + u = new Unit(Army.GE, false, 5, 8, 1, geAtlas.findRegion("wespe")); + break; + case US_AT_GUN: + u = new Unit(Army.US, false, 1, 7, 10, 1, usAtlas.findRegion("at-gun")); + break; + case US_INFANTRY: + u = new Unit(Army.US, false, 1, 7, 10, 1, usAtlas.findRegion("infantry")); + break; + case US_PERSHING: + u = new Unit(Army.US, false, 3, 10, 2, usAtlas.findRegion("pershing")); + break; + case US_PERSHING_HQ: + u = new Unit(Army.US, true, 3, 10, 2, usAtlas.findRegion("pershing-hq")); + break; + case US_PRIEST: + u = new Unit(Army.US, false, 5, 8, 1, usAtlas.findRegion("priest")); + break; + case US_SHERMAN: + u = new Unit(Army.US, false, 2, 9, 2, usAtlas.findRegion("sherman")); + break; + case US_SHERMAN_HQ: + u = new Unit(Army.US, true, 2, 9, 2, usAtlas.findRegion("sherman-hq")); + break; + case US_WOLVERINE: + u = new Unit(Army.US, false, 3, 8, 3, usAtlas.findRegion("wolverine")); + break; + } + + return u; + } + + public enum MapType + { + MAP_A, + MAP_B + } + + private static Map.Config config() + { + Map.Config cfg = new Map.Config(); + cfg.cols = 11; + cfg.rows = 9; + cfg.x0 = 83; + cfg.y0 = 182; + cfg.h = 110; + cfg.dh = 53.6f; + cfg.w = 189; + cfg.dw = 94; + cfg.H = cfg.h + cfg.dh; + cfg.slope = (cfg.dh / (float) cfg.dw); + + return cfg; + } + + public static Map getMap(AssetManager manager, MapType t) + { + Map.Config cfg = config(); + + Hex[][] board = new Hex[cfg.rows][]; + for (int i = 0; i < cfg.rows; i++) { + int c = cfg.cols; + if ((i % 2) == 1) c -= 1; + board[i] = new Hex[c]; + for ( int j = 0; j < c; j ++) + board[i][j] = new MapHex(MapHex.Terrain.CLEAR); + } + + Map m = null; + switch(t) { + case MAP_A: + m = new MapImage(config(), board, manager.get("images/map_a.png", Texture.class)); + break; + case MAP_B: + m = new MapImage(config(), board, manager.get("images/map_b.png", Texture.class)); + break; + } + + return m; + } +} diff --git a/core/src/ch/asynk/tankontank/game/HexMapFactory.java b/core/src/ch/asynk/tankontank/game/HexMapFactory.java deleted file mode 100644 index ee2aa25..0000000 --- a/core/src/ch/asynk/tankontank/game/HexMapFactory.java +++ /dev/null @@ -1,56 +0,0 @@ -package ch.asynk.tankontank.game; - -import com.badlogic.gdx.assets.AssetManager; -import com.badlogic.gdx.graphics.Texture; - -public class HexMapFactory -{ - public enum MapType - { - MAP_A, - MAP_B - } - - private static HexMap.Config config() - { - HexMap.Config cfg = new HexMap.Config(); - cfg.cols = 11; - cfg.rows = 9; - cfg.x0 = 83; - cfg.y0 = 182; - cfg.h = 110; - cfg.dh = 53.6f; - cfg.w = 189; - cfg.dw = 94; - cfg.H = cfg.h + cfg.dh; - cfg.slope = (cfg.dh / (float) cfg.dw); - - return cfg; - } - - public static HexMap getMap(AssetManager manager, MapType t) - { - HexMap.Config cfg = config(); - - Hex[][] board = new Hex[cfg.rows][]; - for (int i = 0; i < cfg.rows; i++) { - int c = cfg.cols; - if ((i % 2) == 1) c -= 1; - board[i] = new Hex[c]; - for ( int j = 0; j < c; j ++) - board[i][j] = new MapHex(MapHex.Terrain.CLEAR); - } - - HexMap m = null; - switch(t) { - case MAP_A: - m = new HexMapImage(config(), board, manager.get("images/map_a.png", Texture.class)); - break; - case MAP_B: - m = new HexMapImage(config(), board, manager.get("images/map_b.png", Texture.class)); - break; - } - - return m; - } -} diff --git a/core/src/ch/asynk/tankontank/game/UnitFactory.java b/core/src/ch/asynk/tankontank/game/UnitFactory.java deleted file mode 100644 index c2156ce..0000000 --- a/core/src/ch/asynk/tankontank/game/UnitFactory.java +++ /dev/null @@ -1,97 +0,0 @@ -package ch.asynk.tankontank.game; - -import com.badlogic.gdx.assets.AssetManager; -import com.badlogic.gdx.graphics.g2d.TextureAtlas; -import com.badlogic.gdx.graphics.g2d.TextureRegion; - -public class UnitFactory -{ - public enum UnitType - { - GE_AT_GUN, - GE_INFANTRY, - GE_KINGTIGER, - GE_PANZER_IV, - GE_PANZER_IV_HQ, - GE_TIGER, - GE_WESPE, - - US_AT_GUN, - US_INFANTRY, - US_PERSHING, - US_PERSHING_HQ, - US_PRIEST, - US_SHERMAN, - US_SHERMAN_HQ, - US_WOLVERINE - } - - private static TextureAtlas usAtlas; - private static TextureAtlas geAtlas; - - public static void init(AssetManager manager) - { - usAtlas = manager.get("images/us.pack", TextureAtlas.class); - geAtlas = manager.get("images/ge.pack", TextureAtlas.class); - } - - public static void dispose() - { - usAtlas.dispose(); - geAtlas.dispose(); - } - - public static Unit getUnit(UnitType t) - { - Unit u = null; - switch(t) { - case GE_AT_GUN: - u = new Unit(Army.GE, false, 3, 8, 9, 1, geAtlas.findRegion("at-gun")); - break; - case GE_INFANTRY: - u = new Unit(Army.GE, false, 1, 7, 10, 1, geAtlas.findRegion("infantry")); - break; - case GE_KINGTIGER: - u = new Unit(Army.GE, false, 3, 12, 1, geAtlas.findRegion("kingtiger")); - break; - case GE_PANZER_IV: - u = new Unit(Army.GE, false, 2, 9, 2, geAtlas.findRegion("panzer-iv")); - break; - case GE_PANZER_IV_HQ: - u = new Unit(Army.GE, true, 2, 9, 2, geAtlas.findRegion("panzer-iv-hq")); - break; - case GE_TIGER: - u = new Unit(Army.GE, false, 3, 11, 1, geAtlas.findRegion("tiger")); - break; - case GE_WESPE: - u = new Unit(Army.GE, false, 5, 8, 1, geAtlas.findRegion("wespe")); - break; - case US_AT_GUN: - u = new Unit(Army.US, false, 1, 7, 10, 1, usAtlas.findRegion("at-gun")); - break; - case US_INFANTRY: - u = new Unit(Army.US, false, 1, 7, 10, 1, usAtlas.findRegion("infantry")); - break; - case US_PERSHING: - u = new Unit(Army.US, false, 3, 10, 2, usAtlas.findRegion("pershing")); - break; - case US_PERSHING_HQ: - u = new Unit(Army.US, true, 3, 10, 2, usAtlas.findRegion("pershing-hq")); - break; - case US_PRIEST: - u = new Unit(Army.US, false, 5, 8, 1, usAtlas.findRegion("priest")); - break; - case US_SHERMAN: - u = new Unit(Army.US, false, 2, 9, 2, usAtlas.findRegion("sherman")); - break; - case US_SHERMAN_HQ: - u = new Unit(Army.US, true, 2, 9, 2, usAtlas.findRegion("sherman-hq")); - break; - case US_WOLVERINE: - u = new Unit(Army.US, false, 3, 8, 3, usAtlas.findRegion("wolverine")); - break; - } - - return u; - } -} diff --git a/core/src/ch/asynk/tankontank/screens/GameScreen.java b/core/src/ch/asynk/tankontank/screens/GameScreen.java index cf946c3..8118489 100644 --- a/core/src/ch/asynk/tankontank/screens/GameScreen.java +++ b/core/src/ch/asynk/tankontank/screens/GameScreen.java @@ -25,14 +25,13 @@ import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.ScreenViewport; import ch.asynk.tankontank.TankOnTank; +import ch.asynk.tankontank.game.GameFactory; +import ch.asynk.tankontank.game.GameFactory.UnitType; import ch.asynk.tankontank.game.Pawn; import ch.asynk.tankontank.game.HexMap; import ch.asynk.tankontank.game.Hex; import ch.asynk.tankontank.game.HexMapImage; -import ch.asynk.tankontank.game.HexMapFactory; import ch.asynk.tankontank.game.Unit; -import ch.asynk.tankontank.game.UnitFactory; -import ch.asynk.tankontank.game.UnitFactory.UnitType; public class GameScreen extends AbstractScreen { @@ -62,10 +61,12 @@ public class GameScreen extends AbstractScreen { super(game); + GameFactory.init(game.manager); + fps = new Label("FPS: 0", game.skin); fps.setPosition( 10, Gdx.graphics.getHeight() - 40); - map = HexMapFactory.getMap(game.manager, HexMapFactory.MapType.MAP_A); + map = GameFactory.getMap(game.manager, GameFactory.MapType.MAP_A); selectedHex = new Image(game.manager.get("images/hex.png", Texture.class)); selectedHex.setVisible(false); @@ -77,7 +78,6 @@ public class GameScreen extends AbstractScreen gameStage.addActor((HexMapImage) map); gameStage.addActor(selectedHex); - UnitFactory.init(game.manager); Hex.Orientation o = Hex.Orientation.SOUTH_EAST; addUnit(gameStage, UnitType.GE_AT_GUN, 1, 4, o); @@ -106,7 +106,7 @@ public class GameScreen extends AbstractScreen private void addUnit(Stage stage, UnitType t, int col, int row, Hex.Orientation o) { - Unit u = UnitFactory.getUnit(t); + Unit u = GameFactory.getUnit(t); map.setPawnAt((Pawn) u, col, row, o); stage.addActor(u); } @@ -230,7 +230,7 @@ public class GameScreen extends AbstractScreen Gdx.app.debug("GameScreen", "dispose()"); hud.dispose(); gameStage.dispose(); - UnitFactory.dispose(); + GameFactory.dispose(); game.unloadAssets(); } -- cgit v1.1-2-g2b99