made drawing of tiles possible

This commit is contained in:
Sem van der Hoeven
2020-05-18 14:01:40 +02:00
parent aa85e076bf
commit ca2747ef03
4 changed files with 84 additions and 34 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View File

@@ -8,39 +8,49 @@ import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import netwerkprog.game.client.Client;
import netwerkprog.game.client.map.Map;
import netwerkprog.game.client.map.MapRenderer;
import netwerkprog.game.server.Server;
import netwerkprog.game.util.FrameRate;
public class MainGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
float xPos = 500;
float yPos = 500;
float xUpdate;
float yUpdate;
float screenWidth;
float screenHeight;
private FrameRate frameRate;
private Client client;
private Map map;
private MapRenderer mapRenderer;
@Override
public void create() {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
float ratio = (float) Gdx.graphics.getWidth() / Gdx.graphics.getHeight();
xUpdate = ratio;
yUpdate = ratio;
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
frameRate = new FrameRate();
String[] strings = new String[]{
"####",
"# #",
"####"
};
map = new Map(strings);
mapRenderer = new MapRenderer(map,32,batch);
// playSong();
// connectToServer();
}
private void playSong() {
// play music
Music music = Gdx.audio.newMusic(Gdx.files.getFileHandle("core/assets/music.mp3", Files.FileType.Internal));
music.setVolume(.1f);
music.play();
music.setLooping(true);
connectToServer();
}
private void connectToServer() {
@@ -58,11 +68,10 @@ public class MainGame extends ApplicationAdapter {
@Override
public void render() {
update();
Gdx.gl.glClearColor(xPos / Gdx.graphics.getWidth(), 0, 0, 1);
// clear screen
Gdx.gl.glClearColor(1, 1, 1, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, xPos, yPos);
batch.end();
mapRenderer.render();
frameRate.render();
}
@@ -71,20 +80,6 @@ public class MainGame extends ApplicationAdapter {
*/
public void update() {
frameRate.update();
updatePos();
}
private void updatePos() {
yPos += yUpdate;
xPos += xUpdate;
if (yPos > screenHeight - img.getHeight() || yPos < 0) {
yUpdate = -yUpdate;
}
if (xPos > screenWidth - img.getWidth() || xPos < 0) {
xUpdate = -xUpdate;
}
}
@Override
@@ -92,6 +87,8 @@ public class MainGame extends ApplicationAdapter {
super.resize(width, height);
screenHeight = height;
screenWidth = width;
frameRate.resize(width,height);
mapRenderer.resize(width,height);
}
@Override
@@ -102,6 +99,5 @@ public class MainGame extends ApplicationAdapter {
@Override
public void dispose() {
batch.dispose();
img.dispose();
}
}

View File

@@ -1,5 +1,7 @@
package netwerkprog.game.client.map;
import java.util.Arrays;
/**
* Map class to hold a 2d array of tiles which will specify the map
*/
@@ -67,5 +69,15 @@ public class Map {
return this.map == null || this.map.length == 0 ? -1 : this.map[0].length;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int row = 0; row < getHeight(); row++) {
for (int col = 0; col < getWidth(); col++) {
sb.append(this.map[row][col]);
}
sb.append("\n");
}
return sb.toString();
}
}

View File

@@ -1,16 +1,35 @@
package netwerkprog.game.client.map;
import com.badlogic.gdx.graphics.g2d.Batch;
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 com.badlogic.gdx.graphics.g2d.TextureRegion;
import netwerkprog.game.util.Renderable;
public class MapRenderer implements Renderable {
private int tileWidth;
private Map map;
private SpriteBatch batch;
private static String tilePath = "core/assets/map/scifitiles-sheet.png";
public static TextureRegion FLOOR_TILE;
public static TextureRegion WALL_TILE;
private OrthographicCamera cam;
private static int x = 0;
private static int y = 0;
public MapRenderer(Map map, int tileWidth) {
public MapRenderer(Map map, int tileWidth, SpriteBatch batch) {
this.map = map;
this.tileWidth = tileWidth;
this.batch = batch;
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
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];
System.out.println(map);
System.out.println(map.getHeight());
System.out.println(map.getWidth());
}
public int getTileWidth() {
@@ -31,12 +50,35 @@ public class MapRenderer implements Renderable {
@Override
public void render() {
Batch batch = new SpriteBatch();
batch.begin();
for (int row = 0; row < map.getHeight(); 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);
}
x += 32;
}
}
// batch.draw(FLOOR_TILE,100,100);
batch.end();
x = 0;
y = 0;
}
@Override
public void update(double deltaTime) {
}
public void resize(int screenWidth, int screenHeight) {
cam = new OrthographicCamera(screenWidth, screenHeight);
cam.translate(screenWidth / 2, screenHeight / 2);
cam.update();
batch.setProjectionMatrix(cam.combined);
}
}