summaryrefslogtreecommitdiff
path: root/same-android/src/main/java
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-20 11:04:44 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-20 11:04:44 +0100
commit2e0c6dd5ca704b85b15921e9e6707f6c3fcb3c9f (patch)
tree31354e5b2c48a295e1246d0dc9ebf0fb349f4ba3 /same-android/src/main/java
parent3f5c54d56de87ac2afc51222dfa2373b5bb8238e (diff)
Refactor Android code.
– Make a Controller and a state that supports several players.
Diffstat (limited to 'same-android/src/main/java')
-rw-r--r--same-android/src/main/java/com/orbekk/GameController.java33
-rw-r--r--same-android/src/main/java/com/orbekk/GameView.java42
-rw-r--r--same-android/src/main/java/com/orbekk/HelloAndroidActivity.java4
3 files changed, 49 insertions, 30 deletions
diff --git a/same-android/src/main/java/com/orbekk/GameController.java b/same-android/src/main/java/com/orbekk/GameController.java
index 8e1e169..0c11a75 100644
--- a/same-android/src/main/java/com/orbekk/GameController.java
+++ b/same-android/src/main/java/com/orbekk/GameController.java
@@ -6,27 +6,48 @@ import java.util.List;
import android.graphics.Paint;
public class GameController {
- private List<Player> players = new ArrayList<Player>();
+ private List<Player> remotePlayers = new ArrayList<Player>();
private Player localPlayer;
+ private ChangeListener changeListener = null;
public static class Player {
public Paint color;
- public int posX;
- public int posY;
+ public float posX;
+ public float posY;
+ }
+
+ public interface ChangeListener {
+ void playerStatesChanged();
+ }
+
+ public static Player newPlayer() {
+ Player player = new Player();
+ player.color = new Paint();
+ player.color.setARGB(255, 255, 0, 0);
+ player.posX = 0.5f;
+ player.posY = 0.5f;
+ return player;
}
public GameController(Player localPlayer) {
this.localPlayer = localPlayer;
}
+ public void setMyPosition(float x, float y) {
+ this.localPlayer.posX = x;
+ this.localPlayer.posY = y;
+ changeListener.playerStatesChanged();
+ }
+
public Player getLocalPlayer() {
return localPlayer;
}
public List<Player> getRemotePlayers() {
- return players;
+ return remotePlayers;
}
-
-
+ public void setChangeListener(ChangeListener listener) {
+ this.changeListener = listener;
+ }
}
diff --git a/same-android/src/main/java/com/orbekk/GameView.java b/same-android/src/main/java/com/orbekk/GameView.java
index 1240671..620194e 100644
--- a/same-android/src/main/java/com/orbekk/GameView.java
+++ b/same-android/src/main/java/com/orbekk/GameView.java
@@ -20,28 +20,22 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private Logger logger = LoggerFactory.getLogger(getClass());
private GameThread thread;
- static class GameThread extends Thread {
+ static class GameThread extends Thread
+ implements GameController.ChangeListener {
private Logger logger = LoggerFactory.getLogger(getClass());
private int height = 0;
private int width = 0;
- private float posX;
- private float posY;
private SurfaceHolder holder;
private Context context;
private Paint background;
- private Paint paint;
- private SameInterface same;
+ private GameController controller;
public GameThread(SurfaceHolder holder, Context context,
- SameInterface client) {
+ GameController controller) {
this.holder = holder;
this.context = context;
- this.same = client;
- posX = 100;
- posY = 100;
- paint = new Paint();
- paint.setAntiAlias(true);
- paint.setARGB(255, 255, 0, 0);
+ this.controller = controller;
+ this.controller.setChangeListener(this);
background = new Paint();
background.setARGB(255, 0, 0, 0);
}
@@ -55,7 +49,12 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private void doDraw(Canvas c) {
c.drawRect(0.0f, 0.0f, width+1.0f, height+1.0f, background);
- c.drawCircle(posX, posY, 20.0f, paint);
+ for (GameController.Player p : controller.getRemotePlayers()) {
+ c.drawCircle(p.posX * width, p.posY * height, 20.0f, p.color);
+ }
+ GameController.Player localPlayer = controller.getLocalPlayer();
+ c.drawCircle(localPlayer.posX * width, localPlayer.posY * height,
+ 20.0f, localPlayer.color);
}
@Override public void run() {
@@ -71,22 +70,19 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback {
}
private synchronized void setPosition(float x, float y) {
- posX = x;
- posY = y;
+ controller.setMyPosition(x / this.width, y / this.height);
+ }
+
+ @Override
+ public void playerStatesChanged() {
run();
- try {
- same.set("position", this.posX + "," + this.posY);
- } catch (UpdateConflict e) {
- logger.warn("Update conflict.", e);
- }
-
}
}
- public GameView(Context context, SameInterface client) {
+ public GameView(Context context, GameController controller) {
super(context);
getHolder().addCallback(this);
- thread = new GameThread(getHolder(), context, client);
+ thread = new GameThread(getHolder(), context, controller);
}
@Override
diff --git a/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java b/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java
index 87c349c..dfa5d20 100644
--- a/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java
+++ b/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java
@@ -31,7 +31,9 @@ public class HelloAndroidActivity extends Activity {
ClientApp client = new ClientApp();
SameInterface client_ = client.getClient(10015, "ClientNetwork",
"http://10.0.0.6:10010/");
- setContentView(new GameView(this, client_));
+ GameController controller = new GameController(
+ GameController.newPlayer());
+ setContentView(new GameView(this, controller));
// Broadcast broadcast = new Broadcast(this);
// broadcast.sendBroadcast("Broadcast test".getBytes(), 10010);