diff options
| author | XaviDCR92 <xavi.dcr@gmail.com> | 2017-03-07 20:57:09 +0100 |
|---|---|---|
| committer | XaviDCR92 <xavi.dcr@gmail.com> | 2017-03-07 20:57:09 +0100 |
| commit | 8ec41b4410aba535008daf991ea59a8740951d44 (patch) | |
| tree | 01ee0846f579d9d139ee46a6a43f67ba522c7196 /Player.cpp | |
+ Initial commit. Added source, sprites and final executable.
Diffstat (limited to 'Player.cpp')
| -rw-r--r-- | Player.cpp | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/Player.cpp b/Player.cpp new file mode 100644 index 0000000..59391db --- /dev/null +++ b/Player.cpp @@ -0,0 +1,192 @@ +/* ************************************** + * Includes * + * **************************************/ + +#include "Player.h" + +/* ************************************** + * Defines * + * **************************************/ + +/* ************************************** + * Local variables * + * **************************************/ + +Player::Player(void) +{ + +} + +Player::~Player(void) +{ + +} + +void Player::Init(void) +{ + uint8_t i; + + unit_i = 0; + bldg_i = 0; + + CameraInit(&Camera); + BuildingInit(); + + for(i = 0; i < PLAYER_MAX_BUILDINGS; i++) + { + memset(&buildings[i], 0, sizeof(TYPE_BUILDING)); + } + + TYPE_COLLISION_BLOCK cl; + + Resources.Wood = 25; + Resources.Gold = 50; + Resources.Food = 75; + + cl.x = SystemRand(0, 20); + cl.y = SystemRand(0, 20); + cl.w = BuildingGetWidthFromID(0); + cl.h = BuildingGetHeightFromID(0); + + if(createBuilding(0, cl) == false) + { + GfxPrintText_Flash(F("Failed to create building!")); + } +} + +bool Player::createUnit(uint8_t id) +{ + if(unit_i < PLAYER_MAX_UNITS) + { + units[unit_i++] = id; + return true; + } + else + { + return false; + } + + return false; +} + +void Player::DrawHandler(void) +{ + uint8_t i; + + //GfxRenderTiles(&Camera); + + for(i = 0; i < PLAYER_MAX_BUILDINGS; i++) + { + BuildingDraw(&Camera, &buildings[i]); + } +} + +bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb) +{ + uint8_t i; + TYPE_COLLISION_BLOCK bldgCB; + bool success; + static uint8_t max_tries = 0; + + for(i = 0; i < PLAYER_MAX_BUILDINGS; i++) + { + success = false; + + if(buildings[i].built == false) + { + continue; + } + + bldgCB.x = buildings[i].x; + bldgCB.y = buildings[i].y; + bldgCB.w = BuildingGetWidthFromID(buildings[i].id); + bldgCB.h = BuildingGetHeightFromID(buildings[i].id); + + if(SystemCollisionCheck(*cb, bldgCB) == true) + { + success = false; + } + else + { + success = true; + } + + if(success == false) + { + cb->x = SystemRand(0, 128); + cb->y = SystemRand(0, 128); + + if(++max_tries < 16) + { + if(checkNewBuildingPosition(cb) == false) + { + return false; + } + else + { + return true; + } + } + else + { + return false; + } + } + } + + max_tries = 0; + return true; +} + +bool Player::createBuilding(uint8_t id, TYPE_COLLISION_BLOCK cb) +{ + if(checkNewBuildingPosition(&cb) == false) + { + return false; + } + + if(bldg_i < PLAYER_MAX_BUILDINGS) + { + buildings[bldg_i].id = id; + buildings[bldg_i].x = cb.x; + buildings[bldg_i].y = cb.y; + buildings[bldg_i].hp = BuildingGetHpFromID(id); + buildings[bldg_i].built = true; + + bldg_i++; + + return true; + } + else + { + return false; + } + + return false; +} + +void Player::Handler(void) +{ + CameraHandler(&Camera); + + if(PadButtonReleased(PAD_A) == true) + { + TYPE_COLLISION_BLOCK cl; + + cl.x = SystemRand(0, 32); + cl.y = SystemRand(0, 32); + cl.w = BuildingGetWidthFromID(BARRACKS); + cl.h = BuildingGetHeightFromID(BARRACKS); + + if(createBuilding(BARRACKS, cl) == false) + { + GfxPrintText_Flash(F("Failed!")); + } + else + { + GfxPrintText_Flash(F("Building built!\0")); + } + } + + GfxShowResources(&Resources); +} |
