Characters moving #9

Merged
SemvdH merged 7 commits from characters-moving into master 2020-06-06 14:30:24 +00:00
5 changed files with 166 additions and 55 deletions
Showing only changes of commit 3827098c42 - Show all commits

BIN
core/assets/square2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 914 B

View File

@@ -8,16 +8,19 @@ import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
import netwerkprog.game.client.game.GAMESTATE;
import netwerkprog.game.client.game.characters.Hacker; import netwerkprog.game.client.game.characters.Hacker;
import netwerkprog.game.client.game.characters.Team; import netwerkprog.game.client.game.characters.Team;
import netwerkprog.game.client.game.characters.abilities.BodySwap; import netwerkprog.game.client.game.characters.abilities.BodySwap;
import netwerkprog.game.client.game.map.Map; import netwerkprog.game.client.game.map.Map;
import netwerkprog.game.client.game.map.MapRenderer; import netwerkprog.game.client.game.map.MapRenderer;
import netwerkprog.game.client.game.map.GameInputProcessor; import netwerkprog.game.client.game.map.GameInputProcessor;
import netwerkprog.game.util.game.Faction;
import netwerkprog.game.util.game.GameCharacter; import netwerkprog.game.util.game.GameCharacter;
import netwerkprog.game.util.graphics.FrameRate; import netwerkprog.game.util.graphics.FrameRate;
import netwerkprog.game.util.graphics.TextRenderer; import netwerkprog.game.util.graphics.TextRenderer;
@@ -35,11 +38,15 @@ public class MainGame extends ApplicationAdapter {
private Team enemyTeam; private Team enemyTeam;
private TextRenderer textRenderer; private TextRenderer textRenderer;
private BitmapFont font; private BitmapFont font;
private GlyphLayout layout;
private GAMESTATE gamestate;
private Faction chosenFaction;
private Map map; private Map map;
public MapRenderer mapRenderer; public MapRenderer mapRenderer;
public GameCharacter testCharacter; public GameCharacter testCharacter;
private static MainGame INSTANCE; private static MainGame INSTANCE;
@@ -64,17 +71,17 @@ public class MainGame extends ApplicationAdapter {
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
textRenderer = new TextRenderer(); textRenderer = new TextRenderer();
font = new BitmapFont(); font = new BitmapFont();
layout = new GlyphLayout();
String[] strings = new String[]{ String[] strings = new String[]{
"#########################", "#########################",
"#xxxx #", "#xxxx #",
"# x #", "# x #",
"# xxxx #", "# xxxx xxxxx #",
"# xx #", "# xxxx xxxxx #",
"# x #", "# xxxx xx xx #",
"# x #", "# x xxxxx #",
"# x #", "# x xxxx #",
"# x #", "# x #",
"# xxxxxx #", "# xxxxxx #",
"# x #", "# x #",
@@ -89,6 +96,7 @@ public class MainGame extends ApplicationAdapter {
camera.viewportWidth = screenWidth / 2; camera.viewportWidth = screenWidth / 2;
camera.viewportHeight = screenHeight / 2; camera.viewportHeight = screenHeight / 2;
camera.update(); camera.update();
setGamestate(GAMESTATE.SELECTING_FACTION);
initCharacters(); initCharacters();
// this.tree.insert(new Hacker(,new BodySwap())); // this.tree.insert(new Hacker(,new BodySwap()));
@@ -103,8 +111,8 @@ public class MainGame extends ApplicationAdapter {
private void initCharacters() { private void initCharacters() {
Texture texture = new Texture(Gdx.files.internal("core/assets/characters.png")); Texture texture = new Texture(Gdx.files.internal("core/assets/characters.png"));
TextureRegion[][] characters = TextureRegion.split(texture, 32, 32); TextureRegion[][] characters = TextureRegion.split(texture, 32, 32);
this.testCharacter = new Hacker("harryyyyyyyyyy",characters[1][0], new BodySwap("test")); this.testCharacter = new Hacker("harryyyyyyyyyy", characters[1][0], new BodySwap("test"));
GameCharacter character2 = new Hacker("test2",characters[2][0], new BodySwap("test")); GameCharacter character2 = new Hacker("test2", characters[2][0], new BodySwap("test"));
this.setSelectedCharacter(testCharacter); this.setSelectedCharacter(testCharacter);
mapRenderer.getGameTiles()[1][1].visit(testCharacter); mapRenderer.getGameTiles()[1][1].visit(testCharacter);
mapRenderer.getGameTiles()[1][2].visit(character2); mapRenderer.getGameTiles()[1][2].visit(character2);
@@ -139,19 +147,36 @@ public class MainGame extends ApplicationAdapter {
*/ */
@Override @Override
public void render() { public void render() {
update(); if (this.gamestate == GAMESTATE.PLAYING) {
// clear screen update();
Gdx.gl.glClearColor(0, 0, 0, 1); // clear screen
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClearColor(0, 0, 0, 1);
mapRenderer.render(); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
frameRate.render(); mapRenderer.render();
renderText(); frameRate.render();
renderText();
} else if (this.gamestate == GAMESTATE.SELECTING_FACTION) {
renderString("FACTION SELECT\nPress 1 for mega corporation, press 2 for hackers",Gdx.graphics.getWidth()/2f,Gdx.graphics.getHeight()/2f);
}
} }
private void renderText() { private void renderText() {
String text = "Selected character: " + selectedCharacter.getName(); String text = "FACION: " + chosenFaction;
textRenderer.render(text,Gdx.graphics.getWidth() - text.length() * 6.5f,Gdx.graphics.getHeight() - 3); text += "\nSelected character: " + selectedCharacter.getName();
text += "\nHealth: " + selectedCharacter.getHealth();
layout.setText(font, text);
textRenderer.render(text, Gdx.graphics.getWidth() - layout.width - 5, Gdx.graphics.getHeight() - 3);
}
private void renderString(String text) {
layout.setText(font, text);
textRenderer.render(text, Gdx.graphics.getWidth() - layout.width - 5, Gdx.graphics.getHeight() - 3);
}
private void renderString(String text, float x, float y) {
layout.setText(font, text);
textRenderer.render(text, x - layout.width/2f, x - layout.height/2f);
} }
/** /**
@@ -170,7 +195,7 @@ public class MainGame extends ApplicationAdapter {
screenWidth = width; screenWidth = width;
frameRate.resize(width, height); frameRate.resize(width, height);
mapRenderer.resize(width, height); mapRenderer.resize(width, height);
textRenderer.resize(width,height); textRenderer.resize(width, height);
} }
@Override @Override
@@ -205,6 +230,22 @@ public class MainGame extends ApplicationAdapter {
System.out.println("selected character set to : " + character); System.out.println("selected character set to : " + character);
} }
public GAMESTATE getGamestate() {
return gamestate;
}
public void setGamestate(GAMESTATE gamestate) {
this.gamestate = gamestate;
}
public Faction getChosenFaction() {
return chosenFaction;
}
public void setChosenFaction(Faction chosenFaction) {
this.chosenFaction = chosenFaction;
}
public GameCharacter getSelectedCharacter() { public GameCharacter getSelectedCharacter() {
return selectedCharacter; return selectedCharacter;
} }
@@ -216,4 +257,5 @@ public class MainGame extends ApplicationAdapter {
public Team getTeam() { public Team getTeam() {
return team; return team;
} }
} }

View File

@@ -0,0 +1,6 @@
package netwerkprog.game.client.game;
public enum GAMESTATE {
PLAYING,
SELECTING_FACTION
}

View File

@@ -8,6 +8,8 @@ import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.TimeUtils; import com.badlogic.gdx.utils.TimeUtils;
import netwerkprog.game.client.MainGame; import netwerkprog.game.client.MainGame;
import netwerkprog.game.client.game.GAMESTATE;
import netwerkprog.game.util.game.Faction;
import netwerkprog.game.util.game.GameCharacter; import netwerkprog.game.util.game.GameCharacter;
import java.util.ArrayList; import java.util.ArrayList;
@@ -89,28 +91,38 @@ public class GameInputProcessor implements InputProcessor {
@Override @Override
public boolean keyUp(int keycode) { public boolean keyUp(int keycode) {
// System.out.println(camera.position.x + " , " + camera.position.y); // System.out.println(camera.position.x + " , " + camera.position.y);
if (keysList.contains(keycode)) { if (mainGame.getGamestate() == GAMESTATE.PLAYING) {
if (keycode == keysList.get(0)) {
this.isWPressed = false;
return true;
}
if (keycode == keysList.get(1)) {
this.isAPressed = false;
return true;
}
if (keycode == keysList.get(2)) {
this.isSPressed = false;
return true;
}
if (keycode == keysList.get(3)) {
this.isDPressed = false;
return true;
}
return true; if (keysList.contains(keycode)) {
} else { if (keycode == keysList.get(0)) {
if (keycode == Input.Keys.ENTER) { this.isWPressed = false;
return true;
}
if (keycode == keysList.get(1)) {
this.isAPressed = false;
return true;
}
if (keycode == keysList.get(2)) {
this.isSPressed = false;
return true;
}
if (keycode == keysList.get(3)) {
this.isDPressed = false;
return true;
}
return true;
}
} else if (mainGame.getGamestate() == GAMESTATE.SELECTING_FACTION) {
if (keycode == Input.Keys.NUM_1) {
System.out.println("MEGA CORP");
mainGame.setChosenFaction(Faction.MEGACORPORATION);
mainGame.setGamestate(GAMESTATE.PLAYING);
}
if (keycode == Input.Keys.NUM_2) {
System.out.println("HACKER");
mainGame.setChosenFaction(Faction.HACKER);
mainGame.setGamestate(GAMESTATE.PLAYING);
} }
} }
return false; return false;
@@ -126,26 +138,31 @@ public class GameInputProcessor implements InputProcessor {
Vector3 touchPoint = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); Vector3 touchPoint = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPoint); camera.unproject(touchPoint);
if (mainGame.getGamestate() == GAMESTATE.PLAYING) {
for (int row = 0; row < mainGame.mapRenderer.getGameTiles().length; row++) {
for (int col = 0; col < mainGame.mapRenderer.getGameTiles()[0].length; col++) { for (int row = 0; row < mainGame.mapRenderer.getGameTiles().length; row++) {
GameTile gameTile = mainGame.mapRenderer.getGameTiles()[row][col]; for (int col = 0; col < mainGame.mapRenderer.getGameTiles()[0].length; col++) {
if (gameTile.contains(touchPoint.x, touchPoint.y)) { GameTile gameTile = mainGame.mapRenderer.getGameTiles()[row][col];
if (button == Input.Buttons.LEFT) { if (gameTile.contains(touchPoint.x, touchPoint.y)) {
if (button == Input.Buttons.LEFT) {
// System.out.println(gameTile + " row: " + row + ", col: " + col); // System.out.println(gameTile + " row: " + row + ", col: " + col);
if (mainGame.hasCharacterSelected() && !gameTile.containsCharacter()) { if (mainGame.hasCharacterSelected() && !gameTile.containsCharacter()) {
// System.out.println(mainGame.getSelectedCharacter()); // System.out.println(mainGame.getSelectedCharacter());
removeCharacterFromTile(mainGame.getSelectedCharacter()); if (gameTile.getSymbol() != '#') {
if (gameTile.getSymbol() != '#') removeCharacterFromTile(mainGame.getSelectedCharacter());
gameTile.visit(mainGame.getSelectedCharacter()); gameTile.visit(mainGame.getSelectedCharacter());
mainGame.mapRenderer.setSurroundedTilesOfCurrentCharacter(col,row);
}
}
if (!mainGame.hasCharacterSelected() && gameTile.containsCharacter()) {
mainGame.setSelectedCharacter(gameTile.getCharacter());
}
if (gameTile.containsCharacter() && !mainGame.getSelectedCharacter().equals(gameTile.getCharacter())) {
mainGame.setSelectedCharacter(gameTile.getCharacter());
}
return true;
} }
if (!mainGame.hasCharacterSelected() && gameTile.containsCharacter()) {
mainGame.setSelectedCharacter(gameTile.getCharacter());
}
if (gameTile.containsCharacter() && !mainGame.getSelectedCharacter().equals(gameTile.getCharacter())) {
mainGame.setSelectedCharacter(gameTile.getCharacter());
}
return true;
} }
} }
} }

View File

@@ -10,6 +10,9 @@ import netwerkprog.game.client.MainGame;
import netwerkprog.game.util.graphics.Renderable; import netwerkprog.game.util.graphics.Renderable;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
import java.util.ArrayList;
import java.util.List;
public class MapRenderer implements Renderable { public class MapRenderer implements Renderable {
private final OrthographicCamera camera; private final OrthographicCamera camera;
private int tileWidth; private int tileWidth;
@@ -25,6 +28,7 @@ public class MapRenderer implements Renderable {
private MainGame mainGame; private MainGame mainGame;
private Texture square; private Texture square;
private Texture square2;
public static TextureRegion FLOOR_TILE; public static TextureRegion FLOOR_TILE;
@@ -32,6 +36,7 @@ public class MapRenderer implements Renderable {
public static TextureRegion PATH_TILE; public static TextureRegion PATH_TILE;
private GameTile[][] gameTiles; private GameTile[][] gameTiles;
private List<GameTile> surroundedTilesOfCurrentCharacter;
/** /**
@@ -51,6 +56,7 @@ public class MapRenderer implements Renderable {
this.mainGame = MainGame.getInstance(); this.mainGame = MainGame.getInstance();
font = new BitmapFont(); font = new BitmapFont();
square = new Texture(Gdx.files.internal("square.png")); square = new Texture(Gdx.files.internal("square.png"));
square2 = new Texture(Gdx.files.internal("square2.png"));
makeTiles(); makeTiles();
} }
@@ -111,17 +117,53 @@ public class MapRenderer implements Renderable {
if (cur.containsCharacter()) { if (cur.containsCharacter()) {
batch.draw(cur.getCharacter().getTextureRegion(), cur.x, cur.y); batch.draw(cur.getCharacter().getTextureRegion(), cur.x, cur.y);
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);
}
} }
} }
} }
if (surroundedTilesOfCurrentCharacter != null && !surroundedTilesOfCurrentCharacter.isEmpty()) {
for (GameTile gameTile : surroundedTilesOfCurrentCharacter) {
batch.draw(square2, gameTile.x, gameTile.y);
}
}
batch.end(); batch.end();
x = 0; x = 0;
y = 0; y = 0;
} }
private 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>();
for (int[] direction : directions) {
int cx = x + direction[0];
int cy = y + direction[1];
if (cy >= 0 && cy < gameTiles.length)
if (cx >= 0 && cx < gameTiles[cy].length)
res.add(gameTiles[cy][cx]);
}
surroundedTilesOfCurrentCharacter = res;
return res;
}
public int getPos(GameTile tile, String choice) {
for (int row = 0; row < this.gameTiles.length; row++) {
for (int col = 0; col < this.gameTiles[0].length; col++) {
if (gameTiles[row][col].equals(tile)) {
if (choice.toLowerCase().equals("row"))
return row;
else if (choice.toLowerCase().equals("col"))
return col;
}
}
}
return -1;
}
@Override @Override
public void update(double deltaTime) { public void update(double deltaTime) {
@@ -137,4 +179,8 @@ public class MapRenderer implements Renderable {
public GameTile[][] getGameTiles() { public GameTile[][] getGameTiles() {
return gameTiles; return gameTiles;
} }
public List<GameTile> getSurroundedTilesOfCurrentCharacter() {
return surroundedTilesOfCurrentCharacter;
}
} }