Skip to content

Commit 62d26f0

Browse files
committed
#22 Refactored Player and Deprecated Multiplayer Code
1 parent a306731 commit 62d26f0

File tree

8 files changed

+141
-188
lines changed

8 files changed

+141
-188
lines changed

src/com/redomar/game/entities/Player.java

Lines changed: 55 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -15,98 +15,83 @@
1515

1616
public class Player extends Mob {
1717

18-
private static Name customeName = new Name();
19-
public static String guestPlayerName = customeName.setName("Player ");
20-
private static double speed = 1;
21-
private static int[] collisionBoders = {-2, 8, 0, 7};
22-
private InputHandler input;
23-
private Swim swim;
24-
private int colour, shirtCol, faceCol;
18+
private static final int[] COLLISION_BORDERS = {-2, 8, 0, 7};
19+
private static final String guestPlayerName = new Name().setName("Player ");
20+
private static final double speed = 1;
21+
private final InputHandler inputHandler;
22+
private final int shirtColour;
23+
private final int faceColour;
24+
private int colour;
2525
private int tickCount = 0;
26+
private Swim swim;
2627
private String userName;
2728
private boolean[] swimType;
28-
private int[] swimColour;
29-
private int fireRate = 0;
30-
31-
// Need to add a class for constants
32-
private final int fontCharSize = 8;
29+
private int fireRate;
3330
// "Cache" the division for the username length, no need for 60 divisions per second here.
3431
private int nameOffset = 0;
3532

36-
public Player(LevelHandler level, int x, int y, InputHandler input,
37-
String userName, int shirtCol, int faceCol) {
38-
super(level, "Player", x, y, speed, collisionBoders);
39-
this.input = input;
33+
public Player(LevelHandler level, int x, int y, InputHandler inputHandler, String userName, int shirtColour, int faceColour) {
34+
super(level, "Player", x, y, speed, COLLISION_BORDERS);
35+
this.inputHandler = inputHandler;
4036
this.userName = userName;
41-
this.faceCol = faceCol;
42-
this.shirtCol = shirtCol;
43-
this.colour = Colours.get(-1, 111, shirtCol, faceCol);
37+
this.faceColour = faceColour;
38+
this.shirtColour = shirtColour;
39+
this.colour = Colours.get(-1, 111, shirtColour, faceColour);
4440
fireRate = Small.FIRE_RATE;
4541
}
4642

47-
public static double getSpeed() {
48-
return speed;
49-
}
50-
51-
public static void setSpeed(double speed) {
52-
Player.speed = speed;
53-
}
54-
5543
public void tick() {
5644
double xa = 0;
5745
double ya = 0;
5846

59-
if (input != null) {
60-
if (input.getUP_KEY().isPressed() && input.isIgnoreInput() == false) {
47+
if (inputHandler != null) {
48+
if (inputHandler.getUP_KEY().isPressed() && !inputHandler.isIgnoreInput()) {
6149
ya -= speed;
6250
}
63-
if (input.getDOWN_KEY().isPressed() && input.isIgnoreInput() == false) {
51+
if (inputHandler.getDOWN_KEY().isPressed() && !inputHandler.isIgnoreInput()) {
6452
ya += speed;
6553
}
66-
if (input.getLEFT_KEY().isPressed() && input.isIgnoreInput() == false) {
54+
if (inputHandler.getLEFT_KEY().isPressed() && !inputHandler.isIgnoreInput()) {
6755
xa -= speed;
6856
}
69-
if (input.getRIGHT_KEY().isPressed() && input.isIgnoreInput() == false) {
57+
if (inputHandler.getRIGHT_KEY().isPressed() && !inputHandler.isIgnoreInput()) {
7058
xa += speed;
7159
}
7260
}
7361

74-
if(fireRate > 0) fireRate--;
62+
if (fireRate > 0) fireRate--;
7563

76-
if (Game.getMouse().getButton() == 1 || Game.getMouse().getButton() == 3){
77-
if(fireRate <= 0){
78-
if(Game.getMouse().getButton()== 1){
64+
if (Game.getMouse().getButton() == 1 || Game.getMouse().getButton() == 3) {
65+
if (fireRate <= 0) {
66+
if (Game.getMouse().getButton() == 1) {
7967
fireRate = Small.FIRE_RATE;
80-
}else if(Game.getMouse().getButton() == 3){
68+
} else if (Game.getMouse().getButton() == 3) {
8169
fireRate = Medium.FIRE_RATE;
8270
}
83-
if(!swim.isActive(swimType)){
84-
double dx = Game.getMouse().getX() - 480/2;
85-
double dy = Game.getMouse().getY() - 320/2;
71+
if (!swim.isActive(swimType)) {
72+
double dx = Game.getMouse().getX() - 480 / 2d;
73+
double dy = Game.getMouse().getY() - 320 / 2d;
8674
double dir = Math.atan2(dy, dx);
8775
shoot(x, y, dir, Game.getMouse().getButton(), false);
8876
}
8977
}
9078
}
9179

92-
for (int i = 0; i < projectiles.size(); i++) {
93-
Projectile p = projectiles.get(i);
94-
if(p.isRemoved()){
95-
projectiles.remove(i);
80+
for (Projectile p : projectiles) {
81+
if (p.isRemoved()) {
82+
p.remove();
9683
Game.getLevel().removeProjectileEntities(p);
9784
}
9885
}
9986

10087
if (xa != 0 || ya != 0) {
10188
move(xa, ya);
10289
isMoving = true;
103-
Game.getGame();
104-
10590
} else {
10691
isMoving = false;
10792
}
10893

109-
setSwim(new Swim(level, (int) getX(), (int) getY()));
94+
swim = new Swim(level, (int) getX(), (int) getY());
11095
swimType = swim.swimming(isSwimming, isMagma, isMuddy);
11196
isSwimming = swimType[0];
11297
isMagma = swimType[1];
@@ -130,15 +115,15 @@ public void render(Screen screen) {
130115

131116
if (movingDir == 1) {
132117
xTile += 2;
133-
if (!isMoving || swim.isActive(swimType)){
118+
if (!isMoving || swim.isActive(swimType)) {
134119
yTile -= 2;
135120
}
136121
} else if (movingDir == 0 && !isMoving || movingDir == 0 && swim.isActive(swimType)) {
137122
yTile -= 2;
138123
} else if (movingDir > 1) {
139124
xTile += 4 + ((numSteps >> walkingSpeed) & 1) * 2;
140125
flipTop = (movingDir - 1) % 2;
141-
if(!isMoving){
126+
if (!isMoving) {
142127
xTile = 4;
143128
}
144129
}
@@ -151,44 +136,36 @@ public void render(Screen screen) {
151136
Game.setChangeLevel(true);
152137
}
153138

154-
if(isSwimming || isMagma || isMuddy){
155-
swimColour = swim.waveCols(isSwimming, isMagma, isMuddy);
139+
if (isSwimming || isMagma || isMuddy) {
140+
int[] swimColour = swim.waveCols(isSwimming, isMagma, isMuddy);
156141

157-
int waterColour = 0;
142+
int waterColour;
158143
yOffset += 4;
159144

160-
colour = Colours.get(-1, 111, -1, faceCol);
145+
colour = Colours.get(-1, 111, -1, faceColour);
161146

162147
if (tickCount % 60 < 15) {
163148
waterColour = Colours.get(-1, -1, swimColour[0], -1);
164-
} else if (15 <= tickCount % 60 && tickCount % 60 < 30) {
149+
} else if (tickCount % 60 < 30) {
165150
yOffset--;
166151
waterColour = Colours.get(-1, swimColour[1], swimColour[2], -1);
167-
} else if (30 <= tickCount % 60 && tickCount % 60 < 45) {
152+
} else if (tickCount % 60 < 45) {
168153
waterColour = Colours.get(-1, swimColour[2], -1, swimColour[1]);
169154
} else {
170155
yOffset--;
171156
waterColour = Colours.get(-1, -1, swimColour[1], swimColour[2]);
172157
}
173158

174-
screen.render(xOffset, yOffset + 3, 31 + 31 * 32, waterColour,
175-
0x00, 1);
176-
screen.render(xOffset + 8, yOffset + 3, 31 + 31 * 32, waterColour,
177-
0x01, 1);
159+
screen.render(xOffset, yOffset + 3, 31 + 31 * 32, waterColour, 0x00, 1);
160+
screen.render(xOffset + 8, yOffset + 3, 31 + 31 * 32, waterColour, 0x01, 1);
178161
}
179162

180-
screen.render((xOffset + (modifier * flipTop)), yOffset,
181-
(xTile + yTile * 32), colour, flipTop, scale);
182-
screen.render((xOffset + modifier - (modifier * flipTop)), yOffset,
183-
((xTile + 1) + yTile * 32), colour, flipTop, scale);
163+
screen.render((xOffset + (modifier * flipTop)), yOffset, (xTile + yTile * 32), colour, flipTop, scale);
164+
screen.render((xOffset + modifier - (modifier * flipTop)), yOffset, ((xTile + 1) + yTile * 32), colour, flipTop, scale);
184165
if (!isSwimming && !isMagma && !isMuddy) {
185-
screen.render((xOffset + (modifier * flipBottom)),
186-
(yOffset + modifier), (xTile + (yTile + 1) * 32), colour,
187-
flipBottom, scale);
188-
screen.render((xOffset + modifier - (modifier * flipBottom)),
189-
(yOffset + modifier), ((xTile + 1) + (yTile + 1) * 32),
190-
colour, flipBottom, scale);
191-
colour = Colours.get(-1, 111, shirtCol, faceCol);
166+
screen.render((xOffset + (modifier * flipBottom)), (yOffset + modifier), (xTile + (yTile + 1) * 32), colour, flipBottom, scale);
167+
screen.render((xOffset + modifier - (modifier * flipBottom)), (yOffset + modifier), ((xTile + 1) + (yTile + 1) * 32), colour, flipBottom, scale);
168+
colour = Colours.get(-1, 111, shirtColour, faceColour);
192169
}
193170

194171
if (userName != null) {
@@ -198,11 +175,7 @@ public void render(Screen screen) {
198175
* -posmicanomaly
199176
*/
200177

201-
Font.render(userName,
202-
screen,
203-
(int)x - nameOffset,
204-
yOffset - 10,
205-
Colours.get(-1, -1, -1, 111), 1);
178+
Font.render(userName, screen, (int) x - nameOffset, yOffset - 10, Colours.get(-1, -1, -1, 111), 1);
206179

207180
}
208181
}
@@ -219,6 +192,8 @@ public void setUsername(String name) {
219192
}
220193

221194
public String getSanitisedUsername() {
195+
// Need to add a class for constants
196+
int fontCharSize = 8;
222197
if (this.getUsername() == null || this.userName.isEmpty()) {
223198
setUsername(guestPlayerName);
224199

@@ -227,20 +202,11 @@ public String getSanitisedUsername() {
227202
nameOffset = (userName.length() / 2) * fontCharSize - offsetUnit;
228203

229204
return guestPlayerName;
230-
} else
231-
if(nameOffset == 0){
232-
int offsetUnit = ((userName.length() & 1) == 0 ? fontCharSize / 2 : 0);
233-
nameOffset = (userName.length() / 2) * fontCharSize - offsetUnit;
234-
}
235-
return this.getUsername();
236-
}
237-
238-
public Swim getSwim() {
239-
return swim;
240-
}
241-
242-
public void setSwim(Swim swim) {
243-
this.swim = swim;
205+
} else if (nameOffset == 0) {
206+
int offsetUnit = ((userName.length() & 1) == 0 ? fontCharSize / 2 : 0);
207+
nameOffset = (userName.length() / 2) * fontCharSize - offsetUnit;
208+
}
209+
return this.getUsername();
244210
}
245211

246212
}

src/com/redomar/game/entities/PlayerMP.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@
55

66
import java.net.InetAddress;
77

8+
@Deprecated
89
public class PlayerMP extends Player {
910

10-
public InetAddress ipAddess;
11+
public InetAddress ipAddress;
1112
public int port;
1213

13-
public PlayerMP(LevelHandler level, int x, int y, InputHandler input,
14-
String userName, InetAddress ipAddress, int port, int shirtCol, int faceCol) {
14+
public PlayerMP(LevelHandler level, int x, int y, InputHandler input, String userName, InetAddress ipAddress, int port, int shirtCol, int faceCol) {
1515
super(level, x, y, input, userName, shirtCol, faceCol);
16-
this.ipAddess = ipAddress;
16+
this.ipAddress = ipAddress;
1717
this.port = port;
1818
}
1919

20-
public PlayerMP(LevelHandler level, int x, int y, String userName,
21-
InetAddress ipAddress, int port, int shirtCol, int faceCol) {
20+
public PlayerMP(LevelHandler level, int x, int y, String userName, InetAddress ipAddress, int port, int shirtCol, int faceCol) {
2221
super(level, x, y, null, userName, shirtCol, faceCol);
23-
this.ipAddess = ipAddress;
22+
this.ipAddress = ipAddress;
2423
this.port = port;
2524
}
2625

src/com/redomar/game/net/GameClient.java

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@
1313
import java.io.IOException;
1414
import java.net.*;
1515

16+
@Deprecated
1617
public class GameClient extends Thread {
1718

19+
private final Printing print = new Printing();
1820
private InetAddress ipAddress;
1921
private DatagramSocket socket;
2022
private Game game;
21-
private Printing print = new Printing();
2223

2324
public GameClient(Game game, String ipAddress) {
2425
this.setGame(game);
2526
try {
2627
this.socket = new DatagramSocket();
2728
this.ipAddress = InetAddress.getByName(ipAddress);
28-
} catch (SocketException e) {
29-
e.printStackTrace();
30-
} catch (UnknownHostException e) {
29+
} catch (SocketException | UnknownHostException e) {
3130
e.printStackTrace();
3231
}
3332
}
@@ -40,17 +39,17 @@ public void run() {
4039
socket.receive(packet);
4140
} catch (IOException e) {
4241
e.printStackTrace();
42+
break;
4343
}
44-
this.parsePacket(packet.getData(), packet.getAddress(),
45-
packet.getPort());
46-
// System.out.println("SERVER > "+new String(packet.getData()));
44+
this.parsePacket(packet.getData(), packet.getAddress(), packet.getPort());
45+
System.out.println("SERVER > " + new String(packet.getData()));
4746
}
4847
}
4948

5049
private void parsePacket(byte[] data, InetAddress address, int port) {
5150
String message = new String(data).trim();
5251
PacketTypes type = Packet.lookupPacket(message.substring(0, 2));
53-
Packet packet = null;
52+
Packet packet;
5453
switch (type) {
5554
default:
5655
case INVALID:
@@ -61,11 +60,8 @@ private void parsePacket(byte[] data, InetAddress address, int port) {
6160
break;
6261
case DISCONNECT:
6362
packet = new Packet01Disconnect(data);
64-
print.print("[" + address.getHostAddress() + ":" + port
65-
+ "] " + ((Packet01Disconnect) packet).getUsername()
66-
+ " has disconnected...", PrintTypes.NETWORK);
67-
Game.getLevel().removeEntity(
68-
((Packet01Disconnect) packet).getUsername());
63+
print.print("[" + address.getHostAddress() + ":" + port + "] " + ((Packet01Disconnect) packet).getUsername() + " has disconnected...", PrintTypes.NETWORK);
64+
Game.getLevel().removeEntity(((Packet01Disconnect) packet).getUsername());
6965
break;
7066
case MOVE:
7167
packet = new Packet02Move(data);
@@ -75,22 +71,17 @@ private void parsePacket(byte[] data, InetAddress address, int port) {
7571
}
7672

7773
private void handleLogin(Packet00Login packet, InetAddress address, int port) {
78-
print.print("[" + address.getHostAddress() + ":" + port + "] "
79-
+ packet.getUsername() + " has joined...", PrintTypes.NETWORK);
80-
PlayerMP player = new PlayerMP(Game.getLevel(), packet.getX(),
81-
packet.getY(), packet.getUsername(), address, port, Game.getShirtCol(), Game.getFaceCol());
74+
print.print("[" + address.getHostAddress() + ":" + port + "] " + packet.getUsername() + " has joined...", PrintTypes.NETWORK);
75+
PlayerMP player = new PlayerMP(Game.getLevel(), packet.getX(), packet.getY(), packet.getUsername(), address, port, Game.getShirtCol(), Game.getFaceCol());
8276
Game.getLevel().addEntity(player);
8377
}
8478

8579
private void handleMove(Packet02Move packet) {
86-
Game.getLevel().movePlayer(packet.getUsername(), packet.getX(),
87-
packet.getY(), packet.getNumSteps(), packet.isMoving(),
88-
packet.getMovingDir());
80+
Game.getLevel().movePlayer(packet.getUsername(), packet.getX(), packet.getY(), packet.getNumSteps(), packet.isMoving(), packet.getMovingDir());
8981
}
9082

9183
public void sendData(byte[] data) {
92-
DatagramPacket packet = new DatagramPacket(data, data.length,
93-
ipAddress, 1331);
84+
DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, 1331);
9485
try {
9586
socket.send(packet);
9687
} catch (IOException e) {

0 commit comments

Comments
 (0)