diff options
| author | XaviDCR92 <xavi.dcr@gmail.com> | 2017-11-05 19:08:38 +0100 |
|---|---|---|
| committer | XaviDCR92 <xavi.dcr@gmail.com> | 2017-11-05 19:08:38 +0100 |
| commit | 7b05778e3ef8ef01a4bb15a6ed6fde5b329a8c9b (patch) | |
| tree | 2629c7ad872b398dc045a863a75879533ce99aa0 /Unit.c | |
| parent | 2d04eba931ac84e2f09d3c8391125f8a991ca7d3 (diff) | |
| download | pocketempires-7b05778e3ef8ef01a4bb15a6ed6fde5b329a8c9b.tar.gz | |
*Removed decceleration for camera.
Improved pathfinding when a unit collides with another unit.
Added generic function to find distances.
Other minor changes.
Diffstat (limited to 'Unit.c')
| -rw-r--r-- | Unit.c | 119 |
1 files changed, 74 insertions, 45 deletions
@@ -36,30 +36,29 @@ struct t_coordinates /* Tables */ static uint8_t const UnitHPTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = 25, - [SOLDIER] = 80, - [BARRACKS] = 100, - [TOWN_CENTER] = 200 }; + [SOLDIER] = 80, + [BARRACKS] = 100, + [TOWN_CENTER] = 200 }; static TYPE_RESOURCES const UnitResourcesTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = {.Wood = 0, .Gold = 0, .Food = 50}, - [SOLDIER] = {.Wood = 25, .Gold = 10, .Food = 50}, - [BARRACKS] = {.Wood = 100, .Gold = 0, .Food = 0}, - [TOWN_CENTER] = {.Wood = 200, .Gold = 0, .Food = 0} }; + [SOLDIER] = {.Wood = 25, .Gold = 10, .Food = 50}, + [BARRACKS] = {.Wood = 100, .Gold = 0, .Food = 0}, + [TOWN_CENTER] = {.Wood = 200, .Gold = 0, .Food = 0} }; static uint8_t const UnitSpeedTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = 1, - [SOLDIER] = 1, - [BARRACKS] = 0, - [TOWN_CENTER] = 0 }; + [SOLDIER] = 1, + [BARRACKS] = 0, + [TOWN_CENTER] = 0 }; static const char* const UnitActionsTable_Level[MAX_ACTIONS] = { [ACTION_BUILD_BARRACKS] = "B.BARR", - [ACTION_ATTACK] = "ATTACK", - [ACTION_CREATE_PEASANT] = "C.PEAS.", - [ACTION_CREATE_SOLDIER] = "C.SLDR.", - [ACTION_BUILD_TOWER_CENTER] = "C.TWNC."}; + [ACTION_CREATE_PEASANT] = "C.PEAS.", + [ACTION_CREATE_SOLDIER] = "C.SLDR.", + [ACTION_BUILD_TOWER_CENTER] = "C.TWNC."}; -static uint8_t const UnitActionsTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = ((1 << ACTION_BUILD_BARRACKS) | (1 << ACTION_BUILD_TOWER_CENTER) | (1 << ACTION_ATTACK)), - [SOLDIER] = (1 << ACTION_ATTACK), - [BARRACKS] = (1 << ACTION_CREATE_SOLDIER), - [TOWN_CENTER] = (1 << ACTION_CREATE_PEASANT) }; +static uint8_t const UnitActionsTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = ((1 << ACTION_BUILD_BARRACKS) | (1 << ACTION_BUILD_TOWER_CENTER)), + [SOLDIER] = 0, + [BARRACKS] = (1 << ACTION_CREATE_SOLDIER), + [TOWN_CENTER] = (1 << ACTION_CREATE_PEASANT) }; // ************** // Sprite tables @@ -282,6 +281,36 @@ void UnitAttackAccepted(TYPE_UNIT* ptrUnit) ptrUnit->selecting_attack = true; } +bool UnitCheckCollisionAgainstOtherUnits(TYPE_COLLISION_BLOCK* cb, TYPE_UNIT* ptrUnitArray, TYPE_UNIT* ptrCurrentUnit) +{ + for (uint8_t i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++) + { + TYPE_UNIT* ptrOtherUnit = &ptrUnitArray[i]; + TYPE_COLLISION_BLOCK ocb = {.x = ptrOtherUnit->x, + .y = ptrOtherUnit->y, + .w = UnitGetWidthFromID(ptrOtherUnit->id), + .h = UnitGetHeightFromID(ptrOtherUnit->id)}; + + if (ptrOtherUnit->alive == false) + { + continue; + } + + if (ptrOtherUnit == ptrCurrentUnit) + { + /* We are referring to the same TYPE_UNIT instance. Discard. */ + continue; + } + + if (SystemCollisionCheck(cb, &ocb) != false) + { + return true; + } + } + + return false; +} + void UnitHandler(TYPE_UNIT* unitArray, size_t sz) { size_t i; @@ -330,41 +359,41 @@ void UnitHandler(TYPE_UNIT* unitArray, size_t sz) if (ptrUnit->walking != false) { - // If player is still walking, check collisions - // against all other active units. - size_t j; + TYPE_COLLISION_BLOCK cu = { .x = ptrUnit->x + x_d, + .y = ptrUnit->y + y_d, + .w = UnitGetWidthFromID(ptrUnit->id), + .h = UnitGetHeightFromID(ptrUnit->id) }; - for (j = 0; j < sz; j++) + if (UnitCheckCollisionAgainstOtherUnits(&cu, unitArray, ptrUnit) == true) { - TYPE_UNIT* ptrOtherUnit = &unitArray[j]; - - TYPE_COLLISION_BLOCK cu = { .x = ptrUnit->x + x_d, - .y = ptrUnit->y + y_d, - .w = UnitGetWidthFromID(ptrUnit->id), - .h = UnitGetHeightFromID(ptrUnit->id) }; - TYPE_COLLISION_BLOCK ou; + uint32_t dist = 0; + uint32_t dist2 = 0; - if (ptrOtherUnit->alive == false) + switch (ptrUnit->dir) { - continue; - } + case DIRECTION_LEFT: + // Fall through + case DIRECTION_RIGHT: + dist = SystemGetHyp(abs(ptrUnit->x - ptrUnit->target_x), abs((ptrUnit->y + y_d) - ptrUnit->target_y)); + dist2 = SystemGetHyp(abs(ptrUnit->x - ptrUnit->target_x), abs((ptrUnit->y - y_d) - ptrUnit->target_y)); - if (j == i) - { - // Do not compare against itself! - continue; - } + y_d = dist < dist2? x_d: -x_d; + ptrUnit->dir = y_d? DIRECTION_DOWN: DIRECTION_UP; - ou.x = ptrOtherUnit->x; - ou.y = ptrOtherUnit->y; - ou.w = UnitGetWidthFromID(ptrOtherUnit->id); - ou.h = UnitGetHeightFromID(ptrOtherUnit->id); + x_d = 0; + break; - if (SystemCollisionCheck(&cu, &ou) != false) - { - //bMoving = false; - return; + case DIRECTION_UP: + // Fall through + case DIRECTION_DOWN: + dist = SystemGetHyp(abs((ptrUnit->x - x_d) - ptrUnit->target_x), abs(ptrUnit->y - ptrUnit->target_y)); + dist2 = SystemGetHyp(abs((ptrUnit->x + x_d) - ptrUnit->target_x), abs(ptrUnit->y - ptrUnit->target_y)); + + x_d = dist < dist2? -y_d: -x_d; + ptrUnit->dir = x_d? DIRECTION_RIGHT: DIRECTION_LEFT; + y_d = 0; + break; } } } @@ -373,7 +402,7 @@ void UnitHandler(TYPE_UNIT* unitArray, size_t sz) if (ptrUnit->walking != false) { - // If no collision is detected, keep moving to the new position + /* If no collision is detected, keep moving to the new position */ ptrUnit->x += x_d; ptrUnit->y += y_d; } |
