Skip to content

Commit

Permalink
Clients that send bullet packets only need to send rotation now.
Browse files Browse the repository at this point in the history
Later, when server does the accuracy calculations, rotation will also
not be needed.

Moved the map stuff to the World class.

Change-Id: I5fb897672fa113a1c7cb20d512c8b1e2f8986218
  • Loading branch information
darcymiranda committed May 26, 2011
1 parent 59e63f1 commit fdb7796
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 78 deletions.
Binary file modified bin/client/revert/Bullet.class
Binary file not shown.
Binary file modified bin/client/revert/Entity.class
Binary file not shown.
Binary file modified bin/client/revert/Ship.class
Binary file not shown.
Binary file modified bin/packet/Packet.class
Binary file not shown.
Binary file modified bin/server/Client.class
Binary file not shown.
Binary file modified bin/server/Server$AcceptConnections.class
Binary file not shown.
Binary file modified bin/server/Server.class
Binary file not shown.
Binary file modified bin/server/World.class
Binary file not shown.
6 changes: 0 additions & 6 deletions src/client/revert/Bullet.java
Expand Up @@ -2,7 +2,6 @@

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.util.Log;

public class Bullet extends Entity {

Expand All @@ -26,9 +25,6 @@ public Bullet(float x, float y, float r, Vector2f shipVel){
velocity = new Vector2f(-(speed * (float) Math.sin(Math.toRadians(rotation+180))) + shipVel.x,
-(speed * (float) Math.cos(Math.toRadians(rotation+180))) + shipVel.y);

//velocity.x = -((speed * delta) * (float) Math.sin(Math.toRadians(rotation+180)));
//velocity.y = -((speed * delta) * (float) Math.cos(Math.toRadians(rotation+180)));

collidable = true;
}

Expand All @@ -40,8 +36,6 @@ public void update(GameContainer gc, int delta){
super.update(gc, delta);

travelTime++;
if(travelTime < maxTravelTime && test_real)
Log.info(clientPosition.x + " : " + clientPosition.y + " -- " + travelTime);
}

public boolean hasExpired(){
Expand Down
22 changes: 19 additions & 3 deletions src/client/revert/Entity.java
Expand Up @@ -23,11 +23,12 @@ public abstract class Entity {
protected boolean isAlive;
protected boolean collidable;

private Image image;

protected String displayText = "";
protected UnicodeFont font;
protected Color minimapColor;
protected Rectangle hitBox;

private Image image;

public Entity(){
clientPosition = new Vector2f();
Expand All @@ -36,6 +37,8 @@ public Entity(){
dirSpeed = new Vector2f();
isAlive = true;

hitBox = new Rectangle(clientPosition.x, clientPosition.y, height, width);

}

abstract public void collide(Entity e);
Expand Down Expand Up @@ -69,13 +72,16 @@ public void update(GameContainer gc, int delta){
clientPosition.x += velocity.x * delta;
clientPosition.y -= velocity.y * delta;

hitBox.setLocation(clientPosition.x, clientPosition.y);

image.rotate(rotation - image.getRotation());

}

public void render(Graphics g){

g.drawImage(image, clientPosition.x, clientPosition.y);
if(image != null)
g.drawImage(image, clientPosition.x, clientPosition.y);

}

Expand All @@ -87,6 +93,16 @@ public void render(Graphics g){

public int getId(){ return id; }

public void setHeight(int h){
this.height = h;
hitBox.setHeight(h);
}

public void setWidth(int w){
this.width = w;
hitBox.setWidth(w);
}

public Vector2f getClientPosition(){ return new Vector2f(clientPosition); }
public Vector2f getMinimapPosition() { return new Vector2f(minimapPosition); }
public Vector2f getVelocity(){ return new Vector2f(velocity); }
Expand Down
14 changes: 7 additions & 7 deletions src/client/revert/Ship.java
Expand Up @@ -94,7 +94,7 @@ public void update(GameContainer gc, int delta){
isShooting = in.isKeyDown(Input.KEY_SPACE);
if(isShooting){

shoot(delta);
shoot();

}

Expand Down Expand Up @@ -289,7 +289,7 @@ public void update(GameContainer gc, int delta){
// REMOTE
else {
if(isShooting){
shoot(delta);
shoot();
}
}

Expand All @@ -307,7 +307,10 @@ public void render(Graphics g){

}

private void shoot(int delta){
/**
* Shoot a bullet from the ship.
*/
private void shoot(){

Bullet bullet;
if (!(System.currentTimeMillis() - lastFire < rate)) {
Expand All @@ -326,10 +329,7 @@ private void shoot(int delta){

// REMOTE
if(super.isLocal()){

// !! sending the ships velocity on purpose
Packet packet = new Packet(Packet.UPDATE_SELF_BULLET, bullet.clientPosition.x,
bullet.clientPosition.y, velocity.x, velocity.y, bullet.rotation);
Packet packet = new Packet(Packet.UPDATE_SELF_BULLET, bullet.rotation);
Revert.net.send(packet);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/packet/Packet.java
Expand Up @@ -106,6 +106,11 @@ public Packet(byte t, float x, float y, float xv, float yv, float r){
this.yv = yv;
}

public Packet(byte t, float r) {
this.type = t;
this.r = r;
}

public void setMessage(String message){ this.message = message; }
public String getMessage(){ return message; }

Expand Down
9 changes: 5 additions & 4 deletions src/server/Client.java
Expand Up @@ -25,7 +25,7 @@ public class Client extends Thread {
private InetAddress host;

/* is the client ready to start a game (Ready button has been clicked) */
private boolean isReady;
private boolean isSpawned;

/* determines wiether a connection is fully established ( a connection packet went back and forth ) */
private boolean connEstablished;
Expand Down Expand Up @@ -95,6 +95,7 @@ public void process(){

public void createShip(){
ship = new Ship(Constants.SPAWN_POSITION_X, Constants.SPAWN_POSITION_Y, 36, 45, true);
ship.id = id;
}

/**
Expand Down Expand Up @@ -129,13 +130,13 @@ public void disconnect(){
}

public void reset(){
isReady = false;
isSpawned = false;
connEstablished = false;
username = "unkown";
}

public boolean getReadyStatus(){ return isReady; }
public void setReadyStatus(boolean r){ this.isReady = r; }
public boolean getSpawnStatus(){ return isSpawned; }
public void setSpawnStatus(boolean r){ this.isSpawned = r; }

public boolean getConnectionStatus(){ return connEstablished; }
public void setConnectionStatus(boolean s){ this.connEstablished = s; }
Expand Down
66 changes: 21 additions & 45 deletions src/server/Server.java
Expand Up @@ -30,7 +30,7 @@ public class Server implements Runnable{
private InetSocketAddress address;
private List<Client> clients = new LinkedList<Client>();
private World world;
private PlayableMap map;


private long time;
private final int tickRate = 16;
Expand All @@ -45,38 +45,39 @@ public Server(int port){
* @throws IOException
*/
public void init() throws IOException{

// Start the world.
Log.info(" Starting world");
world = World.getInstance();
world.setServer(this);
world.init();

// Begin accepting connections.
Log.info(" Starting server on " + address);
serverSocket = new ServerSocket();
serverSocket.bind(address);

accepter = new AcceptConnections();
accepter.start();
isGameStarted = false;
acceptConnections = true;

world = World.getInstance();
world.setServer(this);

map = new PlayableMap(Constants.MAP_001);

}

@Override
public void run(){

try{

Log.info("Starting server on " + address);
init();
Log.info("Server online.\n");
Log.info(" Server online.\n");

for(;;){

// Processes recieved packets.

process();


/* Remove clients that have disconnected. */
// Remove clients that have disconnected.
Client client;
for(int i = 0; i < clients.size(); i ++){
client = clients.get(i);
Expand All @@ -92,7 +93,6 @@ public void run(){
for(int j = 0; j < clients.size(); j++){
Client disClient = clients.get(j);
disClient.send(new Packet(Packet.DISCONNECT, tempId));
//System.out.println("Sent disconnect notifcation about " + tempId + " to " + clients.get(j).id);
}

}
Expand All @@ -114,11 +114,7 @@ public void run(){
}

/**
* Main game processor. Determines what to do with packets.
*/
/**
* TODO: Thinking about putting this on the client process method...may cause a lot
* of headaches.
* Main loop.
*/
public void process(){

Expand Down Expand Up @@ -157,20 +153,20 @@ else if(packet.type == Packet.CONNECT){

// Sends around the names of clients/ids to eachother.
// Send to all clients including self to confirm ready mark client side.
sendToAll(new Packet(Packet.CONNECT, client.id, packet.getUsername(), client.getReadyStatus()), false);
sendToAll(new Packet(Packet.CONNECT, client.id, packet.getUsername(), client.getSpawnStatus()), false);

// Sends data from currently connected clients to the newly connected client.
sendAllToClient(client, new Packet(Packet.CONNECT));
sendAllToClient(client, new Packet(Packet.READY_MARKER)); // make sure new client knows of everyones ready status
// additional client info sent here

}

// Set ready status flag for clients
else if(packet.type == Packet.READY_MARKER && !client.getReadyStatus()){
client.setReadyStatus(packet.getStatus());
else if(packet.type == Packet.READY_MARKER && !client.getSpawnStatus()){
client.setSpawnStatus(packet.getStatus());

if(client.getReadyStatus()) client.createShip();
if(client.getSpawnStatus())
client.createShip();

String rstatus = packet.getStatus() ? "ready" : "not ready";
System.out.println(client + " is " + rstatus + ".");
Expand Down Expand Up @@ -207,6 +203,7 @@ else if(packet.type == Packet.REQUEST_MAP){

//send client the map
ArrayList<Vector2f> list;
PlayableMap map = world.getCurrentMap();
list = map.getAsteroids();

//sends asteroids
Expand Down Expand Up @@ -240,22 +237,6 @@ else if(packet.type == Packet.REQUEST_MAP){

world.process();

/*
// Check if all clients are ready
int countReadyStatus = 0;
if(!isGameStarted){
for(Client client : clients){
if(client.getReadyStatus()){
countReadyStatus++;
}
}
// All clients are ready and host wants to start, start the game!
if(countReadyStatus == clients.size() && startGameASAP){
startGame();
}
}
*/

}

/**
Expand Down Expand Up @@ -309,12 +290,12 @@ public void sendAllToClient(Client client, Packet packet){
// Determine what kind of packet it is and fill accordingly.
if(packet.type == Packet.READY_MARKER){
tempPacket.setId(iClient.id);
tempPacket.setStatus(iClient.getReadyStatus());
tempPacket.setStatus(iClient.getSpawnStatus());
}
else if(packet.type == Packet.CONNECT){
tempPacket.setId(iClient.id);
tempPacket.setUsername(iClient.getUsername());
tempPacket.setStatus(iClient.getReadyStatus());
tempPacket.setStatus(iClient.getSpawnStatus());
}
else{
tempPacket.type = Packet.SERVER_MESSAGE;
Expand Down Expand Up @@ -366,9 +347,6 @@ public synchronized final void disconnect() throws IOException{
*/
private void sleep() throws InterruptedException {




long sleepTime = tickRate - (System.currentTimeMillis() - time);
if (sleepTime > 0) {
Thread.sleep(sleepTime);
Expand Down Expand Up @@ -412,8 +390,6 @@ public void run(){
clients.add(client);
System.out.println(client + " connected.");



}catch(Exception e){
//e.printStackTrace();
try {
Expand Down

0 comments on commit fdb7796

Please sign in to comment.