From ba2af725dafa44ef57d627dd00394b38f28b095b Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 27 May 2020 20:33:55 +0200 Subject: [PATCH] added characters to game tile and map renderer --- .../game/client/game/map/GameTile.java | 25 +++++++++++++++++++ .../game/client/game/map/MapRenderer.java | 17 +++++++------ .../netwerkprog/game/util/game/Character.java | 19 +++++++++++++- 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/core/src/netwerkprog/game/client/game/map/GameTile.java b/core/src/netwerkprog/game/client/game/map/GameTile.java index 964fe9f..4797b2a 100644 --- a/core/src/netwerkprog/game/client/game/map/GameTile.java +++ b/core/src/netwerkprog/game/client/game/map/GameTile.java @@ -2,12 +2,14 @@ package netwerkprog.game.client.game.map; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.math.Rectangle; +import netwerkprog.game.util.game.Character; import java.util.Objects; public class GameTile extends Rectangle { private TextureRegion textureRegion; private char symbol; + private Character character; public GameTile(TextureRegion textureRegion, int xPos, int yPos, char symbol) { this.textureRegion = textureRegion; @@ -18,6 +20,29 @@ public class GameTile extends Rectangle { super.height = textureRegion.getRegionHeight(); } + public Character getCharacter() { + return character; + } + + public boolean containsCharacter() { + return character == null; + } + + /** + * sets the character on this tile + * @param character the character to visit this tile + * @return false if this tile already had a character on it. + */ + public boolean setCharacter(Character character) { + if (this.character != null) return false; + this.character = character; + return true; + } + + public void removeCharacter() { + this.character = null; + } + public TextureRegion getTextureRegion() { return textureRegion; } diff --git a/core/src/netwerkprog/game/client/game/map/MapRenderer.java b/core/src/netwerkprog/game/client/game/map/MapRenderer.java index 86f91d4..30b6a30 100644 --- a/core/src/netwerkprog/game/client/game/map/MapRenderer.java +++ b/core/src/netwerkprog/game/client/game/map/MapRenderer.java @@ -25,13 +25,13 @@ public class MapRenderer implements Renderable { private GameTile[][] gameTiles; - /** * makea a new mapRenderer object - * @param map the map object + * + * @param map the map object * @param tileWidth the width of the tile - * @param batch the batch object so no new ones have to be made - * @param camera the camera object + * @param batch the batch object so no new ones have to be made + * @param camera the camera object */ public MapRenderer(Map map, int tileWidth, SpriteBatch batch, OrthographicCamera camera) { this.map = map; @@ -60,11 +60,11 @@ public class MapRenderer implements Renderable { x = 0; for (int col = 0; col < map.getWidth(); col++) { if (map.get(row, col) == ' ') { - gameTiles[row][col] = new GameTile(FLOOR_TILE,x,y, ' '); + gameTiles[row][col] = new GameTile(FLOOR_TILE, x, y, ' '); } else if (map.get(row, col) == '#') { - gameTiles[row][col] = new GameTile(WALL_TILE,x,y, '#'); + gameTiles[row][col] = new GameTile(WALL_TILE, x, y, '#'); } else if (map.get(row, col) == 'x') { - gameTiles[row][col] = new GameTile(PATH_TILE,x,y, 'x'); + gameTiles[row][col] = new GameTile(PATH_TILE, x, y, 'x'); } x += 32; } @@ -96,6 +96,9 @@ public class MapRenderer implements Renderable { for (int col = 0; col < gameTiles[0].length; col++) { GameTile cur = gameTileRow[col]; batch.draw(cur.getTextureRegion(), cur.x, cur.y); + if (cur.containsCharacter()) { + batch.draw(cur.getCharacter().getTextureRegion(), x, y); + } } } diff --git a/core/src/netwerkprog/game/util/game/Character.java b/core/src/netwerkprog/game/util/game/Character.java index 21bc8bd..807e748 100644 --- a/core/src/netwerkprog/game/util/game/Character.java +++ b/core/src/netwerkprog/game/util/game/Character.java @@ -1,5 +1,8 @@ package netwerkprog.game.util.game; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.g2d.TextureRegion; + import java.util.Arrays; import java.util.HashSet; @@ -8,12 +11,16 @@ public abstract class Character { protected Faction faction; protected HashSet abilities; protected boolean override; + protected TextureRegion textureRegion; + protected SpriteBatch batch; - public Character(String name, Faction faction, Ability... abilities) { + public Character(String name, Faction faction, TextureRegion textureRegion, Ability... abilities) { this.name = name; this.faction = faction; this.abilities = new HashSet<>(Arrays.asList(abilities)); this.override = false; + this.textureRegion = textureRegion; + batch = new SpriteBatch(); } public void addAbilities(Ability ability) { @@ -27,4 +34,14 @@ public abstract class Character { public void changeControl() { this.override = !this.override; } + + public TextureRegion getTextureRegion() { + return textureRegion; + } + + public void render(float x, float y) { + batch.begin(); + batch.draw(this.textureRegion, x, y); + batch.end(); + } }