Skip to content

Commit

Permalink
ship serialization, multiplayer sim in swing
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Lankenau committed May 10, 2011
1 parent ce3ad95 commit 1832ea0
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 37 deletions.
4 changes: 2 additions & 2 deletions AndroidClient/src/org/galaxy/GalaxyScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ public void draw(Canvas canvas) {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Planet planetDragFrom = game.findPlanet(event.getX(), event.getY(),
game.getPlayer());
game.getMe());
if (planetDragFrom != null)
dragFrom.add(planetDragFrom);

} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
possibleTarget = null;
if (dragFrom.size() > 0) {
Planet planetDragFrom = game.findPlanet(event.getX(), event.getY(),
game.getPlayer());
game.getMe());
if (planetDragFrom != null)
dragFrom.add(planetDragFrom);

Expand Down
3 changes: 3 additions & 0 deletions Galaxy/.classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="/home/mlankenau/projects/Galaxy/lib/json_simple-1.1.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
53 changes: 39 additions & 14 deletions Galaxy/src/org/galaxy/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ public class Game {
private ArrayList<Ship> ships = new ArrayList<Ship>();
private ArrayList<Move> moves = new ArrayList<Move>();

private Party player = null;
private Party computer = null;
private Party me = null;
private Party opponent = null;
private Party neutral = null;

Remote remote = null;

public Party getPlayer() {
return player;
public Party getMe() {
return me;
}
public Party getComputer() {
return computer;
public Party getOpponent() {
return opponent;
}
public Party getNeutral() {
return neutral;
Expand All @@ -40,9 +40,11 @@ public void setShips(ArrayList<Ship> ships) {


public void initEmpty() {
player = new Party("me", 0xff00a000);
computer = new Party("other", 0xffa00000);
me = new Party("me", 0xff00a000);
opponent = new Party("opponent", 0xffa00000);
neutral = new Party("", 0xffa0a0a0);

remote = new Remote("localhost", 10001, opponent);
}

/**
Expand All @@ -51,11 +53,11 @@ public void initEmpty() {
public void initRandom() {
float boarder = 30;

player = new Party("player", 0xff00a000);
computer = new Party("computer", 0xffa00000);
me = new Party("player", 0xff00a000);
opponent = new Party("computer", 0xffa00000);
neutral = new Party("", 0xffa0a0a0);

remote = new Remote("localhost", 10001, computer);
remote = new Remote("localhost", 10001, opponent);

for (int i = 0; i < 6; i++) {

Expand All @@ -82,15 +84,30 @@ public void initRandom() {

Party party = neutral;
if (i == 0)
party = player;
party = me;
if (i == 1)
party = computer;
party = opponent;

planets.add(new Planet(party, new Vector(x, y), pc));
planets.add(new Planet(i, party, new Vector(x, y), pc));
}
}


public void chooseSide() {
if (!remote.isFirst()) {
takeOtherSide();
}
}

private void takeOtherSide() {
for (Planet planet : planets) {
if (planet.getParty() == me)
planet.setParty(opponent);
else if (planet.getParty() == opponent)
planet.setParty(me);
}
}

/**
* find a planet at the coordinates
*/
Expand Down Expand Up @@ -143,4 +160,12 @@ public void move(Planet source, Planet target) {
remote.sendMove(newShips);
}

public Planet findPlanet(String id) {
for (Planet p : planets) {
if (p.getId().equals(id))
return p;
}
return null;
}

}
17 changes: 10 additions & 7 deletions Galaxy/src/org/galaxy/GameFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ public static Game getLevel(int level) {

game.initEmpty();

game.getPlanets().add(new Planet(game.getComputer(), new Vector(50, 50), PlanetClass.planetClasses[0]));
int pid = 0;
game.getPlanets().add(new Planet(pid++, game.getOpponent(), new Vector(50, 50), PlanetClass.planetClasses[0]));

game.getPlanets().add(new Planet(game.getNeutral(), new Vector(70, 100), PlanetClass.planetClasses[1]));
game.getPlanets().add(new Planet(pid++, game.getNeutral(), new Vector(70, 100), PlanetClass.planetClasses[1]));

game.getPlanets().add(new Planet(game.getNeutral(), new Vector(200, 150), PlanetClass.planetClasses[1]));
game.getPlanets().add(new Planet(pid++, game.getNeutral(), new Vector(200, 150), PlanetClass.planetClasses[1]));

game.getPlanets().add(new Planet(game.getNeutral(), new Vector(250, 300), PlanetClass.planetClasses[2]));
game.getPlanets().add(new Planet(pid++, game.getNeutral(), new Vector(250, 300), PlanetClass.planetClasses[2]));

game.getPlanets().add(new Planet(game.getNeutral(), new Vector(350, 390), PlanetClass.planetClasses[1]));
game.getPlanets().add(new Planet(game.getNeutral(), new Vector(100, 410), PlanetClass.planetClasses[1]));
game.getPlanets().add(new Planet(pid++, game.getNeutral(), new Vector(350, 390), PlanetClass.planetClasses[1]));
game.getPlanets().add(new Planet(pid++, game.getNeutral(), new Vector(100, 410), PlanetClass.planetClasses[1]));

game.getPlanets().add(new Planet(game.getPlayer(), new Vector(320, 450), PlanetClass.planetClasses[0]));
game.getPlanets().add(new Planet(pid++, game.getMe(), new Vector(320, 450), PlanetClass.planetClasses[0]));


game.chooseSide();
return game;

}
Expand Down
4 changes: 4 additions & 0 deletions Galaxy/src/org/galaxy/Party.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ public String getId() {
return name;
}

public String toString() {
return "party:"+name;
}

}
4 changes: 2 additions & 2 deletions Galaxy/src/org/galaxy/Planet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public void setParty(Party party) {
this.party = party;
}

public Planet(Party party, Vector pos, PlanetClass pc) {
public Planet(int id, Party party, Vector pos, PlanetClass pc) {
this.pos = pos;
this.size = pc.getSize();
this.energy = pc.getInitialEnergy();
this.party = party;
this.pc = pc;
id = nextId++;
this.id = id;
}

public String toJson() {
Expand Down
80 changes: 70 additions & 10 deletions Galaxy/src/org/galaxy/Remote.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,84 @@

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.BindException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.List;

public class Remote {
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class Remote {
DatagramSocket socket = null;
boolean first = true;


public boolean isFirst() {
return first;
}

public Remote(String host, int port, Party party) {
try {
socket = new DatagramSocket(10001);
socket.connect(InetAddress.getByName(host), 10002);
}
catch (BindException be) {
try {
socket = new DatagramSocket(8888);
socket.connect(InetAddress.getByName(host), port);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
socket = new DatagramSocket(10002);
socket.connect(InetAddress.getByName(host), 10001);
first = false;
}
catch (Exception e) {
e.printStackTrace();
}

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}


Runnable runnable = new Runnable() {

@Override
public void run() {
receiveThread();
}
};
new Thread(runnable).start();
}

protected void receiveThread() {
while (true) {
byte[] buffer = new byte[10000];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
try {
socket.receive(packet);
String s = new String(buffer, "UTF-8");
System.out.println("received: " + s);
JSONArray array = (JSONArray) JSONValue.parse(s);
for (int i=0; i<array.size(); i++) {
JSONObject shipObj = (JSONObject) array.get(i);
String party = shipObj.get("party").toString();
String source = shipObj.get("source").toString();
String destination = shipObj.get("destination").toString();
String speed = shipObj.get("speed").toString();
String lauchTime = shipObj.get("lauchTime").toString();
String deviation = shipObj.get("deviation").toString();
}


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

public void sendMove(List<Ship> ships) {
Expand All @@ -46,8 +103,11 @@ public void sendMove(List<Ship> ships) {
catch (IOException e) {
e.printStackTrace();
}


}


public void getMoves() {
}


}
38 changes: 38 additions & 0 deletions Galaxy/src/org/galaxy/Ship.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.galaxy;

import org.json.simple.JSONObject;

public class Ship {
Planet source = null;
Planet dest = null;
Expand All @@ -21,6 +23,42 @@ public void setParty(Party party) {
Vector pos;
float speed;


public long getLauchTime() {
return lauchTime;
}

public float getDeviation() {
return deviation;
}

public float getSpeed() {
return speed;
}

public Ship(Game game, JSONObject json) {
source = game.findPlanet(json.get("source").toString());
dest = game.findPlanet(json.get("destination").toString());


String partyStr = json.get("party").toString();
if (partyStr.equals(""))
party = game.getNeutral();
else if (partyStr.equals("me"))
party = game.getOpponent();
else if (partyStr.equals("opponent"))
party = game.getMe();



String speed = json.get("speed").toString();
String lauchTime = json.get("lauchTime").toString();
String deviation = json.get("deviation").toString();



}

public Ship(Party party, Planet source, Planet dest, float speed, long lauchTime) {
long launchLag = (long) (Math.random() * MAX_LAUNCHING_LAG);

Expand Down
41 changes: 41 additions & 0 deletions Galaxy/test/org/galaxy/ShipTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.galaxy;

import java.util.Date;

import static junit.framework.Assert.*;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.junit.Test;



public class ShipTest {

@Test
public void testJson() {
Game game = new Game();
game.initEmpty();

long time = new Date().getTime();

Planet a = new Planet(0, game.getMe(), new Vector(0,0), PlanetClass.planetClasses[1]);
Planet b = new Planet(1, game.getMe(), new Vector(0,0), PlanetClass.planetClasses[1]);
game.getPlanets().add(a);
game.getPlanets().add(b);

Ship ship = new Ship(game.getMe(),a, b, 10.f, time);
String jsonString = ship.toJson();

assertNotNull(jsonString);

JSONObject obj = (JSONObject) JSONValue.parse(jsonString);
Ship shipCopy = new Ship(game, obj);

assertEquals(ship.getSource(), shipCopy.getSource());
assertEquals(ship.getDest(), shipCopy.getDest());
assertEquals(game.getOpponent(), ship.getParty());
assertEquals(ship.getSpeed(), shipCopy.getSpeed());
assertEquals(ship.getDeviation(), shipCopy.getDeviation());
}
}
Loading

0 comments on commit 1832ea0

Please sign in to comment.