add stuff for character placing

This commit is contained in:
Sem van der Hoeven
2020-05-27 22:31:02 +02:00
parent 9ddf94589b
commit dedb0e1af4
7 changed files with 131 additions and 23 deletions

View File

@@ -12,9 +12,10 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import netwerkprog.game.client.game.characters.Agent;
import netwerkprog.game.client.game.characters.Hacker;
import netwerkprog.game.client.game.characters.SelectedCharacter;
import netwerkprog.game.client.game.characters.abilities.BodySwap;
import netwerkprog.game.client.game.characters.abilities.Implant;
import netwerkprog.game.client.game.characters.abilities.Scrambler;
import netwerkprog.game.client.game.map.GameTile;
import netwerkprog.game.client.game.map.Map;
import netwerkprog.game.client.game.map.MapRenderer;
import netwerkprog.game.client.game.map.GameInputProcessor;
@@ -22,7 +23,7 @@ import netwerkprog.game.util.game.Character;
import netwerkprog.game.util.graphics.FrameRate;
import netwerkprog.game.util.tree.BST;
public class MainGame extends ApplicationAdapter{
public class MainGame extends ApplicationAdapter {
SpriteBatch batch;
float screenWidth;
float screenHeight;
@@ -30,6 +31,7 @@ public class MainGame extends ApplicationAdapter{
private Thread client;
private OrthographicCamera camera;
private GameInputProcessor gameInputProcessor;
private Character selectedCharacter;
private Map map;
public MapRenderer mapRenderer;
@@ -37,6 +39,17 @@ public class MainGame extends ApplicationAdapter{
private BST<Character> tree;
public Character testCharacter;
private static MainGame INSTANCE;
private MainGame() {
}
public static MainGame getInstance() {
if (INSTANCE == null) {
INSTANCE = new MainGame();
}
return INSTANCE;
}
@Override
@@ -64,15 +77,15 @@ public class MainGame extends ApplicationAdapter{
"#########################"
};
map = new Map(strings);
gameInputProcessor = new GameInputProcessor(camera, this);
gameInputProcessor = new GameInputProcessor(camera);
Gdx.input.setInputProcessor(gameInputProcessor);
mapRenderer = new MapRenderer(map, 32, batch, camera);
camera.position.set(screenWidth/2,screenHeight/2,0);
camera.position.set(screenWidth / 2, screenHeight / 2, 0);
camera.viewportWidth = screenWidth / 2;
camera.viewportHeight = screenHeight / 2;
camera.update();
this.tree = new BST<>();
initCharaters();
initCharacters();
// this.tree.insert(new Hacker(,new BodySwap()));
@@ -82,12 +95,15 @@ public class MainGame extends ApplicationAdapter{
// connectToServer();
}
private void initCharaters() {
private void initCharacters() {
Texture texture = new Texture(Gdx.files.internal("core/assets/characters.png"));
TextureRegion[][] characters = TextureRegion.split(texture,32,32);
this.testCharacter = new Hacker(characters[1][0],new BodySwap("test"));
TextureRegion[][] characters = TextureRegion.split(texture, 32, 32);
this.testCharacter = new Hacker(characters[1][0], new BodySwap("test"));
this.tree.insert(testCharacter);
this.tree.insert(new Agent(characters[2][0],new Implant("test")));
this.tree.insert(new Agent(characters[2][0], new Implant("test")));
this.setSelectedCharacter(testCharacter);
mapRenderer.getGameTiles()[1][1].setCharacter(testCharacter);
}
@@ -172,4 +188,16 @@ public class MainGame extends ApplicationAdapter{
return tree;
}
public void setSelectedCharacter(Character character) {
this.selectedCharacter = character;
System.out.println("selected character set to : " + character);
}
public Character getSelectedCharacter() {
return selectedCharacter;
}
public boolean hasCharacterSelected() {
return selectedCharacter != null;
}
}

View File

@@ -0,0 +1,30 @@
package netwerkprog.game.client.game.characters;
import netwerkprog.game.client.game.map.GameTile;
import netwerkprog.game.util.game.Character;
public class SelectedCharacter {
private Character character;
private GameTile currentTile;
public SelectedCharacter(Character character, GameTile tile) {
this.character = character;
this.currentTile =tile;
}
public Character getCharacter() {
return character;
}
public void setCharacter(Character character) {
this.character = character;
}
public GameTile getCurrentTile() {
return currentTile;
}
public void setCurrentTile(GameTile currentTile) {
this.currentTile = currentTile;
}
}

View File

@@ -8,12 +8,13 @@ import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.TimeUtils;
import netwerkprog.game.client.MainGame;
import netwerkprog.game.util.game.Character;
import java.util.ArrayList;
public class GameInputProcessor implements InputProcessor {
private final OrthographicCamera camera;
private MainGame game;
private MainGame mainGame;
private ArrayList<Integer> keysList;
private boolean isWPressed = false;
private boolean isAPressed = false;
@@ -29,11 +30,10 @@ public class GameInputProcessor implements InputProcessor {
* makes a new game input processor
*
* @param camera the camera object to use
* @param game the game object to get objects from
*/
public GameInputProcessor(OrthographicCamera camera, MainGame game) {
public GameInputProcessor(OrthographicCamera camera) {
this.camera = camera;
this.game = game;
this.mainGame = MainGame.getInstance();
keysList = new ArrayList<>();
lastTimeCounted = TimeUtils.millis();
@@ -123,13 +123,21 @@ public class GameInputProcessor implements InputProcessor {
Vector3 touchPoint = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPoint);
for (int row = 0; row < game.mapRenderer.getGameTiles().length; row++) {
for (int col = 0; col < game.mapRenderer.getGameTiles()[0].length; col++) {
GameTile gameTile = game.mapRenderer.getGameTiles()[row][col];
for (int row = 0; row < mainGame.mapRenderer.getGameTiles().length; row++) {
for (int col = 0; col < mainGame.mapRenderer.getGameTiles()[0].length; col++) {
GameTile gameTile = mainGame.mapRenderer.getGameTiles()[row][col];
if (gameTile.contains(touchPoint.x, touchPoint.y)) {
System.out.println(gameTile + " row: " + row + ", col: " + col);
gameTile.setCharacter(this.game.testCharacter);
//TODO make stuff happen with the tile
if (mainGame.hasCharacterSelected() && !gameTile.containsCharacter()) {
System.out.println(mainGame.getSelectedCharacter());
removeCharacterFromTile(mainGame.getSelectedCharacter());
gameTile.setCharacter(mainGame.getSelectedCharacter());
}
if (!mainGame.hasCharacterSelected() && gameTile.containsCharacter()) {
mainGame.setSelectedCharacter(gameTile.getCharacter());
}
return true;
}
}
@@ -137,6 +145,21 @@ public class GameInputProcessor implements InputProcessor {
return false;
}
private void removeCharacterFromTile(Character character) {
rowLoop:
for (int row = 0; row < mainGame.mapRenderer.getGameTiles().length; row++) {
for (int col = 0; col < mainGame.mapRenderer.getGameTiles()[0].length; col++) {
GameTile gameTile = mainGame.mapRenderer.getGameTiles()[row][col];
if (gameTile.containsCharacter() && gameTile.getCharacter().equals(character)) {
gameTile.setCharacter(null);
System.out.println("set character of gametile " + gameTile + " to null");
System.out.println("tile " + mainGame.mapRenderer.getGameTiles()[1][1] + " now has character " + mainGame.mapRenderer.getGameTiles()[1][1].getCharacter());
break rowLoop;
}
}
}
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
@@ -165,21 +188,21 @@ public class GameInputProcessor implements InputProcessor {
public void update() {
long delta = TimeUtils.timeSinceMillis(lastTimeCounted);
lastTimeCounted = TimeUtils.millis();
if (camera.position.x > 5 * game.getHorizontalTileAmount())
if (camera.position.x > 5 * mainGame.getHorizontalTileAmount())
if (isAPressed()) {
camera.position.add(-CAMERA_MOVE_SPEED * delta, 0, 0);
}
if (camera.position.y < 30 * game.getVerticalTileAmount())
if (camera.position.y < 30 * mainGame.getVerticalTileAmount())
if (isWPressed()) {
camera.position.add(0, CAMERA_MOVE_SPEED * delta, 0);
}
if (camera.position.y > 5 * game.getVerticalTileAmount())
if (camera.position.y > 5 * mainGame.getVerticalTileAmount())
if (isSPressed()) {
camera.position.add(0, -CAMERA_MOVE_SPEED * delta, 0);
}
if (camera.position.x < game.getScreenWidth() / 2 + 5 * game.getHorizontalTileAmount())
if (camera.position.x < mainGame.getScreenWidth() / 2 + 5 * mainGame.getHorizontalTileAmount())
if (isDPressed()) {
camera.position.add(CAMERA_MOVE_SPEED * delta, 0, 0);
}

View File

@@ -47,6 +47,10 @@ public class GameTile extends Rectangle {
return textureRegion;
}
public void setTextureRegion(TextureRegion textureRegion) {
this.textureRegion = textureRegion;
}
public char getSymbol() {
return symbol;
}

View File

@@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import netwerkprog.game.client.MainGame;
import netwerkprog.game.util.graphics.Renderable;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
@@ -17,6 +18,8 @@ public class MapRenderer implements Renderable {
private static int x = 0;
private static int y = 0;
private MainGame mainGame;
public static TextureRegion FLOOR_TILE;
public static TextureRegion WALL_TILE;
@@ -39,6 +42,7 @@ public class MapRenderer implements Renderable {
this.batch = batch;
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
this.camera = camera;
this.mainGame = MainGame.getInstance();
makeTiles();
}
@@ -98,6 +102,7 @@ public class MapRenderer implements Renderable {
batch.draw(cur.getTextureRegion(), cur.x, cur.y);
if (cur.containsCharacter()) {
batch.draw(cur.getCharacter().getTextureRegion(), cur.x, cur.y);
// System.out.println("drawing character at " + cur.x + " " + cur.y);
}
}
}

View File

@@ -5,6 +5,7 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
public abstract class Character implements Comparable<Character> {
protected String name;
@@ -49,6 +50,23 @@ public abstract class Character implements Comparable<Character> {
batch.end();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
if (!(o instanceof Character)) return false;
Character character = (Character) o;
return override == character.override &&
Objects.equals(name, character.name) &&
faction == character.faction &&
Objects.equals(abilities, character.abilities);
}
@Override
public int hashCode() {
return Objects.hash(name, faction, abilities, override);
}
@Override
public int compareTo(Character o) {
return this.name.compareTo(o.name);

View File

@@ -7,6 +7,6 @@ import netwerkprog.game.util.application.GameApplicationConfiguration;
public class DesktopLauncher {
public static void main (String[] arg) {
GameApplicationConfiguration config = new GameApplicationConfiguration("Netwerk Game",1200,800);
new LwjglApplication(new MainGame(), config);
new LwjglApplication(MainGame.getInstance(), config);
}
}