Add player
This commit is contained in:
parent
99932e3649
commit
1ad189c20a
13 changed files with 330 additions and 8 deletions
64
First Game Engine Project/src/mrdev023/entities/Entity.java
Normal file
64
First Game Engine Project/src/mrdev023/entities/Entity.java
Normal file
|
@ -0,0 +1,64 @@
|
|||
package mrdev023.entities;
|
||||
|
||||
import mrdev023.math.*;
|
||||
import mrdev023.opengl.*;
|
||||
import mrdev023.opengl.gui.*;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
|
||||
public abstract class Entity {
|
||||
|
||||
private Vector2f position;
|
||||
private String name;
|
||||
private Color4f color;
|
||||
|
||||
public Entity(Vector2f pos,String name){
|
||||
this.name = name;
|
||||
this.position = pos;
|
||||
this.color = new Color4f(Mathf.random(),Mathf.random(),Mathf.random(),1.0f);
|
||||
}
|
||||
|
||||
public Entity(Vector2f pos){
|
||||
this.name = "No Name";
|
||||
this.position = pos;
|
||||
}
|
||||
|
||||
public void render(){
|
||||
TextFont.drawText(name,(int)position.getX() - (name.length() * 16)/2 + 25, (int)position.getY() + 50, 16, 1);
|
||||
glBegin(GL_QUADS);
|
||||
color.bind();
|
||||
glVertex2f(position.getX(), position.getY());
|
||||
glVertex2f(position.getX(), position.getY() + 50);
|
||||
glVertex2f(position.getX() + 50, position.getY() + 50);
|
||||
glVertex2f(position.getX() + 50, position.getY());
|
||||
glEnd();
|
||||
}
|
||||
|
||||
public Vector2f getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public void setPosition(Vector2f position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Color4f getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(Color4f color) {
|
||||
System.out.println(color);
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
11
First Game Engine Project/src/mrdev023/entities/Player.java
Normal file
11
First Game Engine Project/src/mrdev023/entities/Player.java
Normal file
|
@ -0,0 +1,11 @@
|
|||
package mrdev023.entities;
|
||||
|
||||
import mrdev023.math.*;
|
||||
|
||||
public class Player extends Entity{
|
||||
|
||||
public Player(Vector2f pos, String name) {
|
||||
super(pos, name);
|
||||
}
|
||||
|
||||
}
|
|
@ -7,9 +7,10 @@ import java.util.*;
|
|||
|
||||
import javax.swing.*;
|
||||
|
||||
import mrdev023.gameengine.*;
|
||||
import mrdev023.entities.*;
|
||||
import mrdev023.gameengine.gamestate.main.*;
|
||||
import mrdev023.network.client.*;
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.*;
|
||||
import mrdev023.opengl.*;
|
||||
import mrdev023.opengl.gui.*;
|
||||
|
@ -40,10 +41,8 @@ public class MainState implements IGameState{
|
|||
}
|
||||
}
|
||||
|
||||
public void render2D() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void update() {
|
||||
ArrayList<Message> rmList = new ArrayList<Message>();
|
||||
for(Message m : messages){
|
||||
|
@ -67,10 +66,60 @@ public class MainState implements IGameState{
|
|||
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(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_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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void updateMouse() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render2D() {
|
||||
synchronized(Client.entities){
|
||||
try{
|
||||
for(Entity e : Client.entities){
|
||||
e.render();
|
||||
}
|
||||
}catch(ConcurrentModificationException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,6 +5,10 @@ public class Mathf {
|
|||
public static final float PI = (float)Math.PI;
|
||||
public static final float EPSILON = 1.401298e-45f;
|
||||
|
||||
public static float random(){
|
||||
return (float)Math.random();
|
||||
}
|
||||
|
||||
public static float cos(float angle){
|
||||
return (float)Math.cos(angle);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,9 @@ package mrdev023.network.common;
|
|||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
|
||||
import mrdev023.entities.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
|
||||
public class Client extends Thread{
|
||||
|
@ -18,6 +20,13 @@ public class Client extends Thread{
|
|||
private String state = "";
|
||||
private boolean isAdmin = false;
|
||||
|
||||
//Client
|
||||
public static ArrayList<Entity> entities = new ArrayList<Entity>();
|
||||
|
||||
//Server
|
||||
public Player player;
|
||||
|
||||
|
||||
public Client(InetAddress address,int port) throws SocketException{
|
||||
client = new DatagramSocket();
|
||||
this.address = address;
|
||||
|
@ -94,6 +103,24 @@ public class Client extends Thread{
|
|||
client.send(sendPacket);
|
||||
elapsed = 0;
|
||||
}
|
||||
|
||||
public void render2D() {
|
||||
for(Entity e : entities)e.render();
|
||||
}
|
||||
|
||||
public static Entity getEntityByName(String name){
|
||||
for(Entity e : entities){
|
||||
if(e.getName().equals(name))return e;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int getIndexEntityByName(String name){
|
||||
for(int i = 0;i < entities.size();i++){
|
||||
if(entities.get(i).getName().equals(name))return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public DatagramSocket getClient() {
|
||||
return client;
|
||||
|
|
|
@ -16,6 +16,9 @@ public class Register {
|
|||
addClass(MessageTestPacket.class);
|
||||
addClass(OutOfSocketPacket.class);
|
||||
addClass(PingClientPacket.class);
|
||||
addClass(UpdateXPacket.class);
|
||||
addClass(UpdateYPacket.class);
|
||||
addClass(UpdateColor.class);
|
||||
}
|
||||
|
||||
public static void addClass(Class cl){
|
||||
|
|
|
@ -2,7 +2,9 @@ package mrdev023.network.packet;
|
|||
|
||||
import java.net.*;
|
||||
|
||||
import mrdev023.entities.*;
|
||||
import mrdev023.gameengine.gamestate.*;
|
||||
import mrdev023.math.*;
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
import mrdev023.opengl.gui.*;
|
||||
|
@ -24,7 +26,7 @@ public class ClientConnect implements IPacket{
|
|||
|
||||
public void manage(Client client, IPacket packet) throws Exception {
|
||||
MainState.messages.add(new Message(pseudo + " has connected !"));
|
||||
System.out.println("hello");
|
||||
Client.entities.add(new Player(new Vector2f(0,0),pseudo));
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception {
|
||||
|
|
|
@ -2,6 +2,7 @@ package mrdev023.network.packet;
|
|||
|
||||
import java.net.*;
|
||||
|
||||
import mrdev023.entities.*;
|
||||
import mrdev023.gameengine.gamestate.*;
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
|
@ -25,6 +26,8 @@ public String pseudo;
|
|||
|
||||
public void manage(Client client, IPacket packet) throws Exception {
|
||||
MainState.messages.add(new Message(pseudo + " has disconnected !"));
|
||||
Entity e = Client.getEntityByName(pseudo);
|
||||
if(e != null)Client.entities.remove(e);
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception {
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
package mrdev023.network.packet;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
import mrdev023.math.*;
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
import mrdev023.network.server.*;
|
||||
|
||||
public class UpdateColor implements IPacket{
|
||||
|
||||
private int r,g,b,a;
|
||||
private String pseudo;
|
||||
|
||||
public UpdateColor(){}
|
||||
|
||||
public UpdateColor(String pseudo,Color4f color){
|
||||
this.pseudo = pseudo;
|
||||
r = (int)(color.r * 100);
|
||||
g = (int)(color.g * 100);
|
||||
b = (int)(color.b * 100);
|
||||
a = (int)(color.a * 100);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(DataBuffer buff) throws Exception {
|
||||
buff.put(pseudo);
|
||||
buff.put(r);
|
||||
buff.put(g);
|
||||
buff.put(b);
|
||||
buff.put(a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(DataBuffer buff) throws Exception {
|
||||
this.pseudo = buff.getString();
|
||||
r = buff.getInt();
|
||||
g = buff.getInt();
|
||||
b = buff.getInt();
|
||||
a = buff.getInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void manage(Client client, IPacket packet) throws Exception {
|
||||
int i = Client.getIndexEntityByName(pseudo);
|
||||
if(i != -1)Client.entities.get(i).setColor(new Color4f(r/100.0f,g/100.0f,b/100.0f,a/100.0f));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception {
|
||||
client.player.setColor(new Color4f(r/100.0f,g/100.0f,b/100.0f,1.0f));
|
||||
MainServer.sendToClients(packet);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package mrdev023.network.packet;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
import mrdev023.gameengine.gamestate.*;
|
||||
import mrdev023.math.*;
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
import mrdev023.network.server.*;
|
||||
|
||||
public class UpdateXPacket implements IPacket{
|
||||
|
||||
private String pseudo;
|
||||
private int x;
|
||||
|
||||
public UpdateXPacket(){}
|
||||
|
||||
public UpdateXPacket(String pseudo,double x){
|
||||
this.pseudo = pseudo;
|
||||
this.x = (int)x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(DataBuffer buff) throws Exception {
|
||||
buff.put(pseudo);
|
||||
buff.put(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(DataBuffer buff) throws Exception {
|
||||
this.pseudo = buff.getString();
|
||||
this.x = buff.getInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void manage(Client client, IPacket packet) throws Exception {
|
||||
int i = Client.getIndexEntityByName(pseudo);
|
||||
if(i != -1)Client.entities.get(i).setPosition(new Vector2f(x,Client.entities.get(i).getPosition().getY()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception {
|
||||
client.player.setPosition(new Vector2f(x,client.player.getPosition().getY()));
|
||||
MainServer.sendToClients(packet);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package mrdev023.network.packet;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
import mrdev023.gameengine.gamestate.*;
|
||||
import mrdev023.math.*;
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
import mrdev023.network.server.*;
|
||||
|
||||
public class UpdateYPacket implements IPacket{
|
||||
|
||||
private String pseudo;
|
||||
private int x;
|
||||
|
||||
public UpdateYPacket(){}
|
||||
|
||||
public UpdateYPacket(String pseudo,double y){
|
||||
this.pseudo = pseudo;
|
||||
this.x = (int)y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(DataBuffer buff) throws Exception {
|
||||
buff.put(pseudo);
|
||||
buff.put(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(DataBuffer buff) throws Exception {
|
||||
this.pseudo = buff.getString();
|
||||
this.x = buff.getInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void manage(Client client, IPacket packet) throws Exception {
|
||||
int i = Client.getIndexEntityByName(pseudo);
|
||||
if(i != -1)Client.entities.get(i).setPosition(new Vector2f(Client.entities.get(i).getPosition().getX(),x));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception {
|
||||
client.player.setPosition(new Vector2f(client.player.getPosition().getX(),x));
|
||||
MainServer.sendToClients(packet);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,8 +1,9 @@
|
|||
package mrdev023.network.server;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
|
||||
import mrdev023.entities.*;
|
||||
import mrdev023.math.*;
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
|
@ -61,7 +62,15 @@ public class MainServer extends Thread{
|
|||
packet.manage(c, packet,server);
|
||||
clients.add(c);
|
||||
System.out.println(c.getPseudo() + " has connected " + c.getAddress().getHostName() + ":" + c.getPort());
|
||||
sendToClients(new ClientConnect(c.getPseudo()));
|
||||
c.player = new Player(new Vector2f(0,0),c.getPseudo());
|
||||
sendToClientsWithoutClient(new Client[]{c},new ClientConnect(c.getPseudo()));
|
||||
sendToClientsWithoutClient(new Client[]{c},new UpdateColor(c.getPseudo(),c.player.getColor()));
|
||||
for(Client cl : clients){
|
||||
c.send(new ClientConnect(cl.getPseudo()));
|
||||
c.send(new UpdateXPacket(cl.getPseudo(), cl.player.getPosition().getX()));
|
||||
c.send(new UpdateYPacket(cl.getPseudo(), cl.player.getPosition().getY()));
|
||||
c.send(new UpdateColor(cl.getPseudo(), cl.player.getColor()));
|
||||
}
|
||||
}
|
||||
}else{
|
||||
packet.manage(c, packet,server);
|
||||
|
|
|
@ -6,7 +6,6 @@ import static org.lwjgl.opengl.GL11.*;
|
|||
public class DisplayManager {
|
||||
|
||||
public static void clear(){
|
||||
glClearColor(1,1,1,1);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue