Compare commits
80 Commits
Connection
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
edd9d6c34f | ||
|
|
3cb7d2369b | ||
|
|
d5186ceb24 | ||
|
|
f812fedd11 | ||
|
|
4a332d1221 | ||
|
|
26419dc29a | ||
|
|
17df656725 | ||
|
|
0d22df0fab | ||
|
|
fdb80afba5 | ||
|
|
046cade8a0 | ||
|
|
6914bb891c | ||
|
|
50e6b7ca95 | ||
|
|
5fa138b43b | ||
|
|
298fe3af96 | ||
|
|
f2e5504c5c | ||
|
|
87d4627eca | ||
|
|
6ae2b5de51 | ||
|
|
784762991f | ||
|
|
d31a10971e | ||
|
|
9ed89bf642 | ||
|
|
749429b171 | ||
|
|
37cefd6d88 | ||
|
|
2283767c3d | ||
|
|
c9ba1bff30 | ||
|
|
3e316ccc17 | ||
|
|
3d5ad16eba | ||
|
|
a2a7095881 | ||
|
|
3df66c1789 | ||
|
|
7ce4cba2ff | ||
|
|
b80fe293e0 | ||
|
|
355d9757b4 | ||
|
|
e86a48d3cd | ||
|
|
4c23293c2c | ||
|
|
c5411217b8 | ||
|
|
133310a3e2 | ||
|
|
8344cbab0b | ||
|
|
6ac66d8311 | ||
|
|
fd6d91b7ed | ||
|
|
9337c44e20 | ||
|
|
80ebf342c6 | ||
|
|
e33aca517f | ||
|
|
30b343fc84 | ||
|
|
6d22fce2d1 | ||
|
|
e9f22850f7 | ||
|
|
3adf271cfe | ||
|
|
60f3edb656 | ||
|
|
63b1f66567 | ||
|
|
41b8544646 | ||
|
|
8b70287aa0 | ||
|
|
b35161f4de | ||
|
|
1a1d014f42 | ||
|
|
fd0c8ba765 | ||
|
|
360e2cb967 | ||
|
|
ff35e96312 | ||
|
|
79a0348a84 | ||
|
|
a3e79cf6c4 | ||
|
|
0af0ca02b6 | ||
|
|
d913138cda | ||
|
|
a971c3c44e | ||
|
|
2aeec735d4 | ||
|
|
e1029187f6 | ||
|
|
ddaf07df49 | ||
|
|
51558d1d4d | ||
|
|
60d478bb1e | ||
|
|
749f27d448 | ||
|
|
b287360012 | ||
|
|
fdf9b22d58 | ||
|
|
315d447a2a | ||
|
|
1be186e113 | ||
|
|
7abe8e22a1 | ||
|
|
f8074d5e64 | ||
|
|
8d99753f7c | ||
|
|
272a3c6966 | ||
|
|
ec0d0c3531 | ||
|
|
cb2ef909eb | ||
|
|
35a93d3ecb | ||
|
|
6a078550b2 | ||
|
|
24a03a2854 | ||
|
|
bdb7c6e5c5 | ||
|
|
bbfa59ab7f |
BIN
core/assets/dead.png
Normal file
BIN
core/assets/dead.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.3 KiB |
102
core/assets/font/custom.fnt
Normal file
102
core/assets/font/custom.fnt
Normal file
@@ -0,0 +1,102 @@
|
||||
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
|
||||
BIN
core/assets/font/custom.png
Normal file
BIN
core/assets/font/custom.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 26 KiB |
BIN
core/assets/grave.png
Normal file
BIN
core/assets/grave.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.9 KiB |
BIN
core/assets/hit.png
Normal file
BIN
core/assets/hit.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.6 KiB |
BIN
core/assets/sound/beat.mp3
Normal file
BIN
core/assets/sound/beat.mp3
Normal file
Binary file not shown.
BIN
core/assets/sound/earrape.mp3
Normal file
BIN
core/assets/sound/earrape.mp3
Normal file
Binary file not shown.
BIN
core/assets/sound/hit.mp3
Normal file
BIN
core/assets/sound/hit.mp3
Normal file
Binary file not shown.
@@ -1,7 +1,7 @@
|
||||
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;
|
||||
@@ -18,12 +18,18 @@ 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.DataCallback;
|
||||
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.util.game.GameCharacter;
|
||||
import netwerkprog.game.util.graphics.FrameRate;
|
||||
@@ -31,32 +37,17 @@ import netwerkprog.game.util.graphics.TextRenderer;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class MainGame extends ApplicationAdapter implements DataCallback {
|
||||
SpriteBatch batch;
|
||||
float screenWidth;
|
||||
float screenHeight;
|
||||
private FrameRate frameRate;
|
||||
private Thread client;
|
||||
private OrthographicCamera camera;
|
||||
private GameInputProcessor gameInputProcessor;
|
||||
private GameCharacter selectedCharacter;
|
||||
private Team team;
|
||||
private Team enemyTeam;
|
||||
private TextRenderer textRenderer;
|
||||
private BitmapFont font;
|
||||
private GlyphLayout layout;
|
||||
private GAMESTATE gamestate;
|
||||
private Faction chosenFaction;
|
||||
|
||||
private Map map;
|
||||
public MapRenderer mapRenderer;
|
||||
public AssetManager assets;
|
||||
|
||||
/**
|
||||
* Main game class
|
||||
*/
|
||||
public class MainGame extends Game implements ClientCallback {
|
||||
private static MainGame INSTANCE;
|
||||
|
||||
private MainGame() {
|
||||
}
|
||||
|
||||
/**
|
||||
* return the instance of the main game.
|
||||
*
|
||||
* @return the main game
|
||||
*/
|
||||
public static MainGame getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new MainGame();
|
||||
@@ -64,9 +55,41 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
|
||||
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();
|
||||
@@ -79,16 +102,22 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
|
||||
|
||||
String[] strings = new String[]{
|
||||
"#########################",
|
||||
"#xxxx #",
|
||||
"# x #",
|
||||
"# xxxx xxxxx #",
|
||||
"# xxxx xxxxx #",
|
||||
"# xxxx xx xx #",
|
||||
"# x xxxxx #",
|
||||
"# x xxxx #",
|
||||
"# x #",
|
||||
"# xxxxxx #",
|
||||
"# x #",
|
||||
"#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 #",
|
||||
"# x xxxx x x #",
|
||||
"#########################"
|
||||
};
|
||||
@@ -100,63 +129,81 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
|
||||
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);
|
||||
// this.tree.insert(new Hacker(,new BodySwap()));
|
||||
|
||||
|
||||
// playSong();
|
||||
|
||||
|
||||
connectToServer();
|
||||
// playSong();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 texture = assets.get("core/assets/characters.png", Texture.class);
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
GameCharacter temp = new Agent("Agent" + i, characters[11][0], new BodySwap("Test"));
|
||||
mapRenderer.getGameTiles()[3][i].visit(temp);
|
||||
if (chosenFaction == Faction.MEGACORPORATION) {
|
||||
this.team.addMember(temp);
|
||||
this.team.addMember(temp2);
|
||||
this.enemyTeam.addMember(temp);
|
||||
}
|
||||
}
|
||||
this.setSelectedCharacter(this.team.get(0));
|
||||
|
||||
this.setSelectedCharacter(this.team.get(0));
|
||||
}
|
||||
|
||||
|
||||
private void playSong() {
|
||||
// play music
|
||||
Music music = Gdx.audio.newMusic(Gdx.files.getFileHandle("core/assets/music.mp3", Files.FileType.Internal));
|
||||
Music music = Gdx.audio.newMusic(Gdx.files.getFileHandle("core/assets/sound/beat.mp3", Files.FileType.Internal));
|
||||
music.setVolume(.1f);
|
||||
music.play();
|
||||
music.setLooping(true);
|
||||
|
||||
connectToServer();
|
||||
}
|
||||
|
||||
|
||||
private void connectToServer() {
|
||||
client = new Thread(new Client("localhost", this));
|
||||
client = new Client("localhost", this);
|
||||
Thread t = new Thread(client);
|
||||
try {
|
||||
client.start();
|
||||
t.start();
|
||||
} catch (Exception e) {
|
||||
System.out.println("There was an error connecting : " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
@@ -165,19 +212,43 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
|
||||
if (this.gamestate == GAMESTATE.PLAYING) {
|
||||
update();
|
||||
// clear screen
|
||||
Gdx.gl.glClearColor(0, 0, 0, 1);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
clearRender();
|
||||
mapRenderer.render();
|
||||
frameRate.render();
|
||||
renderText();
|
||||
renderTurnText();
|
||||
} else if (this.gamestate == GAMESTATE.SELECTING_FACTION) {
|
||||
renderString("FACTION SELECT\nPress 1 for mega corporation, press 2 for hackers", Gdx.graphics.getWidth() / 2f, Gdx.graphics.getHeight() / 2f);
|
||||
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 = "FACION: " + chosenFaction;
|
||||
String text = "FACTION: " + chosenFaction;
|
||||
text += "\nSelected character: " + selectedCharacter.getName();
|
||||
text += "\nHealth: " + selectedCharacter.getHealth();
|
||||
layout.setText(font, text);
|
||||
@@ -194,13 +265,39 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
|
||||
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 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
|
||||
@@ -223,6 +320,8 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
|
||||
batch.dispose();
|
||||
textRenderer.dispose();
|
||||
assets.dispose();
|
||||
textRenderer.dispose();
|
||||
mapRenderer.dispose();
|
||||
}
|
||||
|
||||
public float getScreenWidth() {
|
||||
@@ -241,11 +340,19 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
|
||||
return map.getWidth();
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
client.disconnect();
|
||||
dispose();
|
||||
Gdx.app.exit();
|
||||
}
|
||||
|
||||
public void setSelectedCharacter(GameCharacter character) {
|
||||
this.selectedCharacter = character;
|
||||
GameTile characterTile = mapRenderer.getTile(character);
|
||||
Point pos = mapRenderer.getPos(characterTile);
|
||||
mapRenderer.setSurroundedTilesOfCurrentCharacter(pos.x, pos.y);
|
||||
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() {
|
||||
@@ -276,8 +383,118 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,5 +2,6 @@ package netwerkprog.game.client.game;
|
||||
|
||||
public enum GAMESTATE {
|
||||
PLAYING,
|
||||
SELECTING_FACTION
|
||||
SELECTING_FACTION,
|
||||
ENDED
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package netwerkprog.game.client.game.characters;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import netwerkprog.game.util.game.Faction;
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
|
||||
public class DevTest1 extends GameCharacter {
|
||||
public DevTest1() {
|
||||
super("DevTest1", Faction.HACKER, new TextureRegion());
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package netwerkprog.game.client.game.characters;
|
||||
|
||||
import netwerkprog.game.util.game.Faction;
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
|
||||
public class DevTest2 extends GameCharacter {
|
||||
public DevTest2() {
|
||||
super("DevTest2", Faction.MEGACORPORATION, null);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package netwerkprog.game.client.game.characters;
|
||||
|
||||
import netwerkprog.game.util.game.Faction;
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
|
||||
public class DevTest3 extends GameCharacter {
|
||||
public DevTest3() {
|
||||
super("DevTest3", Faction.AI, null);
|
||||
}
|
||||
}
|
||||
@@ -10,29 +10,54 @@ public class 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)) {
|
||||
@@ -42,6 +67,25 @@ public class Team {
|
||||
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");
|
||||
@@ -54,6 +98,20 @@ public class Team {
|
||||
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) {
|
||||
@@ -61,4 +119,11 @@ public class Team {
|
||||
}
|
||||
return dead >= this.members.getSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Team{" +
|
||||
"members=" + members +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,24 @@
|
||||
package netwerkprog.game.client.game.connections;
|
||||
|
||||
import netwerkprog.game.util.application.Controller;
|
||||
import netwerkprog.game.util.data.ConnectionData;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.DataCallback;
|
||||
import netwerkprog.game.util.data.connection.ConnectionData;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Client extends Controller {
|
||||
public class Client implements Runnable {
|
||||
private final int port;
|
||||
private final String hostname;
|
||||
private boolean isConnected;
|
||||
private final ClientCallback callback;
|
||||
private Socket socket;
|
||||
private Thread receiveThread;
|
||||
private DataCallback callback;
|
||||
private ObjectOutputStream outputStream;
|
||||
private Thread receiveThread;
|
||||
private boolean connecting;
|
||||
private boolean isConnected;
|
||||
|
||||
public Client(String hostname, DataCallback callback) {
|
||||
public Client(String hostname, ClientCallback callback) {
|
||||
this.port = Data.port();
|
||||
this.hostname = hostname;
|
||||
this.callback = callback;
|
||||
@@ -33,41 +30,43 @@ public class Client extends Controller {
|
||||
@Override
|
||||
public void run() {
|
||||
this.connect();
|
||||
this.receiveThread.start();
|
||||
try {
|
||||
if (this.receiveThread != null){
|
||||
this.receiveThread.start();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects the client to the server.
|
||||
*/
|
||||
public void connect() {
|
||||
System.out.println("[CLIENT] connecting to server on port " + this.port);
|
||||
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));
|
||||
this.receiveThread = new Thread(() -> receive(in));
|
||||
} catch (IOException e) {
|
||||
this.connecting = false;
|
||||
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());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void register(ObjectInputStream in) {
|
||||
while (connecting) {
|
||||
String username = "DEV";
|
||||
send(new ConnectionData("Connect", username));
|
||||
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")){
|
||||
if (connectionData.getAction().equals("Connect") && connectionData.getMessage().equals("Confirm")) {
|
||||
this.connecting = false;
|
||||
this.isConnected = true;
|
||||
}
|
||||
@@ -81,9 +80,10 @@ public class Client extends Controller {
|
||||
|
||||
/**
|
||||
* Sends a message to the server.
|
||||
*
|
||||
* @param data The message to send.
|
||||
*/
|
||||
public void send(Data data) {
|
||||
public void writeData(Data data) {
|
||||
try {
|
||||
this.outputStream.writeObject(data);
|
||||
} catch (IOException e) {
|
||||
@@ -93,6 +93,7 @@ public class Client extends Controller {
|
||||
|
||||
/**
|
||||
* Receives a message from the server.
|
||||
*
|
||||
* @param in The inputStream
|
||||
*/
|
||||
public void receive(ObjectInputStream in) {
|
||||
@@ -115,9 +116,7 @@ public class Client extends Controller {
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
send(new ConnectionData("Disconnect", "DEV"));
|
||||
|
||||
writeData(new ConnectionData("Disconnect", "Request"));
|
||||
try {
|
||||
this.socket.close();
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package netwerkprog.game.client.game.connections;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
public interface ClientCallback {
|
||||
void onDataReceived(Data data);
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package netwerkprog.game.client.game.logic;
|
||||
|
||||
import netwerkprog.game.util.application.Controller;
|
||||
|
||||
public class Logic extends Controller {
|
||||
|
||||
public Logic() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,10 @@ 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;
|
||||
|
||||
@@ -46,6 +48,7 @@ public class GameInputProcessor implements InputProcessor {
|
||||
|
||||
camera.zoom = MathUtils.clamp(camera.zoom, 1.5f, 1.8f);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public boolean isWPressed() {
|
||||
@@ -90,7 +93,6 @@ public class GameInputProcessor implements InputProcessor {
|
||||
|
||||
@Override
|
||||
public boolean keyUp(int keycode) {
|
||||
// System.out.println(camera.position.x + " , " + camera.position.y);
|
||||
if (mainGame.getGamestate() == GAMESTATE.PLAYING) {
|
||||
|
||||
if (keysList.contains(keycode)) {
|
||||
@@ -113,18 +115,22 @@ public class GameInputProcessor implements InputProcessor {
|
||||
|
||||
return true;
|
||||
}
|
||||
} else if (mainGame.getGamestate() == GAMESTATE.SELECTING_FACTION) {
|
||||
} else if (mainGame.getGamestate() == GAMESTATE.SELECTING_FACTION && mainGame.isOtherPlayerConnected()) {
|
||||
if (keycode == Input.Keys.NUM_1) {
|
||||
System.out.println("MEGA CORP");
|
||||
mainGame.setChosenFaction(Faction.MEGACORPORATION);
|
||||
mainGame.initCharacters();
|
||||
mainGame.setGamestate(GAMESTATE.PLAYING);
|
||||
mainGame.send(new TeamData(Faction.MEGACORPORATION, mainGame.getUsername()));
|
||||
mainGame.chooseMegaCorp();
|
||||
}
|
||||
if (keycode == Input.Keys.NUM_2) {
|
||||
System.out.println("HACKER");
|
||||
mainGame.setChosenFaction(Faction.HACKER);
|
||||
mainGame.initCharacters();
|
||||
mainGame.setGamestate(GAMESTATE.PLAYING);
|
||||
mainGame.send(new TeamData(Faction.HACKER, mainGame.getUsername()));
|
||||
mainGame.chooseHacker();
|
||||
}
|
||||
|
||||
} else if (mainGame.getGamestate() == GAMESTATE.ENDED) {
|
||||
if (keycode == Input.Keys.ESCAPE) {
|
||||
mainGame.exit();
|
||||
} else if (keycode == Input.Keys.ENTER) {
|
||||
mainGame.dispose();
|
||||
mainGame.init();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -141,33 +147,54 @@ 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) {
|
||||
// System.out.println(gameTile + " row: " + row + ", col: " + col);
|
||||
if (mainGame.hasCharacterSelected() && !gameTile.containsCharacter()) {
|
||||
// System.out.println(mainGame.getSelectedCharacter());
|
||||
if (gameTile.getSymbol() != '#' && mainGame.mapRenderer.getSurroundedTilesOfCurrentCharacter().contains(gameTile)) {
|
||||
removeCharacterFromTile(mainGame.getSelectedCharacter());
|
||||
gameTile.visit(mainGame.getSelectedCharacter());
|
||||
mainGame.mapRenderer.setSurroundedTilesOfCurrentCharacter(col, row);
|
||||
|
||||
// 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()) {
|
||||
mainGame.setSelectedCharacter(gameTile.getCharacter());
|
||||
mainGame.mapRenderer.setSurroundedTilesOfCurrentCharacter(col, row);
|
||||
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()) {
|
||||
mainGame.setSelectedCharacter(gameTile.getCharacter());
|
||||
mainGame.mapRenderer.setSurroundedTilesOfCurrentCharacter(col, row);
|
||||
if (!gameTile.getCharacter().isDead()) {
|
||||
mainGame.setSelectedCharacter(gameTile.getCharacter());
|
||||
mainGame.mapRenderer.setSurroundedTilesOfCurrentCharacter(col, row);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -178,18 +205,7 @@ 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,6 +20,8 @@ public class GameTile extends Rectangle {
|
||||
super.height = textureRegion.getRegionHeight();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public GameCharacter getCharacter() {
|
||||
return character;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,8 @@ public class MapRenderer implements Renderable {
|
||||
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;
|
||||
@@ -35,6 +36,7 @@ public class MapRenderer implements Renderable {
|
||||
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
|
||||
@@ -61,10 +63,15 @@ public class MapRenderer implements Renderable {
|
||||
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);
|
||||
TextureRegion[][] tileTextures = TextureRegion.split(texture, 32, 32);
|
||||
|
||||
@@ -72,8 +79,10 @@ public class MapRenderer implements Renderable {
|
||||
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;
|
||||
@@ -106,6 +115,9 @@ public class MapRenderer implements Renderable {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
/**
|
||||
* method that renders the whole map
|
||||
*/
|
||||
@Override
|
||||
public void render() {
|
||||
batch.begin();
|
||||
@@ -114,13 +126,22 @@ 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()) {
|
||||
batch.draw(cur.getCharacter().getTextureRegion(), cur.x, cur.y);
|
||||
if (cur.getCharacter().equals(mainGame.getSelectedCharacter())) {
|
||||
batch.draw(square, cur.x, cur.y);
|
||||
|
||||
//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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,10 +157,15 @@ public class MapRenderer implements Renderable {
|
||||
y = 0;
|
||||
}
|
||||
|
||||
public static int[][] directions = new int[][]{{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}};
|
||||
|
||||
public List<GameTile> setSurroundedTilesOfCurrentCharacter(int x, int y) {
|
||||
List<GameTile> res = new ArrayList<GameTile>();
|
||||
|
||||
/**
|
||||
* 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];
|
||||
@@ -149,9 +175,13 @@ public class MapRenderer implements Renderable {
|
||||
res.add(gameTiles[cy][cx]);
|
||||
}
|
||||
surroundedTilesOfCurrentCharacter = res;
|
||||
return 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) {
|
||||
@@ -164,7 +194,19 @@ public class MapRenderer implements Renderable {
|
||||
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++) {
|
||||
@@ -176,11 +218,44 @@ public class MapRenderer implements Renderable {
|
||||
return new Point(-1, -1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(double deltaTime) {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
@@ -188,10 +263,18 @@ 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;
|
||||
}
|
||||
|
||||
7
core/src/netwerkprog/game/server/DataCallback.java
Normal file
7
core/src/netwerkprog/game/server/DataCallback.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
public interface DataCallback {
|
||||
void onDataReceived(Data data, DataSource source);
|
||||
}
|
||||
8
core/src/netwerkprog/game/server/DataSource.java
Normal file
8
core/src/netwerkprog/game/server/DataSource.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
public interface DataSource {
|
||||
void writeData(Data data);
|
||||
String getName();
|
||||
}
|
||||
@@ -1,42 +1,15 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.server.controllers.DataController;
|
||||
import netwerkprog.game.server.controllers.SessionController;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Server {
|
||||
private SessionController sessionController;
|
||||
private DataController dataController;
|
||||
private Thread sessionThread;
|
||||
private HashMap<String, Thread> gameThreads;
|
||||
|
||||
public void start() {
|
||||
this.sessionController = new SessionController(this);
|
||||
this.dataController = new DataController();
|
||||
|
||||
this.gameThreads = new HashMap<>();
|
||||
SessionController sessionController = new SessionController();
|
||||
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();
|
||||
// }
|
||||
}
|
||||
|
||||
public DataController getDataController() {
|
||||
return dataController;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,37 +1,38 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.server.controllers.DataController;
|
||||
import netwerkprog.game.server.controllers.SessionController;
|
||||
import netwerkprog.game.util.data.ConnectionData;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.DataCallback;
|
||||
import netwerkprog.game.util.data.connection.ConnectionData;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class ServerClient implements Runnable {
|
||||
private ObjectInputStream in;
|
||||
private ObjectOutputStream out;
|
||||
public class ServerClient implements Runnable, DataSource {
|
||||
private final String name;
|
||||
private final SessionController server;
|
||||
private final DataCallback callback;
|
||||
private final ObjectInputStream in;
|
||||
private final ObjectOutputStream out;
|
||||
private boolean isConnected;
|
||||
|
||||
public ServerClient(String name, ObjectInputStream in, ObjectOutputStream out, SessionController server, DataController dataController) {
|
||||
public ServerClient(String name, SessionController server, DataCallback callback, ObjectInputStream in, ObjectOutputStream out) {
|
||||
this.name = name;
|
||||
this.server = server;
|
||||
this.callback = dataController;
|
||||
this.callback = callback;
|
||||
this.in = in;
|
||||
this.out = out;
|
||||
this.isConnected = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes data to the connected client.
|
||||
* @param data The data object to write.
|
||||
*/
|
||||
public void writeData(Data data) {
|
||||
try {
|
||||
this.out.writeObject(data);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("Connection suddenly terminated!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,17 +47,14 @@ public class ServerClient implements Runnable {
|
||||
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
||||
if (connectionData.getAction().equals("Disconnect")) {
|
||||
this.isConnected = false;
|
||||
//todo properly remove thread.
|
||||
this.server.disconnect(this);
|
||||
}
|
||||
} else {
|
||||
callback.onDataReceived((Data) this.in.readObject());
|
||||
callback.onDataReceived(data, this);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("[SERVERCLIENT] caught exception - " + e.getMessage());
|
||||
System.out.println("[SERVERCLIENT] terminating failing connection...");
|
||||
this.isConnected = false;
|
||||
System.out.println("[SERVERCLIENT] done!");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package netwerkprog.game.server.controllers;
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.server.Server;
|
||||
import netwerkprog.game.server.ServerClient;
|
||||
import netwerkprog.game.util.application.Controller;
|
||||
import netwerkprog.game.util.data.ConnectionData;
|
||||
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;
|
||||
@@ -12,21 +11,17 @@ 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 Server server;
|
||||
public class SessionController implements DataCallback, Runnable {
|
||||
private ServerSocket serverSocket;
|
||||
private final ArrayList<ServerClient> clients = new ArrayList<>();
|
||||
private final HashMap<String, Thread> clientThreads = new HashMap<>();
|
||||
private final ArrayList<ServerClient> clients;
|
||||
private boolean listening;
|
||||
|
||||
public SessionController(Server server) {
|
||||
this.server = server;
|
||||
public SessionController() {
|
||||
this.clients = new ArrayList<>();
|
||||
this.listening = true;
|
||||
}
|
||||
|
||||
@@ -47,7 +42,6 @@ public class SessionController extends Controller {
|
||||
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) {
|
||||
@@ -61,96 +55,35 @@ public class SessionController extends Controller {
|
||||
*/
|
||||
public void registerClient(Socket socket) {
|
||||
try {
|
||||
System.out.println("[SERVER] got new client on " + socket.getInetAddress().getHostAddress());
|
||||
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
String username = "";
|
||||
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")) {
|
||||
username = connectionData.getMessage();
|
||||
outputStream.writeObject(new ConnectionData("Connect", "Confirm"));
|
||||
registering = false;
|
||||
} else {
|
||||
//todo error messaging.
|
||||
}
|
||||
} else {
|
||||
//todo error messaging.
|
||||
}
|
||||
} else {
|
||||
//todo error messaging.
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("[SERVER] got username " + username);
|
||||
ServerClient serverClient = new ServerClient(username, inputStream, outputStream, this, server.getDataController());
|
||||
|
||||
username = "player" + (this.clients.size() + 1);
|
||||
ServerClient serverClient = new ServerClient(username, this, this, inputStream, outputStream);
|
||||
Thread t = new Thread(serverClient);
|
||||
t.start();
|
||||
|
||||
this.clientThreads.put(username,t);
|
||||
serverClient.writeData(new NameData(username));
|
||||
this.clients.add(serverClient);
|
||||
} catch (IOException | ClassNotFoundException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a server message to all connected clients.
|
||||
* @param data message.
|
||||
*/
|
||||
public void serverMessage(Data data) {
|
||||
for (ServerClient serverClient : clients) {
|
||||
serverClient.writeData(data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to a specific user.
|
||||
* @param name user.
|
||||
* @param data message.
|
||||
*/
|
||||
public void personalMessage(String name, Data data) {
|
||||
for (ServerClient serverClient : clients) {
|
||||
if (serverClient.getName().equals(name)) {
|
||||
serverClient.writeData(data);
|
||||
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.
|
||||
*/
|
||||
@@ -161,6 +94,22 @@ public class SessionController extends Controller {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("[SERVER] networking shutdown ");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package netwerkprog.game.server.controllers;
|
||||
|
||||
import netwerkprog.game.util.data.CharacterData;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.DataCallback;
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class DataController implements DataCallback {
|
||||
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.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataReceived(Data data) {
|
||||
switch (data.getType()) {
|
||||
case "Character" :
|
||||
if (data.getPayload() instanceof CharacterData) {
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package netwerkprog.game.util.application;
|
||||
|
||||
public abstract class Controller implements Runnable {
|
||||
|
||||
}
|
||||
@@ -6,36 +6,6 @@ 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
|
||||
@@ -61,13 +31,4 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package netwerkprog.game.util.application;
|
||||
|
||||
public interface Updatable {
|
||||
void update(double deltaTime);
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package netwerkprog.game.util.data;
|
||||
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class CharacterData extends Data implements Serializable {
|
||||
private final String name;
|
||||
private final GameCharacter character;
|
||||
|
||||
public CharacterData(String name, GameCharacter character) {
|
||||
super("Character");
|
||||
super.setPayload(this);
|
||||
this.name = name;
|
||||
this.character = character;
|
||||
}
|
||||
}
|
||||
@@ -7,26 +7,25 @@ public class Data implements Serializable {
|
||||
return 8000;
|
||||
}
|
||||
|
||||
private String objectType;
|
||||
private final String objectType;
|
||||
private Data payload;
|
||||
|
||||
public Data(String type) {
|
||||
this.objectType = type;
|
||||
}
|
||||
|
||||
public void setObjectType(String objectType) {
|
||||
this.objectType = objectType;
|
||||
}
|
||||
|
||||
public void setPayload(Data payload) {
|
||||
this.payload = payload;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return objectType;
|
||||
}
|
||||
|
||||
public Data getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Data{" +
|
||||
"objectType='" + objectType + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package netwerkprog.game.util.data;
|
||||
|
||||
public interface DataCallback {
|
||||
void onDataReceived(Data data);
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package netwerkprog.game.util.data;
|
||||
|
||||
public class Event {
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package netwerkprog.game.util.data;
|
||||
|
||||
public interface ParserCallback {
|
||||
void onDataReceived(String data);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
31
core/src/netwerkprog/game/util/data/character/MoveData.java
Normal file
31
core/src/netwerkprog/game/util/data/character/MoveData.java
Normal file
@@ -0,0 +1,31 @@
|
||||
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,4 +1,6 @@
|
||||
package netwerkprog.game.util.data;
|
||||
package netwerkprog.game.util.data.connection;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
17
core/src/netwerkprog/game/util/data/connection/NameData.java
Normal file
17
core/src/netwerkprog/game/util/data/connection/NameData.java
Normal file
@@ -0,0 +1,17 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
24
core/src/netwerkprog/game/util/data/connection/TeamData.java
Normal file
24
core/src/netwerkprog/game/util/data/connection/TeamData.java
Normal file
@@ -0,0 +1,24 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package netwerkprog.game.util.data.connection;
|
||||
|
||||
import netwerkprog.game.util.data.Data;
|
||||
|
||||
public class TurnData extends Data {
|
||||
public TurnData() {
|
||||
super("turn");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package netwerkprog.game.util.game;
|
||||
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.scenes.scene2d.Actor;
|
||||
import netwerkprog.game.client.game.map.GameTile;
|
||||
@@ -20,6 +19,8 @@ public abstract class GameCharacter extends Actor implements Comparable<GameChar
|
||||
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();
|
||||
@@ -29,6 +30,7 @@ public abstract class GameCharacter extends Actor implements Comparable<GameChar
|
||||
this.override = false;
|
||||
this.textureRegion = textureRegion;
|
||||
this.health = 100;
|
||||
this.damageAnimation = false;
|
||||
this.allowedToMove = new ArrayList<>();
|
||||
}
|
||||
|
||||
@@ -54,12 +56,19 @@ public abstract class GameCharacter extends Actor implements Comparable<GameChar
|
||||
|
||||
public void heal(int amount) {
|
||||
this.health += amount;
|
||||
if (this.health > 100) this.health = 100;
|
||||
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;
|
||||
if (this.health < 0) {
|
||||
this.health = 0;
|
||||
}
|
||||
|
||||
this.damageAnimation = true;
|
||||
}
|
||||
|
||||
public boolean isDead() {
|
||||
@@ -90,6 +99,17 @@ 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);
|
||||
@@ -97,7 +117,7 @@ public abstract class GameCharacter extends Actor implements Comparable<GameChar
|
||||
|
||||
@Override
|
||||
public int compareTo(GameCharacter o) {
|
||||
return this.health - o.health;
|
||||
return (this.health - o.health) + this.name.compareTo(o.name) + this.faction.compareTo(o.faction);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -121,4 +141,12 @@ public abstract class GameCharacter extends Actor implements Comparable<GameChar
|
||||
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 BitmapFont font;
|
||||
private SpriteBatch batch;
|
||||
private final BitmapFont font;
|
||||
private final SpriteBatch batch;
|
||||
private OrthographicCamera cam;
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package netwerkprog.game.util.graphics;
|
||||
|
||||
import netwerkprog.game.util.application.Updatable;
|
||||
|
||||
public interface Renderable extends Updatable {
|
||||
public interface Renderable {
|
||||
void render();
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.utils.Disposable;
|
||||
|
||||
public class TextRenderer implements Disposable {
|
||||
private BitmapFont font;
|
||||
private SpriteBatch batch;
|
||||
private final BitmapFont font;
|
||||
private final SpriteBatch batch;
|
||||
private OrthographicCamera cam;
|
||||
|
||||
public TextRenderer() {
|
||||
@@ -17,10 +17,15 @@ public class TextRenderer implements Disposable {
|
||||
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
font.dispose();
|
||||
batch.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) {
|
||||
|
||||
@@ -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 /** Postorder traversal from the root */
|
||||
public void postorder() {
|
||||
@Override /* Post order 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,115 +1,112 @@
|
||||
package netwerkprog.game.util.tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
protected TreeNode<E> root;
|
||||
protected int size = 0;
|
||||
|
||||
// Helper methode
|
||||
public int sum () {
|
||||
|
||||
public int sum() {
|
||||
return this.sum(this.getRoot());
|
||||
}
|
||||
|
||||
// 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...
|
||||
public int sum(TreeNode<E> node) {
|
||||
|
||||
if (node == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int nodeValue = (Integer) node.element; // Tip, omdat E nog onbekend is doen we het zo (niet helemaal netjes)
|
||||
int nodeValue = (Integer) node.element;
|
||||
return sum(node.left) + sum(node.right);
|
||||
}
|
||||
|
||||
// Helper methode
|
||||
public int totalLeaves () {
|
||||
|
||||
public int totalLeaves() {
|
||||
return this.totalLeaves(this.getRoot());
|
||||
}
|
||||
// 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);
|
||||
|
||||
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 (int i = 0; i < objects.length; i++)
|
||||
insert(objects[i]);
|
||||
for (E object : objects) insert(object);
|
||||
}
|
||||
|
||||
@Override /** Returns true if the element is in the tree */
|
||||
/**
|
||||
* Returns true if the element is in the tree
|
||||
*/
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override /** Insert element o into the binary tree
|
||||
* Return true if the element is inserted successfully */
|
||||
public boolean insert(E e) {
|
||||
/**
|
||||
* Insert element o into the binary tree
|
||||
* Return true if the element is inserted successfully
|
||||
*/
|
||||
@Override
|
||||
public void insert(E e) {
|
||||
if (root == null) {
|
||||
root = createNewNode(e); // Create a new root
|
||||
size++;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return insert(e, root);
|
||||
} else {
|
||||
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++;
|
||||
@@ -119,50 +116,64 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
|
||||
|
||||
protected TreeNode<E> createNewNode(E e) {
|
||||
return new TreeNode<E>(e);
|
||||
return new TreeNode<>(e);
|
||||
}
|
||||
|
||||
@Override /** Inorder traversal from the root*/
|
||||
/**
|
||||
* Inorder traversal from the root
|
||||
*/
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
@Override /** Postorder traversal from the root */
|
||||
public void postorder() {
|
||||
/**
|
||||
* Post order traversal from the root
|
||||
*/
|
||||
@Override
|
||||
public void postOrder() {
|
||||
postorder(root);
|
||||
}
|
||||
|
||||
/** Postorder traversal from a subtree */
|
||||
/**
|
||||
* Post order 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 + " ");
|
||||
}
|
||||
|
||||
@Override /** Preorder traversal from the root */
|
||||
/**
|
||||
* Preorder traversal from the root
|
||||
*/
|
||||
@Override
|
||||
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;
|
||||
@@ -173,41 +184,49 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override /** Get the number of nodes in the tree */
|
||||
/**
|
||||
* Get the number of nodes in the tree
|
||||
*/
|
||||
@Override
|
||||
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 java.util.ArrayList<TreeNode<E>> path(E e) {
|
||||
java.util.ArrayList<TreeNode<E>> list =
|
||||
new java.util.ArrayList<TreeNode<E>>();
|
||||
/**
|
||||
* Returns a path from the root leading to the specified element
|
||||
*/
|
||||
public ArrayList<TreeNode<E>> path(E e) {
|
||||
ArrayList<TreeNode<E>> list =
|
||||
new ArrayList<>();
|
||||
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
|
||||
}
|
||||
|
||||
@Override /** Delete an element from the binary tree.
|
||||
/**
|
||||
* 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 */
|
||||
public boolean delete(E e) {
|
||||
* Return false if the element is not in the tree
|
||||
*/
|
||||
@Override
|
||||
public void delete(E e) {
|
||||
// Locate the node to be deleted and also locate its parent node
|
||||
TreeNode<E> parent = null;
|
||||
TreeNode<E> current = root;
|
||||
@@ -215,32 +234,28 @@ 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 false; // Element is not in the tree
|
||||
return; // 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
|
||||
@@ -259,15 +274,17 @@ 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
|
||||
}
|
||||
|
||||
@Override /** Obtain an iterator. Use inorder. */
|
||||
/**
|
||||
* Obtain an iterator. Use inorder.
|
||||
*/
|
||||
@Override
|
||||
public java.util.Iterator<E> iterator() {
|
||||
return new InorderIterator();
|
||||
}
|
||||
@@ -275,41 +292,51 @@ 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 java.util.ArrayList<E> list =
|
||||
new java.util.ArrayList<E>();
|
||||
private final java.util.ArrayList<E> list =
|
||||
new java.util.ArrayList<>();
|
||||
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);
|
||||
}
|
||||
|
||||
@Override /** More elements for traversing? */
|
||||
/**
|
||||
* More elements for traversing?
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (current < list.size())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
return current < list.size();
|
||||
}
|
||||
|
||||
@Override /** Get the current element and move to the next */
|
||||
/**
|
||||
* Get the current element and move to the next
|
||||
*/
|
||||
@Override
|
||||
public E next() {
|
||||
return list.get(current++);
|
||||
}
|
||||
|
||||
@Override /** Remove the current element */
|
||||
/**
|
||||
* Remove the current element
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
delete(list.get(current)); // Delete the current element
|
||||
list.clear(); // Clear the list
|
||||
@@ -317,23 +344,24 @@ 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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
// }
|
||||
//
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder res = new StringBuilder();
|
||||
for (E e : this) {
|
||||
res.append(e.toString());
|
||||
}
|
||||
return "BST{" +
|
||||
"root=" + root +
|
||||
", size=" + size +
|
||||
", " + res +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 boolean insert(E e);
|
||||
public void insert(E e);
|
||||
|
||||
/** Delete the specified element from the tree
|
||||
* Return true if the element is deleted successfully */
|
||||
public boolean delete(E e);
|
||||
public void 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.controllers.SessionController;
|
||||
import netwerkprog.game.server.SessionController;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RestartSessionControllerTest {
|
||||
@@ -8,7 +8,7 @@ public class RestartSessionControllerTest {
|
||||
SessionController sessionController;
|
||||
Thread sessionThread;
|
||||
|
||||
sessionController = new SessionController(null);
|
||||
sessionController = new SessionController();
|
||||
sessionThread = new Thread(sessionController);
|
||||
|
||||
sessionThread.start();
|
||||
@@ -23,7 +23,6 @@ public class RestartSessionControllerTest {
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(sessionThread.getState());
|
||||
sessionThread = new Thread(sessionController);
|
||||
sessionThread.start();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user