From c2e793be1266aa74abd649580ee59b3e9f65d758 Mon Sep 17 00:00:00 2001 From: "Florian Richer (MrDev023)" Date: Mon, 18 Apr 2016 20:22:20 +0200 Subject: [PATCH] Add player --- .../src/mrdev023/entities/Entity.java | 34 ++++++- .../gameengine/gamestate/MainState.java | 92 +++++++++++-------- .../src/mrdev023/io/IOFile.java | 24 +++++ .../src/mrdev023/network/common/Register.java | 1 + .../network/packet/ClientDisconnect.java | 2 +- .../network/packet/MessagePacket.java | 10 +- .../network/packet/UpdatePseudoPacket.java | 49 ++++++++++ 7 files changed, 168 insertions(+), 44 deletions(-) create mode 100644 First Game Engine Project/src/mrdev023/io/IOFile.java create mode 100644 First Game Engine Project/src/mrdev023/network/packet/UpdatePseudoPacket.java diff --git a/First Game Engine Project/src/mrdev023/entities/Entity.java b/First Game Engine Project/src/mrdev023/entities/Entity.java index 6456985..c0a8d70 100644 --- a/First Game Engine Project/src/mrdev023/entities/Entity.java +++ b/First Game Engine Project/src/mrdev023/entities/Entity.java @@ -11,11 +11,19 @@ public abstract class Entity { private Vector2f position; private String name; private Color4f color; + private int life; + private int mana; + private int exp; + private int level; public Entity(Vector2f pos,String name){ this.name = name; this.position = pos; this.color = new Color4f(Mathf.random(),Mathf.random(),Mathf.random(),1.0f); + this.life = 100; + this.mana = 100; + this.exp = 0; + this.level = 0; } public Entity(Vector2f pos){ @@ -58,7 +66,29 @@ public abstract class Entity { System.out.println(color); this.color = color; } - - + + public int getLife() { + return life; + } + + public void setLife(int life) { + this.life = life; + } + + public int getMana() { + return mana; + } + + public void setMana(int mana) { + this.mana = mana; + } + + public int getExp() { + return exp; + } + + public void setExp(int exp) { + this.exp = exp; + } } diff --git a/First Game Engine Project/src/mrdev023/gameengine/gamestate/MainState.java b/First Game Engine Project/src/mrdev023/gameengine/gamestate/MainState.java index dc699c4..c72b7e3 100644 --- a/First Game Engine Project/src/mrdev023/gameengine/gamestate/MainState.java +++ b/First Game Engine Project/src/mrdev023/gameengine/gamestate/MainState.java @@ -18,6 +18,8 @@ import mrdev023.opengl.gui.*; public class MainState implements IGameState{ public static ArrayList messages = new ArrayList(); + public static boolean isKeyEvent = false; + public static String textTaped = ""; public void init() { @@ -36,8 +38,9 @@ public class MainState implements IGameState{ } public void renderGUI() { - for(int i = 0;i < messages.size();i++){ - messages.get(i).render(10, 10 + i * 20); + if(isKeyEvent)TextFont.drawText(">" + textTaped, 10, 10, 16, 1); + for(int i = messages.size() - 1;i >= 0;i--){ + messages.get(i).render(10, 30 + (messages.size() - i - 1) * 20); } } @@ -57,49 +60,64 @@ public class MainState implements IGameState{ } public void updateKeyboard() { - if(Input.isKeyDown(GLFW_KEY_M)){ + if(Input.isKeyDown(GLFW_KEY_ENTER)){ try { - MainClient.client.send(new MessagePacket(JOptionPane.showInputDialog(null, "Entrez votre message :"),MainClient.pseudo)); - } catch (HeadlessException e) { - e.printStackTrace(); + if(isKeyEvent){ + if(!textTaped.equals(""))MainClient.client.send(new MessagePacket(textTaped,MainClient.pseudo)); + textTaped = ""; + } + isKeyEvent = !isKeyEvent; } catch (Exception e) { e.printStackTrace(); } } - if(Input.isKey(GLFW_KEY_W)){ - try { - MainClient.client.send(new UpdateYPacket(MainClient.pseudo,Client.getEntityByName(MainClient.pseudo).getPosition().getY()+1)); - } catch (HeadlessException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); + if(isKeyEvent){ + for(int i = 0;i < Input.NBRE_KEY;i++){ + if(Input.isKeyDown(i)){ + if(i == GLFW_KEY_DELETE || i == GLFW_KEY_BACKSPACE){ + textTaped = textTaped.substring(0,textTaped.length()-1); + }else if(i != GLFW_KEY_ENTER && i != GLFW_KEY_LEFT_SHIFT && i != GLFW_KEY_RIGHT_SHIFT + && i != GLFW_KEY_LEFT_CONTROL && i != GLFW_KEY_RIGHT_CONTROL){ + textTaped += (char)i; + } + } } - } - if(Input.isKey(GLFW_KEY_S)){ - try { - MainClient.client.send(new UpdateYPacket(MainClient.pseudo,Client.getEntityByName(MainClient.pseudo).getPosition().getY()-1)); - } catch (HeadlessException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); + }else{ + if(Input.isKey(GLFW_KEY_W)){ + try { + MainClient.client.send(new UpdateYPacket(MainClient.pseudo,Client.getEntityByName(MainClient.pseudo).getPosition().getY()+1)); + } catch (HeadlessException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } } - } - if(Input.isKey(GLFW_KEY_A)){ - try { - MainClient.client.send(new UpdateXPacket(MainClient.pseudo,Client.getEntityByName(MainClient.pseudo).getPosition().getX()-1)); - } catch (HeadlessException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); + if(Input.isKey(GLFW_KEY_S)){ + try { + MainClient.client.send(new UpdateYPacket(MainClient.pseudo,Client.getEntityByName(MainClient.pseudo).getPosition().getY()-1)); + } catch (HeadlessException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } } - } - if(Input.isKey(GLFW_KEY_D)){ - try { - MainClient.client.send(new UpdateXPacket(MainClient.pseudo,Client.getEntityByName(MainClient.pseudo).getPosition().getX()+1)); - } catch (HeadlessException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); + if(Input.isKey(GLFW_KEY_A)){ + try { + MainClient.client.send(new UpdateXPacket(MainClient.pseudo,Client.getEntityByName(MainClient.pseudo).getPosition().getX()-1)); + } catch (HeadlessException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + } + if(Input.isKey(GLFW_KEY_D)){ + try { + MainClient.client.send(new UpdateXPacket(MainClient.pseudo,Client.getEntityByName(MainClient.pseudo).getPosition().getX()+1)); + } catch (HeadlessException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } } } } diff --git a/First Game Engine Project/src/mrdev023/io/IOFile.java b/First Game Engine Project/src/mrdev023/io/IOFile.java new file mode 100644 index 0000000..0cae518 --- /dev/null +++ b/First Game Engine Project/src/mrdev023/io/IOFile.java @@ -0,0 +1,24 @@ +package mrdev023.io; + +import java.io.*; + +public class IOFile { + + public static String readFile(String path) throws Exception{ + String r = ""; + BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path))); + String buffer = ""; + while ((buffer = reader.readLine()) != null) { + r += buffer + "\n"; + } + reader.close(); + return r; + } + + public static void writeFile(String path,String data)throws Exception{ + BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path))); + write.write(data); + write.close(); + } + +} diff --git a/First Game Engine Project/src/mrdev023/network/common/Register.java b/First Game Engine Project/src/mrdev023/network/common/Register.java index 71ee081..7f5cfeb 100644 --- a/First Game Engine Project/src/mrdev023/network/common/Register.java +++ b/First Game Engine Project/src/mrdev023/network/common/Register.java @@ -19,6 +19,7 @@ public class Register { addClass(UpdateXPacket.class); addClass(UpdateYPacket.class); addClass(UpdateColor.class); + addClass(UpdatePseudoPacket.class); } public static void addClass(Class cl){ diff --git a/First Game Engine Project/src/mrdev023/network/packet/ClientDisconnect.java b/First Game Engine Project/src/mrdev023/network/packet/ClientDisconnect.java index d93ace6..84350ab 100644 --- a/First Game Engine Project/src/mrdev023/network/packet/ClientDisconnect.java +++ b/First Game Engine Project/src/mrdev023/network/packet/ClientDisconnect.java @@ -32,7 +32,7 @@ public String pseudo; public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception { System.out.println(client.getPseudo() + " has diconnected " + client.getAddress().getHostName() + ":" + client.getPort()); - MainServer.sendToClients(new ClientDisconnect(client.getPseudo())); + MainServer.sendToClients(new ClientDisconnect((client.isAdmin()?"":"")+client.getPseudo())); client.setRunning(false); MainServer.clients.remove(client); } diff --git a/First Game Engine Project/src/mrdev023/network/packet/MessagePacket.java b/First Game Engine Project/src/mrdev023/network/packet/MessagePacket.java index ac8afbe..6b09aa6 100644 --- a/First Game Engine Project/src/mrdev023/network/packet/MessagePacket.java +++ b/First Game Engine Project/src/mrdev023/network/packet/MessagePacket.java @@ -17,12 +17,12 @@ public class MessagePacket implements IPacket{ public MessagePacket(String message,String pseudo){this.message = message;this.pseudo = pseudo;} public void write(DataBuffer buff) throws Exception { - buff.put(message); + buff.put(message.toLowerCase()); buff.put(pseudo); } public void read(DataBuffer buff) throws Exception { - this.message = buff.getString(); + this.message = buff.getString().toLowerCase(); this.pseudo = buff.getString(); } @@ -32,10 +32,11 @@ public class MessagePacket implements IPacket{ public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception { if(message.startsWith("/")){ - if(message.split(" ")[0].equals("/login") && message.split(" ").length == 2){ - if(message.split(" ")[1].equals(MainServer.password)){ + if(message.split(" ")[0].toLowerCase().equals("/login") && message.split(" ").length == 2){ + if(message.split(" ")[1].toLowerCase().equals(MainServer.password)){ MainServer.sendToClient(client, new MessagePacket("login successful!", "Server")); client.setAdmin(true); + MainServer.sendToClients(new UpdatePseudoPacket(client.getPseudo(), "" + client.getPseudo())); }else{ MainServer.sendToClient(client, new MessagePacket("password incorrect!", "Server")); } @@ -47,6 +48,7 @@ public class MessagePacket implements IPacket{ }else if(cmd[0].equals("/logout")){ MainServer.sendToClient(client, new MessagePacket("logout successful!", "Server")); client.setAdmin(false); + MainServer.sendToClients(new UpdatePseudoPacket("" + client.getPseudo(), client.getPseudo())); }else if(cmd[0].equals("/help")){ MainServer.sendToClient(client, new MessagePacket("Help", "Server")); MainServer.sendToClient(client, new MessagePacket("/login [password]", "Server")); diff --git a/First Game Engine Project/src/mrdev023/network/packet/UpdatePseudoPacket.java b/First Game Engine Project/src/mrdev023/network/packet/UpdatePseudoPacket.java new file mode 100644 index 0000000..394b810 --- /dev/null +++ b/First Game Engine Project/src/mrdev023/network/packet/UpdatePseudoPacket.java @@ -0,0 +1,49 @@ +package mrdev023.network.packet; + +import java.net.*; + +import mrdev023.math.*; +import mrdev023.network.client.*; +import mrdev023.network.common.*; +import mrdev023.network.packet.main.*; +import mrdev023.network.server.*; + +public class UpdatePseudoPacket implements IPacket{ + + public String pseudo,npseudo; + + public UpdatePseudoPacket(){} + + public UpdatePseudoPacket(String pseudo,String npseudo){ + this.pseudo = pseudo; + this.npseudo = npseudo; + } + + @Override + public void write(DataBuffer buff) throws Exception { + buff.put(pseudo); + buff.put(npseudo); + } + + @Override + public void read(DataBuffer buff) throws Exception { + pseudo = buff.getString(); + npseudo = buff.getString(); + } + + @Override + public void manage(Client client, IPacket packet) throws Exception { + int i = Client.getIndexEntityByName(pseudo); + if(i != -1)Client.entities.get(i).setName(npseudo); + if(MainClient.pseudo.equals(pseudo)){ + MainClient.pseudo = npseudo; + } + } + + @Override + public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception { + client.setPseudo(npseudo); + MainServer.sendToClients(packet); + } + +}