Restructured lots of things & implemented game states.

This commit is contained in:
Jeasonfire 2015-05-16 01:35:51 +03:00
parent f87f44da51
commit 35eca95cd8
20 changed files with 464 additions and 278 deletions

View File

@ -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();
}
}

View File

@ -1,13 +1,14 @@
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() {
@ -18,6 +19,10 @@ public class GUIManager {
return this.rootNode;
}
public void clearGUI() {
getRootNode().clearChildren();
}
public ArrayList<GUINode> getAllRecursiveChildren(GUINode guiNode) {
ArrayList<GUINode> list = new ArrayList<GUINode>();
for (GUINode child : guiNode.getChildren()) {
@ -36,19 +41,19 @@ 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;
}

View File

@ -1,8 +0,0 @@
package com.saltosion.gladiator.gui;
import com.badlogic.gdx.graphics.g2d.Sprite;
public interface ImageNode {
public Sprite getImage();
}

View File

@ -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();
}

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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 {

View File

@ -0,0 +1,8 @@
package com.saltosion.gladiator.gui.properties;
import com.badlogic.gdx.graphics.g2d.Sprite;
public interface ImageProperty {
public Sprite getImage();
}

View File

@ -1,10 +1,10 @@
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.
*/
@ -12,6 +12,7 @@ public interface InteractiveNode {
/**
* 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.

View File

@ -1,4 +1,4 @@
package com.saltosion.gladiator.gui;
package com.saltosion.gladiator.gui.properties;
public interface TextProperty {

View File

@ -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;
@ -19,7 +19,7 @@ public class InputHandler implements InputProcessor {
public HashMap<Integer, String> keys = new HashMap<Integer, String>();
private Array<String> hoveredUIElements = new Array<String>();
private final Array<String> hoveredUIElements = new Array<String>();
public InputHandler() {
keys.put(Keys.A, Name.MOVE_LEFT);
@ -60,9 +60,11 @@ public class InputHandler implements InputProcessor {
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;
}
@ -72,9 +74,11 @@ public class InputHandler implements InputProcessor {
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,21 +90,23 @@ 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)) {

View File

@ -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);
}
}

View File

@ -0,0 +1,7 @@
package com.saltosion.gladiator.level;
public interface Level {
public void generate();
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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;

View File

@ -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);
}

View File

@ -9,61 +9,62 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class SpriteLoader {
public static HashMap<String, Texture> textures = new HashMap<String, Texture>();
public static HashMap<String, Texture> textures = new HashMap<String, Texture>();
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();
}
}
}