diff options
Diffstat (limited to 'same-android/src/main/java/com')
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); |