merge Implement mapdrawing into master #3
@@ -3,11 +3,16 @@ package netwerkprog.game;
|
|||||||
import com.badlogic.gdx.ApplicationAdapter;
|
import com.badlogic.gdx.ApplicationAdapter;
|
||||||
import com.badlogic.gdx.Files;
|
import com.badlogic.gdx.Files;
|
||||||
import com.badlogic.gdx.Gdx;
|
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.audio.Music;
|
||||||
|
import com.badlogic.gdx.graphics.Camera;
|
||||||
import com.badlogic.gdx.graphics.GL20;
|
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.graphics.g2d.SpriteBatch;
|
||||||
|
import com.badlogic.gdx.math.Vector3;
|
||||||
import netwerkprog.game.client.Client;
|
import netwerkprog.game.client.Client;
|
||||||
|
import netwerkprog.game.client.map.GameInputProcessor;
|
||||||
import netwerkprog.game.client.map.Map;
|
import netwerkprog.game.client.map.Map;
|
||||||
import netwerkprog.game.client.map.MapRenderer;
|
import netwerkprog.game.client.map.MapRenderer;
|
||||||
import netwerkprog.game.server.Server;
|
import netwerkprog.game.server.Server;
|
||||||
@@ -19,16 +24,23 @@ public class MainGame extends ApplicationAdapter {
|
|||||||
float screenHeight;
|
float screenHeight;
|
||||||
private FrameRate frameRate;
|
private FrameRate frameRate;
|
||||||
private Client client;
|
private Client client;
|
||||||
|
private OrthographicCamera camera;
|
||||||
|
private GameInputProcessor gameInputProcessor;
|
||||||
|
|
||||||
private Map map;
|
private Map map;
|
||||||
private MapRenderer mapRenderer;
|
private MapRenderer mapRenderer;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void create() {
|
public void create() {
|
||||||
batch = new SpriteBatch();
|
batch = new SpriteBatch();
|
||||||
screenWidth = Gdx.graphics.getWidth();
|
screenWidth = Gdx.graphics.getWidth();
|
||||||
screenHeight = Gdx.graphics.getHeight();
|
screenHeight = Gdx.graphics.getHeight();
|
||||||
frameRate = new FrameRate();
|
frameRate = new FrameRate();
|
||||||
|
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
|
||||||
|
camera.translate(0, 0);
|
||||||
|
camera.update();
|
||||||
|
|
||||||
|
|
||||||
String[] strings = new String[]{
|
String[] strings = new String[]{
|
||||||
@@ -47,7 +59,11 @@ public class MainGame extends ApplicationAdapter {
|
|||||||
"#########################"
|
"#########################"
|
||||||
};
|
};
|
||||||
map = new Map(strings);
|
map = new Map(strings);
|
||||||
mapRenderer = new MapRenderer(map,32,batch);
|
gameInputProcessor = new GameInputProcessor(camera, this);
|
||||||
|
Gdx.input.setInputProcessor(gameInputProcessor);
|
||||||
|
mapRenderer = new MapRenderer(map, 32, batch, camera);
|
||||||
|
camera.translate(screenWidth/2,screenHeight/2);
|
||||||
|
|
||||||
|
|
||||||
// playSong();
|
// playSong();
|
||||||
|
|
||||||
@@ -55,6 +71,7 @@ public class MainGame extends ApplicationAdapter {
|
|||||||
// connectToServer();
|
// connectToServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void playSong() {
|
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));
|
||||||
@@ -90,6 +107,8 @@ public class MainGame extends ApplicationAdapter {
|
|||||||
*/
|
*/
|
||||||
public void update() {
|
public void update() {
|
||||||
frameRate.update();
|
frameRate.update();
|
||||||
|
camera.update();
|
||||||
|
this.gameInputProcessor.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -97,8 +116,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);
|
frameRate.resize(width, height);
|
||||||
mapRenderer.resize(width,height);
|
mapRenderer.resize(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -110,4 +129,8 @@ public class MainGame extends ApplicationAdapter {
|
|||||||
public void dispose() {
|
public void dispose() {
|
||||||
batch.dispose();
|
batch.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void keyDown() {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
150
core/src/netwerkprog/game/client/map/GameInputProcessor.java
Normal file
150
core/src/netwerkprog/game/client/map/GameInputProcessor.java
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
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.utils.TimeUtils;
|
||||||
|
import netwerkprog.game.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<Integer> 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
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) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update() {
|
||||||
|
long delta = TimeUtils.timeSinceMillis(lastTimeCounted);
|
||||||
|
lastTimeCounted = TimeUtils.millis();
|
||||||
|
if (isWPressed()) {
|
||||||
|
camera.position.add(0, -CAMERA_MOVE_SPEED * delta, 0);
|
||||||
|
}
|
||||||
|
if (isSPressed()) {
|
||||||
|
camera.position.add(0, CAMERA_MOVE_SPEED * delta, 0);
|
||||||
|
}
|
||||||
|
if (isAPressed()) {
|
||||||
|
camera.position.add(CAMERA_MOVE_SPEED * delta, 0, 0);
|
||||||
|
}
|
||||||
|
if (isDPressed()) {
|
||||||
|
camera.position.add(-CAMERA_MOVE_SPEED * delta, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ 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 final OrthographicCamera camera;
|
||||||
private int tileWidth;
|
private int tileWidth;
|
||||||
private Map map;
|
private Map map;
|
||||||
private SpriteBatch batch;
|
private SpriteBatch batch;
|
||||||
@@ -22,11 +23,12 @@ public class MapRenderer implements Renderable {
|
|||||||
public static TextureRegion PATH_TILE;
|
public static TextureRegion PATH_TILE;
|
||||||
|
|
||||||
|
|
||||||
public MapRenderer(Map map, int tileWidth, SpriteBatch batch) {
|
public MapRenderer(Map map, int tileWidth, SpriteBatch batch, OrthographicCamera camera) {
|
||||||
this.map = map;
|
this.map = map;
|
||||||
this.tileWidth = tileWidth;
|
this.tileWidth = tileWidth;
|
||||||
this.batch = batch;
|
this.batch = batch;
|
||||||
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
|
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
|
||||||
|
this.camera = camera;
|
||||||
makeTiles();
|
makeTiles();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,6 +60,7 @@ public class MapRenderer implements Renderable {
|
|||||||
@Override
|
@Override
|
||||||
public void render() {
|
public void render() {
|
||||||
batch.begin();
|
batch.begin();
|
||||||
|
batch.setProjectionMatrix(camera.combined);
|
||||||
for (int row = map.getHeight(); row >= 0; row--) {
|
for (int row = map.getHeight(); row >= 0; row--) {
|
||||||
y += 32;
|
y += 32;
|
||||||
x = 0;
|
x = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user