Skip to content

Commit 9f699c9

Browse files
committed
Merge commit '3914bc4f0935b9385c7fcd32a44e04f5fcaa9bcc' into Devaside
2 parents ccf9515 + 3914bc4 commit 9f699c9

File tree

7 files changed

+103
-1
lines changed

7 files changed

+103
-1
lines changed

src/com/redomar/game/Game.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class Game extends Canvas implements Runnable {
2929
public static final int HEIGHT = (WIDTH / 3 * 2);
3030
public static final int SCALE = 3;
3131
public static final String NAME = "Game";
32+
public static Game game;
3233

3334
private JFrame frame;
3435

@@ -67,6 +68,7 @@ public Game() {
6768
}
6869

6970
public void init() {
71+
game = this;
7072
int index = 0;
7173
for (int r = 0; r < 6; r++) {
7274
for (int g = 0; g < 6; g++) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.redomar.game.entities;
22

3+
import com.redomar.game.Game;
34
import com.redomar.game.InputHandler;
45
import com.redomar.game.gfx.Colours;
56
import com.redomar.game.gfx.Screen;
67
import com.redomar.game.level.LevelHandler;
78
import com.redomar.game.lib.Font;
9+
import com.redomar.game.net.packets.Packet02Move;
810

911
public class Player extends Mob {
1012

@@ -42,6 +44,10 @@ public void tick() {
4244
if (xa != 0 || ya != 0) {
4345
move(xa, ya);
4446
isMoving = true;
47+
48+
Packet02Move packet = new Packet02Move(this.getUsername(), this.x, this.y);
49+
packet.writeData(Game.game.getSocketClient());
50+
4551
} else {
4652
isMoving = false;
4753
}

src/com/redomar/game/level/LevelHandler.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,22 @@ public void removeEntity(String username) {
156156
}
157157
this.entities.remove(index);
158158
}
159+
160+
private int getPlayerMPIndex(String username){
161+
int index = 0;
162+
for(Entity e : entities){
163+
if(e instanceof PlayerMP && ((PlayerMP)e).getUsername().equalsIgnoreCase(username)){
164+
break;
165+
}
166+
index++;
167+
}
168+
return index;
169+
}
170+
171+
public void movePlayer(String username, int x, int y){
172+
int index = getPlayerMPIndex(username);
173+
this.entities.get(index).x = x;
174+
this.entities.get(index).y = y;
175+
}
159176

160177
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.redomar.game.net.packets.Packet00Login;
1414
import com.redomar.game.net.packets.Packet01Disconnect;
1515
import com.redomar.game.net.packets.Packet.PacketTypes;
16+
import com.redomar.game.net.packets.Packet02Move;
1617

1718
public class GameClient extends Thread {
1819

@@ -71,9 +72,17 @@ private void parsePacket(byte[] data, InetAddress address, int port) {
7172
+ " has disconnected...");
7273
game.getLevel().removeEntity(((Packet01Disconnect)packet).getUsername());
7374
break;
75+
case MOVE:
76+
packet = new Packet02Move(data);
77+
this.handlePacket((Packet02Move)packet);
78+
break;
7479
}
7580
}
7681

82+
private void handlePacket(Packet02Move packet) {
83+
this.game.level.movePlayer(packet.getUsername(), packet.getX(), packet.getY());
84+
}
85+
7786
public void sendData(byte[] data) {
7887
DatagramPacket packet = new DatagramPacket(data, data.length,
7988
ipAddress, 1331);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.redomar.game.net.packets.Packet00Login;
1515
import com.redomar.game.net.packets.Packet.PacketTypes;
1616
import com.redomar.game.net.packets.Packet01Disconnect;
17+
import com.redomar.game.net.packets.Packet02Move;
1718

1819
public class GameServer extends Thread {
1920

@@ -76,6 +77,21 @@ private void parsePacket(byte[] data, InetAddress address, int port) {
7677
+ " has disconnected...");
7778
this.removeConnection((Packet01Disconnect) packet);
7879
break;
80+
case MOVE:
81+
packet = new Packet02Move(data);
82+
System.out.println(((Packet02Move) packet).getUsername()
83+
+ " has moved to " + ((Packet02Move) packet).getX() + ", "
84+
+ ((Packet02Move) packet).getY());
85+
this.handleMove(((Packet02Move)packet));
86+
}
87+
}
88+
89+
private void handleMove(Packet02Move packet) {
90+
if(getPlayerMP(packet.getUsername()) != null){
91+
int index = getPlayerMPIndex(packet.getUsername());
92+
this.connectedPlayers.get(index).x = packet.getX();
93+
this.connectedPlayers.get(index).y = packet.getY();
94+
packet.writeData(this);
7995
}
8096
}
8197

src/com/redomar/game/net/packets/Packet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public abstract class Packet {
77

88
public static enum PacketTypes {
9-
INVALID(-1), LOGIN(00), DISCONNECT(01);
9+
INVALID(-1), LOGIN(00), DISCONNECT(01), MOVE(02);
1010

1111
private int packetId;
1212

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.redomar.game.net.packets;
2+
3+
import com.redomar.game.net.GameClient;
4+
import com.redomar.game.net.GameServer;
5+
6+
public class Packet02Move extends Packet {
7+
8+
private String username;
9+
private int x, y;
10+
11+
public Packet02Move(byte[] data) {
12+
super(02);
13+
String[] dataArray = readData(data).split(",");
14+
this.username = dataArray[0];
15+
this.x = Integer.parseInt(dataArray[1]);
16+
this.y = Integer.parseInt(dataArray[2]);
17+
}
18+
19+
public Packet02Move(String username, int x, int y) {
20+
super(02);
21+
this.username = username;
22+
this.x = x;
23+
this.y = y;
24+
}
25+
26+
@Override
27+
public byte[] getData() {
28+
return ("02" + this.username + "," + this.x + "," + this.y).getBytes();
29+
}
30+
31+
@Override
32+
public void writeData(GameClient client) {
33+
client.sendData(getData());
34+
}
35+
36+
@Override
37+
public void writeData(GameServer server) {
38+
server.sendDataToAllClients(getData());
39+
}
40+
41+
public String getUsername() {
42+
return username;
43+
}
44+
45+
public int getX(){
46+
return this.x;
47+
}
48+
49+
public int getY(){
50+
return this.y;
51+
}
52+
}

0 commit comments

Comments
 (0)