Okay, So I'm working on a game (like not even alpha yet) and it's suppose to be a 1 on 1 fighting game where one person hosts the server and the other one connects. But right now the code is too laggy to do anything with. Can someone take a look at it and tell me how to speed it up? PS: I'm also using the Slick2D lib.
Server:
import java.net.*;
import java.io.*;
public class SlickServer{
public static void main(String[] args) throws IOException {
int MAX_PLAYERS = 3;
int playerNum = 0;
Player[] players = new Player[MAX_PLAYERS];
players[0] = new Player(25,25);
players[1] = new Player(125,125);
players[2] = new Player(225,225);
ServerSocket serverSocket = new ServerSocket(4444);
boolean listening = true;
while(listening){
System.out.println("Waiting to connect with: " + playerNum);
new ClientThread(serverSocket.accept(), players, playerNum).start();
//stops here.
System.out.println("Connected with: " + playerNum + " Now incrementing");
playerNum++;
System.out.println("Incremented to: " + playerNum);
}
serverSocket.close();
System.exit(0);
}
}
Server Thread:
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
import java.io.*;
public class ClientThread extends Thread implements Runnable{
Socket acceptedSocket;
Player[] players;
int playerNum;
public ClientThread(Socket acceptedSocket, Player[] players, int playerNum){
super("ClientThread");
this.acceptedSocket = acceptedSocket;
this.players = players;
this.playerNum = playerNum;
}
public void run(){
try{
Socket clientSocket = acceptedSocket;
System.out.println("Accepted. Now creating I/O.");
ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
System.out.println("I/O with: " + playerNum + " working.");
out.writeInt(playerNum);
out.flush();
while(true){
if(playerNum == 0){
players[0].x = in.readInt();
players[0].y = in.readInt();
out.writeInt(players[1].x);
out.writeInt(players[1].y);
out.flush();
}
else if(playerNum == 1){
players[1].x = in.readInt();
players[1].y = in.readInt();
out.writeInt(players[0].x);
out.writeInt(players[0].y);
out.flush();
}
else if(playerNum == 2){
players[2].x = in.readInt();
players[2].y = in.readInt();
out.writeInt(players[0].x);
out.writeInt(players[0].y);
out.flush();
}
}
}
catch(Exception e){
e.printStackTrace();
System.exit(1);
}
}
}
Client:
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import java.io.*;
import java.net.*;
public class SlickClient extends BasicGame{
int MAX_PLAYERS = 3;
int playerNum = 0;
Player[] players;
ClientThread ct;
int serverDelay = 15;
public SlickClient()
{
super("Client");
}
@Override
public void init(GameContainer gc)
throws SlickException {
try{
players = new Player[MAX_PLAYERS];
players[0] = new Player(25,25);
players[1] = new Player(125,125);
ct = new ClientThread(players);
ct.start();
ct.setPriority(Thread.MAX_PRIORITY);
playerNum = ct.playerNum;
}
catch(Exception e){
e.printStackTrace();
}
}
@Override
public void update(GameContainer gc, int delta)
throws SlickException
{
Input input = gc.getInput();
if(input.isKeyDown(Input.KEY_A))
{
players[playerNum].x-=5;
}
if(input.isKeyDown(Input.KEY_D))
{
players[playerNum].x+=5;
}
if(input.isKeyDown(Input.KEY_W))
{
players[playerNum].y-=5;
}
if(input.isKeyDown(Input.KEY_S))
{
players[playerNum].y+=5;
}
}
public void render(GameContainer gc, Graphics g)
throws SlickException
{
g.fillRect(players[0].x, players[0].y, 50, 50);
g.fillRect(players[1].x, players[1].y, 50, 50);
}
public static void main(String[] args)
throws SlickException
{
AppGameContainer app =
new AppGameContainer( new SlickClient() );
app.setAlwaysRender(true);
app.setTargetFrameRate(30);
app.setDisplayMode(800, 600, false);
app.start();
}
}
class ClientThread extends Thread implements Runnable{
Socket socket;
Player[] players;
int playerNum;
ObjectOutputStream out;
ObjectInputStream in;
public ClientThread(Player[] players){
super("ClientThread");
try{
socket = new Socket("98.203.176.196", 4444);
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
this.players = players;
playerNum = in.readInt();
System.out.println(playerNum);
}
catch(Exception e){
e.printStackTrace();
}
}
public void run(){
try{
while(true){
if(playerNum == 0){
try{
out.writeInt(players[0].x);
out.writeInt(players[0].y);
out.flush();
players[1].x = in.readInt();
players[1].y = in.readInt();
}
catch(Exception e){
e.printStackTrace();
}
}
else if(playerNum == 1){
try{
out.writeInt(players[1].x);
out.writeInt(players[1].y);
out.flush();
players[0].x = in.readInt();
players[0].y = in.readInt();
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}