diff --git a/core/assets/map/scifitiles-sheet.png b/core/assets/map/scifitiles-sheet.png new file mode 100644 index 0000000..e202b1a Binary files /dev/null and b/core/assets/map/scifitiles-sheet.png differ diff --git a/core/src/netwerkprog/game/client/MainGame.java b/core/src/netwerkprog/game/client/MainGame.java index 9319b2a..0f280b8 100644 --- a/core/src/netwerkprog/game/client/MainGame.java +++ b/core/src/netwerkprog/game/client/MainGame.java @@ -3,40 +3,82 @@ package netwerkprog.game.client; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Files; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; +import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.audio.Music; +import com.badlogic.gdx.graphics.Camera; import com.badlogic.gdx.graphics.GL20; -import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.math.Vector3; +import netwerkprog.game.client.Client; +import netwerkprog.game.client.game.map.Map; +import netwerkprog.game.client.game.map.MapRenderer; +import netwerkprog.game.client.map.GameInputProcessor; import netwerkprog.game.util.graphics.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 Thread client; + private OrthographicCamera camera; + private GameInputProcessor gameInputProcessor; + + 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(); + camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + + String[] strings = new String[]{ + "#########################", + "#xxxx #", + "# x #", + "# xxxx #", + "# xx #", + "# x #", + "# x #", + "# x #", + "# x #", + "# xxxxxx #", + "# x #", + "# x xxxx x x #", + "#########################" + }; + map = new Map(strings); + gameInputProcessor = new GameInputProcessor(camera, this); + Gdx.input.setInputProcessor(gameInputProcessor); + mapRenderer = new MapRenderer(map, 32, batch, camera); + camera.position.set(screenWidth/2,screenHeight/2,0); + camera.viewportWidth = screenWidth / 2; + camera.viewportHeight = screenHeight / 2; + camera.update(); + + +// 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(); } @@ -56,11 +98,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(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - batch.begin(); - batch.draw(img, xPos, yPos); - batch.end(); + mapRenderer.render(); frameRate.render(); } @@ -69,20 +110,8 @@ 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; - } + camera.update(); + this.gameInputProcessor.update(); } @Override @@ -90,6 +119,8 @@ public class MainGame extends ApplicationAdapter { super.resize(width, height); screenHeight = height; screenWidth = width; + frameRate.resize(width, height); + mapRenderer.resize(width, height); } @Override @@ -100,6 +131,21 @@ public class MainGame extends ApplicationAdapter { @Override public void dispose() { batch.dispose(); - img.dispose(); + } + + public float getScreenWidth() { + return screenWidth; + } + + public float getScreenHeight() { + return screenHeight; + } + + public int getVerticalTileAmount() { + return map.getHeight(); + } + + public int getHorizontalTileAmount() { + return map.getWidth(); } } diff --git a/core/src/netwerkprog/game/client/game/map/Map.java b/core/src/netwerkprog/game/client/game/map/Map.java index 9fff0f7..9ca8a1e 100644 --- a/core/src/netwerkprog/game/client/game/map/Map.java +++ b/core/src/netwerkprog/game/client/game/map/Map.java @@ -1,5 +1,7 @@ package netwerkprog.game.client.game.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(); + } } diff --git a/core/src/netwerkprog/game/client/game/map/MapRenderer.java b/core/src/netwerkprog/game/client/game/map/MapRenderer.java index 9eca1bf..37457e2 100644 --- a/core/src/netwerkprog/game/client/game/map/MapRenderer.java +++ b/core/src/netwerkprog/game/client/game/map/MapRenderer.java @@ -1,16 +1,44 @@ package netwerkprog.game.client.game.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 netwerkprog.game.util.graphics.Renderable; +import com.badlogic.gdx.graphics.g2d.TextureRegion; public class MapRenderer implements Renderable { + private final OrthographicCamera camera; private int tileWidth; private Map map; + private SpriteBatch batch; + private static String tilePath = "core/assets/map/scifitiles-sheet.png"; + private OrthographicCamera cam; + private static int x = 0; + private static int y = 0; - public MapRenderer(Map map, int tileWidth) { + + public static TextureRegion FLOOR_TILE; + public static TextureRegion WALL_TILE; + public static TextureRegion PATH_TILE; + + + public MapRenderer(Map map, int tileWidth, SpriteBatch batch, OrthographicCamera camera) { this.map = map; this.tileWidth = tileWidth; + this.batch = batch; + cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + this.camera = camera; + makeTiles(); + } + + 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]; + } public int getTileWidth() { @@ -31,12 +59,37 @@ public class MapRenderer implements Renderable { @Override public void render() { - Batch batch = new SpriteBatch(); - + 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; + } + } +// 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); + } } diff --git a/core/src/netwerkprog/game/client/map/GameInputProcessor.java b/core/src/netwerkprog/game/client/map/GameInputProcessor.java new file mode 100644 index 0000000..ee33d38 --- /dev/null +++ b/core/src/netwerkprog/game/client/map/GameInputProcessor.java @@ -0,0 +1,172 @@ +package netwerkprog.game.client.map; + +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.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; + private MainGame game; + private ArrayList keysList; + private boolean isWPressed = false; + private boolean isAPressed = false; + private boolean isSPressed = false; + private boolean isDPressed = false; + + long lastTimeCounted; + + private final float CAMERA_MOVE_SPEED = .3f; + + + public GameInputProcessor(OrthographicCamera camera, MainGame game) { + this.camera = camera; + this.game = game; + keysList = new ArrayList<>(); + lastTimeCounted = TimeUtils.millis(); + + keysList.add(Input.Keys.W); + keysList.add(Input.Keys.A); + keysList.add(Input.Keys.S); + 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); + + } + + public boolean isWPressed() { + return isWPressed; + } + + public boolean isAPressed() { + return isAPressed; + } + + public boolean isSPressed() { + return isSPressed; + } + + public boolean isDPressed() { + return isDPressed; + } + + @Override + public boolean keyDown(int keycode) { + if (keysList.contains(keycode)) { + if (keycode == keysList.get(0)) { + this.isWPressed = true; + return true; + } + if (keycode == keysList.get(1)) { + this.isAPressed = true; + return true; + } + if (keycode == keysList.get(2)) { + this.isSPressed = true; + return true; + } + if (keycode == keysList.get(3)) { + this.isDPressed = true; + return true; + } + return true; + } + return false; + } + + @Override + public boolean keyUp(int keycode) { + System.out.println(camera.position.x + " , " + camera.position.y); + if (keysList.contains(keycode)) { + if (keycode == keysList.get(0)) { + this.isWPressed = false; + return true; + } + if (keycode == keysList.get(1)) { + this.isAPressed = false; + return true; + } + if (keycode == keysList.get(2)) { + this.isSPressed = false; + return true; + } + if (keycode == keysList.get(3)) { + this.isDPressed = false; + return true; + } + + return true; + } + return false; + } + + @Override + public boolean keyTyped(char character) { + return false; + } + + @Override + public boolean touchDown(int screenX, int screenY, int pointer, int button) { + return false; + } + + @Override + public boolean touchUp(int screenX, int screenY, int pointer, int button) { + return false; + } + + @Override + public boolean touchDragged(int screenX, int screenY, int pointer) { + return false; + } + + @Override + public boolean mouseMoved(int screenX, int screenY) { + return false; + } + + @Override + public boolean scrolled(int amount) { + float nextZoom = camera.zoom + amount / 5f; + if (nextZoom >= 0.5 && nextZoom <= 2.5) { + camera.zoom += amount / 2f; + return true; + } + return false; + } + + public void update() { + long delta = TimeUtils.timeSinceMillis(lastTimeCounted); + lastTimeCounted = TimeUtils.millis(); + if (camera.position.x > 5 * game.getHorizontalTileAmount()) + if (isAPressed()) { + camera.position.add(-CAMERA_MOVE_SPEED * delta, 0, 0); + } + if (camera.position.y < 30 * game.getVerticalTileAmount()) + if (isWPressed()) { + camera.position.add(0, CAMERA_MOVE_SPEED * delta, 0); + } + + if (camera.position.y > 5 * game.getVerticalTileAmount()) + if (isSPressed()) { + camera.position.add(0, -CAMERA_MOVE_SPEED * delta, 0); + } + + if (camera.position.x < game.getScreenWidth() / 2 + 5 * game.getHorizontalTileAmount()) + if (isDPressed()) { + camera.position.add(CAMERA_MOVE_SPEED * delta, 0, 0); + } + } +}