added characters to game tile and map renderer

This commit is contained in:
Sem van der Hoeven
2020-05-27 20:33:55 +02:00
parent e069fb1b77
commit ba2af725da
3 changed files with 53 additions and 8 deletions

View File

@@ -2,12 +2,14 @@ package netwerkprog.game.client.game.map;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
import netwerkprog.game.util.game.Character;
import java.util.Objects; import java.util.Objects;
public class GameTile extends Rectangle { public class GameTile extends Rectangle {
private TextureRegion textureRegion; private TextureRegion textureRegion;
private char symbol; private char symbol;
private Character character;
public GameTile(TextureRegion textureRegion, int xPos, int yPos, char symbol) { public GameTile(TextureRegion textureRegion, int xPos, int yPos, char symbol) {
this.textureRegion = textureRegion; this.textureRegion = textureRegion;
@@ -18,6 +20,29 @@ public class GameTile extends Rectangle {
super.height = textureRegion.getRegionHeight(); 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() { public TextureRegion getTextureRegion() {
return textureRegion; return textureRegion;
} }

View File

@@ -25,9 +25,9 @@ public class MapRenderer implements Renderable {
private GameTile[][] gameTiles; private GameTile[][] gameTiles;
/** /**
* makea a new mapRenderer object * makea a new mapRenderer object
*
* @param map the map object * @param map the map object
* @param tileWidth the width of the tile * @param tileWidth the width of the tile
* @param batch the batch object so no new ones have to be made * @param batch the batch object so no new ones have to be made
@@ -96,6 +96,9 @@ public class MapRenderer implements Renderable {
for (int col = 0; col < gameTiles[0].length; col++) { for (int col = 0; col < gameTiles[0].length; col++) {
GameTile cur = gameTileRow[col]; GameTile cur = gameTileRow[col];
batch.draw(cur.getTextureRegion(), cur.x, cur.y); batch.draw(cur.getTextureRegion(), cur.x, cur.y);
if (cur.containsCharacter()) {
batch.draw(cur.getCharacter().getTextureRegion(), x, y);
}
} }
} }

View File

@@ -1,5 +1,8 @@
package netwerkprog.game.util.game; 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.Arrays;
import java.util.HashSet; import java.util.HashSet;
@@ -8,12 +11,16 @@ public abstract class Character {
protected Faction faction; protected Faction faction;
protected HashSet<Ability> abilities; protected HashSet<Ability> abilities;
protected boolean override; 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.name = name;
this.faction = faction; this.faction = faction;
this.abilities = new HashSet<>(Arrays.asList(abilities)); this.abilities = new HashSet<>(Arrays.asList(abilities));
this.override = false; this.override = false;
this.textureRegion = textureRegion;
batch = new SpriteBatch();
} }
public void addAbilities(Ability ability) { public void addAbilities(Ability ability) {
@@ -27,4 +34,14 @@ public abstract class Character {
public void changeControl() { public void changeControl() {
this.override = !this.override; 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();
}
} }