--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/proto2/GameState.cc Sun Nov 09 22:02:03 2008 +0000
@@ -0,0 +1,18 @@
+
+#include "GameState.hh"
+
+bool Player::updatePosition (Coordinate p) {
+ if (!state.isValidCoordinate(p)) {
+ // out-of-bounds
+ return false;
+
+ } else {
+ // valid
+ position = p;
+
+ return true;
+ }
+}
+
+
+
--- a/src/proto2/GameState.hh Sun Nov 09 21:51:13 2008 +0000
+++ b/src/proto2/GameState.hh Sun Nov 09 22:02:03 2008 +0000
@@ -16,13 +16,18 @@
#define MAP_DIM_W 800
#define MAP_DIM_H 640
+// forward-declare GameState
+class GameState;
+
class Player {
protected:
Coordinate position;
+ GameState &state;
+
public:
- Player(Coordinate c, bool visible) : position(c), dimensions(PLAYER_DIM_W, PLAYER_DIM_H), visible(visible) {}
+ Player(GameState &state, Coordinate c, bool visible) : position(c), state(state), dimensions(PLAYER_DIM_W, PLAYER_DIM_H), visible(visible) {}
PlayerType type;
Dimension dimensions;
@@ -38,36 +43,23 @@
*
* Returns true if valid move (not out of bounds), false otherwise (doesn't change position)
*/
- bool updatePosition (Coordinate p) {
- // unsigned...
- if (p.x > dimensions.w || p.y > dimensions.h) {
- // out-of-bounds
- return false;
- }
-
- // valid
- position = p;
-
- return true;
- }
+ bool updatePosition (Coordinate p);
};
class LocalPlayer : public Player {
protected:
- LocalPlayer (Coordinate c, bool visible) : Player(c, visible) { }
+ LocalPlayer (GameState &state, Coordinate c, bool visible) : Player(state, c, visible) { }
public:
virtual bool move (PositionDelta d) {
- return true;
-
- //return updatePosition(position + d);
+ return updatePosition(position + d);
}
};
class RemotePlayer : public Player {
protected:
- RemotePlayer (Coordinate c, bool visible) : Player(c, visible) { }
+ RemotePlayer (GameState &state, Coordinate c, bool visible) : Player(state, c, visible) { }
};
@@ -82,6 +74,14 @@
GameState (void) : map_dimensions(MAP_DIM_W, MAP_DIM_H), local_player(NULL) {
}
+
+ /*
+ * Check if the given coordinate is valid
+ */
+ bool isValidCoordinate (const Coordinate &p) {
+ // unsigned...
+ return !(p.x > map_dimensions.w || p.y > map_dimensions.h);
+ }
/*
* This will return NULL if we don't have a local player - yet
--- a/src/proto2/NetworkClient.cc Sun Nov 09 21:51:13 2008 +0000
+++ b/src/proto2/NetworkClient.cc Sun Nov 09 22:02:03 2008 +0000
@@ -95,7 +95,7 @@
}
NetworkClientLocalPlayer::NetworkClientLocalPlayer (NetworkClient &client, CL_NetObject_Client &obj, Coordinate initial_position) :
- LocalPlayer(initial_position, true), client(client), obj(obj) {
+ LocalPlayer(client.state, initial_position, true), client(client), obj(obj) {
// receive messages
slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientLocalPlayer::on_position);
@@ -125,7 +125,7 @@
}
NetworkClientRemotePlayer::NetworkClientRemotePlayer (NetworkClient &client, CL_NetObject_Client &obj, Coordinate initial_position) :
- RemotePlayer(initial_position, true), client(client), obj(obj) {
+ RemotePlayer(client.state, initial_position, true), client(client), obj(obj) {
// receive messages
slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientRemotePlayer::on_position);
--- a/src/proto2/NetworkClient.hh Sun Nov 09 21:51:13 2008 +0000
+++ b/src/proto2/NetworkClient.hh Sun Nov 09 22:02:03 2008 +0000
@@ -5,9 +5,13 @@
#include "GameState.hh"
// forward-declare
+class NetworkClientLocalPlayer;
class NetworkClientRemotePlayer;
class NetworkClient : public NetworkCore {
+ friend class NetworkClientLocalPlayer;
+ friend class NetworkClientRemotePlayer;
+
private:
CL_NetComputer server;
--- a/src/proto2/NetworkServer.cc Sun Nov 09 21:51:13 2008 +0000
+++ b/src/proto2/NetworkServer.cc Sun Nov 09 22:02:03 2008 +0000
@@ -48,7 +48,7 @@
}
NetworkServerPlayer::NetworkServerPlayer (NetworkServer &server, CL_NetComputer &computer, uint16_t pid) :
- RemotePlayer(Coordinate(100, 100), true), server(server), computer(computer), obj(&server.netobjs), pid(pid) {
+ RemotePlayer(server.state, Coordinate(100, 100), true), server(server), computer(computer), obj(&server.netobjs), pid(pid) {
// log
Engine::log(INFO, "server_player.connected") << "computer=" << computer << ", obj=" << obj;