click click click click #5
@@ -17,7 +17,7 @@ import netwerkprog.game.client.game.map.MapRenderer;
|
|||||||
import netwerkprog.game.client.map.GameInputProcessor;
|
import netwerkprog.game.client.map.GameInputProcessor;
|
||||||
import netwerkprog.game.util.graphics.FrameRate;
|
import netwerkprog.game.util.graphics.FrameRate;
|
||||||
|
|
||||||
public class MainGame extends ApplicationAdapter {
|
public class MainGame extends ApplicationAdapter{
|
||||||
SpriteBatch batch;
|
SpriteBatch batch;
|
||||||
float screenWidth;
|
float screenWidth;
|
||||||
float screenHeight;
|
float screenHeight;
|
||||||
@@ -27,7 +27,7 @@ public class MainGame extends ApplicationAdapter {
|
|||||||
private GameInputProcessor gameInputProcessor;
|
private GameInputProcessor gameInputProcessor;
|
||||||
|
|
||||||
private Map map;
|
private Map map;
|
||||||
private MapRenderer mapRenderer;
|
public MapRenderer mapRenderer;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -148,4 +148,6 @@ public class MainGame extends ApplicationAdapter {
|
|||||||
public int getHorizontalTileAmount() {
|
public int getHorizontalTileAmount() {
|
||||||
return map.getWidth();
|
return map.getWidth();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx;
|
|||||||
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.SpriteBatch;
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
|
import netwerkprog.game.client.map.Tile;
|
||||||
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;
|
||||||
|
|
||||||
@@ -22,7 +23,18 @@ public class MapRenderer implements Renderable {
|
|||||||
public static TextureRegion WALL_TILE;
|
public static TextureRegion WALL_TILE;
|
||||||
public static TextureRegion PATH_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) {
|
public MapRenderer(Map map, int tileWidth, SpriteBatch batch, OrthographicCamera camera) {
|
||||||
this.map = map;
|
this.map = map;
|
||||||
this.tileWidth = tileWidth;
|
this.tileWidth = tileWidth;
|
||||||
@@ -32,13 +44,33 @@ public class MapRenderer implements Renderable {
|
|||||||
makeTiles();
|
makeTiles();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* loads all the images for the tiles and adds all the tiles to the array
|
||||||
|
*/
|
||||||
private void makeTiles() {
|
private void makeTiles() {
|
||||||
Texture texture = new Texture(Gdx.files.internal(tilePath));
|
Texture texture = new Texture(Gdx.files.internal(tilePath));
|
||||||
TextureRegion[][] tiles = TextureRegion.split(texture, 32, 32);
|
TextureRegion[][] tileTextures = TextureRegion.split(texture, 32, 32);
|
||||||
FLOOR_TILE = tiles[1][6];
|
|
||||||
WALL_TILE = tiles[0][4];
|
|
||||||
PATH_TILE = tiles[4][6];
|
|
||||||
|
|
||||||
|
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() {
|
public int getTileWidth() {
|
||||||
@@ -61,21 +93,14 @@ public class MapRenderer implements Renderable {
|
|||||||
public void render() {
|
public void render() {
|
||||||
batch.begin();
|
batch.begin();
|
||||||
batch.setProjectionMatrix(camera.combined);
|
batch.setProjectionMatrix(camera.combined);
|
||||||
for (int row = map.getHeight(); row >= 0; row--) {
|
|
||||||
y += 32;
|
for (Tile[] tileRow : tiles) {
|
||||||
x = 0;
|
for (int col = 0; col < tiles[0].length; col++) {
|
||||||
for (int col = 0; col < map.getWidth(); col++) {
|
Tile cur = tileRow[col];
|
||||||
if (map.get(row, col) == ' ') {
|
batch.draw(cur.getTextureRegion(), cur.x, cur.y);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// batch.draw(FLOOR_TILE,100,100);
|
|
||||||
batch.end();
|
batch.end();
|
||||||
x = 0;
|
x = 0;
|
||||||
y = 0;
|
y = 0;
|
||||||
@@ -88,8 +113,12 @@ public class MapRenderer implements Renderable {
|
|||||||
|
|
||||||
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 / 2, screenHeight / 2);
|
cam.translate(screenWidth / 2f, screenHeight / 2f);
|
||||||
cam.update();
|
cam.update();
|
||||||
batch.setProjectionMatrix(cam.combined);
|
batch.setProjectionMatrix(cam.combined);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Tile[][] getTiles() {
|
||||||
|
return tiles;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package netwerkprog.game.client.map;
|
package netwerkprog.game.client.map;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.Input;
|
import com.badlogic.gdx.Input;
|
||||||
import com.badlogic.gdx.InputProcessor;
|
import com.badlogic.gdx.InputProcessor;
|
||||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||||
import com.badlogic.gdx.math.MathUtils;
|
import com.badlogic.gdx.math.MathUtils;
|
||||||
|
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;
|
||||||
|
|
||||||
@@ -25,6 +27,12 @@ public class GameInputProcessor implements InputProcessor {
|
|||||||
private final float CAMERA_MOVE_SPEED = .3f;
|
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) {
|
public GameInputProcessor(OrthographicCamera camera, MainGame game) {
|
||||||
this.camera = camera;
|
this.camera = camera;
|
||||||
this.game = game;
|
this.game = game;
|
||||||
@@ -37,12 +45,6 @@ public class GameInputProcessor implements InputProcessor {
|
|||||||
keysList.add(Input.Keys.D);
|
keysList.add(Input.Keys.D);
|
||||||
|
|
||||||
camera.zoom = MathUtils.clamp(camera.zoom, 1.5f, 1.8f);
|
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
|
@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 (keysList.contains(keycode)) {
|
||||||
if (keycode == keysList.get(0)) {
|
if (keycode == keysList.get(0)) {
|
||||||
this.isWPressed = false;
|
this.isWPressed = false;
|
||||||
@@ -119,6 +121,20 @@ public class GameInputProcessor implements InputProcessor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
35
core/src/netwerkprog/game/client/map/Tile.java
Normal file
35
core/src/netwerkprog/game/client/map/Tile.java
Normal 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 +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user