diff options
Diffstat (limited to 'src/game_state.cpp')
-rw-r--r-- | src/game_state.cpp | 154 |
1 files changed, 125 insertions, 29 deletions
diff --git a/src/game_state.cpp b/src/game_state.cpp index e6deae7..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; @@ -60,75 +61,170 @@ int GameState::addEntity(int x, int y, char type) _shields.push_back(Shield(x,y)); return 1; case Spaceship::ENEMY_MAP_CHAR: + _enemySpaceship = std::unique_ptr<Spaceship>(new Spaceship(x+1, y)); + return 3; case Spaceship::PLAYER_MAP_CHAR: - _spaceships.push_back(Spaceship(x+1,y)); + _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; } - for (auto spaceship : _spaceships) + if (_playerSpaceship) { - std::cout << "Spaceship" << spaceship.coords() << std::endl; + std::cout << "Player Spaceship" << _playerSpaceship->coords() << std::endl; } + if (_enemySpaceship) + { + std::cout << "Enemy Spaceship" << _enemySpaceship->coords() << std::endl; + } +} + +int getPyramidOffset(int bottomWidth, int x, int y, int resultIfLeft, int resultIfRight) +{ + int currentRowWidth = bottomWidth; + int currentRowOffset = 0; + for (int i=0; i<y; ++i) + { + currentRowOffset += currentRowWidth; + currentRowWidth += 2; + } + + if (x > currentRowWidth/2) + { + return resultIfRight; + } + else if (x < -currentRowWidth/2) + { + return resultIfLeft; + } + else + { + return currentRowOffset + currentRowWidth/2 + x; + } +} + +int getRectangularOffset(int width, int x, int y) +{ + int currentRowOffset = width*y; + return currentRowOffset + width/2 + x; } std::vector<bool> GameState::toBitArray() const { - std::vector<bool> result; + std::vector<bool> result(172); - std::vector<bool> alienNodes((GAME_AREA_LINES-2) * (GAME_WIDTH-2), false); - for (auto alien : _aliens) + int alienOffset = 0; //Aliens are 0 to 100 + int bulletOffset = 101; //Bullets are 101 to 135 + int shieldOffset = 136; //Shields are 136 to 150 + int missileOffset = 151; //Missile info is 151 to 152 + int positionOffset = 153; //Position is 153 to 168 + int existingBuildingsOffset = 169; //Existing buildings is 169 to 170 + int canBuildOffset = 171; //Can build here is 171 + + int playerX = GAME_WIDTH/2; + int playerY = GAME_AREA_LINES-3; + if (_playerSpaceship) { - alienNodes.at((alien.y()-1)*(GAME_WIDTH-2)+(alien.x()-1)) = true; + playerX = _playerSpaceship->x(); } - result.insert(result.end(), alienNodes.begin(), alienNodes.end()); - std::vector<bool> bulletNodes((GAME_AREA_LINES-2) * (GAME_WIDTH-2), false); - for (auto bullet : _bullets) + + for (auto const& alien : _aliens) { - bulletNodes.at((bullet.y()-1)*(GAME_WIDTH-2)+(bullet.x()-1)) = true; + if (alien.y() >= playerY || alien.y() < playerY-9) + { + continue; + } + + result.at(alienOffset + getPyramidOffset(3, alien.x()-playerX, playerY-1-alien.y(), 99, 100)) = true; } - result.insert(result.end(), bulletNodes.begin(), bulletNodes.end()); - std::vector<bool> missileNodes((GAME_AREA_LINES-2) * (GAME_WIDTH-2), false); - for (auto missile : _missiles) + for (auto const& bullet : _bullets) { - missileNodes.at((missile.y()-1)*(GAME_WIDTH-2)+(missile.x()-1)) = true; + if (bullet.y() >= playerY || bullet.y() < playerY-5 || bullet.x() > playerX+3 || bullet.x() < playerX-3) + { + continue; + } + + result.at(bulletOffset + getRectangularOffset(7, bullet.x()-playerX, playerY-1-bullet.y())) = true; } - result.insert(result.end(), missileNodes.begin(), missileNodes.end()); - std::vector<bool> shieldNodes(2 * (GAME_WIDTH-2), false); - for (auto shield : _shields) + for (auto const& shield : _shields) { - int row = shield.y() > GAME_AREA_LINES/2 ? 1 : 0; - shieldNodes.at(row*(GAME_WIDTH-2)+(shield.x()-1)) = true; + 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; } - for (auto spaceship : _spaceships) + if (_missiles.size() > 0) + { + result.at(missileOffset) = true; + } + if (_missiles.size() < 1) + { + result.at(missileOffset + 1) = true; + } + + if (_playerSpaceship) { - std::bitset<5> spaceshipBits(spaceship.x()); - for (int i=0; i<spaceshipBits.size(); ++i) + 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.push_back(spaceshipBits[i]); + result.at(canBuildOffset) = false; } } |