summaryrefslogtreecommitdiff
path: root/Unit.c
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2017-09-09 12:47:17 +0200
committerXaviDCR92 <xavi.dcr@gmail.com>2017-09-09 12:47:17 +0200
commita7dd9781961b5f26d5dca6829e1933dff6209d23 (patch)
tree6e3f1c469e6997fedbaa096f377d67f99a65ae47 /Unit.c
parent786dccd2bc0946d48b8a2758ef2c607678bc8dd9 (diff)
downloadpocketempires-a7dd9781961b5f26d5dca6829e1933dff6209d23.tar.gz
Simple collision detection between units added. libgamebuino was always updating header files, so targets were always being rebuilt.
Diffstat (limited to 'Unit.c')
-rw-r--r--Unit.c151
1 files changed, 102 insertions, 49 deletions
diff --git a/Unit.c b/Unit.c
index 84b7931..204c242 100644
--- a/Unit.c
+++ b/Unit.c
@@ -10,8 +10,6 @@
* Defines *
* **************************************/
-#define MAX_ACTIONS 3
-
/* **************************************
* Structs and enums *
* **************************************/
@@ -26,9 +24,6 @@ struct t_coordinates
* Local prototypes *
* **************************************/
-static void UnitBuildAccepted(TYPE_UNIT* ptrUnit);
-static void UnitAttackAccepted(TYPE_UNIT* ptrUnit);
-
/* **************************************
* Local variables *
* **************************************/
@@ -39,9 +34,9 @@ static uint8_t const UnitHPTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = 25 ,
static uint8_t const UnitSpeedTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = 1 ,
[BARRACKS] = 0 };
-static TYPE_UNIT_ACTION const UnitActionsTable_Level0[MAX_ACTIONS] = { [ACTION_BUILD] = {"BUILD", &UnitBuildAccepted} ,
- [ACTION_ATTACK] = {"ATTACK", &UnitAttackAccepted} ,
- [ACTION_CREATE_UNIT] = {"CREATE", NULL} };
+static const char* const UnitActionsTable_Level[MAX_ACTIONS] = { [ACTION_BUILD] = "BUILD",
+ [ACTION_ATTACK] = "ATTACK",
+ [ACTION_CREATE_UNIT] = "CREATE"};
static uint8_t const UnitActionsTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = ((1 << ACTION_BUILD) | (1 << ACTION_ATTACK)),
[BARRACKS] = (1 << ACTION_CREATE_UNIT) };
@@ -235,45 +230,108 @@ void UnitAttackAccepted(TYPE_UNIT* ptrUnit)
ptrUnit->selecting_attack = true;
}
-void UnitHandler(TYPE_UNIT* ptrUnit)
+void UnitHandler(TYPE_UNIT* unitArray, size_t sz)
{
- bool bMoving = false;
+ size_t i;
- if (ptrUnit->walking == true)
- {
- if ( (ptrUnit->x - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_x)
- {
- ptrUnit->dir = DIRECTION_LEFT;
- ptrUnit->x -= UnitSpeedTable[ptrUnit->id];
- bMoving = true;
- }
- else if ( (ptrUnit->x + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_x)
- {
- ptrUnit->dir = DIRECTION_RIGHT;
- ptrUnit->x += UnitSpeedTable[ptrUnit->id];
- bMoving = true;
- }
-
- if ( (ptrUnit->y - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_y)
- {
- ptrUnit->dir = DIRECTION_UP;
- ptrUnit->y -= UnitSpeedTable[ptrUnit->id];
- bMoving = true;
- }
- else if ( (ptrUnit->y + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_y)
- {
- ptrUnit->dir = DIRECTION_DOWN;
- ptrUnit->y += UnitSpeedTable[ptrUnit->id];
- bMoving = true;
- }
-
- ptrUnit->walking = bMoving;
- }
-}
+ for (i = 0; i < sz; i++)
+ {
+ TYPE_UNIT* ptrUnit = &unitArray[i];
-void UnitAcceptAction(TYPE_UNIT* ptrUnit)
-{
+ if (ptrUnit->alive == false)
+ {
+ continue;
+ }
+
+
+ bool bMoving = false;
+ if (ptrUnit->walking == true)
+ {
+ int8_t x_d = 0;
+ int8_t y_d = 0;
+
+ if ( (ptrUnit->x - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_x)
+ {
+ ptrUnit->dir = DIRECTION_LEFT;
+ x_d = -UnitSpeedTable[ptrUnit->id];
+ //~ ptrUnit->x -= UnitSpeedTable[ptrUnit->id];
+ bMoving = true;
+ }
+ else if ( (ptrUnit->x + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_x)
+ {
+ ptrUnit->dir = DIRECTION_RIGHT;
+ x_d = UnitSpeedTable[ptrUnit->id];
+ //~ ptrUnit->x += UnitSpeedTable[ptrUnit->id];
+ bMoving = true;
+ }
+
+ if ( (ptrUnit->y - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_y)
+ {
+ ptrUnit->dir = DIRECTION_UP;
+ y_d = -UnitSpeedTable[ptrUnit->id];
+ //~ ptrUnit->y -= UnitSpeedTable[ptrUnit->id];
+ bMoving = true;
+ }
+ else if ( (ptrUnit->y + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_y)
+ {
+ ptrUnit->dir = DIRECTION_DOWN;
+ y_d = UnitSpeedTable[ptrUnit->id];
+ //~ ptrUnit->y += UnitSpeedTable[ptrUnit->id];
+ bMoving = true;
+ }
+
+ ptrUnit->walking = bMoving;
+
+ if (ptrUnit->walking == true)
+ {
+ // If player is still walking, check collisions
+ // against all other active units.
+ size_t j;
+
+ for (j = 0; j < sz; j++)
+ {
+ 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;
+
+ if (ptrOtherUnit->alive == false)
+ {
+ continue;
+ }
+
+ if (j == i)
+ {
+ // Do not compare against itself!
+ continue;
+ }
+
+
+ ou.x = ptrOtherUnit->x;
+ ou.y = ptrOtherUnit->x;
+ ou.w = UnitGetWidthFromID(ptrOtherUnit->id);
+ ou.h = UnitGetHeightFromID(ptrOtherUnit->id);
+
+ if (SystemCollisionCheck(cu, ou) == true)
+ {
+ ptrUnit->walking = false;
+ break;
+ }
+ }
+ }
+
+ if (ptrUnit->walking == true)
+ {
+ // If no collision is detected, keep moving to the new position
+ ptrUnit->x += x_d;
+ ptrUnit->y += y_d;
+ }
+ }
+ }
}
uint8_t UnitGetAvailableActions(TYPE_UNIT* ptrUnit)
@@ -281,12 +339,7 @@ uint8_t UnitGetAvailableActions(TYPE_UNIT* ptrUnit)
return UnitActionsTable[ptrUnit->id];
}
-void UnitBuildAccepted(TYPE_UNIT* ptrUnit)
-{
-
-}
-
const char* UnitGetActionString(UNIT_ACTION action)
{
- return UnitActionsTable_Level0[action].str;
+ return UnitActionsTable_Level[action];
}