click click click click #5

Merged
SemvdH merged 5 commits from add-clicking into master 2020-05-25 19:25:51 +00:00
4 changed files with 109 additions and 27 deletions

View File

@@ -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.Tile;
import netwerkprog.game.util.graphics.Renderable;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
@@ -22,7 +23,18 @@ public class MapRenderer implements Renderable {
public static TextureRegion WALL_TILE;
public static TextureRegion PATH_TILE;
private Tile[][] tiles;
private boolean isStarted = false;
/**
* 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 +44,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.tiles = new Tile[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) == ' ') {
tiles[row][col] = new Tile(FLOOR_TILE,x,y, ' ');
} else if (map.get(row, col) == '#') {
tiles[row][col] = new Tile(WALL_TILE,x,y, '#');
} else if (map.get(row, col) == 'x') {
tiles[row][col] = new Tile(PATH_TILE,x,y, 'x');
}
x += 32;
}
}
}
public int getTileWidth() {
@@ -61,21 +93,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 (Tile[] tileRow : tiles) {
for (int col = 0; col < tiles[0].length; col++) {
Tile cur = tileRow[col];
batch.draw(cur.getTextureRegion(), cur.x, cur.y);
}
}
// batch.draw(FLOOR_TILE,100,100);
batch.end();
x = 0;
y = 0;
@@ -88,8 +113,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 Tile[][] getTiles() {
return tiles;
}
}

View File

@@ -1,9 +1,11 @@
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;
@@ -25,6 +27,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 +45,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 +90,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 +121,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.getTiles().length; row++) {
for (int col = 0; col < game.mapRenderer.getTiles()[0].length; col++) {
Tile tile = game.mapRenderer.getTiles()[row][col];
if (tile.contains(touchPoint.x, touchPoint.y)) {
System.out.println(tile + " row: " + row + ", col: " + col);
//TODO make stuff happen with the tile
return true;
}
}
}
return false;
}

View File

@@ -0,0 +1,35 @@
package netwerkprog.game.client.map;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
public class Tile extends Rectangle{
private TextureRegion textureRegion;
private char symbol;
public Tile(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 +
'}';
}
}