From c8e23e4fa4f7ad5cad49a4e9258639468ac60be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Tue, 28 Feb 2012 12:53:30 +0100 Subject: Update player position with AtomicReference. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit – Need to trigger updates somehow. --- .../src/main/java/com/orbekk/same/GameView.java | 30 ++++++++++++++++++---- .../orbekk/same/android/ClientInterfaceBridge.java | 2 +- 2 files changed, 26 insertions(+), 6 deletions(-) (limited to 'same-android/src') 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; + private AtomicReference currentPosition = + new AtomicReference(); + 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 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); -- cgit v1.2.3