summaryrefslogtreecommitdiff
path: root/same-android/src/main/java/com/orbekk/same
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-28 12:53:30 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-28 12:53:30 +0100
commitc8e23e4fa4f7ad5cad49a4e9258639468ac60be5 (patch)
tree37507fceb1d2ec86c846899d2261ee8ec123b507 /same-android/src/main/java/com/orbekk/same
parentb1ffd97b42eecbc1da5ec79775f2cc94cbeedc09 (diff)
Update player position with AtomicReference.
– Need to trigger updates somehow.
Diffstat (limited to 'same-android/src/main/java/com/orbekk/same')
-rw-r--r--same-android/src/main/java/com/orbekk/same/GameView.java30
-rw-r--r--same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java2
2 files changed, 26 insertions, 6 deletions
diff --git a/same-android/src/main/java/com/orbekk/same/GameView.java b/same-android/src/main/java/com/orbekk/same/GameView.java
index d9a8052..9290bad 100644
--- a/same-android/src/main/java/com/orbekk/same/GameView.java
+++ b/same-android/src/main/java/com/orbekk/same/GameView.java
@@ -16,6 +16,8 @@ import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
+import java.util.concurrent.atomic.AtomicReference;
+
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private Logger logger = LoggerFactory.getLogger(getClass());
private GameThread thread;
@@ -34,6 +36,9 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private Context context;
private Paint background;
private Variable<Player> player;
+ private AtomicReference<Player> currentPosition =
+ new AtomicReference<Player>();
+
private Paint color = new Paint();
public GameThread(SurfaceHolder holder, Context context,
@@ -41,6 +46,15 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback {
this.holder = holder;
this.context = context;
this.player = player;
+ if (player.get() != null) {
+ currentPosition.set(player.get());
+ } else {
+ currentPosition.set(new Player() {{
+ posX = 0.5f;
+ posY = 0.5f;
+ }});
+ }
+
background = new Paint();
background.setARGB(255, 0, 0, 0);
color.setARGB(255, 255, 0, 0);
@@ -83,17 +97,23 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback {
}
}
- private synchronized void setPosition(float x, float y) {
- Player newPlayer = new Player();
- newPlayer.posX = x / width;
- newPlayer.posY = y / width;
- player.set(newPlayer);
+ private synchronized void setPosition(final float x, final float y) {
+ Player newPlayer = new Player() {{
+ posX = x / width;
+ posY = y / width;
+ }};
+ currentPosition.set(newPlayer);
}
@Override
public void valueChanged(Variable<Player> unused) {
logger.info("Variable updated.");
player.update();
+ if (player.get() == null ||
+ currentPosition.get().posX != player.get().posX ||
+ currentPosition.get().posY != player.get().posY) {
+ player.set(currentPosition.get());
+ }
run();
}
}
diff --git a/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java b/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java
index dfab773..faa1072 100644
--- a/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java
+++ b/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java
@@ -90,7 +90,7 @@ public class ClientInterfaceBridge implements ClientInterface {
};
private void updateState(State.Component component) {
- state.update(component.getName(), component.getData(),
+ state.forceUpdate(component.getName(), component.getData(),
component.getRevision());
for (StateChangedListener listener : listeners) {
listener.stateChanged(component);