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

View File

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

View File

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