1
0
Fork 0

Add player

This commit is contained in:
Florian Richer (MrDev023) 2016-04-18 20:22:20 +02:00
parent 1ad189c20a
commit c2e793be12
7 changed files with 168 additions and 44 deletions

View file

@ -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;
}
}

View file

@ -18,6 +18,8 @@ import mrdev023.opengl.gui.*;
public class MainState implements IGameState{
public static ArrayList<Message> messages = new ArrayList<Message>();
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();
}
}
}
}

View file

@ -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();
}
}

View file

@ -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){

View file

@ -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()?"<admin>":"")+client.getPseudo()));
client.setRunning(false);
MainServer.clients.remove(client);
}

View file

@ -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(), "<admin>" + 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("<admin>" + 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"));

View file

@ -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);
}
}