Restructured Util for better usability

Moved MainGame from util to client.
Moved server port static from serverData to Data
This commit is contained in:
MickWerf
2020-05-18 15:53:14 +02:00
parent 198c1bfdd8
commit a20568b427
26 changed files with 53 additions and 53 deletions

View File

@@ -1,7 +1,8 @@
package netwerkprog.game.client;
import netwerkprog.game.util.Controller;
import netwerkprog.game.util.ServerData;
import netwerkprog.game.util.application.Controller;
import netwerkprog.game.util.data.Data;
import netwerkprog.game.util.data.DataParser;
import java.io.DataInputStream;
import java.io.DataOutputStream;
@@ -13,13 +14,20 @@ import java.util.Scanner;
public class Client extends Controller {
private int port;
private String hostname;
private DataParser parser;
private boolean isConnected = true;
public Client(String hostname) {
this.port = ServerData.port();
this.port = Data.port();
this.hostname = hostname;
this.parser = new DataParser();
}
@Override
public void run() {
this.connect();
}
public void connect() {
@@ -66,15 +74,12 @@ public class Client extends Controller {
while (isConnected) {
try {
received = in.readUTF();
System.out.println(received);
} catch (IOException e) {
System.out.println("exception caught - " + e.getMessage());;
}
}
}
@Override
public void run() {
this.connect();
}
}

View File

@@ -0,0 +1,105 @@
package netwerkprog.game.client;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
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;
@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();
// 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() {
client = new Thread(new Client("localhost"));
try {
client.start();
} catch (Exception e) {
System.out.println("There was an error connecting : " + e.getMessage());
}
}
/**
* render method that is called after the update method
*/
@Override
public void render() {
update();
Gdx.gl.glClearColor(xPos / Gdx.graphics.getWidth(), 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, xPos, yPos);
batch.end();
frameRate.render();
}
/**
* update method that does all calculation before something is being drawn
*/
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
public void resize(int width, int height) {
super.resize(width, height);
screenHeight = height;
screenWidth = width;
}
@Override
public void resume() {
super.resume();
}
@Override
public void dispose() {
batch.dispose();
img.dispose();
}
}

View File

@@ -1,6 +1,6 @@
package netwerkprog.game.client.game;
import netwerkprog.game.util.Controller;
import netwerkprog.game.util.application.Controller;
public class Game extends Controller {

View File

@@ -1,6 +1,6 @@
package netwerkprog.game.client.game;
import netwerkprog.game.util.Controller;
import netwerkprog.game.util.application.Controller;
public class Graphics extends Controller {
public Graphics() {

View File

@@ -1,6 +1,6 @@
package netwerkprog.game.client.game.logic;
import netwerkprog.game.util.Controller;
import netwerkprog.game.util.application.Controller;
public class Logic extends Controller {

View File

@@ -2,7 +2,7 @@ package netwerkprog.game.client.game.map;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import netwerkprog.game.util.Renderable;
import netwerkprog.game.util.graphics.Renderable;
public class MapRenderer implements Renderable {
private int tileWidth;