Permalink
Browse files

Working synchronized player moving

  • Loading branch information...
teamon committed Nov 23, 2010
1 parent 4b9e947 commit 515f05a79ecdec9efe50438866ec500fef2fb897
View
@@ -7,4 +7,12 @@
mvn assembly:assembly
## Run
- java -jar target/volley-0.1.0-jar-with-dependencies.jar
+ bin/server
+ bin/client
+
+
+
+## TODO (dirty):
+* remove players when disconnected
+* send message to server when disconnecting
+
@@ -9,9 +9,10 @@
private Player player;
private Map<String, Player> players;
private ConnectionThread connection;
+ private ClientGame game;
public Client(){
-
+ this.players = new HashMap<String, Player>();
}
public void setChat(Chat chat){
@@ -20,7 +21,6 @@ public void setChat(Chat chat){
public void connect(String host, int port, Player player) throws IOException {
this.player = player;
- this.players = new HashMap<String, Player>();
this.players.put(player.getNick(), player);
this.connection = new ConnectionThread(this, new Socket(host, port));
this.connection.start();
@@ -37,7 +37,7 @@ public void processMessage(ConnectionThread from, String message){
String[] chunks = message.split(" ", 2);
switch(chunks[0].charAt(0)){
- case 'c':
+ case 'c': // c [nick] [content]
if(chunks.length == 2){
String[] nickAndContent = chunks[1].split(" ", 2);
if(nickAndContent.length == 2){
@@ -47,6 +47,25 @@ public void processMessage(ConnectionThread from, String message){
}
break;
+ case 's': // s [nick] [xpos] [ypos]
+ if(chunks.length == 2){
+ String[] nickAndPosition = chunks[1].split(" ", 3);
+ Player player = this.players.get(nickAndPosition[0]);
+ if(player != null){
+ player.setX(Integer.parseInt(nickAndPosition[1]));
+ player.setY(Integer.parseInt(nickAndPosition[2]));
+ }
+
+ }
+ break;
+
+ case 'r': // r [nick]
+ // register new game player
+ if(chunks.length == 2){
+ players.put(chunks[1], new Player(chunks[1]));
+ }
+ break;
+
default:
Logger.error("Unknown command: " + message);
break;
@@ -76,6 +95,10 @@ public Player getPlayer(){
return this.player;
}
+ public Collection<Player> getPlayers(){
+ return players.values();
+ }
+
public void disconnect(){
try {
if(this.connection != null) this.connection.kill();
@@ -27,7 +27,7 @@
private JTextArea chatTextArea;
private Client client;
- private JPanel gamePanel;
+ private ClientGame game;
public ClientFrame() {
setTitle("Volley Client");
@@ -124,13 +124,15 @@ public void actionPerformed(ActionEvent e) {
((DefaultCaret)chatTextArea.getCaret()).setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
pane.add(chatPanel);
- gamePanel = new GamePanel();
- gamePanel.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
- gamePanel.setBounds(6, 6, 430, 410);
- pane.add(gamePanel);
-
client = new Client();
client.setChat(new Chat(client, this));
+
+ game = new ClientGame(client);
+ game.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
+ game.setBounds(6, 6, 430, 410);
+ pane.add(game);
+
+
}
public void addChatMessage(Message message){
@@ -163,6 +165,7 @@ protected void connect(){
chatTextArea.setEnabled(true);
chatMessageInput.setEnabled(true);
+ game.start(); // TEMPORARY!!
} catch (EmptyNickException e){
showError("Nick can't be empty");
} catch (IOException e){
@@ -0,0 +1,119 @@
+package eu.teamon.volley;
+
+import java.awt.*;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+
+import javax.swing.JPanel;
+
+public class ClientGame extends JPanel {
+ private final int WIDTH = 430;
+ private final int HEIGHT = 410;
+
+ private Client client;
+ private boolean running = false;
+
+ private boolean keyLeftPressed = false;
+ private boolean keyRightPressed = false;
+ private boolean keyUpPressed = false;
+
+ private Thread gameThread;
+
+ public ClientGame(Client client){
+ this.client = client;
+
+ setSize(WIDTH, HEIGHT);
+ this.addKeyListener(new KeyAdapter(){
+ public void keyPressed(KeyEvent e) {
+ switch(e.getKeyCode()){
+ case KeyEvent.VK_LEFT:
+ keyLeftPressed = true;
+ break;
+
+ case KeyEvent.VK_RIGHT:
+ keyRightPressed = true;
+ break;
+
+ case KeyEvent.VK_UP:
+ keyUpPressed = true;
+ break;
+
+ case KeyEvent.VK_SPACE:
+
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent e) {
+ switch(e.getKeyCode()){
+ case KeyEvent.VK_LEFT:
+ keyLeftPressed = false;
+ break;
+
+ case KeyEvent.VK_RIGHT:
+ keyRightPressed = false;
+ break;
+
+ case KeyEvent.VK_UP:
+ keyUpPressed = false;
+ break;
+
+ case KeyEvent.VK_SPACE:
+
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyTyped(KeyEvent e) {
+ Logger.debug("Key typed: " + e.getKeyCode());
+ }
+ });
+ }
+
+ public void start(){
+ requestFocus();
+
+ gameThread = new Thread(){
+ public void run(){
+ while(true) {
+ process();
+ try { Thread.sleep(10); } catch (InterruptedException e){ }
+ }
+ }
+ };
+
+ gameThread.start();
+ Logger.debug("Game started");
+ }
+
+ private void process(){
+ if(keyLeftPressed){
+ client.sendMessage(Command.moveX(-1));
+ }
+
+ if(keyRightPressed){
+ client.sendMessage(Command.moveX(1));
+ }
+
+ this.repaint();
+ }
+
+ public void paintComponent(Graphics g){
+ g.setColor(Color.WHITE);
+ g.clearRect(0, 0, WIDTH, HEIGHT);
+
+ g.setColor(Color.RED);
+
+ g.translate(0, HEIGHT-50);
+ for(Player player : client.getPlayers()){
+ g.fillRect(player.getX(), player.getY(), 50, 50);
+ }
+
+ }
+}
@@ -16,4 +16,12 @@ public static String chatMessage(Player player, String message){
public static String chatMessage(String message){
return "c " + message;
}
+
+ public static String moveX(int x){
+ return "m " + x;
+ }
+
+ public static String playerPosition(Player player){
+ return "s " + player.getNick() + " " + player.getX() + " " + player.getY();
+ }
}
@@ -38,7 +38,9 @@ public void run() {
}
public void sendMessage(String message){
- while(out == null);
+ while(out == null){
+ Logger.error("out == null kurwa!");
+ }
out.println(message);
}
@@ -1,16 +0,0 @@
-package eu.teamon.volley;
-
-import java.awt.*;
-import javax.swing.JPanel;
-
-public class GamePanel extends JPanel {
-
- public GamePanel(){
- setSize(430, 410);
- }
-
- public void paintComponent(Graphics g){
- g.setColor(Color.red);
- g.drawRect(50, 50, 200, 250);
- }
-}
@@ -1,22 +1,59 @@
package eu.teamon.volley;
public class Player {
- private String nick;
+ private final int X_SPEED = 4;
+
+ private String nick;
+
+ // Game stuff
+ private int x;
+ private int y;
+
+
public Player(){
this("");
}
public Player(String nick){
this.nick = nick;
+ this.x = 0;
+ this.y = 0;
}
public void setNick(String nick){
this.nick = nick;
}
public String getNick(){
- Logger.debug("getNick " + this.nick);
return this.nick;
}
-}
+
+ public int getX() {
+ return x;
+ }
+
+ public void setX(int x) {
+ this.x = x;
+ }
+
+ public int getY() {
+ return y;
+ }
+
+ public void setY(int y) {
+ this.y = y;
+ }
+
+ public void incrementX(){
+ this.x += X_SPEED;
+ // TODO: Add range handling!!
+ }
+
+ public void decrementX(){
+ this.x -= X_SPEED;
+ // TODO: Add range handling
+ }
+
+
+}
@@ -71,20 +71,38 @@ public void processMessage(ConnectionThread from, String message){
switch(chunks[0].charAt(0)){
case 'c': // chat "c [CONTENT]"
if(chunks.length == 2){
- String x = Command.chatMessage(this.connections.get(from), chunks[1]);
- Logger.debug("x=" + x);
- sendToAll(x);
+ sendToAll(Command.chatMessage(this.connections.get(from), chunks[1]));
}
break;
case 'r': // register new user "r [NICK]"
- Logger.debug("USer registered: " + chunks[1]);
+ Logger.debug("User registered: " + chunks[1]);
if(chunks.length == 2){
this.connections.get(from).setNick(chunks[1]);
sendToAll(message);
+
+ // send list of all players to new player
+ for (Player player : connections.values()) {
+ from.sendMessage(Command.newPlayerRegistered(player));
+ }
+
}
break;
-
+
+ case 'm': // move player "m [1|-1]"
+ if(chunks.length == 2){
+ int dir = Integer.parseInt(chunks[1]);
+ Player player = this.connections.get(from);
+ if(dir > 0){
+ player.incrementX();
+ } else if(dir < 0){
+ player.decrementX();
+ }
+ sendToAll(Command.playerPosition(player));
+ }
+
+ break;
+
default:
Logger.error("Unknown command: " + message);
break;

0 comments on commit 515f05a

Please sign in to comment.