From 35eca95cd85d46298b5a3b837913c50d5ee73334 Mon Sep 17 00:00:00 2001 From: Jeasonfire Date: Sat, 16 May 2015 01:35:51 +0300 Subject: [PATCH] Restructured lots of things & implemented game states. --- .../saltosion/gladiator/GladiatorBrawler.java | 202 +++++------------- .../saltosion/gladiator/gui/GUIManager.java | 35 +-- .../saltosion/gladiator/gui/ImageNode.java | 8 - .../gladiator/gui/creators/GUICreator.java | 11 + .../gui/creators/TestGUICreator.java | 38 ++++ .../gladiator/gui/{ => nodes}/ButtonNode.java | 7 +- .../gladiator/gui/{ => nodes}/GUINode.java | 2 +- .../gladiator/gui/{ => nodes}/TextNode.java | 4 +- .../gui/properties/ImageProperty.java | 8 + .../InteractiveProperty.java} | 14 +- .../gui/{ => properties}/TextProperty.java | 2 +- .../gladiator/input/InputHandler.java | 60 +++--- .../gladiator/level/EntityFactory.java | 81 +++++++ .../com/saltosion/gladiator/level/Level.java | 7 + .../saltosion/gladiator/level/TestLevel.java | 58 +++++ .../saltosion/gladiator/state/BaseState.java | 27 +++ .../gladiator/state/InGameState.java | 39 ++++ .../gladiator/systems/RenderingSystem.java | 26 ++- .../com/saltosion/gladiator/util/AppUtil.java | 10 +- .../gladiator/util/SpriteLoader.java | 103 ++++----- 20 files changed, 464 insertions(+), 278 deletions(-) delete mode 100644 core/src/com/saltosion/gladiator/gui/ImageNode.java create mode 100644 core/src/com/saltosion/gladiator/gui/creators/GUICreator.java create mode 100644 core/src/com/saltosion/gladiator/gui/creators/TestGUICreator.java rename core/src/com/saltosion/gladiator/gui/{ => nodes}/ButtonNode.java (70%) rename core/src/com/saltosion/gladiator/gui/{ => nodes}/GUINode.java (96%) rename core/src/com/saltosion/gladiator/gui/{ => nodes}/TextNode.java (68%) create mode 100644 core/src/com/saltosion/gladiator/gui/properties/ImageProperty.java rename core/src/com/saltosion/gladiator/gui/{InteractiveNode.java => properties/InteractiveProperty.java} (87%) rename core/src/com/saltosion/gladiator/gui/{ => properties}/TextProperty.java (56%) create mode 100644 core/src/com/saltosion/gladiator/level/EntityFactory.java create mode 100644 core/src/com/saltosion/gladiator/level/Level.java create mode 100644 core/src/com/saltosion/gladiator/level/TestLevel.java create mode 100644 core/src/com/saltosion/gladiator/state/BaseState.java create mode 100644 core/src/com/saltosion/gladiator/state/InGameState.java diff --git a/core/src/com/saltosion/gladiator/GladiatorBrawler.java b/core/src/com/saltosion/gladiator/GladiatorBrawler.java index 39b06bc..1e5b152 100644 --- a/core/src/com/saltosion/gladiator/GladiatorBrawler.java +++ b/core/src/com/saltosion/gladiator/GladiatorBrawler.java @@ -5,40 +5,27 @@ import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.EntityListener; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.g2d.Sprite; -import com.badlogic.gdx.math.Vector2; -import com.saltosion.gladiator.components.CAI; -import com.saltosion.gladiator.components.CCombat; -import com.saltosion.gladiator.components.CPhysics; -import com.saltosion.gladiator.components.CRenderedObject; -import com.saltosion.gladiator.gui.ButtonNode; import com.saltosion.gladiator.gui.GUIManager; -import com.saltosion.gladiator.gui.GUINode; -import com.saltosion.gladiator.gui.TextNode; -import com.saltosion.gladiator.gui.TextProperty; import com.saltosion.gladiator.input.InputHandler; -import com.saltosion.gladiator.listeners.CombatListener; -import com.saltosion.gladiator.listeners.ai.DummyAI; +import com.saltosion.gladiator.level.EntityFactory; +import com.saltosion.gladiator.state.BaseState; +import com.saltosion.gladiator.state.InGameState; import com.saltosion.gladiator.systems.AISystem; import com.saltosion.gladiator.systems.CombatSystem; import com.saltosion.gladiator.systems.MiscManagerSystem; import com.saltosion.gladiator.systems.PhysicsSystem; import com.saltosion.gladiator.systems.RenderingSystem; import com.saltosion.gladiator.util.AppUtil; -import com.saltosion.gladiator.util.Direction; -import com.saltosion.gladiator.util.Global; import com.saltosion.gladiator.util.Log; -import com.saltosion.gladiator.util.Name; -import com.saltosion.gladiator.util.SpriteLoader; -import com.saltosion.gladiator.util.SpriteSequence; public class GladiatorBrawler extends ApplicationAdapter { private Engine engine; + private EntityFactory entityFactory; private GUIManager guiManager; private InputHandler inputHandler; - private Entity player; + private BaseState currentState; @Override public void create() { @@ -47,7 +34,28 @@ public class GladiatorBrawler extends ApplicationAdapter { // Initialize the Engine engine = new Engine(); AppUtil.engine = engine; + setupSystems(); + // Initialize the EntityFactory + entityFactory = new EntityFactory(); + AppUtil.entityFactory = entityFactory; + + // Initialize GUI + guiManager = new GUIManager(); + AppUtil.guiManager = this.guiManager; + + // Initialize input + inputHandler = new InputHandler(); + Gdx.input.setInputProcessor(inputHandler); + + // Initialize states + BaseState.setMainClass(this); + setState(new InGameState()); + + Log.info("Successfully started the game."); + } + + private void setupSystems() { engine.addSystem(new PhysicsSystem()); engine.addSystem(new RenderingSystem()); engine.addSystem(new CombatSystem()); @@ -82,157 +90,43 @@ public class GladiatorBrawler extends ApplicationAdapter { ais.updateEntities(engine); } }); - - // Initialize GUI - guiManager = new GUIManager(); - AppUtil.guiManager = this.guiManager; - initializeTestGUI(); - - // Initialize stuff in the world - initializePlayer(); - initializeTestDummy(); - initializeLevel(); - - // Initialize input - inputHandler = new InputHandler(); - Gdx.input.setInputProcessor(inputHandler); - - Log.info("Successfully started the game."); } @Override public void render() { engine.update(Gdx.graphics.getDeltaTime()); + currentState.update(Gdx.graphics.getDeltaTime()); } - public void initializePlayer() { - player = new Entity(); - - CRenderedObject renderedObject = new CRenderedObject(); - Sprite playertorso1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 0, 128, 112); - Sprite playertorso2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 1, 128, 112); - Sprite playerlegs1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 0, 128, 112); - Sprite playerlegs2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 1, 128, 112); - SpriteSequence torsosequence = new SpriteSequence(1).addSprite(playertorso1).addSprite(playertorso2); - SpriteSequence legsequence = new SpriteSequence(1).addSprite(playerlegs1).addSprite(playerlegs2); - renderedObject.setChannelName("default", "torso"); - renderedObject.addChannel("legs"); - renderedObject.addSequence("Torso-Idle", torsosequence); - renderedObject.addSequence("Legs-Idle", legsequence); - renderedObject.playAnimation("torso", "Torso-Idle"); - renderedObject.playAnimation("legs", "Legs-Idle"); - player.add(renderedObject); - player.add(new CPhysics().setSize(2, 4).setPosition(0, 5)); - player.add(new CCombat().setBaseDamage(100).setHealth(1000)); - engine.addEntity(player); - - AppUtil.player = player; - } - - public void initializeTestDummy() { - Entity dummy = new Entity(); - CRenderedObject renderedObject = new CRenderedObject(); - Sprite playertorso1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 0, 128, 112); - Sprite playertorso2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 1, 128, 112); - Sprite playerlegs1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 0, 128, 112); - Sprite playerlegs2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 1, 128, 112); - SpriteSequence torsosequence = new SpriteSequence(1).addSprite(playertorso1).addSprite(playertorso2); - SpriteSequence legsequence = new SpriteSequence(1).addSprite(playerlegs1).addSprite(playerlegs2); - renderedObject.setChannelName("default", "torso"); - renderedObject.addChannel("legs"); - renderedObject.addSequence("Torso-Idle", torsosequence); - renderedObject.addSequence("Legs-Idle", legsequence); - renderedObject.playAnimation("torso", "Torso-Idle"); - renderedObject.playAnimation("legs", "Legs-Idle"); - dummy.add(renderedObject); - dummy.add(new CPhysics().setSize(2, 4).setPosition(-6, 5)); - dummy.add(new CCombat().setBaseDamage(100).setHealth(1000).setSwingCD(.5f).setCombatListener( - new CombatListener() { - @Override - public void died(Entity source, int damageTaken) { - System.out.println("Nooooo! I died! I will revenge this!"); - } - - @Override - public void damageTaken(Entity source, int damageTaken) { - System.out.println(String.format("I took %d damage! Damnit!", damageTaken)); - } - - })); - dummy.add(new CAI().setReactDistance(5).setAIListener(new DummyAI())); - engine.addEntity(dummy); - dummy.getComponent(CCombat.class).inputs.put(Direction.UP, true); - } - - public void initializeLevel() { - Entity ground = new Entity(); - - Sprite groundSprite = SpriteLoader.loadSprite(Name.GROUNDIMG); - CRenderedObject renderedObject = new CRenderedObject(groundSprite); - ground.add(renderedObject); - CPhysics physics = new CPhysics().setMovable(false).setGravityApplied(false).setProcessCollisions(false) - .setSize(groundSprite.getRegionWidth() * Global.SPRITE_SCALE, - groundSprite.getRegionHeight() * Global.SPRITE_SCALE); - physics.getPosition().set(new Vector2(0, -4)); - ground.add(physics); - - Sprite wallSprite = SpriteLoader.loadSprite(Name.WALLIMG); - - Entity wall0 = new Entity(); - CRenderedObject wall0RenderedObject = new CRenderedObject(wallSprite); - CPhysics wall0Physics = new CPhysics().setMovable(false).setGravityApplied(false).setProcessCollisions(false) - .setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE, - wallSprite.getRegionHeight() * Global.SPRITE_SCALE); - wall0Physics.getPosition().set(new Vector2(6, 0)); - wall0.add(wall0RenderedObject); - wall0.add(wall0Physics); - - Entity wall1 = new Entity(); - CRenderedObject wall1RenderedObject = new CRenderedObject(wallSprite); - CPhysics wall1Physics = new CPhysics().setMovable(false).setGravityApplied(false).setProcessCollisions(false) - .setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE, - wallSprite.getRegionHeight() * Global.SPRITE_SCALE); - wall1Physics.getPosition().set(new Vector2(-6, 0)); - wall1.add(wall1RenderedObject); - wall1.add(wall1Physics); - - engine.addEntity(ground); - engine.addEntity(wall0); - engine.addEntity(wall1); - } - - public void initializeTestGUI() { - Sprite img1 = SpriteLoader.loadSprite(Name.WALLIMG, 0, 0, 32, 64); - Sprite img2 = SpriteLoader.loadSprite(Name.WALLIMG, 1, 0, 32, 64); - ButtonNode button = new ButtonNode("test-button", img1, img2) { - @Override - public void pressed(int x, int y, int mouseButton) { - Log.info("I should never be pressed against my will!"); - + public void setState(BaseState newState) { + if (newState != null) { + if (currentState != null) { + currentState.destroy(); } - - @Override - public void released(int x, int y, int mouseButton) { - Log.info("And now I was even released! Blasphemy!"); - - } - }; - button.setPosition(0.12f, 0.5f); - guiManager.getRootNode().addChild(button); - - TextNode text = new TextNode("test-text", "Test!"); - text.setPosition(0.8f, 0.5f); - guiManager.getRootNode().addChild(text); + newState.create(); + currentState = newState; + } else { + Log.error("Tried to set state to null!"); + } } @Override public void resize(int width, int height) { super.resize(width, height); - RenderingSystem.screenHeight = height; - RenderingSystem.screenWidth = width; + RenderingSystem rs = engine.getSystem(RenderingSystem.class); + rs.screenHeight = height; + rs.screenWidth = width; float aspectratio = ((float) width) / ((float) height); - RenderingSystem.aspectratio = aspectratio; + rs.aspectratio = aspectratio; rs.setViewport((int) (AppUtil.VPHEIGHT_CONST * aspectratio), AppUtil.VPHEIGHT_CONST); } + + @Override + public void dispose() { + if (currentState != null) { + currentState.destroy(); + } + AppUtil.engine.getSystem(RenderingSystem.class).dispose(); + } } diff --git a/core/src/com/saltosion/gladiator/gui/GUIManager.java b/core/src/com/saltosion/gladiator/gui/GUIManager.java index b8670a5..ec62720 100644 --- a/core/src/com/saltosion/gladiator/gui/GUIManager.java +++ b/core/src/com/saltosion/gladiator/gui/GUIManager.java @@ -1,23 +1,28 @@ package com.saltosion.gladiator.gui; +import com.saltosion.gladiator.gui.nodes.GUINode; import java.util.ArrayList; import com.badlogic.gdx.math.Vector2; import com.saltosion.gladiator.systems.RenderingSystem; import com.saltosion.gladiator.util.AppUtil; -import com.saltosion.gladiator.util.Global; public class GUIManager { + private final GUINode rootNode; public GUIManager() { this.rootNode = new GUINode("root").setPosition(-.5f, -.5f); } - + public GUINode getRootNode() { return this.rootNode; } - + + public void clearGUI() { + getRootNode().clearChildren(); + } + public ArrayList getAllRecursiveChildren(GUINode guiNode) { ArrayList list = new ArrayList(); for (GUINode child : guiNode.getChildren()) { @@ -26,7 +31,7 @@ public class GUIManager { } return list; } - + public GUINode getNode(String id) { for (GUINode node : getAllRecursiveChildren(rootNode)) { if (node.getID().equals(id)) { @@ -35,21 +40,21 @@ public class GUIManager { } return null; } - - public static Vector2 physicsLocationToGUILocation(Vector2 physicslocation ) { + + public static Vector2 physicsLocationToGUILocation(Vector2 physicslocation) { RenderingSystem rs = AppUtil.engine.getSystem(RenderingSystem.class); - float cameraY = 1-(rs.getCameraLocation().y/AppUtil.VPHEIGHT_CONST - +.5f); - float cameraX = 1-(rs.getCameraLocation().x/(AppUtil.VPHEIGHT_CONST*RenderingSystem.aspectratio) - +.5f); + float cameraY = 1 - (rs.getCameraLocation().y / AppUtil.VPHEIGHT_CONST + + .5f); + float cameraX = 1 - (rs.getCameraLocation().x / (AppUtil.VPHEIGHT_CONST * rs.aspectratio) + + .5f); System.out.println(cameraX + ":" + cameraY); - float y = physicslocation.y/AppUtil.VPHEIGHT_CONST + float y = physicslocation.y / AppUtil.VPHEIGHT_CONST + cameraY; - float x = physicslocation.x/(AppUtil.VPHEIGHT_CONST*RenderingSystem.aspectratio) + float x = physicslocation.x / (AppUtil.VPHEIGHT_CONST * rs.aspectratio) + cameraX; - - Vector2 v = new Vector2(x, y); - + + Vector2 v = new Vector2(x, y); + return v; } diff --git a/core/src/com/saltosion/gladiator/gui/ImageNode.java b/core/src/com/saltosion/gladiator/gui/ImageNode.java deleted file mode 100644 index a73b43c..0000000 --- a/core/src/com/saltosion/gladiator/gui/ImageNode.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.saltosion.gladiator.gui; - -import com.badlogic.gdx.graphics.g2d.Sprite; - -public interface ImageNode { - - public Sprite getImage(); -} diff --git a/core/src/com/saltosion/gladiator/gui/creators/GUICreator.java b/core/src/com/saltosion/gladiator/gui/creators/GUICreator.java new file mode 100644 index 0000000..9dae6d5 --- /dev/null +++ b/core/src/com/saltosion/gladiator/gui/creators/GUICreator.java @@ -0,0 +1,11 @@ +package com.saltosion.gladiator.gui.creators; + +/** + * The GUIController is to GUI elements what the Level is to entities. A + * builder-kind-of class. + */ +public interface GUICreator { + + public void create(); + +} diff --git a/core/src/com/saltosion/gladiator/gui/creators/TestGUICreator.java b/core/src/com/saltosion/gladiator/gui/creators/TestGUICreator.java new file mode 100644 index 0000000..108dc70 --- /dev/null +++ b/core/src/com/saltosion/gladiator/gui/creators/TestGUICreator.java @@ -0,0 +1,38 @@ +package com.saltosion.gladiator.gui.creators; + +import com.badlogic.gdx.graphics.g2d.Sprite; +import com.saltosion.gladiator.gui.nodes.ButtonNode; +import com.saltosion.gladiator.gui.nodes.TextNode; +import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.Log; +import com.saltosion.gladiator.util.Name; +import com.saltosion.gladiator.util.SpriteLoader; + +public class TestGUICreator implements GUICreator { + + @Override + public void create() { + Sprite img1 = SpriteLoader.loadSprite(Name.WALLIMG, 0, 0, 32, 64); + Sprite img2 = SpriteLoader.loadSprite(Name.WALLIMG, 1, 0, 32, 64); + ButtonNode button = new ButtonNode("test-button", img1, img2) { + @Override + public void pressed(int x, int y, int mouseButton) { + Log.info("I should never be pressed against my will!"); + + } + + @Override + public void released(int x, int y, int mouseButton) { + Log.info("And now I was even released! Blasphemy!"); + + } + }; + button.setPosition(0.12f, 0.5f); + AppUtil.guiManager.getRootNode().addChild(button); + + TextNode text = new TextNode("test-text", "Test!"); + text.setPosition(0.8f, 0.5f); + AppUtil.guiManager.getRootNode().addChild(text); + } + +} diff --git a/core/src/com/saltosion/gladiator/gui/ButtonNode.java b/core/src/com/saltosion/gladiator/gui/nodes/ButtonNode.java similarity index 70% rename from core/src/com/saltosion/gladiator/gui/ButtonNode.java rename to core/src/com/saltosion/gladiator/gui/nodes/ButtonNode.java index 68169fc..ed6cad2 100644 --- a/core/src/com/saltosion/gladiator/gui/ButtonNode.java +++ b/core/src/com/saltosion/gladiator/gui/nodes/ButtonNode.java @@ -1,8 +1,11 @@ -package com.saltosion.gladiator.gui; +package com.saltosion.gladiator.gui.nodes; +import com.saltosion.gladiator.gui.properties.ImageProperty; +import com.saltosion.gladiator.gui.properties.InteractiveProperty; +import com.saltosion.gladiator.gui.nodes.GUINode; import com.badlogic.gdx.graphics.g2d.Sprite; -public abstract class ButtonNode extends GUINode implements InteractiveNode, ImageNode { +public abstract class ButtonNode extends GUINode implements InteractiveProperty, ImageProperty { private final Sprite onHover; private final Sprite normal; private boolean hovered = false; diff --git a/core/src/com/saltosion/gladiator/gui/GUINode.java b/core/src/com/saltosion/gladiator/gui/nodes/GUINode.java similarity index 96% rename from core/src/com/saltosion/gladiator/gui/GUINode.java rename to core/src/com/saltosion/gladiator/gui/nodes/GUINode.java index fcfd466..8a5ea13 100644 --- a/core/src/com/saltosion/gladiator/gui/GUINode.java +++ b/core/src/com/saltosion/gladiator/gui/nodes/GUINode.java @@ -1,4 +1,4 @@ -package com.saltosion.gladiator.gui; +package com.saltosion.gladiator.gui.nodes; import com.badlogic.gdx.math.Vector2; import java.util.ArrayList; diff --git a/core/src/com/saltosion/gladiator/gui/TextNode.java b/core/src/com/saltosion/gladiator/gui/nodes/TextNode.java similarity index 68% rename from core/src/com/saltosion/gladiator/gui/TextNode.java rename to core/src/com/saltosion/gladiator/gui/nodes/TextNode.java index d09e034..840363d 100644 --- a/core/src/com/saltosion/gladiator/gui/TextNode.java +++ b/core/src/com/saltosion/gladiator/gui/nodes/TextNode.java @@ -1,4 +1,6 @@ -package com.saltosion.gladiator.gui; +package com.saltosion.gladiator.gui.nodes; + +import com.saltosion.gladiator.gui.properties.TextProperty; public class TextNode extends GUINode implements TextProperty { diff --git a/core/src/com/saltosion/gladiator/gui/properties/ImageProperty.java b/core/src/com/saltosion/gladiator/gui/properties/ImageProperty.java new file mode 100644 index 0000000..312b170 --- /dev/null +++ b/core/src/com/saltosion/gladiator/gui/properties/ImageProperty.java @@ -0,0 +1,8 @@ +package com.saltosion.gladiator.gui.properties; + +import com.badlogic.gdx.graphics.g2d.Sprite; + +public interface ImageProperty { + + public Sprite getImage(); +} diff --git a/core/src/com/saltosion/gladiator/gui/InteractiveNode.java b/core/src/com/saltosion/gladiator/gui/properties/InteractiveProperty.java similarity index 87% rename from core/src/com/saltosion/gladiator/gui/InteractiveNode.java rename to core/src/com/saltosion/gladiator/gui/properties/InteractiveProperty.java index 64bc7e5..8ca07cd 100644 --- a/core/src/com/saltosion/gladiator/gui/InteractiveNode.java +++ b/core/src/com/saltosion/gladiator/gui/properties/InteractiveProperty.java @@ -1,17 +1,18 @@ -package com.saltosion.gladiator.gui; +package com.saltosion.gladiator.gui.properties; -import com.badlogic.gdx.Input.Buttons; +public interface InteractiveProperty { -public interface InteractiveNode { /** * Called when the mouse enters the node's elements. + * * @param x position of the mouse. * @param y position of the mouse. */ public void mouseEnter(float x, float y); - + /** * Called when the mouse leaves the node's elements. + * * @param x position of the mouse. * @param y position of the mouse. */ @@ -19,14 +20,17 @@ public interface InteractiveNode { /** * Called when someone presses a mouse button on the node's elements. + * * @param x position of the mouse. * @param y position of the mouse. * @param mouseButton button pressed. */ public void pressed(int x, int y, int mouseButton); + /** - * + * * Called when someone releases a mouse button on the node's elements. + * * @param x position of the mouse. * @param y position of the mouse. * @param mouseButton button pressed. diff --git a/core/src/com/saltosion/gladiator/gui/TextProperty.java b/core/src/com/saltosion/gladiator/gui/properties/TextProperty.java similarity index 56% rename from core/src/com/saltosion/gladiator/gui/TextProperty.java rename to core/src/com/saltosion/gladiator/gui/properties/TextProperty.java index 33f7269..fcb4339 100644 --- a/core/src/com/saltosion/gladiator/gui/TextProperty.java +++ b/core/src/com/saltosion/gladiator/gui/properties/TextProperty.java @@ -1,4 +1,4 @@ -package com.saltosion.gladiator.gui; +package com.saltosion.gladiator.gui.properties; public interface TextProperty { diff --git a/core/src/com/saltosion/gladiator/input/InputHandler.java b/core/src/com/saltosion/gladiator/input/InputHandler.java index 4943aae..e98757c 100644 --- a/core/src/com/saltosion/gladiator/input/InputHandler.java +++ b/core/src/com/saltosion/gladiator/input/InputHandler.java @@ -6,9 +6,9 @@ import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.utils.Array; -import com.saltosion.gladiator.gui.GUINode; -import com.saltosion.gladiator.gui.ImageNode; -import com.saltosion.gladiator.gui.InteractiveNode; +import com.saltosion.gladiator.gui.nodes.GUINode; +import com.saltosion.gladiator.gui.properties.ImageProperty; +import com.saltosion.gladiator.gui.properties.InteractiveProperty; import com.saltosion.gladiator.systems.RenderingSystem; import com.saltosion.gladiator.util.AppUtil; import com.saltosion.gladiator.util.Global; @@ -18,8 +18,8 @@ import com.saltosion.gladiator.util.Name; public class InputHandler implements InputProcessor { public HashMap keys = new HashMap(); - - private Array hoveredUIElements = new Array(); + + private final Array hoveredUIElements = new Array(); public InputHandler() { keys.put(Keys.A, Name.MOVE_LEFT); @@ -59,10 +59,12 @@ public class InputHandler implements InputProcessor { public boolean touchDown(int screenX, int screenY, int pointer, int button) { for (String id : hoveredUIElements) { GUINode node = AppUtil.guiManager.getNode(id); - - if (node instanceof InteractiveNode) { - ((InteractiveNode) node).pressed(screenX, screenY, button); - } else { Log.error("Attempted to call 'pressed' on a non-interactive node!"); } + + if (node instanceof InteractiveProperty) { + ((InteractiveProperty) node).pressed(screenX, screenY, button); + } else { + Log.error("Attempted to call 'pressed' on a non-interactive node!"); + } } return true; } @@ -71,10 +73,12 @@ public class InputHandler implements InputProcessor { public boolean touchUp(int screenX, int screenY, int pointer, int button) { for (String id : hoveredUIElements) { GUINode node = AppUtil.guiManager.getNode(id); - - if (node instanceof InteractiveNode) { - ((InteractiveNode) node).released(screenX, screenY, button); - } else { Log.error("Attempted to call 'released' on a non-interactive node!"); } + + if (node instanceof InteractiveProperty) { + ((InteractiveProperty) node).released(screenX, screenY, button); + } else { + Log.error("Attempted to call 'released' on a non-interactive node!"); + } } return true; } @@ -86,22 +90,24 @@ public class InputHandler implements InputProcessor { @Override public boolean mouseMoved(int screenX, int screenY) { - float x = (float)(screenX)/RenderingSystem.screenWidth; - float y = 1-(float)(screenY)/RenderingSystem.screenHeight; - + RenderingSystem rs = AppUtil.engine.getSystem(RenderingSystem.class); + + float x = (float) (screenX) / rs.screenWidth; + float y = 1 - (float) (screenY) / rs.screenHeight; + for (GUINode node : AppUtil.guiManager.getAllRecursiveChildren(AppUtil.guiManager.getRootNode())) { - if (node instanceof ImageNode) { - Sprite s = ((ImageNode) node).getImage(); - float height = (s.getHeight()*Global.SPRITE_SCALE)/AppUtil.VPHEIGHT_CONST; - float width = (s.getWidth()*Global.SPRITE_SCALE)/(AppUtil.VPHEIGHT_CONST*RenderingSystem.aspectratio); - float x0 = node.getPosition().x-width/2; - float x1 = node.getPosition().x+width/2; - float y0 = node.getPosition().y-height/2; - float y1 = node.getPosition().y+height/2; + if (node instanceof ImageProperty) { + Sprite s = ((ImageProperty) node).getImage(); + float height = (s.getHeight() * Global.SPRITE_SCALE) / AppUtil.VPHEIGHT_CONST; + float width = (s.getWidth() * Global.SPRITE_SCALE) / (AppUtil.VPHEIGHT_CONST * rs.aspectratio); + float x0 = node.getPosition().x - width / 2; + float x1 = node.getPosition().x + width / 2; + float y0 = node.getPosition().y - height / 2; + float y1 = node.getPosition().y + height / 2; x += 0.01f; - if (node instanceof InteractiveNode) { - InteractiveNode interactiveNode = (InteractiveNode) node; - + if (node instanceof InteractiveProperty) { + InteractiveProperty interactiveNode = (InteractiveProperty) node; + if (x >= x0 && x <= x1 && y >= y0 && y <= y1) { if (hoveredUIElements.contains(node.getID(), false)) { continue; diff --git a/core/src/com/saltosion/gladiator/level/EntityFactory.java b/core/src/com/saltosion/gladiator/level/EntityFactory.java new file mode 100644 index 0000000..93a4042 --- /dev/null +++ b/core/src/com/saltosion/gladiator/level/EntityFactory.java @@ -0,0 +1,81 @@ +package com.saltosion.gladiator.level; + +import com.badlogic.ashley.core.Entity; +import com.badlogic.gdx.graphics.g2d.Sprite; +import com.badlogic.gdx.math.Vector2; +import com.saltosion.gladiator.components.CAI; +import com.saltosion.gladiator.components.CCombat; +import com.saltosion.gladiator.components.CPhysics; +import com.saltosion.gladiator.components.CRenderedObject; +import com.saltosion.gladiator.listeners.CombatListener; +import com.saltosion.gladiator.listeners.ai.DummyAI; +import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.Direction; +import com.saltosion.gladiator.util.Log; +import com.saltosion.gladiator.util.Name; +import com.saltosion.gladiator.util.SpriteLoader; +import com.saltosion.gladiator.util.SpriteSequence; + +public class EntityFactory { + + public void createPlayer(Vector2 pos) { + Entity player = new Entity(); + + CRenderedObject renderedObject = new CRenderedObject(); + Sprite playertorso1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 0, 128, 112); + Sprite playertorso2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 1, 128, 112); + Sprite playerlegs1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 0, 128, 112); + Sprite playerlegs2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 1, 128, 112); + SpriteSequence torsosequence = new SpriteSequence(1).addSprite(playertorso1).addSprite(playertorso2); + SpriteSequence legsequence = new SpriteSequence(1).addSprite(playerlegs1).addSprite(playerlegs2); + renderedObject.setChannelName("default", "torso"); + renderedObject.addChannel("legs"); + renderedObject.addSequence("Torso-Idle", torsosequence); + renderedObject.addSequence("Legs-Idle", legsequence); + renderedObject.playAnimation("torso", "Torso-Idle"); + renderedObject.playAnimation("legs", "Legs-Idle"); + player.add(renderedObject); + player.add(new CPhysics().setSize(2, 4).setPosition(pos.x, pos.y)); + player.add(new CCombat().setBaseDamage(100).setHealth(1000)); + AppUtil.engine.addEntity(player); + + Log.info("Created player!"); + AppUtil.player = player; + } + + public void createDummy(Vector2 pos) { + Entity dummy = new Entity(); + CRenderedObject renderedObject = new CRenderedObject(); + Sprite playertorso1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 0, 128, 112); + Sprite playertorso2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 1, 128, 112); + Sprite playerlegs1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 0, 128, 112); + Sprite playerlegs2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 1, 128, 112); + SpriteSequence torsosequence = new SpriteSequence(1).addSprite(playertorso1).addSprite(playertorso2); + SpriteSequence legsequence = new SpriteSequence(1).addSprite(playerlegs1).addSprite(playerlegs2); + renderedObject.setChannelName("default", "torso"); + renderedObject.addChannel("legs"); + renderedObject.addSequence("Torso-Idle", torsosequence); + renderedObject.addSequence("Legs-Idle", legsequence); + renderedObject.playAnimation("torso", "Torso-Idle"); + renderedObject.playAnimation("legs", "Legs-Idle"); + dummy.add(renderedObject); + dummy.add(new CPhysics().setSize(2, 4).setPosition(pos.x, pos.y)); + dummy.add(new CCombat().setBaseDamage(100).setHealth(1000).setSwingCD(.5f).setCombatListener( + new CombatListener() { + @Override + public void died(Entity source, int damageTaken) { + System.out.println("Nooooo! I died! I will revenge this!"); + } + + @Override + public void damageTaken(Entity source, int damageTaken) { + System.out.println(String.format("I took %d damage! Damnit!", damageTaken)); + } + + })); + dummy.add(new CAI().setReactDistance(5).setAIListener(new DummyAI())); + AppUtil.engine.addEntity(dummy); + dummy.getComponent(CCombat.class).inputs.put(Direction.UP, true); + } + +} diff --git a/core/src/com/saltosion/gladiator/level/Level.java b/core/src/com/saltosion/gladiator/level/Level.java new file mode 100644 index 0000000..ab8e665 --- /dev/null +++ b/core/src/com/saltosion/gladiator/level/Level.java @@ -0,0 +1,7 @@ +package com.saltosion.gladiator.level; + +public interface Level { + + public void generate(); + +} diff --git a/core/src/com/saltosion/gladiator/level/TestLevel.java b/core/src/com/saltosion/gladiator/level/TestLevel.java new file mode 100644 index 0000000..3047013 --- /dev/null +++ b/core/src/com/saltosion/gladiator/level/TestLevel.java @@ -0,0 +1,58 @@ +package com.saltosion.gladiator.level; + +import com.badlogic.ashley.core.Entity; +import com.badlogic.gdx.graphics.g2d.Sprite; +import com.badlogic.gdx.math.Vector2; +import com.saltosion.gladiator.components.CPhysics; +import com.saltosion.gladiator.components.CRenderedObject; +import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.Global; +import com.saltosion.gladiator.util.Name; +import com.saltosion.gladiator.util.SpriteLoader; + +public class TestLevel implements Level { + + @Override + public void generate() { + // Generate entities + AppUtil.entityFactory.createPlayer(new Vector2(0, 5)); + AppUtil.entityFactory.createDummy(new Vector2(-6, 5)); + + // Generate level + Entity ground = new Entity(); + + Sprite groundSprite = SpriteLoader.loadSprite(Name.GROUNDIMG); + CRenderedObject renderedObject = new CRenderedObject(groundSprite); + ground.add(renderedObject); + CPhysics physics = new CPhysics().setMovable(false).setGravityApplied(false).setProcessCollisions(false) + .setSize(groundSprite.getRegionWidth() * Global.SPRITE_SCALE, + groundSprite.getRegionHeight() * Global.SPRITE_SCALE); + physics.getPosition().set(new Vector2(0, -4)); + ground.add(physics); + + Sprite wallSprite = SpriteLoader.loadSprite(Name.WALLIMG); + + Entity wall0 = new Entity(); + CRenderedObject wall0RenderedObject = new CRenderedObject(wallSprite); + CPhysics wall0Physics = new CPhysics().setMovable(false).setGravityApplied(false).setProcessCollisions(false) + .setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE, + wallSprite.getRegionHeight() * Global.SPRITE_SCALE); + wall0Physics.getPosition().set(new Vector2(6, 0)); + wall0.add(wall0RenderedObject); + wall0.add(wall0Physics); + + Entity wall1 = new Entity(); + CRenderedObject wall1RenderedObject = new CRenderedObject(wallSprite); + CPhysics wall1Physics = new CPhysics().setMovable(false).setGravityApplied(false).setProcessCollisions(false) + .setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE, + wallSprite.getRegionHeight() * Global.SPRITE_SCALE); + wall1Physics.getPosition().set(new Vector2(-6, 0)); + wall1.add(wall1RenderedObject); + wall1.add(wall1Physics); + + AppUtil.engine.addEntity(ground); + AppUtil.engine.addEntity(wall0); + AppUtil.engine.addEntity(wall1); + } + +} diff --git a/core/src/com/saltosion/gladiator/state/BaseState.java b/core/src/com/saltosion/gladiator/state/BaseState.java new file mode 100644 index 0000000..c796d40 --- /dev/null +++ b/core/src/com/saltosion/gladiator/state/BaseState.java @@ -0,0 +1,27 @@ +package com.saltosion.gladiator.state; + +import com.saltosion.gladiator.GladiatorBrawler; + +public abstract class BaseState { + + /** + * This is private because it should really only be used to change states + * (for now) + */ + private static GladiatorBrawler mainClass; + + public abstract void create(); + + public abstract void update(float deltaTime); + + public abstract void destroy(); + + public void changeState(BaseState newState) { + mainClass.setState(newState); + } + + public static void setMainClass(GladiatorBrawler mainClass) { + BaseState.mainClass = mainClass; + } + +} diff --git a/core/src/com/saltosion/gladiator/state/InGameState.java b/core/src/com/saltosion/gladiator/state/InGameState.java new file mode 100644 index 0000000..78b5e1a --- /dev/null +++ b/core/src/com/saltosion/gladiator/state/InGameState.java @@ -0,0 +1,39 @@ +package com.saltosion.gladiator.state; + +import com.saltosion.gladiator.gui.creators.GUICreator; +import com.saltosion.gladiator.gui.creators.TestGUICreator; +import com.saltosion.gladiator.level.Level; +import com.saltosion.gladiator.level.TestLevel; +import com.saltosion.gladiator.util.AppUtil; + +public class InGameState extends BaseState { + + private Level level; + private GUICreator guiCreator; + + @Override + public void create() { + // Start from a clean slate + AppUtil.engine.removeAllEntities(); + AppUtil.guiManager.clearGUI(); + + level = new TestLevel(); + level.generate(); + + guiCreator = new TestGUICreator(); + guiCreator.create(); + } + + @Override + public void update(float deltaTime) { + } + + @Override + public void destroy() { + // Clear all entities that are left as they are no longer needed + AppUtil.engine.removeAllEntities(); + // Clear GUI so there's nothing leftover for the next state + AppUtil.guiManager.clearGUI(); + } + +} diff --git a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java index f52a3c4..1d6fd14 100644 --- a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java +++ b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java @@ -19,12 +19,13 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.Vector2; import com.saltosion.gladiator.components.CPhysics; import com.saltosion.gladiator.components.CRenderedObject; -import com.saltosion.gladiator.gui.GUINode; -import com.saltosion.gladiator.gui.ImageNode; -import com.saltosion.gladiator.gui.TextNode; -import com.saltosion.gladiator.gui.TextProperty; +import com.saltosion.gladiator.gui.nodes.GUINode; +import com.saltosion.gladiator.gui.properties.ImageProperty; +import com.saltosion.gladiator.gui.nodes.TextNode; +import com.saltosion.gladiator.gui.properties.TextProperty; import com.saltosion.gladiator.util.AppUtil; import com.saltosion.gladiator.util.Global; +import com.saltosion.gladiator.util.SpriteLoader; import com.saltosion.gladiator.util.SpriteSequence; import java.util.ArrayList; import java.util.List; @@ -40,9 +41,9 @@ public class RenderingSystem extends EntitySystem { private ShapeRenderer debugRenderer; private OrthographicCamera camera, fontCamera; - public static float aspectratio; - public static int screenHeight = 0; - public static int screenWidth = 0; + public float aspectratio; + public int screenHeight = 0; + public int screenWidth = 0; private boolean debug = true; private final Color debugColor = new Color(0, 1, 0, 1); @@ -156,8 +157,8 @@ public class RenderingSystem extends EntitySystem { private void renderGUINode(GUINode node, Vector2 position) { position.add(node.getPosition()); - if (node instanceof ImageNode) { - Sprite s = ((ImageNode) node).getImage(); + if (node instanceof ImageProperty) { + Sprite s = ((ImageProperty) node).getImage(); s.setPosition(position.x * AppUtil.VPHEIGHT_CONST * aspectratio - s.getWidth() / 2 + camera.position.x, position.y * AppUtil.VPHEIGHT_CONST - s.getHeight() / 2 + camera.position.y); s.draw(batch); @@ -229,6 +230,13 @@ public class RenderingSystem extends EntitySystem { return new Vector2(this.camera.position.x, this.camera.position.y); } + public void dispose() { + batch.dispose(); + debugRenderer.dispose(); + font.dispose(); + SpriteLoader.dispose(); + } + private class TextObject { public String text; diff --git a/core/src/com/saltosion/gladiator/util/AppUtil.java b/core/src/com/saltosion/gladiator/util/AppUtil.java index 1d1e93e..efa0522 100644 --- a/core/src/com/saltosion/gladiator/util/AppUtil.java +++ b/core/src/com/saltosion/gladiator/util/AppUtil.java @@ -4,14 +4,16 @@ import com.badlogic.ashley.core.Engine; import com.badlogic.ashley.core.Entity; import com.badlogic.gdx.math.Vector2; import com.saltosion.gladiator.gui.GUIManager; +import com.saltosion.gladiator.level.EntityFactory; public class AppUtil { - public static Entity player; - public static Engine engine; + public static Entity player; + public static Engine engine; + public static EntityFactory entityFactory; public static GUIManager guiManager; - public static final int VPHEIGHT_CONST = 24; + public static final int VPHEIGHT_CONST = 24; - public static final Vector2 JUMP_FORCE = new Vector2(0, 12000); + public static final Vector2 JUMP_FORCE = new Vector2(0, 12000); } diff --git a/core/src/com/saltosion/gladiator/util/SpriteLoader.java b/core/src/com/saltosion/gladiator/util/SpriteLoader.java index a4efa07..16dbfa1 100644 --- a/core/src/com/saltosion/gladiator/util/SpriteLoader.java +++ b/core/src/com/saltosion/gladiator/util/SpriteLoader.java @@ -9,61 +9,62 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion; public class SpriteLoader { - public static HashMap textures = new HashMap(); + public static HashMap textures = new HashMap(); - static { - loadTexture(Name.STATICPLAYER, "sprites/staticplayer.png"); - loadTexture(Name.PLAYERIMG, "sprites/player/player.png"); - loadTexture(Name.GROUNDIMG, "sprites/ground.png"); - loadTexture(Name.WALLIMG, "sprites/wall.png"); - loadTexture(Name.SWINGHITBOXIMG, "sprites/swinghitbox.png"); - } + static { + loadTexture(Name.STATICPLAYER, "sprites/staticplayer.png"); + loadTexture(Name.PLAYERIMG, "sprites/player/player.png"); + loadTexture(Name.GROUNDIMG, "sprites/ground.png"); + loadTexture(Name.WALLIMG, "sprites/wall.png"); + loadTexture(Name.SWINGHITBOXIMG, "sprites/swinghitbox.png"); + } - /** - * Returns a sprite, which is a piece from texture. - * - * @param texKey - * @param x - * @param y - * @param width - * @param height - * @return - */ - public static Sprite loadSprite(String texKey, int x, int y, int width, int height) { - TextureRegion tr = new TextureRegion(textures.get(texKey), x * width, y * height, width, height); - Sprite s = new Sprite(tr); - s.setScale(Global.SPRITE_SCALE); - return s; - } + /** + * Returns a sprite, which is a piece from texture. + * + * @param texKey + * @param x + * @param y + * @param width + * @param height + * @return + */ + public static Sprite loadSprite(String texKey, int x, int y, int width, int height) { + TextureRegion tr = new TextureRegion(textures.get(texKey), x * width, y * height, width, height); + Sprite s = new Sprite(tr); + s.setScale(Global.SPRITE_SCALE); + return s; + } - /** - * Returns a sprite, which is originally a texture. - * - * @param texKey - * @return - */ - public static Sprite loadSprite(String texKey) { - return loadSprite(texKey, 0, 0, textures.get(texKey).getWidth(), textures.get(texKey).getHeight()); - } + /** + * Returns a sprite, which is originally a texture. + * + * @param texKey + * @return + */ + public static Sprite loadSprite(String texKey) { + return loadSprite(texKey, 0, 0, textures.get(texKey).getWidth(), textures.get(texKey).getHeight()); + } - /** - * Load texture from path. - * - * @param filePath - */ - public static Texture loadTexture(String key, String filePath) { - Texture t = new Texture(Gdx.files.internal(filePath)); - textures.put(key, t); - return t; - } + /** + * Load texture from path. + * + * @param filePath + */ + public static Texture loadTexture(String key, String filePath) { + Texture t = new Texture(Gdx.files.internal(filePath)); + textures.put(key, t); + return t; + } - /** - * Disposes all the textures loaded so far. - */ - public static void dispose() { - for (Texture tex : textures.values()) { - tex.dispose(); - } - } + /** + * Disposes all the textures loaded so far. + */ + public static void dispose() { + Log.info("Disposed textures!"); + for (Texture tex : textures.values()) { + tex.dispose(); + } + } }