Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f89dbbb6b3 |
|
Before Width: | Height: | Size: 80 KiB After Width: | Height: | Size: 51 KiB |
|
Before Width: | Height: | Size: 1.3 KiB |
@@ -1,102 +0,0 @@
|
||||
info face="Arcade Rounded" size=32 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=1,1,1,1 spacing=-2,-2
|
||||
common lineHeight=41 base=29 scaleW=512 scaleH=512 pages=1 packed=0
|
||||
page id=0 file="custom.png"
|
||||
chars count=97
|
||||
char id=0 x=0 y=0 width=25 height=30 xoffset=3 yoffset=0 xadvance=31 page=0 chnl=0
|
||||
char id=10 x=0 y=0 width=0 height=0 xoffset=-1 yoffset=0 xadvance=0 page=0 chnl=0
|
||||
char id=32 x=0 y=0 width=0 height=0 xoffset=-1 yoffset=0 xadvance=31 page=0 chnl=0
|
||||
char id=33 x=494 y=30 width=10 height=29 xoffset=8 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=34 x=0 y=88 width=22 height=17 xoffset=4 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=35 x=453 y=59 width=22 height=21 xoffset=4 yoffset=5 xadvance=31 page=0 chnl=0
|
||||
char id=36 x=305 y=59 width=22 height=29 xoffset=4 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=37 x=327 y=59 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=38 x=357 y=59 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=39 x=22 y=88 width=10 height=17 xoffset=4 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=40 x=119 y=59 width=18 height=29 xoffset=4 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=41 x=137 y=59 width=18 height=29 xoffset=7 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=42 x=475 y=59 width=22 height=21 xoffset=4 yoffset=5 xadvance=31 page=0 chnl=0
|
||||
char id=43 x=431 y=59 width=22 height=21 xoffset=4 yoffset=5 xadvance=31 page=0 chnl=0
|
||||
char id=44 x=497 y=59 width=10 height=18 xoffset=4 yoffset=12 xadvance=31 page=0 chnl=0
|
||||
char id=45 x=110 y=88 width=22 height=6 xoffset=4 yoffset=12 xadvance=31 page=0 chnl=0
|
||||
char id=46 x=54 y=88 width=10 height=10 xoffset=4 yoffset=20 xadvance=31 page=0 chnl=0
|
||||
char id=47 x=215 y=59 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=48 x=60 y=59 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=49 x=289 y=30 width=26 height=29 xoffset=3 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=50 x=315 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=51 x=345 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=52 x=375 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=53 x=405 y=30 width=29 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=54 x=434 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=55 x=464 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=56 x=0 y=59 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=57 x=30 y=59 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=58 x=387 y=59 width=10 height=25 xoffset=4 yoffset=5 xadvance=31 page=0 chnl=0
|
||||
char id=59 x=500 y=0 width=10 height=29 xoffset=4 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=60 x=397 y=59 width=17 height=21 xoffset=4 yoffset=5 xadvance=31 page=0 chnl=0
|
||||
char id=61 x=32 y=88 width=22 height=14 xoffset=4 yoffset=8 xadvance=31 page=0 chnl=0
|
||||
char id=62 x=414 y=59 width=17 height=21 xoffset=8 yoffset=5 xadvance=31 page=0 chnl=0
|
||||
char id=63 x=90 y=59 width=29 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=64 x=245 y=59 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=65 x=25 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=66 x=55 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=67 x=85 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=68 x=115 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=69 x=145 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=70 x=175 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=71 x=205 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=72 x=235 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=73 x=265 y=0 width=26 height=29 xoffset=3 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=74 x=291 y=0 width=29 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=75 x=320 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=76 x=350 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=77 x=380 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=78 x=410 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=79 x=440 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=80 x=470 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=81 x=0 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=82 x=30 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=83 x=60 y=30 width=29 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=84 x=89 y=30 width=26 height=29 xoffset=3 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=85 x=115 y=30 width=29 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=86 x=144 y=30 width=29 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=87 x=173 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=88 x=203 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=89 x=233 y=30 width=26 height=29 xoffset=3 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=90 x=259 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=91 x=155 y=59 width=14 height=29 xoffset=8 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=92 x=275 y=59 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=93 x=169 y=59 width=14 height=29 xoffset=8 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=94 x=74 y=88 width=14 height=9 xoffset=8 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=95 x=132 y=88 width=30 height=6 xoffset=0 yoffset=24 xadvance=31 page=0 chnl=0
|
||||
char id=96 x=64 y=88 width=10 height=9 xoffset=11 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=97 x=25 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=98 x=55 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=99 x=85 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=100 x=115 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=101 x=145 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=102 x=175 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=103 x=205 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=104 x=235 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=105 x=265 y=0 width=26 height=29 xoffset=3 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=106 x=291 y=0 width=29 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=107 x=320 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=108 x=350 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=109 x=380 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=110 x=410 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=111 x=440 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=112 x=470 y=0 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=113 x=0 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=114 x=30 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=115 x=60 y=30 width=29 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=116 x=89 y=30 width=26 height=29 xoffset=3 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=117 x=115 y=30 width=29 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=118 x=144 y=30 width=29 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=119 x=173 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=120 x=203 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=121 x=233 y=30 width=26 height=29 xoffset=3 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=122 x=259 y=30 width=30 height=29 xoffset=0 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=123 x=183 y=59 width=16 height=29 xoffset=6 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=124 x=504 y=30 width=6 height=29 xoffset=12 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=125 x=199 y=59 width=16 height=29 xoffset=8 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
char id=126 x=88 y=88 width=22 height=9 xoffset=4 yoffset=1 xadvance=31 page=0 chnl=0
|
||||
kernings count=0
|
||||
|
Before Width: | Height: | Size: 26 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 913 B |
|
Before Width: | Height: | Size: 907 B |
104
core/src/netwerkprog/game/client/Client.java
Normal file
@@ -0,0 +1,104 @@
|
||||
package netwerkprog.game.client;
|
||||
|
||||
import netwerkprog.game.util.application.Controller;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.ParserCallback;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Client extends Controller implements ParserCallback {
|
||||
private final int port;
|
||||
private final String hostname;
|
||||
private final Parser parser;
|
||||
private boolean isConnected = true;
|
||||
private Socket socket;
|
||||
private Thread receiveThread;
|
||||
private DataOutputStream outputStream;
|
||||
|
||||
public Client(String hostname) {
|
||||
this.port = Data.port();
|
||||
this.hostname = hostname;
|
||||
this.parser = new Parser(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the client process.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
this.connect();
|
||||
this.receiveThread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects the client to the server.
|
||||
*/
|
||||
public void connect() {
|
||||
System.out.println("[CLIENT] connecting to server on port " + this.port);
|
||||
try {
|
||||
this.socket = new Socket(this.hostname, this.port);
|
||||
DataInputStream in = new DataInputStream(socket.getInputStream());
|
||||
this.outputStream = new DataOutputStream(socket.getOutputStream());
|
||||
|
||||
this.receiveThread = new Thread( () -> receive(in));
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println("[CLIENT] there was an error connecting : " + e.getMessage());
|
||||
StringBuilder sb = new StringBuilder(" Stacktrace : ");
|
||||
Arrays.stream(e.getStackTrace()).forEach(n -> sb.append("\t\t").append(n).append("\n"));
|
||||
System.out.println(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to the server.
|
||||
* @param message The message to send.
|
||||
*/
|
||||
public void send(String message) {
|
||||
try {
|
||||
this.outputStream.writeUTF(message);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Receives a message from the server.
|
||||
* @param in The inputStream
|
||||
*/
|
||||
public void receive(DataInputStream in) {
|
||||
while (isConnected) {
|
||||
try {
|
||||
this.parser.parse(in.readUTF());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
this.isConnected = false;
|
||||
try {
|
||||
this.receiveThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
send("Disconnect");
|
||||
|
||||
try {
|
||||
this.socket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataReceived(String data) {
|
||||
System.out.println(data);
|
||||
}
|
||||
}
|
||||
@@ -1,53 +1,45 @@
|
||||
package netwerkprog.game.client;
|
||||
|
||||
import com.badlogic.gdx.ApplicationAdapter;
|
||||
import com.badlogic.gdx.Files;
|
||||
import com.badlogic.gdx.Game;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.assets.AssetManager;
|
||||
import com.badlogic.gdx.audio.Music;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import netwerkprog.game.client.game.GAMESTATE;
|
||||
import netwerkprog.game.client.game.characters.Agent;
|
||||
import netwerkprog.game.client.game.characters.Hacker;
|
||||
import netwerkprog.game.client.game.characters.Team;
|
||||
import netwerkprog.game.client.game.characters.abilities.BodySwap;
|
||||
import netwerkprog.game.client.game.connections.Client;
|
||||
import netwerkprog.game.client.game.connections.ClientCallback;
|
||||
import netwerkprog.game.client.game.map.GameInputProcessor;
|
||||
import netwerkprog.game.client.game.map.GameTile;
|
||||
import netwerkprog.game.client.game.map.Map;
|
||||
import netwerkprog.game.client.game.map.MapRenderer;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.character.DamageData;
|
||||
import netwerkprog.game.util.data.character.MoveData;
|
||||
import netwerkprog.game.util.data.connection.NameData;
|
||||
import netwerkprog.game.util.data.connection.PlayerConnectData;
|
||||
import netwerkprog.game.util.data.connection.TeamData;
|
||||
import netwerkprog.game.util.data.connection.TurnData;
|
||||
import netwerkprog.game.util.game.Faction;
|
||||
import netwerkprog.game.client.game.map.GameInputProcessor;
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
import netwerkprog.game.util.graphics.FrameRate;
|
||||
import netwerkprog.game.util.graphics.TextRenderer;
|
||||
import netwerkprog.game.util.tree.BST;
|
||||
|
||||
import java.awt.*;
|
||||
public class MainGame extends ApplicationAdapter {
|
||||
SpriteBatch batch;
|
||||
float screenWidth;
|
||||
float screenHeight;
|
||||
private FrameRate frameRate;
|
||||
private Thread client;
|
||||
private OrthographicCamera camera;
|
||||
private GameInputProcessor gameInputProcessor;
|
||||
private GameCharacter selectedCharacter;
|
||||
|
||||
private Map map;
|
||||
public MapRenderer mapRenderer;
|
||||
|
||||
private BST<GameCharacter> tree;
|
||||
public GameCharacter testCharacter;
|
||||
|
||||
/**
|
||||
* Main game class
|
||||
*/
|
||||
public class MainGame extends Game implements ClientCallback {
|
||||
private static MainGame INSTANCE;
|
||||
|
||||
/**
|
||||
* return the instance of the main game.
|
||||
*
|
||||
* @return the main game
|
||||
*/
|
||||
private MainGame() {
|
||||
}
|
||||
|
||||
public static MainGame getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new MainGame();
|
||||
@@ -55,69 +47,28 @@ public class MainGame extends Game implements ClientCallback {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private GAMESTATE gamestate;
|
||||
private Client client;
|
||||
private FrameRate frameRate;
|
||||
private OrthographicCamera camera;
|
||||
private GameInputProcessor gameInputProcessor;
|
||||
private SpriteBatch batch;
|
||||
private BitmapFont font;
|
||||
private GlyphLayout layout;
|
||||
private TextRenderer textRenderer;
|
||||
private Map map;
|
||||
public MapRenderer mapRenderer; //todo public?
|
||||
public AssetManager assets; //todo public?
|
||||
private Faction chosenFaction;
|
||||
private Team team;
|
||||
private Team enemyTeam;
|
||||
private GameCharacter selectedCharacter;
|
||||
private float screenWidth;
|
||||
private float screenHeight;
|
||||
private String username;
|
||||
private int turn;
|
||||
private boolean playersTurn;
|
||||
private boolean ready;
|
||||
private boolean enemyReady;
|
||||
private boolean gameOver;
|
||||
private boolean otherPlayerConnected;
|
||||
|
||||
private MainGame() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create() {
|
||||
init();
|
||||
}
|
||||
|
||||
public void init() {
|
||||
batch = new SpriteBatch();
|
||||
screenWidth = Gdx.graphics.getWidth();
|
||||
screenHeight = Gdx.graphics.getHeight();
|
||||
frameRate = new FrameRate();
|
||||
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
|
||||
textRenderer = new TextRenderer();
|
||||
font = new BitmapFont();
|
||||
layout = new GlyphLayout();
|
||||
assets = new AssetManager();
|
||||
|
||||
|
||||
String[] strings = new String[]{
|
||||
"#########################",
|
||||
"#xxxx # #",
|
||||
"# x # #",
|
||||
"# xxxx #xxxx #",
|
||||
"# xxxx #xxxx #",
|
||||
"# xxxx #x xx #",
|
||||
"# x #xxxx #",
|
||||
"# # x #xxx # #",
|
||||
"# # x # # #",
|
||||
"# # x#xxxx # #",
|
||||
"# # #",
|
||||
"# x xxxx x x #",
|
||||
"# x xxxx x x #",
|
||||
"# x xxxx x x #",
|
||||
"# x xxxx x x #",
|
||||
"# ## x ## xxxx x x #",
|
||||
"# x #### xxxx x x #",
|
||||
"#xxxx #",
|
||||
"# x #",
|
||||
"# xxxx #",
|
||||
"# xx #",
|
||||
"# x #",
|
||||
"# x #",
|
||||
"# x #",
|
||||
"# x #",
|
||||
"# xxxxxx #",
|
||||
"# x #",
|
||||
"# x xxxx x x #",
|
||||
"#########################"
|
||||
};
|
||||
@@ -129,175 +80,72 @@ public class MainGame extends Game implements ClientCallback {
|
||||
camera.viewportWidth = screenWidth / 2;
|
||||
camera.viewportHeight = screenHeight / 2;
|
||||
camera.update();
|
||||
otherPlayerConnected = false;
|
||||
gameOver = false;
|
||||
ready = false;
|
||||
enemyReady = false;
|
||||
playersTurn = true;
|
||||
turn = 0;
|
||||
chosenFaction = null;
|
||||
setGamestate(GAMESTATE.SELECTING_FACTION);
|
||||
connectToServer();
|
||||
this.tree = new BST<>();
|
||||
initCharacters();
|
||||
// this.tree.insert(new Hacker(,new BodySwap()));
|
||||
|
||||
|
||||
// playSong();
|
||||
|
||||
|
||||
// connectToServer();
|
||||
}
|
||||
|
||||
/**
|
||||
* initialize all characters.
|
||||
*/
|
||||
public void initCharacters() {
|
||||
assets.load("core/assets/characters.png", Texture.class);
|
||||
assets.finishLoading();
|
||||
|
||||
Texture texture = assets.get("core/assets/characters.png", Texture.class);
|
||||
private void initCharacters() {
|
||||
Texture texture = new Texture(Gdx.files.internal("core/assets/characters.png"));
|
||||
TextureRegion[][] characters = TextureRegion.split(texture, 32, 32);
|
||||
this.team = new Team();
|
||||
this.enemyTeam = new Team();
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
GameCharacter temp = new Hacker("hacker" + i, characters[5][0], new BodySwap("test"));
|
||||
mapRenderer.getGameTiles()[1][i].visit(temp);
|
||||
this.testCharacter = new Hacker("harry",characters[1][0], new BodySwap("test"));
|
||||
GameCharacter character2 = new Hacker("test2",characters[2][0], new BodySwap("test"));
|
||||
// this.tree.insert(testCharacter);
|
||||
// this.tree.insert(character2);
|
||||
// this.tree.insert(new Agent(characters[2][0], new Implant("test")));
|
||||
this.setSelectedCharacter(testCharacter);
|
||||
mapRenderer.getGameTiles()[0][1].visit(testCharacter);
|
||||
mapRenderer.getGameTiles()[0][2].visit(character2);
|
||||
|
||||
GameCharacter temp2 = new Agent("Agent" + i, characters[11][0], new BodySwap("Test"));
|
||||
int width = mapRenderer.getGameTiles()[0].length;
|
||||
mapRenderer.getGameTiles()[1][width - (i + 1)].visit(temp2);
|
||||
|
||||
if (chosenFaction == Faction.HACKER) {
|
||||
this.team.addMember(temp);
|
||||
this.enemyTeam.addMember(temp2);
|
||||
}
|
||||
if (chosenFaction == Faction.MEGACORPORATION) {
|
||||
this.team.addMember(temp2);
|
||||
this.enemyTeam.addMember(temp);
|
||||
}
|
||||
}
|
||||
|
||||
this.setSelectedCharacter(this.team.get(0));
|
||||
}
|
||||
|
||||
|
||||
private void playSong() {
|
||||
// play music
|
||||
Music music = Gdx.audio.newMusic(Gdx.files.getFileHandle("core/assets/sound/beat.mp3", Files.FileType.Internal));
|
||||
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 Client("localhost", this);
|
||||
Thread t = new Thread(client);
|
||||
client = new Thread(new Client("localhost"));
|
||||
try {
|
||||
t.start();
|
||||
client.start();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("There was an error connecting : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void clearRender() {
|
||||
clearRender(0, 0, 0, 1);
|
||||
}
|
||||
|
||||
private void clearRender(float r, float g, float b, float alpha) {
|
||||
Gdx.gl.glClearColor(r / 255f, g / 255f, b / 255f, alpha);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* render method that is called after the update method
|
||||
*/
|
||||
@Override
|
||||
public void render() {
|
||||
if (this.gamestate == GAMESTATE.PLAYING) {
|
||||
update();
|
||||
// clear screen
|
||||
clearRender();
|
||||
mapRenderer.render();
|
||||
frameRate.render();
|
||||
renderText();
|
||||
renderTurnText();
|
||||
} else if (this.gamestate == GAMESTATE.SELECTING_FACTION) {
|
||||
clearRender(67, 168, 186, 1);
|
||||
String text = "";
|
||||
if (otherPlayerConnected) {
|
||||
text = username == null ? "Connecting to server..." : "FACTION SELECT\nYou are: " + username + "\nPress 1 for mega corporation, press 2 for hackers";
|
||||
text += "\n\n\nHOW TO PLAY:\nUse WASD to move the camera, and scroll to zoom.\nClick on one of your characters to select them, and click on one of the available surrounding squares to move.\n" +
|
||||
"Click on an enemy when he is in range to attack him.\nFirst to kill the other's team wins!\n\n\nGood luck!";
|
||||
} else text = "Waiting for other player...";
|
||||
renderString(text, Gdx.graphics.getWidth() / 2f, Gdx.graphics.getHeight() / 2f);
|
||||
if (this.ready && this.enemyReady) {
|
||||
if (this.chosenFaction == Faction.HACKER) {
|
||||
chooseHacker();
|
||||
} else if (this.chosenFaction == Faction.MEGACORPORATION) {
|
||||
chooseMegaCorp();
|
||||
}
|
||||
}
|
||||
} else if (this.gamestate == GAMESTATE.ENDED) {
|
||||
clearRender(67, 168, 186, 1);
|
||||
String text = "Game ended!\n";
|
||||
if (this.enemyTeam.isDead()) {
|
||||
text += "Congratulations! You won!";
|
||||
} else if (this.team.isDead()) {
|
||||
text += "Too bad! You lost!";
|
||||
}
|
||||
text += "\nPress ESC to exit the game, or ENTER to start a new game";
|
||||
renderString(text, Gdx.graphics.getWidth() / 2f, Gdx.graphics.getHeight() / 2f);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void renderText() {
|
||||
String text = "FACTION: " + chosenFaction;
|
||||
text += "\nSelected character: " + selectedCharacter.getName();
|
||||
text += "\nHealth: " + selectedCharacter.getHealth();
|
||||
layout.setText(font, text);
|
||||
textRenderer.render(text, Gdx.graphics.getWidth() - layout.width - 5, Gdx.graphics.getHeight() - 3);
|
||||
}
|
||||
|
||||
private void renderString(String text) {
|
||||
layout.setText(font, text);
|
||||
textRenderer.render(text, Gdx.graphics.getWidth() - layout.width - 5, Gdx.graphics.getHeight() - 3);
|
||||
}
|
||||
|
||||
private void renderString(String text, float x, float y) {
|
||||
layout.setText(font, text);
|
||||
textRenderer.render(text, x - layout.width / 2f, x - layout.height / 2f);
|
||||
}
|
||||
|
||||
private void renderTurnText() {
|
||||
String text = playersTurn ? "Your turn, moves left: " + (3 - this.turn) : "Other player's turn";
|
||||
layout.setText(font, text);
|
||||
textRenderer.render(text, (Gdx.graphics.getWidth() / 2f) - layout.width / 2f, Gdx.graphics.getHeight() - 3);
|
||||
update();
|
||||
// clear screen
|
||||
Gdx.gl.glClearColor(0, 0, 0, 1);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
mapRenderer.render();
|
||||
frameRate.render();
|
||||
}
|
||||
|
||||
/**
|
||||
* update method that does all calculation before something is being drawn
|
||||
*/
|
||||
public void update() {
|
||||
|
||||
frameRate.update();
|
||||
camera.update();
|
||||
this.gameInputProcessor.update();
|
||||
|
||||
if (this.team.isDead() || this.enemyTeam.isDead()) {
|
||||
this.setGameOver(true);
|
||||
}
|
||||
if (this.isGameOver()) {
|
||||
this.setGamestate(GAMESTATE.ENDED);
|
||||
}
|
||||
|
||||
if (selectedCharacter.isDead()) {
|
||||
nextCharacter(selectedCharacter);
|
||||
}
|
||||
this.team.update(Gdx.graphics.getDeltaTime());
|
||||
this.enemyTeam.update(Gdx.graphics.getDeltaTime());
|
||||
}
|
||||
|
||||
private void nextCharacter(GameCharacter c) {
|
||||
for (GameCharacter character : this.team.getMembers()) {
|
||||
if (!character.equals(c)) this.setSelectedCharacter(character);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -307,7 +155,6 @@ public class MainGame extends Game implements ClientCallback {
|
||||
screenWidth = width;
|
||||
frameRate.resize(width, height);
|
||||
mapRenderer.resize(width, height);
|
||||
textRenderer.resize(width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -318,10 +165,6 @@ public class MainGame extends Game implements ClientCallback {
|
||||
@Override
|
||||
public void dispose() {
|
||||
batch.dispose();
|
||||
textRenderer.dispose();
|
||||
assets.dispose();
|
||||
textRenderer.dispose();
|
||||
mapRenderer.dispose();
|
||||
}
|
||||
|
||||
public float getScreenWidth() {
|
||||
@@ -340,35 +183,13 @@ public class MainGame extends Game implements ClientCallback {
|
||||
return map.getWidth();
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
client.disconnect();
|
||||
dispose();
|
||||
Gdx.app.exit();
|
||||
public BST<GameCharacter> getTree() {
|
||||
return tree;
|
||||
}
|
||||
|
||||
public void setSelectedCharacter(GameCharacter character) {
|
||||
if (!character.isDead()) {
|
||||
this.selectedCharacter = character;
|
||||
GameTile characterTile = mapRenderer.getTile(character);
|
||||
Point pos = mapRenderer.getPos(characterTile);
|
||||
mapRenderer.setSurroundedTilesOfCurrentCharacter(pos.x, pos.y);
|
||||
}
|
||||
}
|
||||
|
||||
public GAMESTATE getGamestate() {
|
||||
return gamestate;
|
||||
}
|
||||
|
||||
public void setGamestate(GAMESTATE gamestate) {
|
||||
this.gamestate = gamestate;
|
||||
}
|
||||
|
||||
public Faction getChosenFaction() {
|
||||
return chosenFaction;
|
||||
}
|
||||
|
||||
public void setChosenFaction(Faction chosenFaction) {
|
||||
this.chosenFaction = chosenFaction;
|
||||
this.selectedCharacter = character;
|
||||
System.out.println("selected character set to : " + character);
|
||||
}
|
||||
|
||||
public GameCharacter getSelectedCharacter() {
|
||||
@@ -378,123 +199,4 @@ public class MainGame extends Game implements ClientCallback {
|
||||
public boolean hasCharacterSelected() {
|
||||
return selectedCharacter != null;
|
||||
}
|
||||
|
||||
public Team getTeam() {
|
||||
return team;
|
||||
}
|
||||
|
||||
public void setGameOver(boolean gameOver) {
|
||||
this.gameOver = gameOver;
|
||||
}
|
||||
|
||||
public boolean isGameOver() {
|
||||
return gameOver;
|
||||
}
|
||||
|
||||
public boolean isOtherPlayerConnected() {
|
||||
return otherPlayerConnected;
|
||||
}
|
||||
|
||||
public void increaseTurn() {
|
||||
this.turn++;
|
||||
if (turn == 3) {
|
||||
this.turn = 0;
|
||||
this.setPlayersTurn(false);
|
||||
send(new TurnData());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isPlayersTurn() {
|
||||
return this.playersTurn;
|
||||
}
|
||||
|
||||
public void setPlayersTurn(boolean playersTurn) {
|
||||
this.playersTurn = playersTurn;
|
||||
}
|
||||
|
||||
public void send(Data data) {
|
||||
this.client.writeData(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataReceived(Data data) {
|
||||
if (data instanceof NameData) {
|
||||
this.username = ((NameData) data).getName();
|
||||
send(new PlayerConnectData(username));
|
||||
} else if (data instanceof TeamData) {
|
||||
// check if it is not our own message
|
||||
if (!((TeamData) data).getUsername().equals(this.username)) {
|
||||
// if we have not yet chosen a faction, select the opposing faction
|
||||
TeamData teamData = (TeamData) data;
|
||||
Faction enemyFaction = teamData.getFaction();
|
||||
if (this.chosenFaction == null) {
|
||||
if (enemyFaction == Faction.HACKER) {
|
||||
this.chosenFaction = Faction.MEGACORPORATION;
|
||||
} else {
|
||||
this.chosenFaction = Faction.HACKER;
|
||||
}
|
||||
this.enemyReady = true;
|
||||
this.ready = true;
|
||||
}
|
||||
}
|
||||
} else if (data instanceof MoveData) {
|
||||
MoveData moveData = (MoveData) data;
|
||||
if (!moveData.getUsername().equals(this.username)) {
|
||||
GameTile tile = mapRenderer.getGameTile(moveData.getPos());
|
||||
GameCharacter character = enemyTeam.get(moveData.getCharacterName());
|
||||
mapRenderer.removeCharacterFromTile(character);
|
||||
tile.visit(character);
|
||||
}
|
||||
} else if (data instanceof DamageData) {
|
||||
DamageData damageData = (DamageData) data;
|
||||
team.get(damageData.getName()).damage(10);
|
||||
} else if (data instanceof TurnData) {
|
||||
this.playersTurn = !this.playersTurn;
|
||||
} else if (data instanceof PlayerConnectData) {
|
||||
if (!((PlayerConnectData) data).getUsername().equals(this.username) && !this.otherPlayerConnected) {
|
||||
otherPlayerConnected = true;
|
||||
send(new PlayerConnectData(username));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void chooseHacker() {
|
||||
setChosenFaction(Faction.HACKER);
|
||||
send(new TeamData(Faction.MEGACORPORATION, getUsername()));
|
||||
|
||||
initCharacters();
|
||||
camera.translate(-400, 0);
|
||||
|
||||
this.playersTurn = true;
|
||||
setGamestate(GAMESTATE.PLAYING);
|
||||
}
|
||||
|
||||
public void chooseMegaCorp() {
|
||||
setChosenFaction(Faction.MEGACORPORATION);
|
||||
send(new TeamData(Faction.MEGACORPORATION, getUsername()));
|
||||
initCharacters();
|
||||
this.playersTurn = false;
|
||||
setGamestate(GAMESTATE.PLAYING);
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public boolean isEnemyReady() {
|
||||
return enemyReady;
|
||||
}
|
||||
|
||||
public boolean isReady() {
|
||||
return ready;
|
||||
}
|
||||
|
||||
public void setReady(boolean ready) {
|
||||
this.ready = ready;
|
||||
}
|
||||
|
||||
public void setEnemyReady(boolean enemyReady) {
|
||||
this.enemyReady = enemyReady;
|
||||
}
|
||||
}
|
||||
|
||||
15
core/src/netwerkprog/game/client/Parser.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package netwerkprog.game.client;
|
||||
|
||||
import netwerkprog.game.util.data.ParserCallback;
|
||||
|
||||
public class Parser {
|
||||
private final ParserCallback callback;
|
||||
|
||||
public Parser(ParserCallback callback) {
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
public void parse(String data) {
|
||||
callback.onDataReceived(data);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package netwerkprog.game.client.game;
|
||||
|
||||
public enum GAMESTATE {
|
||||
PLAYING,
|
||||
SELECTING_FACTION,
|
||||
ENDED
|
||||
}
|
||||
15
core/src/netwerkprog/game/client/game/Game.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package netwerkprog.game.client.game;
|
||||
|
||||
import netwerkprog.game.util.application.Controller;
|
||||
|
||||
public class Game extends Controller {
|
||||
|
||||
|
||||
public Game() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
}
|
||||
}
|
||||
14
core/src/netwerkprog/game/client/game/Graphics.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package netwerkprog.game.client.game;
|
||||
|
||||
import netwerkprog.game.util.application.Controller;
|
||||
|
||||
public class Graphics extends Controller {
|
||||
public Graphics() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import netwerkprog.game.util.game.Faction;
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
|
||||
public class Agent extends GameCharacter {
|
||||
public Agent(String name, TextureRegion textureRegion, Ability... abilities) {
|
||||
super(name, Faction.MEGACORPORATION, textureRegion, abilities);
|
||||
public Agent(TextureRegion textureRegion, Ability... abilities) {
|
||||
super("Agent", Faction.MEGACORPORATION, textureRegion, abilities);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package netwerkprog.game.client.game.characters;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
import netwerkprog.game.util.game.Faction;
|
||||
|
||||
public class DevTest1 extends GameCharacter {
|
||||
public DevTest1() {
|
||||
super("DevTest1", Faction.HACKER, new TextureRegion());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package netwerkprog.game.client.game.characters;
|
||||
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
import netwerkprog.game.util.game.Faction;
|
||||
|
||||
public class DevTest2 extends GameCharacter {
|
||||
public DevTest2() {
|
||||
super("DevTest2", Faction.MEGACORPORATION, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package netwerkprog.game.client.game.characters;
|
||||
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
import netwerkprog.game.util.game.Faction;
|
||||
|
||||
public class DevTest3 extends GameCharacter {
|
||||
public DevTest3() {
|
||||
super("DevTest3", Faction.AI, null);
|
||||
}
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package netwerkprog.game.client.game.characters;
|
||||
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
import netwerkprog.game.util.tree.BST;
|
||||
|
||||
public class Team {
|
||||
private BST<GameCharacter> members;
|
||||
|
||||
public Team() {
|
||||
this.members = new BST<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* create a new team with the given characters
|
||||
* @param characters the characters
|
||||
*/
|
||||
public Team(BST<GameCharacter> characters) {
|
||||
this.members = characters;
|
||||
}
|
||||
|
||||
/**
|
||||
* add new member
|
||||
* @param gameCharacter the member to add
|
||||
*/
|
||||
public void addMember(GameCharacter gameCharacter) {
|
||||
if (this.members.getSize() != 6)
|
||||
this.members.insert(gameCharacter);
|
||||
}
|
||||
|
||||
/**
|
||||
* get members
|
||||
* @return the members
|
||||
*/
|
||||
public BST<GameCharacter> getMembers() {
|
||||
return this.members;
|
||||
}
|
||||
|
||||
/**
|
||||
* set members
|
||||
* @param members the members to set
|
||||
*/
|
||||
public void setMembers(BST<GameCharacter> members) {
|
||||
this.members = members;
|
||||
}
|
||||
|
||||
/**
|
||||
* add members
|
||||
* @param characters the members to add
|
||||
*/
|
||||
public void addMember(GameCharacter... characters) {
|
||||
for (GameCharacter gameCharacter : characters) {
|
||||
this.members.insert(gameCharacter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get the specific character
|
||||
* @param character the character to get
|
||||
* @return the character, null if it doesnt exist
|
||||
*/
|
||||
public GameCharacter get(GameCharacter character) {
|
||||
for (GameCharacter cur : this.members) {
|
||||
if (cur.equals(character)) {
|
||||
return cur;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the character with the specified username
|
||||
* @param username the username of the character
|
||||
* @return the character, null if it doesnt exist
|
||||
*/
|
||||
public GameCharacter get(String username) {
|
||||
for (GameCharacter cur : this.members) {
|
||||
if (cur.getName().equals(username)) {
|
||||
return cur;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the character at the specified position
|
||||
* @param position the position of the character
|
||||
* @return the character, null if it can't be found
|
||||
*/
|
||||
public GameCharacter get(int position) {
|
||||
if (position >= this.members.getSize()) {
|
||||
throw new IndexOutOfBoundsException("position out of range");
|
||||
}
|
||||
int i = 0;
|
||||
for (GameCharacter cur : this.members) {
|
||||
if (i == position) return cur;
|
||||
i++;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* update all characters
|
||||
* @param deltaTime the time between the last frame
|
||||
*/
|
||||
public void update(double deltaTime) {
|
||||
for (GameCharacter character : this.members) {
|
||||
character.update(deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* check if all players are dead
|
||||
* @return true if all players are dead
|
||||
*/
|
||||
public boolean isDead() {
|
||||
int dead = 0;
|
||||
for (GameCharacter character : this.members) {
|
||||
if (character.isDead()) dead++;
|
||||
}
|
||||
return dead >= this.members.getSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Team{" +
|
||||
"members=" + members +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
package netwerkprog.game.client.game.connections;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.connection.ConnectionData;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
public class Client implements Runnable {
|
||||
private final int port;
|
||||
private final String hostname;
|
||||
private final ClientCallback callback;
|
||||
private Socket socket;
|
||||
private ObjectOutputStream outputStream;
|
||||
private Thread receiveThread;
|
||||
private boolean connecting;
|
||||
private boolean isConnected;
|
||||
|
||||
public Client(String hostname, ClientCallback callback) {
|
||||
this.port = Data.port();
|
||||
this.hostname = hostname;
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the client process.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
this.connect();
|
||||
try {
|
||||
if (this.receiveThread != null){
|
||||
this.receiveThread.start();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects the client to the server.
|
||||
*/
|
||||
public void connect() {
|
||||
this.connecting = true;
|
||||
try {
|
||||
this.socket = new Socket(this.hostname, this.port);
|
||||
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||
this.outputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||
register(in);
|
||||
this.receiveThread = new Thread(() -> receive(in));
|
||||
} catch (IOException e) {
|
||||
this.connecting = false;
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void register(ObjectInputStream in) {
|
||||
while (connecting) {
|
||||
writeData(new ConnectionData("Connect", "Request"));
|
||||
try {
|
||||
Object object = in.readObject();
|
||||
if (object instanceof Data) {
|
||||
Data data = (Data) object;
|
||||
if (data.getPayload() instanceof ConnectionData) {
|
||||
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
||||
if (connectionData.getAction().equals("Connect") && connectionData.getMessage().equals("Confirm")) {
|
||||
this.connecting = false;
|
||||
this.isConnected = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to the server.
|
||||
*
|
||||
* @param data The message to send.
|
||||
*/
|
||||
public void writeData(Data data) {
|
||||
try {
|
||||
this.outputStream.writeObject(data);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Receives a message from the server.
|
||||
*
|
||||
* @param in The inputStream
|
||||
*/
|
||||
public void receive(ObjectInputStream in) {
|
||||
while (isConnected) {
|
||||
try {
|
||||
Object object = in.readObject();
|
||||
if (object instanceof Data) {
|
||||
callback.onDataReceived((Data) object);
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
this.isConnected = false;
|
||||
try {
|
||||
this.receiveThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
writeData(new ConnectionData("Disconnect", "Request"));
|
||||
try {
|
||||
this.socket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package netwerkprog.game.client.game.connections;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
public interface ClientCallback {
|
||||
void onDataReceived(Data data);
|
||||
}
|
||||
15
core/src/netwerkprog/game/client/game/logic/Logic.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package netwerkprog.game.client.game.logic;
|
||||
|
||||
import netwerkprog.game.util.application.Controller;
|
||||
|
||||
public class Logic extends Controller {
|
||||
|
||||
public Logic() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,7 @@ import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.utils.TimeUtils;
|
||||
import netwerkprog.game.client.MainGame;
|
||||
import netwerkprog.game.client.game.GAMESTATE;
|
||||
import netwerkprog.game.util.data.character.DamageData;
|
||||
import netwerkprog.game.util.data.character.MoveData;
|
||||
import netwerkprog.game.util.data.connection.TeamData;
|
||||
import netwerkprog.game.util.game.Faction;
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -48,7 +44,6 @@ public class GameInputProcessor implements InputProcessor {
|
||||
|
||||
camera.zoom = MathUtils.clamp(camera.zoom, 1.5f, 1.8f);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public boolean isWPressed() {
|
||||
@@ -93,45 +88,26 @@ public class GameInputProcessor implements InputProcessor {
|
||||
|
||||
@Override
|
||||
public boolean keyUp(int keycode) {
|
||||
if (mainGame.getGamestate() == GAMESTATE.PLAYING) {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// System.out.println(camera.position.x + " , " + camera.position.y);
|
||||
if (keysList.contains(keycode)) {
|
||||
if (keycode == keysList.get(0)) {
|
||||
this.isWPressed = false;
|
||||
return true;
|
||||
}
|
||||
} else if (mainGame.getGamestate() == GAMESTATE.SELECTING_FACTION && mainGame.isOtherPlayerConnected()) {
|
||||
if (keycode == Input.Keys.NUM_1) {
|
||||
mainGame.send(new TeamData(Faction.MEGACORPORATION, mainGame.getUsername()));
|
||||
mainGame.chooseMegaCorp();
|
||||
if (keycode == keysList.get(1)) {
|
||||
this.isAPressed = false;
|
||||
return true;
|
||||
}
|
||||
if (keycode == Input.Keys.NUM_2) {
|
||||
mainGame.send(new TeamData(Faction.HACKER, mainGame.getUsername()));
|
||||
mainGame.chooseHacker();
|
||||
if (keycode == keysList.get(2)) {
|
||||
this.isSPressed = false;
|
||||
return true;
|
||||
}
|
||||
if (keycode == keysList.get(3)) {
|
||||
this.isDPressed = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
} else if (mainGame.getGamestate() == GAMESTATE.ENDED) {
|
||||
if (keycode == Input.Keys.ESCAPE) {
|
||||
mainGame.exit();
|
||||
} else if (keycode == Input.Keys.ENTER) {
|
||||
mainGame.dispose();
|
||||
mainGame.init();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -146,58 +122,25 @@ public class GameInputProcessor implements InputProcessor {
|
||||
|
||||
Vector3 touchPoint = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
|
||||
camera.unproject(touchPoint);
|
||||
if (mainGame.getGamestate() == GAMESTATE.PLAYING) {
|
||||
for (int row = 0; row < mainGame.mapRenderer.getGameTiles().length; row++) {
|
||||
for (int col = 0; col < mainGame.mapRenderer.getGameTiles()[0].length; col++) {
|
||||
GameTile gameTile = mainGame.mapRenderer.getGameTiles()[row][col];
|
||||
if (gameTile.contains(touchPoint.x, touchPoint.y)) {
|
||||
if (button == Input.Buttons.LEFT) {
|
||||
|
||||
// moving selected character
|
||||
if (mainGame.isPlayersTurn()) {
|
||||
|
||||
if (mainGame.hasCharacterSelected() && !gameTile.containsCharacter()) {
|
||||
|
||||
if (gameTile.getSymbol() != '#' && mainGame.mapRenderer.getSurroundedTilesOfCurrentCharacter().contains(gameTile)) {
|
||||
mainGame.mapRenderer.removeCharacterFromTile(mainGame.getSelectedCharacter());
|
||||
gameTile.visit(mainGame.getSelectedCharacter());
|
||||
mainGame.mapRenderer.setSurroundedTilesOfCurrentCharacter(col, row);
|
||||
mainGame.increaseTurn();
|
||||
mainGame.send(new MoveData(mainGame.getUsername(), mainGame.getSelectedCharacter().getName(), mainGame.mapRenderer.getPos(gameTile)));
|
||||
}
|
||||
}
|
||||
// clicking on enemy
|
||||
if (mainGame.hasCharacterSelected() && gameTile.containsCharacter() && gameTile.getCharacter().getFaction() != mainGame.getChosenFaction()) {
|
||||
if (mainGame.mapRenderer.getSurroundedTilesOfCurrentCharacter().contains(gameTile)) {
|
||||
if (!gameTile.getCharacter().isDead()) {
|
||||
gameTile.getCharacter().damage(gameTile.getCharacter().getDamageAmount());
|
||||
mainGame.increaseTurn();
|
||||
mainGame.send(new DamageData(gameTile.getCharacter().getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// set selected character
|
||||
if (!mainGame.hasCharacterSelected() && gameTile.containsCharacter()) {
|
||||
if (gameTile.getCharacter().getFaction() == mainGame.getChosenFaction()) {
|
||||
if (!gameTile.getCharacter().isDead()) {
|
||||
mainGame.setSelectedCharacter(gameTile.getCharacter());
|
||||
mainGame.mapRenderer.setSurroundedTilesOfCurrentCharacter(col, row);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
// switch character
|
||||
if (gameTile.containsCharacter()
|
||||
&& !mainGame.getSelectedCharacter().equals(gameTile.getCharacter())
|
||||
&& gameTile.getCharacter().getFaction() == mainGame.getChosenFaction()) {
|
||||
if (!gameTile.getCharacter().isDead()) {
|
||||
mainGame.setSelectedCharacter(gameTile.getCharacter());
|
||||
mainGame.mapRenderer.setSurroundedTilesOfCurrentCharacter(col, row);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
for (int row = 0; row < mainGame.mapRenderer.getGameTiles().length; row++) {
|
||||
for (int col = 0; col < mainGame.mapRenderer.getGameTiles()[0].length; col++) {
|
||||
GameTile gameTile = mainGame.mapRenderer.getGameTiles()[row][col];
|
||||
if (gameTile.contains(touchPoint.x, touchPoint.y)) {
|
||||
if (button == Input.Buttons.LEFT) {
|
||||
// System.out.println(gameTile + " row: " + row + ", col: " + col);
|
||||
if (mainGame.hasCharacterSelected() && !gameTile.containsCharacter()) {
|
||||
// System.out.println(mainGame.getSelectedCharacter());
|
||||
removeCharacterFromTile(mainGame.getSelectedCharacter());
|
||||
gameTile.visit(mainGame.getSelectedCharacter());
|
||||
}
|
||||
if (!mainGame.hasCharacterSelected() && gameTile.containsCharacter()) {
|
||||
mainGame.setSelectedCharacter(gameTile.getCharacter());
|
||||
}
|
||||
if (gameTile.containsCharacter() && !mainGame.getSelectedCharacter().equals(gameTile.getCharacter())) {
|
||||
mainGame.setSelectedCharacter(gameTile.getCharacter());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -205,7 +148,18 @@ public class GameInputProcessor implements InputProcessor {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private void removeCharacterFromTile(GameCharacter character) {
|
||||
rowLoop:
|
||||
for (int row = 0; row < mainGame.mapRenderer.getGameTiles().length; row++) {
|
||||
for (int col = 0; col < mainGame.mapRenderer.getGameTiles()[0].length; col++) {
|
||||
GameTile gameTile = mainGame.mapRenderer.getGameTiles()[row][col];
|
||||
if (gameTile.containsCharacter() && gameTile.getCharacter().equals(character)) {
|
||||
gameTile.removeCharacter();
|
||||
break rowLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
||||
|
||||
@@ -20,8 +20,6 @@ public class GameTile extends Rectangle {
|
||||
super.height = textureRegion.getRegionHeight();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public GameCharacter getCharacter() {
|
||||
return character;
|
||||
}
|
||||
@@ -38,8 +36,6 @@ public class GameTile extends Rectangle {
|
||||
public boolean visit(GameCharacter character) {
|
||||
if (this.character != null) return false;
|
||||
this.character = character;
|
||||
this.character.setX(this.x);
|
||||
this.character.setY(this.y);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -65,7 +61,6 @@ public class GameTile extends Rectangle {
|
||||
"symbol=" + symbol +
|
||||
", x=" + x +
|
||||
", y=" + y +
|
||||
", character=" + this.character +
|
||||
'}';
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -4,14 +4,9 @@ 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.client.MainGame;
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
import netwerkprog.game.util.graphics.Renderable;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
|
||||
public class MapRenderer implements Renderable {
|
||||
private final OrthographicCamera camera;
|
||||
@@ -24,19 +19,14 @@ public class MapRenderer implements Renderable {
|
||||
private static int y = 0;
|
||||
|
||||
private MainGame mainGame;
|
||||
private Texture square;
|
||||
private Texture square2;
|
||||
private Texture hitMarker;
|
||||
private Texture tombStone;
|
||||
|
||||
|
||||
public static TextureRegion FLOOR_TILE;
|
||||
public static TextureRegion WALL_TILE;
|
||||
public static TextureRegion PATH_TILE;
|
||||
|
||||
private GameTile[][] gameTiles;
|
||||
private List<GameTile> surroundedTilesOfCurrentCharacter;
|
||||
|
||||
public static int[][] directions = new int[][]{{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}};
|
||||
|
||||
/**
|
||||
* makea a new mapRenderer object
|
||||
@@ -60,29 +50,15 @@ public class MapRenderer implements Renderable {
|
||||
* loads all the images for the tiles and adds all the tiles to the array
|
||||
*/
|
||||
private void makeTiles() {
|
||||
mainGame.assets.load("square.png", Texture.class);
|
||||
mainGame.assets.load("square2.png", Texture.class);
|
||||
mainGame.assets.load(tilePath, Texture.class);
|
||||
mainGame.assets.load("hit.png", Texture.class);
|
||||
mainGame.assets.load("dead.png", Texture.class);
|
||||
mainGame.assets.finishLoading();
|
||||
square = mainGame.assets.get("square.png");
|
||||
square2 = mainGame.assets.get("square2.png");
|
||||
hitMarker = mainGame.assets.get("hit.png");
|
||||
tombStone = mainGame.assets.get("dead.png");
|
||||
|
||||
// load the texture file
|
||||
Texture texture = mainGame.assets.get(tilePath);
|
||||
Texture texture = new Texture(Gdx.files.internal(tilePath));
|
||||
TextureRegion[][] tileTextures = TextureRegion.split(texture, 32, 32);
|
||||
|
||||
FLOOR_TILE = tileTextures[1][6];
|
||||
WALL_TILE = tileTextures[0][4];
|
||||
PATH_TILE = tileTextures[4][6];
|
||||
|
||||
// init the array
|
||||
this.gameTiles = new GameTile[map.getHeight()][map.getWidth()];
|
||||
|
||||
// for each game tile, put the corresponding tile image in the array
|
||||
for (int row = map.getHeight(); row >= 0; row--) {
|
||||
y += 32;
|
||||
x = 0;
|
||||
@@ -115,9 +91,6 @@ public class MapRenderer implements Renderable {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
/**
|
||||
* method that renders the whole map
|
||||
*/
|
||||
@Override
|
||||
public void render() {
|
||||
batch.begin();
|
||||
@@ -126,136 +99,24 @@ public class MapRenderer implements Renderable {
|
||||
for (GameTile[] gameTileRow : gameTiles) {
|
||||
for (int col = 0; col < gameTiles[0].length; col++) {
|
||||
GameTile cur = gameTileRow[col];
|
||||
//draw each tile
|
||||
batch.draw(cur.getTextureRegion(), cur.x, cur.y);
|
||||
if (cur.containsCharacter()) {
|
||||
//draw each character on a tile
|
||||
GameCharacter character = cur.getCharacter();
|
||||
if (!character.isDead()) {
|
||||
batch.draw(character.getTextureRegion(), cur.x, cur.y);
|
||||
//if he's showing an animation, draw the hitmarker.
|
||||
if (character.isShowingAnimation())
|
||||
batch.draw(hitMarker, cur.x, cur.y);
|
||||
// if hes selected, draw the green square
|
||||
if (character.equals(mainGame.getSelectedCharacter()))
|
||||
batch.draw(square, cur.x, cur.y);
|
||||
} else {
|
||||
// if hes dead, draw a tombstone
|
||||
batch.draw(tombStone, cur.x, cur.y);
|
||||
}
|
||||
batch.draw(cur.getCharacter().getTextureRegion(), cur.x, cur.y);
|
||||
// System.out.println("drawing character at " + cur.x + " " + cur.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (surroundedTilesOfCurrentCharacter != null && !surroundedTilesOfCurrentCharacter.isEmpty()) {
|
||||
for (GameTile gameTile : surroundedTilesOfCurrentCharacter) {
|
||||
batch.draw(square2, gameTile.x, gameTile.y);
|
||||
}
|
||||
}
|
||||
|
||||
batch.end();
|
||||
x = 0;
|
||||
y = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(double deltaTime) {
|
||||
|
||||
|
||||
/**
|
||||
* gets the 8 surrounding tiles of the character, to see where he can move.
|
||||
* @param x the x position of the character
|
||||
* @param y the y position of the character
|
||||
*/
|
||||
public void setSurroundedTilesOfCurrentCharacter(int x, int y) {
|
||||
List<GameTile> res = new ArrayList<>();
|
||||
for (int[] direction : directions) {
|
||||
int cx = x + direction[0];
|
||||
int cy = y + direction[1];
|
||||
if (cy >= 0 && cy < gameTiles.length)
|
||||
if (cx >= 0 && cx < gameTiles[cy].length)
|
||||
if (gameTiles[cy][cx].getSymbol() != '#')
|
||||
res.add(gameTiles[cy][cx]);
|
||||
}
|
||||
surroundedTilesOfCurrentCharacter = res;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets the game tile of the character.
|
||||
* @param character the character
|
||||
* @return the game tile of the character, null if it is not found
|
||||
*/
|
||||
public GameTile getTile(GameCharacter character) {
|
||||
for (GameTile[] tiles : this.gameTiles) {
|
||||
for (GameTile tile : tiles) {
|
||||
if (tile.containsCharacter())
|
||||
if (tile.getCharacter().equals(character)) {
|
||||
return tile;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
tombStone.dispose();
|
||||
square.dispose();
|
||||
square2.dispose();
|
||||
hitMarker.dispose();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* gets the position of the specified tile.
|
||||
* @param tile the tile to get the position of
|
||||
* @return the position of the tile, a point of -1,-1 if the tile is not found
|
||||
*/
|
||||
public Point getPos(GameTile tile) {
|
||||
for (int row = 0; row < this.gameTiles.length; row++) {
|
||||
for (int col = 0; col < this.gameTiles[0].length; col++) {
|
||||
if (gameTiles[row][col].equals(tile)) {
|
||||
return new Point(col, row);
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Point(-1, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* gets the game tile at the specified position.
|
||||
* @param pos the position of the tile
|
||||
* @return the game tile on the position, <code>null</code> if it is not found
|
||||
*/
|
||||
public GameTile getGameTile(Point pos) {
|
||||
for (int row = 0; row < this.gameTiles.length; row++) {
|
||||
for (int col = 0; col < this.gameTiles[0].length; col++) {
|
||||
if (row == pos.y && col == pos.x) {
|
||||
return this.gameTiles[row][col];
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* remove character from tile
|
||||
* @param character the character to remove
|
||||
*/
|
||||
public void removeCharacterFromTile(GameCharacter character) {
|
||||
rowLoop:
|
||||
for (int row = 0; row < getGameTiles().length; row++) {
|
||||
for (int col = 0; col < getGameTiles()[0].length; col++) {
|
||||
GameTile gameTile = getGameTiles()[row][col];
|
||||
if (gameTile.containsCharacter() && gameTile.getCharacter().equals(character)) {
|
||||
gameTile.removeCharacter();
|
||||
break rowLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* resize the screen
|
||||
* @param screenWidth the width of the screen
|
||||
* @param screenHeight the height of the screen
|
||||
*/
|
||||
public void resize(int screenWidth, int screenHeight) {
|
||||
cam = new OrthographicCamera(screenWidth, screenHeight);
|
||||
cam.translate(screenWidth / 2f, screenHeight / 2f);
|
||||
@@ -263,20 +124,7 @@ public class MapRenderer implements Renderable {
|
||||
batch.setProjectionMatrix(cam.combined);
|
||||
}
|
||||
|
||||
/**
|
||||
* return the game tiles
|
||||
* @return the game tiles.
|
||||
*/
|
||||
public GameTile[][] getGameTiles() {
|
||||
return gameTiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the surrounding tiles of character
|
||||
* @return the surrounding tiles of character
|
||||
*/
|
||||
public List<GameTile> getSurroundedTilesOfCurrentCharacter() {
|
||||
return surroundedTilesOfCurrentCharacter;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
public interface DataCallback {
|
||||
void onDataReceived(Data data, DataSource source);
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
public interface DataSource {
|
||||
void writeData(Data data);
|
||||
String getName();
|
||||
}
|
||||
43
core/src/netwerkprog/game/server/Parser.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.client.game.characters.DevTest1;
|
||||
import netwerkprog.game.client.game.characters.DevTest2;
|
||||
import netwerkprog.game.client.game.characters.DevTest3;
|
||||
import netwerkprog.game.server.controllers.DataController;
|
||||
import netwerkprog.game.util.data.ParserCallback;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Parser {
|
||||
private final ParserCallback callback;
|
||||
private Scanner scanner;
|
||||
|
||||
private final DataController dataController;
|
||||
|
||||
public Parser(ParserCallback callback) {
|
||||
this.callback = callback;
|
||||
this.dataController = new DataController();
|
||||
|
||||
this.dataController.addAllCharacters(new DevTest1(), new DevTest2(), new DevTest3());
|
||||
}
|
||||
|
||||
public void parse(String request) {
|
||||
String data = "";
|
||||
this.scanner = new Scanner(request);
|
||||
scanner.useDelimiter("~");
|
||||
String type = scanner.next();
|
||||
String name = scanner.next();
|
||||
|
||||
if (type.equals("character")) {
|
||||
try {
|
||||
data = dataController.getCharacter(name).toString();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
data = ex.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
callback.onDataReceived(data);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,35 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.server.controllers.SessionController;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Server {
|
||||
private SessionController sessionController;
|
||||
private Thread sessionThread;
|
||||
private HashMap<String, Thread> gameThreads;
|
||||
|
||||
public void start() {
|
||||
SessionController sessionController = new SessionController();
|
||||
this.sessionController = new SessionController();
|
||||
|
||||
this.gameThreads = new HashMap<>();
|
||||
this.sessionThread = new Thread(sessionController);
|
||||
|
||||
run();
|
||||
}
|
||||
|
||||
private void run() {
|
||||
setTestGames();
|
||||
this.sessionThread.start();
|
||||
}
|
||||
|
||||
private void setTestGames() {
|
||||
// for (int i = 0; i < 10; i++) {
|
||||
// gameThreads.put("game " + i, new Thread(new GameController(i)));
|
||||
// }
|
||||
//
|
||||
// for (String game : gameThreads.keySet()) {
|
||||
// gameThreads.get(game).start();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +1,40 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.connection.ConnectionData;
|
||||
import netwerkprog.game.server.controllers.SessionController;
|
||||
import netwerkprog.game.util.data.ParserCallback;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ServerClient implements Runnable, DataSource {
|
||||
public class ServerClient implements Runnable, ParserCallback {
|
||||
private DataInputStream in;
|
||||
private DataOutputStream out;
|
||||
private final String name;
|
||||
private final SessionController server;
|
||||
private final DataCallback callback;
|
||||
private final ObjectInputStream in;
|
||||
private final ObjectOutputStream out;
|
||||
private final Parser parser;
|
||||
private boolean isConnected;
|
||||
|
||||
public ServerClient(String name, SessionController server, DataCallback callback, ObjectInputStream in, ObjectOutputStream out) {
|
||||
public ServerClient(String name, Socket socket, SessionController server) {
|
||||
this.name = name;
|
||||
this.server = server;
|
||||
this.callback = callback;
|
||||
this.in = in;
|
||||
this.out = out;
|
||||
this.isConnected = true;
|
||||
this.parser = new Parser(this);
|
||||
try {
|
||||
this.in = new DataInputStream(socket.getInputStream());
|
||||
this.out = new DataOutputStream(socket.getOutputStream());
|
||||
this.isConnected = true;
|
||||
} catch (IOException e) {
|
||||
this.isConnected = false;
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes data to the connected client.
|
||||
* @param data The data object to write.
|
||||
*/
|
||||
public void writeData(Data data) {
|
||||
public void writeUTF(String text) {
|
||||
try {
|
||||
this.out.writeObject(data);
|
||||
this.out.writeUTF(text);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Connection suddenly terminated!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,23 +42,14 @@ public class ServerClient implements Runnable, DataSource {
|
||||
public void run() {
|
||||
while (this.isConnected) {
|
||||
try {
|
||||
Object object = this.in.readObject();
|
||||
if (object instanceof Data) {
|
||||
Data data = (Data) object;
|
||||
if (data.getPayload() instanceof ConnectionData) {
|
||||
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
||||
if (connectionData.getAction().equals("Disconnect")) {
|
||||
this.isConnected = false;
|
||||
this.server.disconnect(this);
|
||||
}
|
||||
} else {
|
||||
callback.onDataReceived(data, this);
|
||||
}
|
||||
}
|
||||
String received = this.in.readUTF();
|
||||
this.parser.parse(received);
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println("[SERVERCLIENT] caught exception - " + e.getMessage());
|
||||
System.out.println("[SERVERCLIENT] terminating failing connection...");
|
||||
this.isConnected = false;
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("[SERVERCLIENT] done!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,4 +57,9 @@ public class ServerClient implements Runnable, DataSource {
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataReceived(String data) {
|
||||
writeUTF(data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.connection.ConnectionData;
|
||||
import netwerkprog.game.util.data.connection.NameData;
|
||||
import netwerkprog.game.util.data.connection.PlayerConnectData;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* The sessionController manages any connections from new clients and assigns individual threads to said clients.
|
||||
*/
|
||||
public class SessionController implements DataCallback, Runnable {
|
||||
private ServerSocket serverSocket;
|
||||
private final ArrayList<ServerClient> clients;
|
||||
private boolean listening;
|
||||
|
||||
public SessionController() {
|
||||
this.clients = new ArrayList<>();
|
||||
this.listening = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Thread run method.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
this.listening = true;
|
||||
while (listening) {
|
||||
listen();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listens for any new clients.
|
||||
*/
|
||||
public void listen() {
|
||||
try {
|
||||
this.serverSocket = new ServerSocket(Data.port());
|
||||
registerClient(serverSocket.accept());
|
||||
this.serverSocket.close();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a client to the server.
|
||||
* @param socket The socket used for the client connections.
|
||||
*/
|
||||
public void registerClient(Socket socket) {
|
||||
try {
|
||||
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
|
||||
String username;
|
||||
boolean registering = true;
|
||||
while (registering) {
|
||||
outputStream.writeObject(new ConnectionData("Connect", "Please give a username"));
|
||||
Object object = inputStream.readObject();
|
||||
if (object instanceof Data) {
|
||||
Data data = (Data) object;
|
||||
if (data instanceof ConnectionData) {
|
||||
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
||||
if (connectionData.getAction().equals("Connect")) {
|
||||
outputStream.writeObject(new ConnectionData("Connect", "Confirm"));
|
||||
registering = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
username = "player" + (this.clients.size() + 1);
|
||||
ServerClient serverClient = new ServerClient(username, this, this, inputStream, outputStream);
|
||||
Thread t = new Thread(serverClient);
|
||||
t.start();
|
||||
serverClient.writeData(new NameData(username));
|
||||
this.clients.add(serverClient);
|
||||
} catch (IOException | ClassNotFoundException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuts down the sessionController.
|
||||
*/
|
||||
public void shutdown() {
|
||||
this.listening = false;
|
||||
try {
|
||||
serverSocket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects the client from the server list.
|
||||
* @param client The Client to disconnect.
|
||||
*/
|
||||
public void disconnect(ServerClient client) {
|
||||
this.clients.remove(client);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataReceived(Data data, DataSource source) {
|
||||
for (ServerClient client: clients) {
|
||||
if (!client.getName().equals(source.getName())){
|
||||
client.writeData(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package netwerkprog.game.server.controllers;
|
||||
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class DataController {
|
||||
private final HashSet<GameCharacter> gameCharacters;
|
||||
|
||||
public DataController() {
|
||||
gameCharacters = new HashSet<>();
|
||||
}
|
||||
|
||||
public void addCharacter(GameCharacter gameCharacter) {
|
||||
this.gameCharacters.add(gameCharacter);
|
||||
}
|
||||
|
||||
public void addAllCharacters(GameCharacter... gameCharacters) {
|
||||
this.gameCharacters.addAll(Arrays.asList(gameCharacters));
|
||||
}
|
||||
|
||||
public void removeCharacter(String name) {
|
||||
this.gameCharacters.removeIf(character -> character.getName().equals(name));
|
||||
}
|
||||
|
||||
public void removeCharacter(GameCharacter character) {
|
||||
this.gameCharacters.remove(character);
|
||||
}
|
||||
|
||||
public void clearCharacters() {
|
||||
this.gameCharacters.clear();
|
||||
}
|
||||
|
||||
public HashSet<GameCharacter> getGameCharacters() {
|
||||
return gameCharacters;
|
||||
}
|
||||
|
||||
public GameCharacter getCharacter(String name) throws IllegalArgumentException {
|
||||
for (GameCharacter character : gameCharacters) {
|
||||
if (character.getName().equals(name)) {
|
||||
return character;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("The character does not exist.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package netwerkprog.game.server.controllers;
|
||||
|
||||
import netwerkprog.game.server.ServerClient;
|
||||
import netwerkprog.game.util.application.Controller;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* The sessionController manages any connections from new clients and assigns individual threads to said clients.
|
||||
*/
|
||||
public class SessionController extends Controller {
|
||||
private ServerSocket serverSocket;
|
||||
private final ArrayList<ServerClient> clients = new ArrayList<>();
|
||||
private final HashMap<String, Thread> clientThreads = new HashMap<>();
|
||||
private boolean listening;
|
||||
|
||||
public SessionController() {
|
||||
this.listening = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Thread run method.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
this.listening = true;
|
||||
while (listening) {
|
||||
listen();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listens for any new clients.
|
||||
*/
|
||||
public void listen() {
|
||||
try {
|
||||
this.serverSocket = new ServerSocket(Data.port());
|
||||
System.out.println("[SERVER] listening on port " + Data.port());
|
||||
registerClient(serverSocket.accept());
|
||||
this.serverSocket.close();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a client to the server.
|
||||
* @param socket The socket used for the client connections.
|
||||
*/
|
||||
public void registerClient(Socket socket) {
|
||||
try {
|
||||
System.out.println("[SERVER] got new client on " + socket.getInetAddress().getHostAddress());
|
||||
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
|
||||
|
||||
outputStream.writeUTF("Enter username: ");
|
||||
String username = inputStream.readUTF();
|
||||
|
||||
System.out.println("[SERVER] got username " + username);
|
||||
ServerClient serverClient = new ServerClient(username, socket, this);
|
||||
|
||||
Thread t = new Thread(serverClient);
|
||||
t.start();
|
||||
|
||||
this.clientThreads.put(username,t);
|
||||
this.clients.add(serverClient);
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a server message to all connected clients.
|
||||
* @param text message.
|
||||
*/
|
||||
public void serverMessage(String text) {
|
||||
for (ServerClient serverClient : clients) {
|
||||
serverClient.writeUTF(text);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to a specific user.
|
||||
* @param name user.
|
||||
* @param text message.
|
||||
*/
|
||||
public void personalMessage(String name, String text) {
|
||||
for (ServerClient serverClient : clients) {
|
||||
if (serverClient.getName().equals(name)) {
|
||||
serverClient.writeUTF(text);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a client from the server.
|
||||
* @param serverClient The client to remove.
|
||||
*/
|
||||
public void removeClient(ServerClient serverClient) {
|
||||
this.clients.remove(serverClient);
|
||||
try {
|
||||
this.clientThreads.get(serverClient.getName()).join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.clientThreads.remove(serverClient.getName());
|
||||
this.serverMessage(serverClient.getName() + " left!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of all connected users.
|
||||
* @return Set of all connected users.
|
||||
*/
|
||||
public Set<String> getUsernames() {
|
||||
return this.clientThreads.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuts down the sessionController.
|
||||
*/
|
||||
public void shutdown() {
|
||||
this.listening = false;
|
||||
try {
|
||||
serverSocket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("[SERVER] networking shutdown ");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package netwerkprog.game.util.application;
|
||||
|
||||
public abstract class Controller implements Runnable {
|
||||
|
||||
}
|
||||
@@ -6,6 +6,36 @@ import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
|
||||
* custom application config class for the game
|
||||
*/
|
||||
public class GameApplicationConfiguration extends LwjglApplicationConfiguration {
|
||||
/**
|
||||
* makes a new configuration with the given parameters
|
||||
* @param width the width (in pixels)
|
||||
* @param height the height (in pixels)
|
||||
* @param fullscreen whether the app should run in fullscreen
|
||||
*/
|
||||
public GameApplicationConfiguration(int width, int height, boolean fullscreen) {
|
||||
super();
|
||||
super.width = width;
|
||||
super.height = height;
|
||||
super.fullscreen = fullscreen;
|
||||
}
|
||||
|
||||
/**
|
||||
* makes a new configuration with the given parameters.
|
||||
* No fullscreen
|
||||
* @param width the width (in pixels)
|
||||
* @param height the height (in pixels)
|
||||
*/
|
||||
public GameApplicationConfiguration(int width, int height) {
|
||||
this(width,height,false);
|
||||
}
|
||||
|
||||
/**
|
||||
* makes a new configuration with standard full hd width and height
|
||||
*/
|
||||
public GameApplicationConfiguration() {
|
||||
this(1920,1080,false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a new configuration with the given parameters
|
||||
* @param title the title of the window
|
||||
@@ -31,4 +61,13 @@ public class GameApplicationConfiguration extends LwjglApplicationConfiguration
|
||||
public GameApplicationConfiguration(String title, int width, int height) {
|
||||
this(title,width,height,false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a new configuration with the given title
|
||||
* the window will be 1920 x 1080 and fullscreen will be off
|
||||
* @param title the window title
|
||||
*/
|
||||
public GameApplicationConfiguration(String title) {
|
||||
this(title, 1920,1080);
|
||||
}
|
||||
}
|
||||
|
||||
102
core/src/netwerkprog/game/util/application/Timer.java
Normal file
@@ -0,0 +1,102 @@
|
||||
package netwerkprog.game.util.application;
|
||||
|
||||
public class Timer implements Updatable {
|
||||
private double wait;
|
||||
private double time;
|
||||
private boolean loop;
|
||||
private int timeout;
|
||||
|
||||
/**
|
||||
* makes a new timer that doesnt start automatically and doesnt loop
|
||||
* @param wait the time in ms to wait
|
||||
*/
|
||||
public Timer(double wait) {
|
||||
this(wait, false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* makes a new timer with the given parameters
|
||||
* @param wait the wait time in ms
|
||||
* @param autoStart whether the timer should start automatically
|
||||
*/
|
||||
public Timer(double wait, boolean autoStart) {
|
||||
this(wait, autoStart, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* makes a new timer with the given parameters
|
||||
* @param wait the wait time in ms
|
||||
* @param autoStart wether the timer should start automatically
|
||||
* @param loop if this timer should loop
|
||||
*/
|
||||
public Timer(double wait, boolean autoStart, boolean loop) {
|
||||
this.wait = wait;
|
||||
this.loop = loop;
|
||||
this.time = 0.0;
|
||||
this.timeout = autoStart ? 0 : -1;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
this.timeout = 0;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
this.timeout = -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether the timer has timed out.
|
||||
* Timeouts will stack and will not be reset when this method has been called,
|
||||
* instead the timeout will decrease by one.
|
||||
*
|
||||
* @return timeout occurred
|
||||
*/
|
||||
public boolean timeout() {
|
||||
if (this.timeout == -1) {
|
||||
// timeout has occurred and is not looped
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.timeout - 1 < 0) {
|
||||
// timeout has not occurred
|
||||
this.timeout = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.loop) {
|
||||
// when looped, decrease by one
|
||||
this.timeout -= 1;
|
||||
} else {
|
||||
// else abort the timer
|
||||
this.timeout = -1;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the time until the timeout.
|
||||
* @param wait the new timeout time
|
||||
*/
|
||||
public void setWait(double wait) {
|
||||
this.wait = wait;
|
||||
}
|
||||
|
||||
|
||||
public double getWait() {
|
||||
return this.wait;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(double deltaTime) {
|
||||
if (this.timeout != -1) {
|
||||
// Only update when timer is not aborted
|
||||
this.time += deltaTime;
|
||||
if (this.time >= this.wait) {
|
||||
// timeout occurred
|
||||
this.time -= this.wait;
|
||||
this.timeout++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package netwerkprog.game.util.application;
|
||||
|
||||
public interface Updatable {
|
||||
void update(double deltaTime);
|
||||
}
|
||||
@@ -1,31 +1,7 @@
|
||||
package netwerkprog.game.util.data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Data implements Serializable {
|
||||
public class Data {
|
||||
public static int port() {
|
||||
return 8000;
|
||||
}
|
||||
|
||||
private final String objectType;
|
||||
private Data payload;
|
||||
|
||||
public Data(String type) {
|
||||
this.objectType = type;
|
||||
}
|
||||
|
||||
public void setPayload(Data payload) {
|
||||
this.payload = payload;
|
||||
}
|
||||
|
||||
public Data getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Data{" +
|
||||
"objectType='" + objectType + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
4
core/src/netwerkprog/game/util/data/Event.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package netwerkprog.game.util.data;
|
||||
|
||||
public class Event {
|
||||
}
|
||||
5
core/src/netwerkprog/game/util/data/ParserCallback.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package netwerkprog.game.util.data;
|
||||
|
||||
public interface ParserCallback {
|
||||
void onDataReceived(String data);
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package netwerkprog.game.util.data.character;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
public class DamageData extends Data {
|
||||
private final String name;
|
||||
|
||||
public DamageData(String name) {
|
||||
super("Damage");
|
||||
super.setPayload(this);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package netwerkprog.game.util.data.character;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class MoveData extends Data {
|
||||
private final String username;
|
||||
private final String characterName;
|
||||
private final Point pos;
|
||||
|
||||
public MoveData(String username, String characterName, Point pos) {
|
||||
super("Move");
|
||||
super.setPayload(this);
|
||||
this.username = username;
|
||||
this.characterName = characterName;
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getCharacterName() {
|
||||
return characterName;
|
||||
}
|
||||
|
||||
public Point getPos() {
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package netwerkprog.game.util.data.connection;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class ConnectionData extends Data implements Serializable {
|
||||
private final String action;
|
||||
private final String message;
|
||||
|
||||
public ConnectionData(String action, String message) {
|
||||
super("Connection");
|
||||
super.setPayload(this);
|
||||
this.action = action;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package netwerkprog.game.util.data.connection;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
public class NameData extends Data {
|
||||
private final String name;
|
||||
public NameData(String name) {
|
||||
super("name");
|
||||
super.setPayload(this);
|
||||
this.name = name;
|
||||
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package netwerkprog.game.util.data.connection;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
public class PlayerConnectData extends Data {
|
||||
private String username;
|
||||
public PlayerConnectData(String username) {
|
||||
super("player-connect");
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package netwerkprog.game.util.data.connection;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.game.Faction;
|
||||
|
||||
public class TeamData extends Data {
|
||||
private final Faction faction;
|
||||
private final String username;
|
||||
|
||||
public TeamData(Faction faction, String username) {
|
||||
super("Team");
|
||||
super.setPayload(this);
|
||||
this.faction = faction;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public Faction getFaction() {
|
||||
return faction;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package netwerkprog.game.util.data.connection;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
public class TurnData extends Data {
|
||||
public TurnData() {
|
||||
super("turn");
|
||||
}
|
||||
}
|
||||
@@ -1,37 +1,29 @@
|
||||
package netwerkprog.game.util.game;
|
||||
|
||||
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.scenes.scene2d.Actor;
|
||||
import netwerkprog.game.client.game.map.GameTile;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public abstract class GameCharacter extends Actor implements Comparable<GameCharacter>, Serializable {
|
||||
public abstract class GameCharacter extends Actor implements Comparable<GameCharacter> {
|
||||
protected String name;
|
||||
protected Faction faction;
|
||||
protected HashSet<Ability> abilities;
|
||||
protected boolean override;
|
||||
protected TextureRegion textureRegion;
|
||||
protected int health;
|
||||
protected List<GameTile> allowedToMove;
|
||||
protected boolean damageAnimation;
|
||||
protected double hitTimout = 0;
|
||||
|
||||
public GameCharacter(String name, Faction faction, TextureRegion textureRegion, Ability... abilities) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.faction = faction;
|
||||
this.abilities = new HashSet<>(Arrays.asList(abilities));
|
||||
this.override = false;
|
||||
this.textureRegion = textureRegion;
|
||||
this.health = 100;
|
||||
this.damageAnimation = false;
|
||||
this.allowedToMove = new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@@ -46,35 +38,6 @@ public abstract class GameCharacter extends Actor implements Comparable<GameChar
|
||||
this.abilities.remove(ability);
|
||||
}
|
||||
|
||||
public int getHealth() {
|
||||
return health;
|
||||
}
|
||||
|
||||
public void setHealth(int health) {
|
||||
this.health = health;
|
||||
}
|
||||
|
||||
public void heal(int amount) {
|
||||
this.health += amount;
|
||||
if (this.health > 100) this.health = 10;
|
||||
}
|
||||
public int getDamageAmount() {
|
||||
return 10;
|
||||
}
|
||||
|
||||
public void damage(int amount) {
|
||||
this.health -= amount;
|
||||
if (this.health < 0) {
|
||||
this.health = 0;
|
||||
}
|
||||
|
||||
this.damageAnimation = true;
|
||||
}
|
||||
|
||||
public boolean isDead() {
|
||||
return this.health <= 0;
|
||||
}
|
||||
|
||||
public void changeControl() {
|
||||
this.override = !this.override;
|
||||
}
|
||||
@@ -99,17 +62,6 @@ public abstract class GameCharacter extends Actor implements Comparable<GameChar
|
||||
Objects.equals(abilities, character.abilities);
|
||||
}
|
||||
|
||||
public void update(double deltaTime) {
|
||||
if (this.damageAnimation) {
|
||||
this.hitTimout += deltaTime;
|
||||
}
|
||||
if (this.hitTimout >= 0.4) {
|
||||
this.damageAnimation = false;
|
||||
this.hitTimout = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, faction, abilities, override);
|
||||
@@ -117,7 +69,7 @@ public abstract class GameCharacter extends Actor implements Comparable<GameChar
|
||||
|
||||
@Override
|
||||
public int compareTo(GameCharacter o) {
|
||||
return (this.health - o.health) + this.name.compareTo(o.name) + this.faction.compareTo(o.faction);
|
||||
return this.name.compareTo(o.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -125,28 +77,6 @@ public abstract class GameCharacter extends Actor implements Comparable<GameChar
|
||||
return "GameCharacter{" +
|
||||
"name='" + name + '\'' +
|
||||
", faction=" + faction +
|
||||
", x=" + super.getX() +
|
||||
", y=" + super.getY() +
|
||||
'}';
|
||||
}
|
||||
|
||||
public List<GameTile> getAllowedToMove() {
|
||||
return allowedToMove;
|
||||
}
|
||||
|
||||
public void setAllowedToMove(List<GameTile> allowedToMove) {
|
||||
this.allowedToMove = allowedToMove;
|
||||
}
|
||||
|
||||
public Faction getFaction() {
|
||||
return faction;
|
||||
}
|
||||
|
||||
public boolean isShowingAnimation() {
|
||||
return this.damageAnimation;
|
||||
}
|
||||
|
||||
public void setShowingDamageAnimation(boolean damageAnimation) {
|
||||
this.damageAnimation = damageAnimation;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,8 @@ public class FrameRate implements Disposable{
|
||||
long lastTimeCounted;
|
||||
private float sinceChange;
|
||||
private float frameRate;
|
||||
private final BitmapFont font;
|
||||
private final SpriteBatch batch;
|
||||
private BitmapFont font;
|
||||
private SpriteBatch batch;
|
||||
private OrthographicCamera cam;
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package netwerkprog.game.util.graphics;
|
||||
|
||||
public interface Renderable {
|
||||
import netwerkprog.game.util.application.Updatable;
|
||||
|
||||
public interface Renderable extends Updatable {
|
||||
void render();
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package netwerkprog.game.util.graphics;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.utils.Disposable;
|
||||
|
||||
public class TextRenderer implements Disposable {
|
||||
private final BitmapFont font;
|
||||
private final SpriteBatch batch;
|
||||
private OrthographicCamera cam;
|
||||
|
||||
public TextRenderer() {
|
||||
font = new BitmapFont();
|
||||
batch = new SpriteBatch();
|
||||
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
try {
|
||||
batch.dispose();
|
||||
font.dispose();
|
||||
} catch (IllegalArgumentException e) {
|
||||
System.out.println("There was an error disposing textRenderer: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void resize(int screenWidth, int screenHeight) {
|
||||
cam = new OrthographicCamera(screenWidth, screenHeight);
|
||||
cam.translate(screenWidth / 2, screenHeight / 2);
|
||||
cam.update();
|
||||
batch.setProjectionMatrix(cam.combined);
|
||||
}
|
||||
|
||||
public void render(String text, float x, float y) {
|
||||
batch.begin();
|
||||
font.draw(batch, text, x, y);
|
||||
batch.end();
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
package netwerkprog.game.util.tree;
|
||||
|
||||
public abstract class AbstractTree<E> implements Tree<E> {
|
||||
@Override /* Inorder traversal from the root*/
|
||||
@Override /** Inorder traversal from the root*/
|
||||
public void inorder() {
|
||||
}
|
||||
|
||||
@Override /* Post order traversal from the root */
|
||||
public void postOrder() {
|
||||
@Override /** Postorder traversal from the root */
|
||||
public void postorder() {
|
||||
}
|
||||
|
||||
@Override /* Preorder traversal from the root */
|
||||
@Override /** Preorder traversal from the root */
|
||||
public void preorder() {
|
||||
}
|
||||
|
||||
@Override /* Return true if the tree is empty */
|
||||
@Override /** Return true if the tree is empty */
|
||||
public boolean isEmpty() {
|
||||
return getSize() == 0;
|
||||
}
|
||||
|
||||
@@ -1,112 +1,117 @@
|
||||
package netwerkprog.game.util.tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.*;
|
||||
|
||||
public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
protected TreeNode<E> root;
|
||||
protected int size = 0;
|
||||
|
||||
|
||||
public int sum() {
|
||||
// Helper methode
|
||||
public int sum () {
|
||||
return this.sum(this.getRoot());
|
||||
}
|
||||
|
||||
public int sum(TreeNode<E> node) {
|
||||
|
||||
// Opgave 1b (10 punten): Maak de recursieve methode sum af in de klasse bst.BST. Deze methode telt de getallen
|
||||
// van alle elementen van de binaire zoekboom bij elkaar op. De methode geeft de totale som terug van alle getallen
|
||||
// in de boom.
|
||||
public int sum( TreeNode<E> node ) {
|
||||
// Schrijf hier jouw code...
|
||||
if (node == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int nodeValue = (Integer) node.element;
|
||||
int nodeValue = (Integer) node.element; // Tip, omdat E nog onbekend is doen we het zo (niet helemaal netjes)
|
||||
return sum(node.left) + sum(node.right);
|
||||
}
|
||||
|
||||
|
||||
public int totalLeaves() {
|
||||
// Helper methode
|
||||
public int totalLeaves () {
|
||||
return this.totalLeaves(this.getRoot());
|
||||
}
|
||||
|
||||
public int totalLeaves(TreeNode<E> node) {
|
||||
if (node == null) {
|
||||
return 0;
|
||||
}
|
||||
if (node.left == null && node.right == null) {
|
||||
return 1;
|
||||
}
|
||||
return totalLeaves(node.left) + totalLeaves(node.right);
|
||||
// Opgave 1c (10 punten): Maak de methode totalLeaves af om de klasse bst.BST. Deze methode telt het aantal
|
||||
// bladeren (leaves) van de gegeven binaire zoekboom en geeft deze terug. Je hoeft deze methode niet recursief te
|
||||
// implementeren. Het mag wel.
|
||||
public int totalLeaves ( TreeNode<E> node ) {
|
||||
if (node == null) {
|
||||
return 0;
|
||||
}
|
||||
if (node.left == null && node.right == null) {
|
||||
return 1;
|
||||
}
|
||||
return totalLeaves(node.left) + totalLeaves(node.right);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a default binary tree
|
||||
*/
|
||||
|
||||
/** Create a default binary tree */
|
||||
public BST() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a binary tree from an array of objects
|
||||
*/
|
||||
/** Create a binary tree from an array of objects */
|
||||
public BST(E[] objects) {
|
||||
for (E object : objects) insert(object);
|
||||
for (int i = 0; i < objects.length; i++)
|
||||
insert(objects[i]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the element is in the tree
|
||||
*/
|
||||
@Override
|
||||
@Override /** Returns true if the element is in the tree */
|
||||
public boolean search(E e) {
|
||||
return search(e, root);
|
||||
}
|
||||
|
||||
private boolean search(E e, TreeNode<E> tree) {
|
||||
private boolean search(E e, TreeNode<E> tree)
|
||||
{
|
||||
// nog niet correct
|
||||
if (tree == null) {
|
||||
if (tree == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (e.compareTo(tree.element) == 0) {
|
||||
if (e.compareTo(tree.element) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (e.compareTo(tree.element) < 0) {
|
||||
if (e.compareTo(tree.element) < 0)
|
||||
{
|
||||
return search(e, tree.left);
|
||||
} else // (e.compareTo(tree.element) > 0)
|
||||
}
|
||||
|
||||
else // (e.compareTo(tree.element) > 0)
|
||||
{
|
||||
return search(e, tree.right);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert element o into the binary tree
|
||||
* Return true if the element is inserted successfully
|
||||
*/
|
||||
@Override
|
||||
public void insert(E e) {
|
||||
@Override /** Insert element o into the binary tree
|
||||
* Return true if the element is inserted successfully */
|
||||
public boolean insert(E e) {
|
||||
if (root == null) {
|
||||
root = createNewNode(e); // Create a new root
|
||||
size++;
|
||||
} else {
|
||||
insert(e, root);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return insert(e, root);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert element o into the binary tree
|
||||
/** Insert element o into the binary tree
|
||||
* Return true if the element is inserted successfully
|
||||
* pre: root != null
|
||||
pre: root != null
|
||||
*/
|
||||
public boolean insert(E e, TreeNode<E> tree) {
|
||||
if (e.compareTo(tree.element) == 0) {
|
||||
return false; // Duplicate node not inserted
|
||||
} else if (e.compareTo(tree.element) < 0 && tree.left != null)
|
||||
return insert(e, tree.left);
|
||||
}
|
||||
else if (e.compareTo(tree.element) < 0 && tree.left != null)
|
||||
return insert(e, tree.left);
|
||||
|
||||
else if (e.compareTo(tree.element) > 0 && tree.right != null)
|
||||
return insert(e, tree.right);
|
||||
|
||||
// Create the new node and attach it to the parent node
|
||||
// Create the new node and attach it to the parent node
|
||||
else {
|
||||
if (e.compareTo(tree.element) < 0) {
|
||||
tree.left = createNewNode(e);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
tree.right = createNewNode(e);
|
||||
}
|
||||
size++;
|
||||
@@ -116,64 +121,50 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
|
||||
|
||||
protected TreeNode<E> createNewNode(E e) {
|
||||
return new TreeNode<>(e);
|
||||
return new TreeNode<E>(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inorder traversal from the root
|
||||
*/
|
||||
@Override
|
||||
@Override /** Inorder traversal from the root*/
|
||||
public void inorder() {
|
||||
inorder(root);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inorder traversal from a subtree
|
||||
*/
|
||||
/** Inorder traversal from a subtree */
|
||||
protected void inorder(TreeNode<E> root) {
|
||||
if (root == null) return;
|
||||
inorder(root.left);
|
||||
System.out.print(root.element + " ");
|
||||
inorder(root.right);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post order traversal from the root
|
||||
*/
|
||||
@Override
|
||||
public void postOrder() {
|
||||
@Override /** Postorder traversal from the root */
|
||||
public void postorder() {
|
||||
postorder(root);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post order traversal from a subtree
|
||||
*/
|
||||
/** Postorder traversal from a subtree */
|
||||
protected void postorder(TreeNode<E> root) {
|
||||
if (root == null) return;
|
||||
postorder(root.left);
|
||||
postorder(root.right);
|
||||
System.out.print(root.element + " ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Preorder traversal from the root
|
||||
*/
|
||||
@Override
|
||||
@Override /** Preorder traversal from the root */
|
||||
public void preorder() {
|
||||
preorder(root);
|
||||
}
|
||||
|
||||
/**
|
||||
* Preorder traversal from a subtree
|
||||
*/
|
||||
/** Preorder traversal from a subtree */
|
||||
protected void preorder(TreeNode<E> root) {
|
||||
if (root == null) return;
|
||||
System.out.print(root.element + " ");
|
||||
preorder(root.left);
|
||||
preorder(root.right);
|
||||
}
|
||||
|
||||
/**
|
||||
* This inner class is static, because it does not access
|
||||
* any instance members defined in its outer class
|
||||
*/
|
||||
/** This inner class is static, because it does not access
|
||||
any instance members defined in its outer class */
|
||||
public static class TreeNode<E extends Comparable<E>> {
|
||||
protected E element;
|
||||
protected TreeNode<E> left;
|
||||
@@ -184,49 +175,41 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of nodes in the tree
|
||||
*/
|
||||
@Override
|
||||
@Override /** Get the number of nodes in the tree */
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root of the tree
|
||||
*/
|
||||
/** Returns the root of the tree */
|
||||
public TreeNode<E> getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a path from the root leading to the specified element
|
||||
*/
|
||||
public ArrayList<TreeNode<E>> path(E e) {
|
||||
ArrayList<TreeNode<E>> list =
|
||||
new ArrayList<>();
|
||||
/** Returns a path from the root leading to the specified element */
|
||||
public java.util.ArrayList<TreeNode<E>> path(E e) {
|
||||
java.util.ArrayList<TreeNode<E>> list =
|
||||
new java.util.ArrayList<TreeNode<E>>();
|
||||
TreeNode<E> current = root; // Start from the root
|
||||
|
||||
while (current != null) {
|
||||
list.add(current); // Add the node to the list
|
||||
if (e.compareTo(current.element) < 0) {
|
||||
current = current.left;
|
||||
} else if (e.compareTo(current.element) > 0) {
|
||||
}
|
||||
else if (e.compareTo(current.element) > 0) {
|
||||
current = current.right;
|
||||
} else
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
return list; // Return an array list of nodes
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an element from the binary tree.
|
||||
@Override /** Delete an element from the binary tree.
|
||||
* Return true if the element is deleted successfully
|
||||
* Return false if the element is not in the tree
|
||||
*/
|
||||
@Override
|
||||
public void delete(E e) {
|
||||
* Return false if the element is not in the tree */
|
||||
public boolean delete(E e) {
|
||||
// Locate the node to be deleted and also locate its parent node
|
||||
TreeNode<E> parent = null;
|
||||
TreeNode<E> current = root;
|
||||
@@ -234,28 +217,32 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
if (e.compareTo(current.element) < 0) {
|
||||
parent = current;
|
||||
current = current.left;
|
||||
} else if (e.compareTo(current.element) > 0) {
|
||||
}
|
||||
else if (e.compareTo(current.element) > 0) {
|
||||
parent = current;
|
||||
current = current.right;
|
||||
} else
|
||||
}
|
||||
else
|
||||
break; // Element is in the tree pointed at by current
|
||||
}
|
||||
|
||||
if (current == null)
|
||||
return; // Element is not in the tree
|
||||
return false; // Element is not in the tree
|
||||
|
||||
// Case 1: current has no left child
|
||||
if (current.left == null) {
|
||||
// Connect the parent with the right child of the current node
|
||||
if (parent == null) {
|
||||
root = current.right;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (e.compareTo(parent.element) < 0)
|
||||
parent.left = current.right;
|
||||
else
|
||||
parent.right = current.right;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
// Case 2: The current node has a left child
|
||||
// Locate the rightmost node in the left subtree of
|
||||
// the current node and also its parent
|
||||
@@ -274,17 +261,15 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
if (parentOfRightMost.right == rightMost)
|
||||
parentOfRightMost.right = rightMost.left;
|
||||
else
|
||||
// Special case: parentOfRightMost == current
|
||||
parentOfRightMost.left = rightMost.left;
|
||||
// Special case: parentOfRightMost == current
|
||||
parentOfRightMost.left = rightMost.left;
|
||||
}
|
||||
|
||||
size--;
|
||||
return true; // Element deleted successfully
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain an iterator. Use inorder.
|
||||
*/
|
||||
@Override
|
||||
@Override /** Obtain an iterator. Use inorder. */
|
||||
public java.util.Iterator<E> iterator() {
|
||||
return new InorderIterator();
|
||||
}
|
||||
@@ -292,51 +277,41 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
// Inner class InorderIterator
|
||||
private class InorderIterator implements java.util.Iterator<E> {
|
||||
// Store the elements in a list
|
||||
private final java.util.ArrayList<E> list =
|
||||
new java.util.ArrayList<>();
|
||||
private java.util.ArrayList<E> list =
|
||||
new java.util.ArrayList<E>();
|
||||
private int current = 0; // Point to the current element in list
|
||||
|
||||
public InorderIterator() {
|
||||
inorder(); // Traverse binary tree and store elements in list
|
||||
}
|
||||
|
||||
/**
|
||||
* Inorder traversal from the root
|
||||
*/
|
||||
/** Inorder traversal from the root*/
|
||||
private void inorder() {
|
||||
inorder(root);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inorder traversal from a subtree
|
||||
*/
|
||||
/** Inorder traversal from a subtree */
|
||||
private void inorder(TreeNode<E> root) {
|
||||
if (root == null) return;
|
||||
if (root == null)return;
|
||||
inorder(root.left);
|
||||
list.add(root.element);
|
||||
inorder(root.right);
|
||||
}
|
||||
|
||||
/**
|
||||
* More elements for traversing?
|
||||
*/
|
||||
@Override
|
||||
@Override /** More elements for traversing? */
|
||||
public boolean hasNext() {
|
||||
return current < list.size();
|
||||
if (current < list.size())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current element and move to the next
|
||||
*/
|
||||
@Override
|
||||
@Override /** Get the current element and move to the next */
|
||||
public E next() {
|
||||
return list.get(current++);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the current element
|
||||
*/
|
||||
@Override
|
||||
@Override /** Remove the current element */
|
||||
public void remove() {
|
||||
delete(list.get(current)); // Delete the current element
|
||||
list.clear(); // Clear the list
|
||||
@@ -344,24 +319,23 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all elements from the tree
|
||||
*/
|
||||
/** Remove all elements from the tree */
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder res = new StringBuilder();
|
||||
for (E e : this) {
|
||||
res.append(e.toString());
|
||||
}
|
||||
return "BST{" +
|
||||
"root=" + root +
|
||||
", size=" + size +
|
||||
", " + res +
|
||||
'}';
|
||||
}
|
||||
// if (tree == null) {
|
||||
// return false;
|
||||
// }
|
||||
// else if (e.compareTo(tree.element) > 0) {
|
||||
// return search(e, tree.right);
|
||||
// }
|
||||
// else if (e.compareTo(tree.element) < 0) {
|
||||
// return search(e, tree.left);
|
||||
// }
|
||||
// else {
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
}
|
||||
|
||||
@@ -6,17 +6,17 @@ public interface Tree<E> extends Iterable<E> {
|
||||
|
||||
/** Insert element o into the binary tree
|
||||
* Return true if the element is inserted successfully */
|
||||
public void insert(E e);
|
||||
public boolean insert(E e);
|
||||
|
||||
/** Delete the specified element from the tree
|
||||
* Return true if the element is deleted successfully */
|
||||
public void delete(E e);
|
||||
public boolean delete(E e);
|
||||
|
||||
/** Inorder traversal from the root*/
|
||||
public void inorder();
|
||||
|
||||
/** Postorder traversal from the root */
|
||||
public void postOrder();
|
||||
public void postorder();
|
||||
|
||||
/** Preorder traversal from the root */
|
||||
public void preorder();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import netwerkprog.game.server.SessionController;
|
||||
import netwerkprog.game.server.controllers.SessionController;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RestartSessionControllerTest {
|
||||
@@ -23,6 +23,7 @@ public class RestartSessionControllerTest {
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(sessionThread.getState());
|
||||
sessionThread = new Thread(sessionController);
|
||||
sessionThread.start();
|
||||
}
|
||||
|
||||