--- a/src/proto2/Dimension.cc Tue Nov 18 18:34:33 2008 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-#include "Dimension.hh"
-
-std::ostream& operator<< (std::ostream &s, const Coordinate &c) {
- s << "(" << c.x << ", " << c.y << ")";
-
- return s;
-}
-
-std::ostream& operator<< (std::ostream &s, const PositionDelta &c) {
- s << "(" << c.dx << ", " << c.dy << ")";
-
- return s;
-}
-
--- a/src/proto2/Dimension.hh Tue Nov 18 18:34:33 2008 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-#ifndef DIMENSION_HH
-#define DIMENSION_HH
-
-#include <iostream>
-
-class Vector {
- public:
- uint32_t x;
- uint32_t y;
-
- Coordinate (uint32_t x, uint32_t y) : x(x), y(y) { }
-
- Coordinate &operator+= (const PositionDelta &d) {
- this->x += d.dx;
- this->y += d.dy;
-
- return *this;
- }
-
- Coordinate operator+ (const PositionDelta &d) {
- return Coordinate(x + d.dx, y + d.dy);
- }
-
- // Scale the coordinate so that it matches the pixel resolution
- uint32_t scaledX() { return x; }
-
- uint32_t scaledY() { return y; }
-};
-
-typedef Vector Dimension;
-typedef Vector PositionDelta;
-typedef Vector Coordinate;
-
-std::ostream& operator<< (std::ostream &s, const Coordinate &c);
-std::ostream& operator<< (std::ostream &s, const PositionDelta &c);
-
-#endif
--- a/src/proto2/GameState.cc Tue Nov 18 18:34:33 2008 +0000
+++ b/src/proto2/GameState.cc Tue Nov 18 18:50:32 2008 +0000
@@ -1,18 +1,22 @@
#include "GameState.hh"
-bool Player::updatePosition (Coordinate p) {
- if (!state.isValidCoordinate(p)) {
- // out-of-bounds
- return false;
+bool LocalPlayer::handleMove (PlayerInput_Move input) {
+ int dx = 0, dy = 0;
- } else {
- // valid
- position = p;
+ // handle up/down/left/right
+ if (input & MOVE_UP)
+ dy -= 3;
+
+ if (input & MOVE_DOWN)
+ dy += 3;
- return true;
- }
+ if (input & MOVE_LEFT)
+ dx -= 3;
+
+ if (input & MOVE_RIGHT)
+ dx += 3;
+
+ // apply force
+ applyForce(Vector(dx, dy));
}
-
-
-
--- a/src/proto2/GameState.hh Tue Nov 18 18:34:33 2008 +0000
+++ b/src/proto2/GameState.hh Tue Nov 18 18:50:32 2008 +0000
@@ -1,38 +1,35 @@
#ifndef GAMESTATE_HH
#define GAMESTATE_HH
-#include "Dimension.hh"
+#include "Physics.hh"
+#include "Input.hh"
#include <list>
#include <stdexcept>
-enum PlayerType {
- PLAYER_LOCAL,
- PLAYER_REMOTE
-};
-
-#define PLAYER_DIM_W 10
-#define PLAYER_DIM_H 10
-#define MAP_DIM_W 800
-#define MAP_DIM_H 640
+const uint16_t PLAYER_MASS = 8035;
+const uint16_t PLAYER_DIM_W = 10;
+const uint16_t PLAYER_DIM_H = 10;
+const uint16_t MAP_DIM_W = 800;
+const uint16_t MAP_DIM_H = 640;
// forward-declare GameState
class GameState;
class Player : public PhysicsObject {
+ protected:
+
public:
- Player(GameState &state, Coordinate c, bool visible) :
- PhysicsObject(...), dimensions(PLAYER_DIM_W, PLAYER_DIM_H), visible(visible) { }
+ Player(Vector position, bool visible) :
+ PhysicsObject(PLAYER_MASS, position, Vector(0, 0), Vector(0, 0)), visible(visible) { }
- PlayerType type;
- Dimension dimensions;
bool visible;
};
class LocalPlayer : public Player {
protected:
- LocalPlayer (Coordinate c, bool visible) : Player(c, visible) { }
+ LocalPlayer (Vector pos, bool visible) : Player(pos, visible) { }
public:
virtual bool handleMove (PlayerInput_Move input);
@@ -40,7 +37,7 @@
class RemotePlayer : public Player {
protected:
- RemotePlayer (Coordinate c, bool visible) : Player(c, visible) { }
+ RemotePlayer (Vector pos, bool visible) : Player(pos, visible) { }
};
class GameState : public PhysicsWorld {
@@ -50,7 +47,7 @@
// only one local player is supported
LocalPlayer *local_player;
- GameState (void) : PhysicsWorld(...), local_player(NULL) {
+ GameState (void) : PhysicsWorld(Vector(MAP_DIM_W, MAP_DIM_H)), local_player(NULL) {
}
--- a/src/proto2/Graphics.cc Tue Nov 18 18:34:33 2008 +0000
+++ b/src/proto2/Graphics.cc Tue Nov 18 18:50:32 2008 +0000
@@ -17,7 +17,7 @@
void Graphics::check_input (void) {
LocalPlayer *player;
- int dx = 0, dy = 0;
+ enum PlayerInput_Move input_move;
// stop on escape
if (keyboard.get_keycode(CL_KEY_ESCAPE)) {
@@ -32,20 +32,20 @@
// handle up/down/left/right
if (keyboard.get_keycode(CL_KEY_UP))
- dy -= 3;
+ input_move |= MOVE_UP;
if (keyboard.get_keycode(CL_KEY_DOWN))
- dy += 3;
+ input_move |= MOVE_DOWN;
if (keyboard.get_keycode(CL_KEY_LEFT))
- dx -= 3;
+ input_move |= MOVE_LEFT;
if (keyboard.get_keycode(CL_KEY_RIGHT))
- dx += 3;
+ input_move |= MOVE_RIGHT;
// apply movement if applicable
- if (dx || dy)
- player->move(PositionDelta(dx, dy));
+ if (input_move)
+ player->handleMove(input_move);
}
void Graphics::do_redraw (void) {
--- a/src/proto2/Physics.cc Tue Nov 18 18:34:33 2008 +0000
+++ b/src/proto2/Physics.cc Tue Nov 18 18:50:32 2008 +0000
@@ -1,23 +1,25 @@
#include <algorithm>
+#include <functional>
#include "Physics.hh"
-PhysicsWorld::PhysicsWorld (Vector dimensions) : dimensions(dimensions) {}
+PhysicsWorld::PhysicsWorld (Vector dimensions)
+ : dimensions(dimensions), tick_timer(PHYSICS_TICK_MS) {
+
+ slots.connect(tick_timer.sig_timer(), this, PhysicsWorld::tick);
+ tick_timer.enable();
+}
void PhysicsWorld::addObject (PhysicsObject *object) {
objects.push_back(object);
}
void PhysicsWorld::tick () {
- std::for_each(objects.begin(), objects.end(), tickObject);
+ std::for_each(objects.begin(), objects.end(), mem_fun(&PhysicsObject::tick));
}
-void tickObject(PhysicsObject* obj) {
- obj->tick();
-}
-
-PhysicsObject::PhysicsObject (mass, position, velocity, force)
+PhysicsObject::PhysicsObject (uint16_t mass, Vector position, Vector velocity, Vector force)
: mass(mass), position(position), velocity(velocity), force(force) {}
void PhysicsObject::updatePosition () {
@@ -28,11 +30,12 @@
Vector newPosition = position + velocity * PHYSICS_TICK_MS * 1000;
//TODO Handle the object as a square or a polygon
- if(newPosition.x < 0 || (newPosition.x > PHYSICS_WORLD_WIDTH)
+ if (newPosition.x < 0 || (newPosition.x > PHYSICS_WORLD_WIDTH)
|| (newPosition.y < 0) || (newPosition.y >= PHYSICS_WORLD_HEIGHT)) {
- // CRASH!
- this->velocity *= -1;
+ // CRASH!
+ this->velocity *= -1;
+
} else {
this->position = newPosition;
}
--- a/src/proto2/Physics.hh Tue Nov 18 18:34:33 2008 +0000
+++ b/src/proto2/Physics.hh Tue Nov 18 18:50:32 2008 +0000
@@ -3,15 +3,25 @@
#include "Vector.hh"
+#include <ClanLib/core.h>
+
const uint16_t PHYSICS_TICK_MS = 50;
const uint16_t PHYSICS_WORLD_WIDTH = 800;
const uint16_t PHYSICS_WORLD_HEIGHT = 600;
const Vector GRAVITY_FORCE(0,-1);
+// forward-declare
+class PhysicsObject;
+
class PhysicsWorld {
+ private:
+ CL_Timer tick_timer;
+
protected:
std::vector<PhysicsObject*> objects;
Vector dimensions;
+
+ CL_SlotContainer slots;
PhysicsWorld (Vector dimensions);
@@ -28,7 +38,7 @@
Vector force;
uint16_t mass;
- PhysicsObject (mass, position, velocity, force);
+ PhysicsObject (uint16_t mass, Vector position, Vector velocity, Vector force);
private:
void updatePosition (void);
--- a/src/proto2/Vector.hh Tue Nov 18 18:34:33 2008 +0000
+++ b/src/proto2/Vector.hh Tue Nov 18 18:50:32 2008 +0000
@@ -3,6 +3,6 @@
#include <complex>
-typedef Vector std::complex<float>;
+typedef std::complex<float> Vector;
#endif