From 97d6287a0710ec59d746f6340a796bbe6c7c8aa2 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sat, 6 Jun 2015 15:39:02 +0200 Subject: Abstracted game objects into an entity base class --- include/alien.h | 7 ++++--- include/enemy_bullet.h | 7 ++++--- include/game_entity.h | 13 +++++++++++++ include/game_state.h | 1 + include/player_missile.h | 6 +++--- include/shield.h | 7 ++++--- include/spaceship.h | 7 ++++--- src/alien.cpp | 2 +- src/enemy_bullet.cpp | 2 +- src/game_entity.cpp | 5 +++++ src/game_state.cpp | 8 ++++++++ src/player_missile.cpp | 2 +- src/shield.cpp | 2 +- src/spacebot.cpp | 1 + src/spaceship.cpp | 2 +- 15 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 include/game_entity.h create mode 100644 src/game_entity.cpp diff --git a/include/alien.h b/include/alien.h index 2f7d596..5dda726 100644 --- a/include/alien.h +++ b/include/alien.h @@ -1,10 +1,11 @@ #pragma once -class Alien { +#include "game_entity.h" + +class Alien : public GameEntity +{ public: Alien(int x, int y); const static char MAP_CHAR = 'x'; private: - int x; - int y; }; diff --git a/include/enemy_bullet.h b/include/enemy_bullet.h index db3fcf9..ff3b001 100644 --- a/include/enemy_bullet.h +++ b/include/enemy_bullet.h @@ -1,11 +1,12 @@ #pragma once -class EnemyBullet { +#include "game_entity.h" + +class EnemyBullet: public GameEntity +{ public: EnemyBullet(int x, int y); const static char ALIEN_MAP_CHAR = '|'; const static char ENEMY_MISSILE_MAP_CHAR = 'i'; private: - int x; - int y; }; diff --git a/include/game_entity.h b/include/game_entity.h new file mode 100644 index 0000000..215333c --- /dev/null +++ b/include/game_entity.h @@ -0,0 +1,13 @@ +#pragma once + +class GameEntity +{ +public: + GameEntity(int x, int y); + int x() const {return _x;} + int y() const {return _y;} +private: + int _x; + int _y; +}; + diff --git a/include/game_state.h b/include/game_state.h index defe691..db20b84 100644 --- a/include/game_state.h +++ b/include/game_state.h @@ -12,6 +12,7 @@ class GameState { public: GameState(std::string mapFilename); + void logState(); private: std::vector aliens; std::vector bullets; diff --git a/include/player_missile.h b/include/player_missile.h index 6f304df..c504a79 100644 --- a/include/player_missile.h +++ b/include/player_missile.h @@ -1,11 +1,11 @@ #pragma once -class PlayerMissile +#include "game_entity.h" + +class PlayerMissile: public GameEntity { public: PlayerMissile(int x, int y); const static char MAP_CHAR = '!'; private: - int x; - int y; }; diff --git a/include/shield.h b/include/shield.h index bd64e3e..be65be3 100644 --- a/include/shield.h +++ b/include/shield.h @@ -1,11 +1,12 @@ #pragma once -class Shield { +#include "game_entity.h" + +class Shield: public GameEntity +{ public: Shield(int x, int y); const static char MAP_CHAR = '-'; private: - int x; - int y; }; diff --git a/include/spaceship.h b/include/spaceship.h index 5e6afd0..bd26b64 100644 --- a/include/spaceship.h +++ b/include/spaceship.h @@ -1,11 +1,12 @@ #pragma once -class Spaceship { +#include "game_entity.h" + +class Spaceship: public GameEntity +{ public: Spaceship(int x, int y); const static char ENEMY_MAP_CHAR = 'V'; const static char PLAYER_MAP_CHAR = 'A'; private: - int x; - int y; }; diff --git a/src/alien.cpp b/src/alien.cpp index 2b222fd..3fa9b28 100644 --- a/src/alien.cpp +++ b/src/alien.cpp @@ -1,7 +1,7 @@ #include "alien.h" Alien::Alien(int x, int y) - :x(x), y(y) + :GameEntity(x, y) { } diff --git a/src/enemy_bullet.cpp b/src/enemy_bullet.cpp index 75e3e8b..6ae5066 100644 --- a/src/enemy_bullet.cpp +++ b/src/enemy_bullet.cpp @@ -1,6 +1,6 @@ #include "enemy_bullet.h" EnemyBullet::EnemyBullet(int x, int y) - :x(x), y(y) + :GameEntity(x, y) { } diff --git a/src/game_entity.cpp b/src/game_entity.cpp new file mode 100644 index 0000000..1b4e7e3 --- /dev/null +++ b/src/game_entity.cpp @@ -0,0 +1,5 @@ +#include "game_entity.h" + +GameEntity::GameEntity(int x, int y) + :_x(x), _y(y) +{} diff --git a/src/game_state.cpp b/src/game_state.cpp index 340bcd3..2a9975e 100644 --- a/src/game_state.cpp +++ b/src/game_state.cpp @@ -50,3 +50,11 @@ GameState::GameState(std::string mapFilename) } } } + +void GameState::logState() +{ + for (auto alien : aliens) + { + std::cout << "Alien (" << alien.x() << ", " << alien.y() << ")" << std::endl; + } +} diff --git a/src/player_missile.cpp b/src/player_missile.cpp index b0dbdc2..a95e928 100644 --- a/src/player_missile.cpp +++ b/src/player_missile.cpp @@ -1,6 +1,6 @@ #include "player_missile.h" PlayerMissile::PlayerMissile(int x, int y) - :x(x), y(y) + :GameEntity(x, y) { } diff --git a/src/shield.cpp b/src/shield.cpp index b58714a..7457554 100644 --- a/src/shield.cpp +++ b/src/shield.cpp @@ -1,6 +1,6 @@ #include "shield.h" Shield::Shield(int x, int y) - :x(x), y(y) + :GameEntity(x, y) { } diff --git a/src/spacebot.cpp b/src/spacebot.cpp index f30bb34..9d2df5c 100644 --- a/src/spacebot.cpp +++ b/src/spacebot.cpp @@ -8,6 +8,7 @@ Spacebot::Spacebot(std::string outputPath) : outputFilename(outputPath+"/move.txt"), gameState(outputPath+"/map.txt") { + gameState.logState(); } void Spacebot::writeNextMove() diff --git a/src/spaceship.cpp b/src/spaceship.cpp index faf2501..b1cee9a 100644 --- a/src/spaceship.cpp +++ b/src/spaceship.cpp @@ -1,6 +1,6 @@ #include "spaceship.h" Spaceship::Spaceship(int x, int y) - :x(x), y(y) + :GameEntity(x, y) { } -- cgit v1.2.3