diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-18 21:08:26 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-18 21:08:26 +0100 |
commit | 5b27e3c7b289792b6520a099280a6c1bc28c20e8 (patch) | |
tree | d89b964dd0e4ea0f49c000e8853a15a8762e62e8 /same-android/src/main/java/com/orbekk/GameView.java | |
parent | 6c150264b21eded212b9e2cf9c802b36c3f75144 (diff) |
Hack: Use Same to synchronize state in the android app.
Diffstat (limited to 'same-android/src/main/java/com/orbekk/GameView.java')
-rw-r--r-- | same-android/src/main/java/com/orbekk/GameView.java | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/same-android/src/main/java/com/orbekk/GameView.java b/same-android/src/main/java/com/orbekk/GameView.java index 4accd7a..edb41cb 100644 --- a/same-android/src/main/java/com/orbekk/GameView.java +++ b/same-android/src/main/java/com/orbekk/GameView.java @@ -3,6 +3,10 @@ package com.orbekk; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.orbekk.same.State.Component; +import com.orbekk.same.ClientService; +import com.orbekk.same.ClientServiceImpl; + import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; @@ -15,6 +19,7 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback { private GameThread thread; static class GameThread extends Thread { + private Logger logger = LoggerFactory.getLogger(getClass()); private int height = 0; private int width = 0; private float posX; @@ -23,10 +28,13 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback { private Context context; private Paint background; private Paint paint; + private ClientServiceImpl client; - public GameThread(SurfaceHolder holder, Context context) { + public GameThread(SurfaceHolder holder, Context context, + ClientServiceImpl client) { this.holder = holder; this.context = context; + this.client = client; posX = 100; posY = 100; paint = new Paint(); @@ -60,17 +68,27 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback { } } - private void setPosition(float x, float y) { + private synchronized void setPosition(float x, float y) { posX = x; posY = y; run(); + long rev = 0; + Component c = client.getState("position"); + if (c != null) { + rev = c.getRevision(); + } + + if (client.sendStateUpdate("position", this.posX + "," + this.posY, + rev + 1)) { + logger.warn("Unable to set state."); + } } } - public GameView(Context context) { + public GameView(Context context, ClientServiceImpl client) { super(context); getHolder().addCallback(this); - thread = new GameThread(getHolder(), context); + thread = new GameThread(getHolder(), context, client); } @Override |