diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Cell.cpp | 22 | ||||
-rw-r--r-- | Cell.h | 38 | ||||
-rw-r--r-- | Controller.cpp | 6 | ||||
-rw-r--r-- | Controller.h | 4 | ||||
-rw-r--r-- | Field.cpp | 15 | ||||
-rw-r--r-- | Field.h | 6 | ||||
-rw-r--r-- | Game.cpp | 58 | ||||
-rw-r--r-- | Game.h | 6 | ||||
-rw-r--r-- | KeyboardController.cpp | 1 | ||||
-rw-r--r-- | KeyboardController.h | 4 | ||||
-rw-r--r-- | MainWindow.h | 2 | ||||
-rw-r--r-- | Makefile | 22 | ||||
-rw-r--r-- | Player.cpp | 58 | ||||
-rw-r--r-- | Player.h | 19 | ||||
-rw-r--r-- | types.h | 26 |
16 files changed, 236 insertions, 54 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b9fc595 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +trolltron +#*# +*~
\ No newline at end of file diff --git a/Cell.cpp b/Cell.cpp new file mode 100644 index 0000000..f4793b5 --- /dev/null +++ b/Cell.cpp @@ -0,0 +1,22 @@ +#include "Cell.h"
+
+Cell::Cell(void){
+}
+
+Cell::Cell(CellState cs): m_currentState(cs) {
+}
+
+Cell::~Cell(void){
+}
+
+void Cell::setState(CellState cs) {
+ m_currentState = cs;
+}
+
+CellState Cell::getState() {
+ return(m_currentState);
+}
+
+PlayerNumber Cell::getPlayer(){
+ return(m_currentState/2);
+}
@@ -7,44 +7,38 @@ class Player;
// Nothing more to define here except if you want intelligence to be in the cells.
-typedef struct
+struct Coordinates
{
- uint x;
- uint y;
+ int x;
+ int y;
+ Coordinates(int _x = 0, int _y = 0): x(_x), y(_y) {}
+};
- void set(uint _x, uint _y)
- {
- x = _x;
- y = _y;
- }
-} Coordinates;
-typedef enum
-{
- EMPTY,
- BORDER_WALL,
- PLAYER,
- Player_WALL
-} CellState;
+typedef uint8 CellState;
+// empty if 0 so we can just do "if (cell)"
+// player id is CellState/2, it's a real player if it's odd, a wall if even.
+#define EMPTY_CELL 0;
+#define WALL 1;
class Cell
{
public:
Cell(void);
+ Cell(CellState cs);
virtual ~Cell(void);
public:
- void setCellState(CellState cs);
- CellState getCellState();
+ void setState(CellState cs);
+ CellState getState();
+
+ PlayerNumber getPlayer();
- void setPlayer(Player* pPlayer);
- Player* getPlayer();
private:
CellState m_currentState;
- Player *m_pPlayer; // A pointer to the player currently or previously on this space. May be NULL.
};
-#endif
\ No newline at end of file +#endif
diff --git a/Controller.cpp b/Controller.cpp index 66c42e2..8fa14ab 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -6,7 +6,11 @@ Controller::Controller(Player &player): {
}
-
Controller::~Controller(void)
{
}
+
+//void Controller::changeDirection(VelocityVector v) = 0;
+bool Controller::changeDirection(VelocityVector v){
+ return(false);
+}
diff --git a/Controller.h b/Controller.h index 2fa8749..f45b711 100644 --- a/Controller.h +++ b/Controller.h @@ -18,10 +18,10 @@ public: **/
//Game &getGame();
protected:
- virtual void changeDirection(VelocityVector v);
+ virtual bool changeDirection(VelocityVector v);
protected:
Player &m_player;
};
-#endif
\ No newline at end of file +#endif
@@ -1,11 +1,24 @@ #include "Field.h"
-Field::Field(uint width, uint height)
+Field::Field(uint width, uint height): m_width(width), m_height(height)
{
+ m_fieldCells.resize(width*height);
}
Field::~Field(void)
{
}
+
+bool Field::drawAllPlayers(uint numCycles) {
+ return(true);
+}
+
+Cell& Field::getCell(Coordinates c) {
+ return(m_fieldCells[c.x+m_width*c.y]);
+}
+
+void Field::setCell(Coordinates coord, Cell cell) {
+ m_fieldCells[coord.x+m_width*coord.y] = cell;
+}
@@ -13,11 +13,13 @@ public: Field(uint width, uint height);
virtual ~Field(void);
bool drawAllPlayers(uint numCycles);
- Cell &getCell(Coordinates c);
+ Cell& getCell(Coordinates c);
+ void setCell(Coordinates coord, Cell cell);
+
private:
uint m_width;
uint m_height;
std::vector<Cell> m_fieldCells;
};
-#endif
\ No newline at end of file +#endif
@@ -1,17 +1,67 @@ #include "Game.h"
+PlayerColor colors[] = { 'R', 'B', 'G', 'P' };
+
Game::Game(uint width, uint height, PlayerNumber num):
m_Field(width, height)
{
- m_vPlayers.resize(num);
+ //m_vPlayers.resize(num); // is it possible to resize it without trying to fill it? (i.e. fill with null pointers)
for(PlayerNumber i = 0; i<num; ++i)
{
-
-
+ m_vPlayers.push_back(Player(i, colors[i], Coordinates(5,5+i*10), VelocityVector(0,1))); // TODO: place players correctly :P
}
}
-
Game::~Game(void)
{
+ // ~m_Field; ?
+ // same for each player ?
+}
+
+
+Player& Game::getPlayerByID(PlayerNumber id) {
+ return(m_vPlayers[id]);
+}
+
+Field& Game::getField() {
+ return(m_Field);
+}
+
+void Game::startGame() {
+ m_gameStarted = true;
+ m_gameRunning = true;
+}
+
+void Game::stopGame() {
+ m_gameStarted = false;
+ m_gameRunning = false;
+}
+
+void Game::pauseGame() {
+ m_gameRunning = false;
+}
+
+void Game::resumeGame() {
+ m_gameRunning = true;
+}
+
+// main function to be called within glut loop
+void Game::updateGame(uint timeElapsedMs) {
+ std::vector<Coordinates> path;
+
+ for(std::vector<Player>::iterator i = m_vPlayers.begin(); i != m_vPlayers.end(); ++i) {
+ Player player = *i;
+ if (player.isAlive()) {
+ path = player.move();
+ for(std::vector<Coordinates>::iterator j = path.begin(); j != path.end(); ++j) {
+ Coordinates coord = *j;
+ Cell cell = m_Field.getCell(coord);
+ if (cell.getState()) {
+ player.kill();
+ } else {
+ cell.setState(player.getNumber()*2+1);
+ }
+ }
+ }
+ }
}
@@ -20,9 +20,9 @@ public: **/
void updateGame(uint timeElapsedMs);
- Player &getPlayerByID(PlayerNumber id);
+ Player& getPlayerByID(PlayerNumber id);
- Field &getField();
+ Field& getField();
private:
static PlayerColor getColor(PlayerNumber n);
@@ -32,4 +32,4 @@ private: Field m_Field;
};
-#endif
\ No newline at end of file +#endif
diff --git a/KeyboardController.cpp b/KeyboardController.cpp index 9eb083a..1a5c91e 100644 --- a/KeyboardController.cpp +++ b/KeyboardController.cpp @@ -7,6 +7,7 @@ KeyboardController::KeyboardController(Player &player): }
+
KeyboardController::~KeyboardController(void)
{
}
diff --git a/KeyboardController.h b/KeyboardController.h index 3c6140d..6c4295b 100644 --- a/KeyboardController.h +++ b/KeyboardController.h @@ -10,11 +10,11 @@ class KeyboardController : public Controller
{
- friend MainWindow;
+ friend class MainWindow;
public:
KeyboardController(Player &player);
virtual ~KeyboardController(void);
};
-#endif
\ No newline at end of file +#endif
diff --git a/MainWindow.h b/MainWindow.h index 18cafe0..6c76ce2 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -48,4 +48,4 @@ private: Game *m_pGame;
};
-#endif
\ No newline at end of file +#endif
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5f664e2 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +CC=g++ +#CXXFLAGS=-O3 +LDFLAGS=-lGLU -lglut +SOURCES=Cell.cpp Field.cpp Controller.cpp Game.cpp MainWindow.cpp KeyboardController.cpp Player.cpp +OBJECTS=$(SOURCES:.cpp=.o) +MAIN_SOURCE=TrollTron.cpp +MAIN=trolltron + + +all: $(OBJECTS) lab + +opt: $(OBJECTS) + $(CC) $(CXXFLAGS) -o $(MAIN) $(LDFLAGS) $(OBJECTS) $(MAIN_SOURCE) + +lab: + $(CC) -o $(MAIN) $(LDFLAGS) $(OBJECTS) $(MAIN_SOURCE) + +.o: + $(CC) $(CFLAGS) $< -o $@ + +clean: + rm -f *.o $(MAIN) @@ -1,11 +1,63 @@ #include "Player.h"
-
-Player::Player(uint PlayerID, PlayerColor color)
+Player::Player(PlayerNumber PlayerID, PlayerColor color, Coordinates InitialPoint, VelocityVector initialVel) : m_color(color), m_playerID(PlayerID), m_currentCoordinates(InitialPoint), m_velocityVector(initialVel)
{
+ m_alive = true;
+ // m_controller=KeyboardController(self);
}
-
Player::~Player(void)
{
}
+
+std::vector<Coordinates> Player::move() {
+
+ std::vector<Coordinates> cases;
+
+ for (uint i = 0; i <= m_velocityVector.x ; ++i ) {
+ for (uint j = 0; j <= m_velocityVector.y ; ++j ) {
+ cases.push_back(Coordinates(m_currentCoordinates.x+i, m_currentCoordinates.y+j));
+ }
+ }
+ m_currentCoordinates.x += m_velocityVector.x;
+ m_currentCoordinates.y += m_velocityVector.y;
+ return(cases);
+}
+
+void Player::setVelocityVector(VelocityVector v) {
+ m_velocityVector = v;
+}
+
+bool Player::changeDirection(VelocityVector v) {
+ if ((m_velocityVector.x != 0 && v.x != 0) || (m_velocityVector.y != 0 && v.y != 0)) {
+ return(false);
+ } else {
+ m_velocityVector = v;
+ return(true);
+ }
+}
+
+bool Player::isAlive() {
+ return(m_alive);
+}
+
+void Player::kill() {
+ m_alive = false;
+}
+
+PlayerNumber Player::getNumber() {
+ return(m_playerID);
+}
+/*
+
+
+ bool changeDirection(VelocityVector v);
+
+ PlayerNumber m_playerID;
+ std::string m_name;
+ Coordinates m_currentCoordinates;
+ PlayerColor m_color;
+ VelocityVector m_velocityVector;
+ bool m_alive;
+ Controller* m_controller;
+*/
@@ -3,23 +3,15 @@ #include <vector>
#include <string>
+#include "types.h"
#include "Cell.h"
-typedef struct
-{
- int x;
- int y;
-} VelocityVector;
-
-typedef char PlayerColor;
-typedef char PlayerNumber;
-
class Controller;
class Player
{
public:
- Player(uint PlayerID=0, PlayerColor color=0);
+ Player(PlayerNumber PlayerID, PlayerColor color, Coordinates InitialPoint, VelocityVector initialVel);
virtual ~Player(void);
public:
@@ -36,6 +28,11 @@ public: **/
bool changeDirection(VelocityVector v);
+ bool isAlive();
+ void kill();
+
+ PlayerNumber getNumber();
+
private:
PlayerNumber m_playerID;
std::string m_name;
@@ -46,4 +43,4 @@ private: Controller* m_controller;
};
-#endif
\ No newline at end of file +#endif
@@ -1,3 +1,6 @@ +#ifndef _TYPES_H
+#define _TYPES_H
+
#ifdef _MSC_VER
typedef unsigned int uint32 ;
@@ -5,9 +8,28 @@ typedef char uint8 ;
typedef unsigned long int uint64 ;
#else
- // Please set here the appropriate types for your compiler.
+ // couldn't he have used _t like everyone else?
+ #include <stdint.h>
+ typedef uint32_t uint32 ;
+ typedef int32_t int32 ;
+ typedef uint8_t uint8 ;
+ typedef uint64_t uint64 ;
#endif
// Default types
-typedef uint32 uint;
\ No newline at end of file +typedef uint32 uint;
+
+
+// our types
+struct VelocityVector
+{
+ int x;
+ int y;
+ VelocityVector(int _x, int _y): x(_x), y(_y) {};
+};
+
+typedef char PlayerColor;
+typedef char PlayerNumber;
+
+#endif // _TYPES_H
|