merge Implement mapdrawing into master #3

Merged
SemvdH merged 5 commits from implement-mapdrawing into master 2020-05-25 17:28:43 +00:00
4 changed files with 84 additions and 34 deletions
Showing only changes of commit ca2747ef03 - Show all commits

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

View File

@@ -1,5 +1,7 @@
package netwerkprog.game.client.map; package netwerkprog.game.client.map;
import java.util.Arrays;
/** /**
* Map class to hold a 2d array of tiles which will specify the map * 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; 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; 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.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import netwerkprog.game.util.Renderable; import netwerkprog.game.util.Renderable;
public class MapRenderer implements Renderable { public class MapRenderer implements Renderable {
private int tileWidth; private int tileWidth;
private Map map; 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.map = map;
this.tileWidth = tileWidth; 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() { public int getTileWidth() {
@@ -31,12 +50,35 @@ public class MapRenderer implements Renderable {
@Override @Override
public void render() { 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 @Override
public void update(double deltaTime) { 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);
}
} }