This commit is contained in:
Sem van der Hoeven
2020-06-07 16:51:41 +02:00
parent 133310a3e2
commit b80fe293e0
2 changed files with 70 additions and 38 deletions

View File

@@ -53,15 +53,12 @@ public class MainGame extends Game implements ClientCallback {
private GlyphLayout layout; private GlyphLayout layout;
private GAMESTATE gamestate; private GAMESTATE gamestate;
private Faction chosenFaction; private Faction chosenFaction;
private Faction enemyFaction;
private long lastTimeCounted = 0;
private boolean gameOver = false; private boolean gameOver = false;
private int turn = 0; private int turn = 0;
private boolean playersTurn = true; private boolean playersTurn = true;
private String username; private String username;
private boolean ready = false; private boolean ready = false;
private boolean enemyReady = false; private boolean enemyReady = false;
private Texture texture;
private Map map; private Map map;
public MapRenderer mapRenderer; public MapRenderer mapRenderer;
@@ -116,19 +113,14 @@ public class MainGame extends Game implements ClientCallback {
camera.viewportHeight = screenHeight / 2; camera.viewportHeight = screenHeight / 2;
camera.update(); camera.update();
setGamestate(GAMESTATE.SELECTING_FACTION); setGamestate(GAMESTATE.SELECTING_FACTION);
// this.tree.insert(new Hacker(,new BodySwap()));
// playSong();
connectToServer(); connectToServer();
} }
public void initCharacters() { public void initCharacters() {
assets.load("core/assets/characters.png", Texture.class); assets.load("core/assets/characters.png", Texture.class);
assets.finishLoading(); assets.finishLoading();
texture = assets.get("core/assets/characters.png", Texture.class);
Texture texture = assets.get("core/assets/characters.png", Texture.class);
TextureRegion[][] characters = TextureRegion.split(texture, 32, 32); TextureRegion[][] characters = TextureRegion.split(texture, 32, 32);
this.team = new Team(); this.team = new Team();
this.enemyTeam = new Team(); this.enemyTeam = new Team();
@@ -154,6 +146,7 @@ public class MainGame extends Game implements ClientCallback {
this.setSelectedCharacter(this.team.get(0)); this.setSelectedCharacter(this.team.get(0));
} }
@@ -297,6 +290,7 @@ public class MainGame extends Game implements ClientCallback {
textRenderer.dispose(); textRenderer.dispose();
assets.dispose(); assets.dispose();
textRenderer.dispose(); textRenderer.dispose();
mapRenderer.dispose();
} }
public float getScreenWidth() { public float getScreenWidth() {
@@ -383,26 +377,22 @@ public class MainGame extends Game implements ClientCallback {
@Override @Override
public void onDataReceived(Data data) { public void onDataReceived(Data data) {
// System.out.println("[MAINGAME" + this.username + "] Got data: " + data.toString());
if (data instanceof NameData) { if (data instanceof NameData) {
this.username = ((NameData) data).getName(); this.username = ((NameData) data).getName();
// System.out.println("[MAINGAME" + this.username + "] username is: " + username);
} else if (data instanceof TeamData) { } else if (data instanceof TeamData) {
// check if it is not our own message // check if it is not our own message
if (!((TeamData) data).getUsername().equals(this.username)) { if (!((TeamData) data).getUsername().equals(this.username)) {
// if we have already chosen a faction, so we were first // if we have already chosen a faction, so we were first
TeamData teamData = (TeamData) data; TeamData teamData = (TeamData) data;
enemyFaction = teamData.getFaction(); Faction enemyFaction = teamData.getFaction();
if (this.chosenFaction == null) { if (this.chosenFaction == null) {
if (enemyFaction == Faction.HACKER) { if (enemyFaction == Faction.HACKER) {
this.chosenFaction = Faction.MEGACORPORATION; this.chosenFaction = Faction.MEGACORPORATION;
this.enemyReady = true;
this.ready = true;
} else { } else {
this.chosenFaction = Faction.HACKER; this.chosenFaction = Faction.HACKER;
this.enemyReady = true;
this.ready = true;
} }
this.enemyReady = true;
this.ready = true;
} }
} }
} else if (data instanceof MoveData) { } else if (data instanceof MoveData) {
@@ -423,7 +413,6 @@ public class MainGame extends Game implements ClientCallback {
} }
public void chooseHacker() { public void chooseHacker() {
System.out.println("chose HACKER");
setChosenFaction(Faction.HACKER); setChosenFaction(Faction.HACKER);
send(new TeamData(Faction.MEGACORPORATION, getUsername())); send(new TeamData(Faction.MEGACORPORATION, getUsername()));
@@ -435,7 +424,6 @@ public class MainGame extends Game implements ClientCallback {
} }
public void chooseMegaCorp() { public void chooseMegaCorp() {
System.out.println("chose MEGA CORP");
setChosenFaction(Faction.MEGACORPORATION); setChosenFaction(Faction.MEGACORPORATION);
send(new TeamData(Faction.MEGACORPORATION, getUsername())); send(new TeamData(Faction.MEGACORPORATION, getUsername()));
initCharacters(); initCharacters();

View File

@@ -36,6 +36,7 @@ public class MapRenderer implements Renderable {
private GameTile[][] gameTiles; private GameTile[][] gameTiles;
private List<GameTile> surroundedTilesOfCurrentCharacter; private List<GameTile> surroundedTilesOfCurrentCharacter;
public static int[][] directions = new int[][]{{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}};
/** /**
* makea a new mapRenderer object * makea a new mapRenderer object
@@ -62,14 +63,15 @@ public class MapRenderer implements Renderable {
mainGame.assets.load("square.png", Texture.class); mainGame.assets.load("square.png", Texture.class);
mainGame.assets.load("square2.png", Texture.class); mainGame.assets.load("square2.png", Texture.class);
mainGame.assets.load(tilePath, Texture.class); mainGame.assets.load(tilePath, Texture.class);
mainGame.assets.load("hit.png",Texture.class); mainGame.assets.load("hit.png", Texture.class);
mainGame.assets.load("dead.png",Texture.class); mainGame.assets.load("dead.png", Texture.class);
mainGame.assets.finishLoading(); mainGame.assets.finishLoading();
square = mainGame.assets.get("square.png"); square = mainGame.assets.get("square.png");
square2 = mainGame.assets.get("square2.png"); square2 = mainGame.assets.get("square2.png");
hitMarker = mainGame.assets.get("hit.png"); hitMarker = mainGame.assets.get("hit.png");
tombStone = mainGame.assets.get("dead.png"); tombStone = mainGame.assets.get("dead.png");
// load the texture file
Texture texture = mainGame.assets.get(tilePath); Texture texture = mainGame.assets.get(tilePath);
TextureRegion[][] tileTextures = TextureRegion.split(texture, 32, 32); TextureRegion[][] tileTextures = TextureRegion.split(texture, 32, 32);
@@ -77,8 +79,10 @@ public class MapRenderer implements Renderable {
WALL_TILE = tileTextures[0][4]; WALL_TILE = tileTextures[0][4];
PATH_TILE = tileTextures[4][6]; PATH_TILE = tileTextures[4][6];
// init the array
this.gameTiles = new GameTile[map.getHeight()][map.getWidth()]; this.gameTiles = new GameTile[map.getHeight()][map.getWidth()];
// for each game tile, put the corresponding tile image in the array
for (int row = map.getHeight(); row >= 0; row--) { for (int row = map.getHeight(); row >= 0; row--) {
y += 32; y += 32;
x = 0; x = 0;
@@ -111,6 +115,9 @@ public class MapRenderer implements Renderable {
this.map = map; this.map = map;
} }
/**
* method that renders the whole map
*/
@Override @Override
public void render() { public void render() {
batch.begin(); batch.begin();
@@ -119,25 +126,28 @@ public class MapRenderer implements Renderable {
for (GameTile[] gameTileRow : gameTiles) { for (GameTile[] gameTileRow : gameTiles) {
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];
//draw each tile
batch.draw(cur.getTextureRegion(), cur.x, cur.y); batch.draw(cur.getTextureRegion(), cur.x, cur.y);
if (cur.containsCharacter()) { if (cur.containsCharacter()) {
//draw each character on a tile
GameCharacter character = cur.getCharacter(); GameCharacter character = cur.getCharacter();
if (!character.isDead()) { if (!character.isDead()) {
batch.draw(character.getTextureRegion(), cur.x, cur.y); batch.draw(character.getTextureRegion(), cur.x, cur.y);
// System.out.println("character " + character.getName() + " showing: " + character.isShowingAnimation());
if (character.isShowingAnimation()) { //if he's showing an animation, draw the hitmarker.
// System.out.println("animation"); if (character.isShowingAnimation())
batch.draw(hitMarker, cur.x, cur.y); batch.draw(hitMarker, cur.x, cur.y);
}
// if hes selected, draw the green square
if (cur.getCharacter().equals(mainGame.getSelectedCharacter())) { if (cur.getCharacter().equals(mainGame.getSelectedCharacter()))
batch.draw(square, cur.x, cur.y); batch.draw(square, cur.x, cur.y);
}
} else { } else {
batch.draw(tombStone,cur.x,cur.y); // if hes dead, draw a tombstone
batch.draw(tombStone, cur.x, cur.y);
} }
} }
} }
@@ -153,10 +163,15 @@ public class MapRenderer implements Renderable {
y = 0; y = 0;
} }
public static int[][] directions = new int[][]{{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}};
public List<GameTile> setSurroundedTilesOfCurrentCharacter(int x, int y) {
List<GameTile> res = new ArrayList<GameTile>(); /**
* gets the 8 surrounding tiles of the character, to see where he can move.
* @param x the x position of the character
* @param y the y position of the character
*/
public void setSurroundedTilesOfCurrentCharacter(int x, int y) {
List<GameTile> res = new ArrayList<>();
for (int[] direction : directions) { for (int[] direction : directions) {
int cx = x + direction[0]; int cx = x + direction[0];
int cy = y + direction[1]; int cy = y + direction[1];
@@ -166,9 +181,13 @@ public class MapRenderer implements Renderable {
res.add(gameTiles[cy][cx]); res.add(gameTiles[cy][cx]);
} }
surroundedTilesOfCurrentCharacter = res; surroundedTilesOfCurrentCharacter = res;
return res;
} }
/**
* gets the game tile of the character.
* @param character the character
* @return the game tile of the character, null if it is not found
*/
public GameTile getTile(GameCharacter character) { public GameTile getTile(GameCharacter character) {
for (GameTile[] tiles : this.gameTiles) { for (GameTile[] tiles : this.gameTiles) {
for (GameTile tile : tiles) { for (GameTile tile : tiles) {
@@ -181,7 +200,19 @@ public class MapRenderer implements Renderable {
return null; return null;
} }
public void dispose() {
tombStone.dispose();
square.dispose();
square2.dispose();
hitMarker.dispose();
}
/**
* gets the position of the specified tile.
* @param tile the tile to get the position of
* @return the position of the tile, a point of -1,-1 if the tile is not found
*/
public Point getPos(GameTile tile) { public Point getPos(GameTile tile) {
for (int row = 0; row < this.gameTiles.length; row++) { for (int row = 0; row < this.gameTiles.length; row++) {
for (int col = 0; col < this.gameTiles[0].length; col++) { for (int col = 0; col < this.gameTiles[0].length; col++) {
@@ -193,6 +224,11 @@ public class MapRenderer implements Renderable {
return new Point(-1, -1); return new Point(-1, -1);
} }
/**
* gets the game tile at the specified position.
* @param pos the position of the tile
* @return the game tile on the position, <code>null</code> if it is not found
*/
public GameTile getGameTile(Point pos) { public GameTile getGameTile(Point pos) {
for (int row = 0; row < this.gameTiles.length; row++) { for (int row = 0; row < this.gameTiles.length; row++) {
for (int col = 0; col < this.gameTiles[0].length; col++) { for (int col = 0; col < this.gameTiles[0].length; col++) {
@@ -204,11 +240,11 @@ public class MapRenderer implements Renderable {
return null; return null;
} }
@Override /**
public void update(double deltaTime) { * resize the screen
* @param screenWidth the width of the screen
} * @param screenHeight the height of the screen
*/
public void resize(int screenWidth, int screenHeight) { public void resize(int screenWidth, int screenHeight) {
cam = new OrthographicCamera(screenWidth, screenHeight); cam = new OrthographicCamera(screenWidth, screenHeight);
cam.translate(screenWidth / 2f, screenHeight / 2f); cam.translate(screenWidth / 2f, screenHeight / 2f);
@@ -216,10 +252,18 @@ public class MapRenderer implements Renderable {
batch.setProjectionMatrix(cam.combined); batch.setProjectionMatrix(cam.combined);
} }
/**
* return the game tiles
* @return the game tiles.
*/
public GameTile[][] getGameTiles() { public GameTile[][] getGameTiles() {
return gameTiles; return gameTiles;
} }
/**
* get the surrounding tiles of character
* @return the surrounding tiles of character
*/
public List<GameTile> getSurroundedTilesOfCurrentCharacter() { public List<GameTile> getSurroundedTilesOfCurrentCharacter() {
return surroundedTilesOfCurrentCharacter; return surroundedTilesOfCurrentCharacter;
} }