Merge branch 'master' into Mick

* master:
  refactor tile to gametile
  add tile
  removed inputtransform class, add comments and made smol fixes
  add ability to see which tile was selected when you click on it
  remade the map drawing with tile classes that have a position
  stuff
  add inputProcesser
This commit is contained in:
MickWerf
2020-05-25 21:58:51 +02:00
4 changed files with 128 additions and 29 deletions

View File

@@ -17,7 +17,7 @@ import netwerkprog.game.client.game.map.MapRenderer;
import netwerkprog.game.client.map.GameInputProcessor;
import netwerkprog.game.util.graphics.FrameRate;
public class MainGame extends ApplicationAdapter {
public class MainGame extends ApplicationAdapter{
SpriteBatch batch;
float screenWidth;
float screenHeight;
@@ -27,7 +27,7 @@ public class MainGame extends ApplicationAdapter {
private GameInputProcessor gameInputProcessor;
private Map map;
private MapRenderer mapRenderer;
public MapRenderer mapRenderer;
@@ -148,4 +148,6 @@ public class MainGame extends ApplicationAdapter {
public int getHorizontalTileAmount() {
return map.getWidth();
}
}

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.map.GameTile;
import netwerkprog.game.util.graphics.Renderable;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
@@ -22,7 +23,17 @@ public class MapRenderer implements Renderable {
public static TextureRegion WALL_TILE;
public static TextureRegion PATH_TILE;
private GameTile[][] gameTiles;
/**
* makea a new mapRenderer 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
*/
public MapRenderer(Map map, int tileWidth, SpriteBatch batch, OrthographicCamera camera) {
this.map = map;
this.tileWidth = tileWidth;
@@ -32,13 +43,33 @@ public class MapRenderer implements Renderable {
makeTiles();
}
/**
* loads all the images for the tiles and adds all the tiles to the array
*/
private void makeTiles() {
Texture texture = new Texture(Gdx.files.internal(tilePath));
TextureRegion[][] tiles = TextureRegion.split(texture, 32, 32);
FLOOR_TILE = tiles[1][6];
WALL_TILE = tiles[0][4];
PATH_TILE = tiles[4][6];
TextureRegion[][] tileTextures = TextureRegion.split(texture, 32, 32);
FLOOR_TILE = tileTextures[1][6];
WALL_TILE = tileTextures[0][4];
PATH_TILE = tileTextures[4][6];
this.gameTiles = new GameTile[map.getHeight()][map.getWidth()];
for (int row = map.getHeight(); row >= 0; row--) {
y += 32;
x = 0;
for (int col = 0; col < map.getWidth(); col++) {
if (map.get(row, col) == ' ') {
gameTiles[row][col] = new GameTile(FLOOR_TILE,x,y, ' ');
} else if (map.get(row, col) == '#') {
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');
}
x += 32;
}
}
}
public int getTileWidth() {
@@ -61,21 +92,14 @@ public class MapRenderer implements Renderable {
public void render() {
batch.begin();
batch.setProjectionMatrix(camera.combined);
for (int row = map.getHeight(); row >= 0; row--) {
y += 32;
x = 0;
for (int col = 0; col < map.getWidth(); col++) {
if (map.get(row, col) == ' ') {
batch.draw(FLOOR_TILE, x, y);
} else if (map.get(row, col) == '#') {
batch.draw(WALL_TILE, x, y);
} else if (map.get(row,col) == 'x') {
batch.draw(PATH_TILE,x,y);
}
x += 32;
for (GameTile[] gameTileRow : gameTiles) {
for (int col = 0; col < gameTiles[0].length; col++) {
GameTile cur = gameTileRow[col];
batch.draw(cur.getTextureRegion(), cur.x, cur.y);
}
}
// batch.draw(FLOOR_TILE,100,100);
batch.end();
x = 0;
y = 0;
@@ -88,8 +112,12 @@ public class MapRenderer implements Renderable {
public void resize(int screenWidth, int screenHeight) {
cam = new OrthographicCamera(screenWidth, screenHeight);
cam.translate(screenWidth / 2, screenHeight / 2);
cam.translate(screenWidth / 2f, screenHeight / 2f);
cam.update();
batch.setProjectionMatrix(cam.combined);
}
public GameTile[][] getGameTiles() {
return gameTiles;
}
}

View File

@@ -1,15 +1,15 @@
package netwerkprog.game.client.map;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.TimeUtils;
import netwerkprog.game.client.MainGame;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class GameInputProcessor implements InputProcessor {
private final OrthographicCamera camera;
@@ -25,6 +25,12 @@ public class GameInputProcessor implements InputProcessor {
private final float CAMERA_MOVE_SPEED = .3f;
/**
* 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) {
this.camera = camera;
this.game = game;
@@ -37,12 +43,6 @@ public class GameInputProcessor implements InputProcessor {
keysList.add(Input.Keys.D);
camera.zoom = MathUtils.clamp(camera.zoom, 1.5f, 1.8f);
//
// float effectiveViewportWidth = camera.viewportWidth * camera.zoom;
// float effectiveViewportHeight = camera.viewportHeight * camera.zoom;
//
// camera.position.x = MathUtils.clamp(camera.position.x, effectiveViewportWidth / 2f, game.getScreenWidth() - effectiveViewportWidth / 2f);
// camera.position.y = MathUtils.clamp(camera.position.y, effectiveViewportHeight / 2f, game.getScreenHeight() - effectiveViewportHeight / 2f);
}
@@ -88,7 +88,7 @@ public class GameInputProcessor implements InputProcessor {
@Override
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 (keycode == keysList.get(0)) {
this.isWPressed = false;
@@ -119,6 +119,20 @@ public class GameInputProcessor implements InputProcessor {
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
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];
if (gameTile.contains(touchPoint.x, touchPoint.y)) {
System.out.println(gameTile + " row: " + row + ", col: " + col);
//TODO make stuff happen with the tile
return true;
}
}
}
return false;
}

View File

@@ -0,0 +1,55 @@
package netwerkprog.game.client.map;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import java.util.Objects;
public class GameTile extends Rectangle {
private TextureRegion textureRegion;
private char symbol;
public GameTile(TextureRegion textureRegion, int xPos, int yPos, char symbol) {
this.textureRegion = textureRegion;
this.symbol = symbol;
super.x = xPos;
super.y = yPos;
super.width = textureRegion.getRegionWidth();
super.height = textureRegion.getRegionHeight();
}
public TextureRegion getTextureRegion() {
return textureRegion;
}
public char getSymbol() {
return symbol;
}
@Override
public String toString() {
return "Tile{" +
"symbol=" + symbol +
", x=" + x +
", y=" + y +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof GameTile)) return false;
if (!super.equals(o)) return false;
GameTile gameTile = (GameTile) o;
return getSymbol() == gameTile.getSymbol() &&
gameTile.x == this.x &&
gameTile.y == this.y &&
this.width == gameTile.width &&
this.height == gameTile.height;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), getTextureRegion(), getSymbol());
}
}