Simple collision detection between units added. libgamebuino was always updating header files, so targets were always being rebuilt.

This commit is contained in:
XaviDCR92 2017-09-09 12:47:17 +02:00
parent 786dccd2bc
commit a7dd978196
13 changed files with 1848 additions and 1771 deletions

Binary file not shown.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -82,6 +82,8 @@ void GameInit(void)
GamePlayers[i].Init();
}
GamePlayers[0].setHuman(true);
MouseSpr.Data = MouseSprData;
MouseSpr.color = GFX_INVERT;
MouseSpr.rotation = NOROT;
@ -129,7 +131,7 @@ void GameGraphics(void)
{
uint8_t i;
//GfxClearScreen();
GfxClearScreen();
for (i = 0; i < GAME_MAX_PLAYERS; i++)
{

22
Gfx.cpp
View File

@ -53,7 +53,7 @@ void GfxShowKeyboard(char* str, uint8_t length)
void GfxClearScreen(void)
{
//gb.display.fillScreen(GFX_WHITE);
gb.display.fillScreen(GFX_WHITE);
gb.display.clear();
}
@ -214,23 +214,3 @@ void GfxFillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, int8_t color)
gb.display.setColor(orig_color);
}
}
void GfxShowResources(TYPE_RESOURCES* ptrResources)
{
char str[8];
gb.display.setColor(GFX_WHITE);
gb.display.fillRect(0, 0, X_SCREEN_RESOLUTION, 5);
snprintf(str, 8, "W=%d", ptrResources->Wood);
GfxPrintTextFont(str, font3x3, 4, 1);
snprintf(str, 8, "G=%d", ptrResources->Gold);
GfxPrintTextFont(str, font3x3, 24, 1);
snprintf(str, 8, "F=%d", ptrResources->Food);
GfxPrintTextFont(str, font3x3, 48, 1);
}

1
Gfx.h
View File

@ -75,7 +75,6 @@ void GfxPrintText_Flash(const __FlashStringHelper * str);
#endif // __cplusplus
void GfxPrintText(const char* str, uint8_t x, uint8_t y);
void GfxPrintTextFont(const char* str, const uint8_t* font, uint8_t x, uint8_t y);
void GfxShowResources(TYPE_RESOURCES* ptrResources);
void GfxInit(void);
void GfxRenderTiles(TYPE_CAMERA* ptrCamera);

View File

@ -23,9 +23,9 @@
* Defines
* *************************************/
#define DEBUG_VAR(x) char buffer[8]; \
snprintf(buffer, sizeof(buffer), "%d", x); \
GfxPrintText(buffer, X_SCREEN_RESOLUTION - (strlen(buffer)<<3), Y_SCREEN_RESOLUTION - 10)
#define DEBUG_VAR(var, suff, x, y) char buffer##suff[16]; \
snprintf(buffer##suff, sizeof(buffer##suff), "%d", var); \
GfxPrintText(buffer##suff, x - (strlen(buffer##suff)<<3), y)
/* *************************************
* Structs and enums

View File

@ -13,13 +13,13 @@ OBJECTS= Backlight.o Battery.o Buttons.o Display.o font3x3.o \
font3x5.o font5x7.o Gamebuino.o settings.o Sound.o
default: $(LIBNAME)
cp *.h $(INCLUDE_FOLDER)/$(PROJECT)
$(LIBNAME): $(OBJECTS)
avr-ar rcs $@ $^
mkdir -p $(INCLUDE_FOLDER)/$(PROJECT)
mv $@ $(LIBS_FOLDER)
avr-size $(LIBS_FOLDER)/$@
cp *.h $(INCLUDE_FOLDER)/$(PROJECT)
%.o: %.cpp
$(CXX) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)

View File

@ -33,7 +33,13 @@ libs:
run: $(EXE_DIR)/$(PROJECT).ELF
$(GBSIM) $^
depend: $(DEPS)
-include $(DEPS)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(OBJ_DIR)/%.d
$(CXX) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS) -MMD
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(OBJ_DIR)/%.d
$(CC) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS) -MMD
$(OBJ_DIR)/%.d: $(SRC_DIR)/%.c
$(CC) $< $(DEFINE) $(INCLUDE) -M -MF $@
@ -41,14 +47,6 @@ $(OBJ_DIR)/%.d: $(SRC_DIR)/%.c
$(OBJ_DIR)/%.d: $(SRC_DIR)/%.cpp
$(CXX) $< $(DEFINE) $(INCLUDE) -M -MF $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(OBJ_DIR)/%.d
$(CXX) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(OBJ_DIR)/%.d
$(CC) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
-include $(DEPS)
clean:
rm *.elf -f
rm $(OBJ_DIR)/*.o $(OBJ_DIR)/*.d -f

View File

@ -4,6 +4,7 @@
#include "Player.h"
#include "Pad.h"
#include "Unit.h"
#include <limits.h>
/* **************************************
@ -131,17 +132,45 @@ void Player::DrawHandler(void)
}
}
if (bAnyoneSelected == true)
if (human == true)
{
GfxDrawRectangle(PROGRESS_BAR_X, PROGRESS_BAR_Y, PROGRESS_BAR_W, PROGRESS_BAR_H, GFX_BLACK);
if (showActionsMenu_counter != 0)
if (bAnyoneSelected == true)
{
GfxFillRectangle(PROGRESS_BAR_X, PROGRESS_BAR_Y, showActionsMenu_counter << 1, PROGRESS_BAR_H, GFX_BLACK);
GfxDrawRectangle(PROGRESS_BAR_X, PROGRESS_BAR_Y, PROGRESS_BAR_W, PROGRESS_BAR_H, GFX_BLACK);
if (showActionsMenu_counter != 0)
{
GfxFillRectangle(PROGRESS_BAR_X, PROGRESS_BAR_Y, showActionsMenu_counter << 1, PROGRESS_BAR_H, GFX_BLACK);
}
}
ActionsMenu();
ShowResources();
}
}
void Player::ShowResources(void)
{
char str[16];
gb.display.setColor(GFX_WHITE);
gb.display.fillRect(0, 0, X_SCREEN_RESOLUTION, 5);
snprintf(str, sizeof(str), "W=%d", Resources.Wood);
GfxPrintTextFont(str, font3x3, 2, 1);
snprintf(str, sizeof(str), "G=%d", Resources.Gold);
GfxPrintTextFont(str, font3x3, 22, 1);
snprintf(str, sizeof(str), "F=%d", Resources.Food);
GfxPrintTextFont(str, font3x3, 42, 1);
}
bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb)
{
uint8_t i;
@ -293,7 +322,7 @@ void Player::ActionsMenu(void)
const char* str = UnitGetActionString(action);
GfxPrintText(str, 40, Y_SCREEN_RESOLUTION - 4);
GfxPrintTextFont(str, font3x3, 40, Y_SCREEN_RESOLUTION - 4);
break;
}
@ -311,18 +340,9 @@ void Player::Handler(void)
UnitBuildingSelection();
for (uint8_t i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
TYPE_UNIT* ptrUnit = &units[i];
UnitHandler(ptrUnit);
}
ActionsMenu();
UnitHandler(units, sizeof(units) / sizeof(units[0]));
ButtonHandler();
GfxShowResources(&Resources);
}
void Player::ButtonHandler(void)
@ -398,18 +418,14 @@ void Player::ButtonAReleased(void)
if (ptrUnit->selected == true)
{
if (showActionsMenu_index == ACTION_CREATE_UNIT)
switch (showActionsMenu_index)
{
uint8_t w = UnitGetWidthFromID(ptrUnit->id);
uint8_t h = UnitGetHeightFromID(ptrUnit->id);
uint8_t new_pos_x = ptrUnit->x + SystemRand(w, w + (w >> 1));
uint8_t new_pos_y = ptrUnit->y + SystemRand(h, h + (h >> 1));
TYPE_COLLISION_BLOCK cb = {.x = new_pos_x, .y = new_pos_y};
createUnit(PEASANT, cb);
case ACTION_CREATE_UNIT:
ActionCreateUnit(ptrUnit);
break;
}
break;
}
}
}
@ -417,6 +433,20 @@ void Player::ButtonAReleased(void)
showActionsMenu = (showActionsMenu_counter < ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES)? false: true;
}
void Player::ActionCreateUnit(TYPE_UNIT* ptrUnit)
{
if (showActionsMenu_index == ACTION_CREATE_UNIT)
{
uint8_t w = UnitGetWidthFromID(ptrUnit->id);
uint8_t h = UnitGetHeightFromID(ptrUnit->id);
uint8_t new_pos_x = ptrUnit->x + SystemRand(w, w + (w >> 1));
uint8_t new_pos_y = ptrUnit->y + SystemRand(h, h + (h >> 1));
TYPE_COLLISION_BLOCK cb = {.x = new_pos_x, .y = new_pos_y};
createUnit(PEASANT, cb);
}
}
void Player::ButtonBPressed(void)
{
enum
@ -496,8 +526,6 @@ void Player::ButtonLeftReleased(void)
// for current unit.
uint8_t availableActions = UnitGetAvailableActions(ptrUnit);
DEBUG_VAR(availableActions);
for (uint8_t j = showActionsMenu_index - 1; j != showActionsMenu_index ; j--)
{
if (j > (sizeof(uint8_t) << 3))
@ -539,8 +567,6 @@ void Player::IncreaseShowActionsMenuIndex(void)
// for current unit.
uint8_t availableActions = UnitGetAvailableActions(ptrUnit);
DEBUG_VAR(availableActions);
for (uint8_t j = showActionsMenu_index + 1; j != showActionsMenu_index ; j++)
{
if (j >= (sizeof(uint8_t) << 3) )

View File

@ -46,6 +46,7 @@ class Player
bool isHuman(void) {return human;}
bool createUnit(TYPE_UNIT_ID id, TYPE_COLLISION_BLOCK cb);
uint8_t getPopulation(void) {return (unit_i + 1);}
void ShowResources(void);
private:
// Player definition
@ -85,6 +86,9 @@ class Player
uint8_t showActionsMenu_counter;
uint8_t showActionsMenu_index;
void IncreaseShowActionsMenuIndex();
// Action callbacks
void ActionCreateUnit (TYPE_UNIT* ptrUnit);
};
#endif // __cplusplus

147
Unit.c
View File

@ -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;
}
for (i = 0; i < sz; i++)
{
TYPE_UNIT* ptrUnit = &unitArray[i];
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;
}
if (ptrUnit->alive == false)
{
continue;
}
ptrUnit->walking = bMoving;
}
}
bool bMoving = false;
void UnitAcceptAction(TYPE_UNIT* ptrUnit)
{
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];
}

8
Unit.h
View File

@ -77,7 +77,8 @@ typedef enum t_availableactions
{
ACTION_BUILD,
ACTION_ATTACK,
ACTION_CREATE_UNIT
ACTION_CREATE_UNIT,
MAX_ACTIONS
}UNIT_ACTION;
typedef struct t_Camera TYPE_CAMERA;
@ -88,7 +89,7 @@ typedef struct t_Camera TYPE_CAMERA;
// Initialization and handling
void UnitInit(void);
void UnitHandler(TYPE_UNIT* ptrUnit);
void UnitHandler(TYPE_UNIT* unitArray, size_t sz);
// Unit information
uint8_t UnitGetHpFromID(uint8_t id);
@ -101,9 +102,6 @@ void UnitDraw(TYPE_UNIT* ptrUnit, TYPE_CAMERA* ptrCamera, bool bHighlight
void UnitMoveTo(TYPE_UNIT* ptrUnit, uint16_t x, uint16_t y);
void UnitAcceptAction(TYPE_UNIT* ptrUnit);
void UnitResetMenuLevel(void);
// Selection index
const char* UnitGetActionString(UNIT_ACTION action);