Permalink
Browse files

holy shit I"m going to fail

  • Loading branch information...
TheChandler committed Dec 7, 2017
1 parent 4fbe788 commit 86c8dff8af88ddd990aa07717c671fe86a26dd38
@@ -54,10 +54,12 @@ void gameConnection(){
}
void connectToServer(){
byte[] buffer=new byte[256];
Connection connection=new Connection(5656,buffer);
Connection connection=new Connection(5656,buffer,"Client");
connection.start();
buffer[0]=120;
buffer[1]=119;
while(!connection.isNewMessage()){
connection.send(5656);
}
System.out.println(buffer.toString());
}
@@ -4,6 +4,7 @@
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
@@ -26,31 +27,34 @@
this.name=name;
try{
address=InetAddress.getLocalHost();
socket=new DatagramSocket();
}catch (UnknownHostException uhe){
System.out.println(name+" Unknown Host can't start connection");
}catch(SocketException se){
System.out.println(name+" Socket exception can't start connection");
}
}
boolean isNewMessage(){
return newMessage;
}
byte[] getMessage(){
void resetMessage(){
newMessage=false;
return buffer;
}
void send(byte[] sendBuffer){
DatagramPacket packet=new DatagramPacket(sendBuffer,sendBuffer.length,address,port);
void send(int p){
DatagramPacket packet=new DatagramPacket(buffer,buffer.length,address,p);
try{
socket.send(packet);
}catch(IOException io){
System.out.println(name+" Packet not snent");
System.out.println(name+" Packet not sent");
}
}
public void run(){
try {
socket=new DatagramSocket(port);
while (true){
DatagramPacket packet=new DatagramPacket(buffer,buffer.length,port);
DatagramPacket packet=new DatagramPacket(buffer,buffer.length);
socket.receive(packet);
System.out.println(name+" "+buffer[0]+" "+buffer[1]+" "+packet.getPort());
newMessage=true;
}
}catch(SocketException se){
@@ -10,31 +10,58 @@
public class GameClient implements State {
ShapeRenderer sr;
Client client;
Player player;
Connection serverConnection;
float[][] actionLog;
int currentAction;
float totalTime;
byte[] buffer;
boolean isConnected;
GameClient(ShapeRenderer s){
sr=s;
player=new Player(200,200);
buffer=new byte[256];
isConnected=false;
establishConnection();
}
void establishConnection(){
Connection connection=new Connection(2727,buffer, "Client establishment");
connection.start();
buffer[1]=120;//arbitrary numbers I chose to represent a connectin request
buffer[0]=120;
connection.send(6565);
while(!connection.isNewMessage()){
//waits for a response from the server
}
connection.resetMessage();
System.out.println(buffer);
if (buffer[0]!=-1){
serverConnection=new Connection(buffer[1],this.buffer,"Client's connection to gameServer");
isConnected=true;
}
}
@Override
public void update(float dt) {
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
player.moveLeft(dt);
} else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
player.moveRight(dt);
} else if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
player.moveUp(dt);
} else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
player.moveDown(dt);
if (isConnected) {
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
player.moveLeft(dt);
} else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
player.moveRight(dt);
} else if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
player.moveUp(dt);
} else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
player.moveDown(dt);
}
}
}
@Override
public void render() {
@@ -9,21 +9,32 @@
public class GameServer implements State {
ShapeRenderer sr;
Server server;
Connection connection;
byte[] buffer;
GameServer(ShapeRenderer s){
sr=s;
server=new Server("Seerverrrr");
buffer=new byte[256];
connection=new Connection(6565,buffer,"Establsihment server");
connection.start();
System.out.println("Server started");
}
@Override
public void update(float dt) {
if (connection.isNewMessage()){
connection.resetMessage();
if (buffer[0]==120&&buffer[1]==120){
buffer[1]=12;
connection.send(2727);
System.out.println("Sent");
}
}
}
@Override
public void render() {
sr.setAutoShapeType(true);
sr.begin();
sr.set(ShapeRenderer.ShapeType.Filled);
sr.box(0,0,0,200,200,0);
sr.end();
}
}
@@ -20,7 +20,7 @@
Server(byte[] buffer){
super("server");
try {
socket = new DatagramSocket(6565);
socket = new DatagramSocket(5656);
}catch(SocketException e){
System.out.println("Socket not opened;");
}

0 comments on commit 86c8dff

Please sign in to comment.