summaryrefslogtreecommitdiff
path: root/src/game_state.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/game_state.cpp')
-rw-r--r--src/game_state.cpp77
1 files changed, 69 insertions, 8 deletions
diff --git a/src/game_state.cpp b/src/game_state.cpp
index a302bfc..eb02005 100644
--- a/src/game_state.cpp
+++ b/src/game_state.cpp
@@ -3,6 +3,7 @@
#include <fstream>
#include <limits>
#include <bitset>
+#include <cmath>
const int OPENING_LINES = 6;
const int GAME_AREA_LINES = 25;
@@ -65,25 +66,31 @@ int GameState::addEntity(int x, int y, char type)
case Spaceship::PLAYER_MAP_CHAR:
_playerSpaceship = std::unique_ptr<Spaceship>(new Spaceship(x+1, y));
return 3;
+ case Building::MISSILE_CONTROLLER_CHAR:
+ _missileControllers.push_back(Building(x+1, y));
+ return 3;
+ case Building::ALIEN_FACTORY_CHAR:
+ _alienFactories.push_back(Building(x+1, y));
+ return 3;
}
return 1;
}
-void GameState::logState()
+void GameState::logState() const
{
- for (auto alien : _aliens)
+ for (auto const& alien : _aliens)
{
std::cout << "Alien " << alien.coords() << std::endl;
}
- for (auto bullet : _bullets)
+ for (auto const& bullet : _bullets)
{
std::cout << "Enemy Bullet" << bullet.coords() << std::endl;
}
- for (auto missile : _missiles)
+ for (auto const& missile : _missiles)
{
std::cout << "Player Missile" << missile.coords() << std::endl;
}
- for (auto shield : _shields)
+ for (auto const& shield : _shields)
{
std::cout << "Shield" << shield.coords() << std::endl;
}
@@ -129,7 +136,7 @@ int getRectangularOffset(int width, int x, int y)
std::vector<bool> GameState::toBitArray() const
{
- std::vector<bool> result(171);
+ std::vector<bool> result(172);
int alienOffset = 0; //Aliens are 0 to 100
int bulletOffset = 101; //Bullets are 101 to 135
@@ -147,7 +154,7 @@ std::vector<bool> GameState::toBitArray() const
}
- for (auto alien : _aliens)
+ for (auto const& alien : _aliens)
{
if (alien.y() >= playerY || alien.y() < playerY-9)
{
@@ -157,7 +164,7 @@ std::vector<bool> GameState::toBitArray() const
result.at(alienOffset + getPyramidOffset(3, alien.x()-playerX, playerY-1-alien.y(), 99, 100)) = true;
}
- for (auto bullet : _bullets)
+ for (auto const& bullet : _bullets)
{
if (bullet.y() >= playerY || bullet.y() < playerY-5 || bullet.x() > playerX+3 || bullet.x() < playerX-3)
{
@@ -166,6 +173,60 @@ std::vector<bool> GameState::toBitArray() const
result.at(bulletOffset + getRectangularOffset(7, bullet.x()-playerX, playerY-1-bullet.y())) = true;
}
+
+ for (auto const& shield : _shields)
+ {
+ if (shield.y() < playerY-3 || shield.x() < playerX-2 || shield.x() > playerX+2)
+ {
+ continue;
+ }
+
+ result.at(shieldOffset + getRectangularOffset(5, shield.x()-playerX, playerY-1-shield.y())) = true;
+ }
+
+ if (_missiles.size() > 0)
+ {
+ result.at(missileOffset) = true;
+ }
+ if (_missiles.size() < 1)
+ {
+ result.at(missileOffset + 1) = true;
+ }
+
+ if (_playerSpaceship)
+ {
+ result.at(positionOffset + playerX-2) = true;
+ }
+
+ result.at(canBuildOffset) = true;
+ for (auto const& missileController : _missileControllers)
+ {
+ if (missileController.y() < playerY)
+ {
+ continue;
+ }
+ result.at(existingBuildingsOffset + 0) = true;
+ if (_missiles.size() < 2)
+ {
+ result.at(missileOffset+1) = true;
+ }
+ if (abs(missileController.x() - playerX) < 3)
+ {
+ result.at(canBuildOffset) = false;
+ }
+ }
+ for (auto const& alienFactory : _alienFactories)
+ {
+ if (alienFactory.y() < playerY)
+ {
+ continue;
+ }
+ result.at(existingBuildingsOffset + 1) = true;
+ if (abs(alienFactory.x() - playerX) < 3)
+ {
+ result.at(canBuildOffset) = false;
+ }
+ }
return result;
}