Removed Building and GameStructures modules. Restructured SW for Unit and Player, still a lot TODO

This commit is contained in:
XaviDCR92 2017-09-08 18:39:22 +02:00
parent 72d350a37a
commit 786dccd2bc
34 changed files with 2813 additions and 2878 deletions

3
.directory Normal file
View File

@ -0,0 +1,3 @@
[Dolphin]
Timestamp=2017,8,28,19,30,2
Version=3

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
Libs/*
*.o
Obj/

View File

@ -1,112 +0,0 @@
/* **************************************
* Includes *
* **************************************/
#include "Building.h"
#include "BarracksSpr.c"
/* **************************************
* Defines *
* **************************************/
/* **************************************
* Local variables *
* **************************************/
/* Sprites */
static TYPE_SPRITE BarracksSpr;
static TYPE_SPRITE BarracksShadowSpr;
/* Tables */
static uint8_t BuildingHPTable[] = { 100 };
static TYPE_SPRITE * BuildingSprTable[] = {&BarracksSpr};
static TYPE_SPRITE * BuildingShadowSprTable[] = {&BarracksShadowSpr};
void BuildingInit(void)
{
BarracksSpr.Data = BarracksSpr_Data;
BarracksSpr.w = GfxGetWidthFromSpriteData(BarracksSpr_Data);
BarracksSpr.h = GfxGetHeightFromSpriteData(BarracksSpr_Data);
BarracksSpr.flip = 0;
BarracksSpr.rotation = 0;
BarracksSpr.color = GFX_BLACK;
BarracksShadowSpr.Data = BarracksShadowSpr_Data;
BarracksShadowSpr.w = GfxGetWidthFromSpriteData(BarracksShadowSpr_Data);
BarracksShadowSpr.h = GfxGetHeightFromSpriteData(BarracksShadowSpr_Data);
BarracksShadowSpr.flip = 0;
BarracksShadowSpr.rotation = 0;
BarracksShadowSpr.color = GFX_GRAY;
}
uint8_t BuildingGetHpFromID(TYPE_BUILDING_ID id)
{
return BuildingHPTable[id];
}
void BuildingSelectedOptions(TYPE_BUILDING * ptrBuilding)
{
}
void BuildingDraw(TYPE_CAMERA * ptrCamera, TYPE_BUILDING * ptrBuilding, bool bSelected)
{
uint8_t id = ptrBuilding->id;
if(ptrBuilding->built == false)
{
return;
}
CameraApplyCoordinatesToSprite( ptrCamera,
BuildingShadowSprTable[id],
ptrBuilding->x - 6,
ptrBuilding->y );
GfxDrawSprite(BuildingShadowSprTable[id]);
CameraApplyCoordinatesToSprite( ptrCamera,
BuildingSprTable[id],
ptrBuilding->x,
ptrBuilding->y );
GfxDrawSprite(BuildingSprTable[id]);
int8_t color = GFX_WHITE;
if( (bSelected == true) && (ptrBuilding->selected == false) )
{
color = GFX_GRAY;
}
else if(ptrBuilding->selected == true)
{
color = GFX_BLACK;
}
if(color != GFX_WHITE)
{
TYPE_COLLISION_BLOCK cb = CameraApplyCoordinatesToCoordinates(ptrCamera, ptrBuilding->x, ptrBuilding->y);
cb.w = BuildingGetWidthFromID(ptrBuilding->id);
cb.h = BuildingGetWidthFromID(ptrBuilding->id);
GfxDrawRectangle( cb.x - (cb.w / 10),
cb.y - (cb.h / 10),
BuildingGetWidthFromID(ptrBuilding->id) + (cb.w / 5),
BuildingGetHeightFromID(ptrBuilding->id) + (cb.h / 5),
color );
}
}
uint8_t BuildingGetWidthFromID(TYPE_BUILDING_ID id)
{
return GfxGetWidthFromSpriteData(BuildingSprTable[id]->Data);
}
uint8_t BuildingGetHeightFromID(TYPE_BUILDING_ID id)
{
return GfxGetHeightFromSpriteData(BuildingSprTable[id]->Data);
}

View File

@ -1,42 +0,0 @@
#ifndef __BUILDING_HEADER__
#define __BUILDING_HEADER__
/* **************************************
* Includes *
* **************************************/
#include "Global_Inc.h"
#include "Gfx.h"
#include "GameStructures.h"
#include "Camera.h"
#ifdef __cplusplus
extern "C"
{
#endif //__cplusplus
/* **************************************
* Defines *
* **************************************/
/* **************************************
* Structs and enums *
* **************************************/
/* **************************************
* Global prototypes *
* **************************************/
void BuildingInit(void);
uint8_t BuildingGetHpFromID(TYPE_BUILDING_ID id);
uint8_t BuildingGetWidthFromID(TYPE_BUILDING_ID id);
uint8_t BuildingGetHeightFromID(TYPE_BUILDING_ID id);
void BuildingDraw(TYPE_CAMERA * ptrCamera, TYPE_BUILDING * ptrBuilding, bool bSelected);
void BuildingSelectedOptions(TYPE_BUILDING * ptrBuilding);
#ifdef __cplusplus
}
#endif //__cplusplus
#endif //__BUILDING_HEADER__

106
Camera.c
View File

@ -3,6 +3,8 @@
* *************************************/ * *************************************/
#include "Camera.h" #include "Camera.h"
#include "Gfx.h"
#include "Pad.h"
/* ************************************* /* *************************************
* Defines * Defines
@ -16,33 +18,31 @@
* Local Prototypes * Local Prototypes
* *************************************/ * *************************************/
static void CameraUpdateSpeed(TYPE_CAMERA * ptrCamera); static void CameraUpdateSpeed(TYPE_CAMERA* ptrCamera);
static bool CameraSpecialConditions(TYPE_CAMERA * ptrCamera);
void CameraInit(TYPE_CAMERA * ptrCamera) void CameraInit(TYPE_CAMERA* ptrCamera)
{ {
ptrCamera->X_Offset = 0; ptrCamera->X_Offset = 0;
ptrCamera->Y_Offset = 0; ptrCamera->Y_Offset = 0;
ptrCamera->X_Speed = 0; ptrCamera->X_Speed = 0;
ptrCamera->Y_Speed = 0; ptrCamera->Y_Speed = 0;
ptrCamera->Speed_Timer = SPEED_CALCULATION_TIME; ptrCamera->Speed_Timer = SPEED_CALCULATION_TIME;
ptrCamera->locked = false;
} }
TYPE_COLLISION_BLOCK CameraApplyCoordinatesToCoordinates( TYPE_CAMERA * ptrCamera, TYPE_COLLISION_BLOCK CameraApplyCoordinatesToCoordinates( TYPE_CAMERA* ptrCamera,
uint16_t x, uint16_t x,
uint16_t y ) uint16_t y )
{ {
TYPE_COLLISION_BLOCK cb; TYPE_COLLISION_BLOCK cb = {0};
memset(&cb, 0, sizeof(TYPE_COLLISION_BLOCK));
cb.x = x + ptrCamera->X_Offset; cb.x = x + ptrCamera->X_Offset;
cb.y = y + ptrCamera->Y_Offset; cb.y = y + ptrCamera->Y_Offset;
return cb; return cb;
} }
void CameraApplyCoordinatesToSprite(TYPE_CAMERA * ptrCamera, void CameraApplyCoordinatesToSprite(TYPE_CAMERA* ptrCamera,
TYPE_SPRITE * spr, TYPE_SPRITE * spr,
uint16_t x, uint16_t x,
uint16_t y ) uint16_t y )
@ -51,99 +51,98 @@ void CameraApplyCoordinatesToSprite(TYPE_CAMERA * ptrCamera,
spr->y = (uint8_t)(y + ptrCamera->Y_Offset); spr->y = (uint8_t)(y + ptrCamera->Y_Offset);
} }
void CameraUpdateSpeed(TYPE_CAMERA * ptrCamera) void CameraUpdateSpeed(TYPE_CAMERA* ptrCamera)
{ {
if(PadDirectionKeyPressed() == true) if (PadDirectionKeyPressed() == true)
{ {
if(PadButtonPressed(PAD_LEFT) == true) if (PadButtonPressed(PAD_LEFT) == true)
{ {
if(ptrCamera->X_Speed < 0) if (ptrCamera->X_Speed < 0)
{ {
ptrCamera->X_Speed += 2; ptrCamera->X_Speed += 2;
} }
else if(ptrCamera->X_Speed < MAX_CAMERA_SPEED) else if (ptrCamera->X_Speed < MAX_CAMERA_SPEED)
{ {
ptrCamera->X_Speed++; ptrCamera->X_Speed++;
} }
} }
if(PadButtonPressed(PAD_UP) == true) if (PadButtonPressed(PAD_UP) == true)
{ {
if(ptrCamera->Y_Speed < 0) if (ptrCamera->Y_Speed < 0)
{ {
ptrCamera->Y_Speed += 2; ptrCamera->Y_Speed += 2;
} }
else if(ptrCamera->Y_Speed < MAX_CAMERA_SPEED) else if (ptrCamera->Y_Speed < MAX_CAMERA_SPEED)
{ {
ptrCamera->Y_Speed++; ptrCamera->Y_Speed++;
} }
} }
if(PadButtonPressed(PAD_DOWN) == true) if (PadButtonPressed(PAD_DOWN) == true)
{ {
if(ptrCamera->Y_Speed > 0) if (ptrCamera->Y_Speed > 0)
{ {
ptrCamera->Y_Speed -= 2; ptrCamera->Y_Speed -= 2;
} }
else if(ptrCamera->Y_Speed > -MAX_CAMERA_SPEED) else if (ptrCamera->Y_Speed > -MAX_CAMERA_SPEED)
{ {
ptrCamera->Y_Speed--; ptrCamera->Y_Speed--;
} }
} }
if(PadButtonPressed(PAD_RIGHT) == true) if (PadButtonPressed(PAD_RIGHT) == true)
{ {
if(ptrCamera->X_Speed > 0) if (ptrCamera->X_Speed > 0)
{ {
ptrCamera->X_Speed -= 2; ptrCamera->X_Speed -= 2;
} }
else if(ptrCamera->X_Speed > -MAX_CAMERA_SPEED) else if (ptrCamera->X_Speed > -MAX_CAMERA_SPEED)
{ {
ptrCamera->X_Speed--; ptrCamera->X_Speed--;
} }
} }
} }
if( (PadButtonPressed(PAD_LEFT) == false) if ( (PadButtonPressed(PAD_LEFT) == false)
&& &&
(PadButtonPressed(PAD_RIGHT) == false) ) (PadButtonPressed(PAD_RIGHT) == false) )
{ {
if(ptrCamera->X_Speed > 0) if (ptrCamera->X_Speed > 0)
{ {
ptrCamera->X_Speed--; ptrCamera->X_Speed--;
} }
else if(ptrCamera->X_Speed < 0) else if (ptrCamera->X_Speed < 0)
{ {
ptrCamera->X_Speed++; ptrCamera->X_Speed++;
} }
} }
if( (PadButtonPressed(PAD_UP) == false) if ( (PadButtonPressed(PAD_UP) == false)
&& &&
(PadButtonPressed(PAD_DOWN) == false) ) (PadButtonPressed(PAD_DOWN) == false) )
{ {
if(ptrCamera->Y_Speed > 0) if (ptrCamera->Y_Speed > 0)
{ {
ptrCamera->Y_Speed--; ptrCamera->Y_Speed--;
} }
else if(ptrCamera->Y_Speed < 0) else if (ptrCamera->Y_Speed < 0)
{ {
ptrCamera->Y_Speed++; ptrCamera->Y_Speed++;
} }
} }
} }
void CameraHandler(TYPE_CAMERA * ptrCamera) void CameraHandler(TYPE_CAMERA* ptrCamera)
{ {
if (ptrCamera->locked == true)
if(CameraSpecialConditions(ptrCamera) == true)
{ {
ptrCamera->X_Speed = 0; ptrCamera->X_Speed = 0;
ptrCamera->Y_Speed = 0; ptrCamera->Y_Speed = 0;
return; return;
} }
if(ptrCamera->Speed_Timer < SPEED_CALCULATION_TIME) if (ptrCamera->Speed_Timer < SPEED_CALCULATION_TIME)
{ {
ptrCamera->Speed_Timer++; ptrCamera->Speed_Timer++;
} }
@ -152,12 +151,12 @@ void CameraHandler(TYPE_CAMERA * ptrCamera)
ptrCamera->Speed_Timer = 0; ptrCamera->Speed_Timer = 0;
CameraUpdateSpeed(ptrCamera); CameraUpdateSpeed(ptrCamera);
} }
if((ptrCamera->X_Offset + ptrCamera->X_Speed) < 0) if ((ptrCamera->X_Offset + ptrCamera->X_Speed) < 0)
{ {
ptrCamera->X_Offset += ptrCamera->X_Speed; ptrCamera->X_Offset += ptrCamera->X_Speed;
if(ptrCamera->X_Offset == 0) if (ptrCamera->X_Offset == 0)
{ {
ptrCamera->X_Speed = 0; ptrCamera->X_Speed = 0;
} }
@ -167,18 +166,18 @@ void CameraHandler(TYPE_CAMERA * ptrCamera)
ptrCamera->X_Offset = 0; ptrCamera->X_Offset = 0;
ptrCamera->X_Speed = 0; ptrCamera->X_Speed = 0;
} }
/*char str[8]; /*char str[8];
snprintf(str, 8, "%u", ptrCamera->Y_Offset); snprintf(str, 8, "%u", ptrCamera->Y_Offset);
GfxPrintText(str, 40, 40);*/ GfxPrintText(str, 40, 40);*/
if((ptrCamera->Y_Offset + ptrCamera->Y_Speed) < 0) if ((ptrCamera->Y_Offset + ptrCamera->Y_Speed) < 0)
{ {
ptrCamera->Y_Offset += ptrCamera->Y_Speed; ptrCamera->Y_Offset += ptrCamera->Y_Speed;
if(ptrCamera->Y_Offset == 0) if (ptrCamera->Y_Offset == 0)
{ {
ptrCamera->Y_Speed = 0; ptrCamera->Y_Speed = 0;
} }
@ -190,8 +189,7 @@ void CameraHandler(TYPE_CAMERA * ptrCamera)
} }
} }
bool CameraSpecialConditions(TYPE_CAMERA * ptrCamera) void CameraSetLock(TYPE_CAMERA* ptrCamera, bool value)
{ {
ptrCamera->locked = value;
return false;
} }

View File

@ -7,7 +7,6 @@
#include "Global_Inc.h" #include "Global_Inc.h"
#include "Gfx.h" #include "Gfx.h"
#include "Pad.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
@ -18,24 +17,37 @@ extern "C"
/* ************************************* /* *************************************
* Defines * Defines
* *************************************/ * *************************************/
/* ************************************* /* *************************************
* Structs and enums * Structs and enums
* *************************************/ * *************************************/
typedef struct t_Camera
{
int16_t X_Offset;
int16_t Y_Offset;
int8_t X_Speed;
int8_t Y_Speed;
uint8_t Speed_Timer;
bool locked;
}TYPE_CAMERA;
typedef struct t_sprite TYPE_SPRITE;
/* ************************************* /* *************************************
* Global prototypes * Global prototypes
* *************************************/ * *************************************/
void CameraInit(TYPE_CAMERA * ptrCamera); void CameraInit(TYPE_CAMERA* ptrCamera);
void CameraHandler(TYPE_CAMERA * ptrCamera); void CameraHandler(TYPE_CAMERA* ptrCamera);
TYPE_COLLISION_BLOCK CameraApplyCoordinatesToCoordinates( TYPE_CAMERA * ptrCamera, void CameraSetLock(TYPE_CAMERA* ptrCamera, bool value);
uint16_t x, void CameraApplyCoordinatesToSprite( TYPE_CAMERA* ptrCamera,
uint16_t y ); TYPE_SPRITE * spr,
void CameraApplyCoordinatesToSprite(TYPE_CAMERA * ptrCamera, uint16_t x,
TYPE_SPRITE * spr, uint16_t y );
uint16_t x, TYPE_COLLISION_BLOCK CameraApplyCoordinatesToCoordinates( TYPE_CAMERA* ptrCamera,
uint16_t y ); uint16_t x,
uint16_t y );
#ifdef __cplusplus #ifdef __cplusplus
} }

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

@ -1,79 +0,0 @@
#ifndef __GAME_STRUCTURES__HEADER__
#define __GAME_STRUCTURES__HEADER__
#ifdef __cplusplus
extern "C" {
#endif //__cplusplus
/* *************************************
* Includes
* *************************************/
/* *************************************
* Defines
* *************************************/
/* *************************************
* Structs and enums
* *************************************/
typedef struct t_Camera
{
int16_t X_Offset;
int16_t Y_Offset;
int8_t X_Speed;
int8_t Y_Speed;
uint8_t Speed_Timer;
}TYPE_CAMERA;
typedef enum t_buildingIds
{
BARRACKS = 0 ,
TOWER ,
}TYPE_BUILDING_ID;
typedef struct t_Building
{
uint16_t x;
uint16_t y;
uint8_t hp;
TYPE_BUILDING_ID id;
bool built;
bool selected;
}TYPE_BUILDING;
typedef struct t_Unit
{
uint16_t x;
uint16_t y;
uint16_t target_x;
uint16_t target_y;
uint8_t hp;
uint8_t id;
bool dir; // False = up-down; true = left-right
bool mirror; // True = down or left; true = up or right
bool walking;
bool alive;
bool selected;
}TYPE_UNIT;
typedef struct t_CollisionBlock
{
uint16_t x;
uint16_t y;
uint8_t w;
uint8_t h;
}TYPE_COLLISION_BLOCK;
typedef struct t_Resource
{
uint8_t Wood;
uint8_t Gold;
uint8_t Food;
}TYPE_RESOURCES;
#ifdef __cplusplus
}
#endif //__cplusplus
#endif // __GAME_STRUCTURES__HEADER__

View File

@ -1,18 +1,19 @@
/* ************************************** /* **************************************
* Includes * * Includes *
* **************************************/ * **************************************/
#include "Gameplay.h" #include "Gameplay.h"
#include "MouseSpr.c" #include "MouseSpr.c"
#include "Pad.h"
/* ************************************** /* **************************************
* Defines * * Defines *
* **************************************/ * **************************************/
/* ************************************** /* **************************************
* Global variables * * Global variables *
* **************************************/ * **************************************/
Player GamePlayers[GAME_MAX_PLAYERS]; Player GamePlayers[GAME_MAX_PLAYERS];
/* ************************************** /* **************************************
@ -22,8 +23,8 @@ Player GamePlayers[GAME_MAX_PLAYERS];
static const char PauseMenuOption_0[] PROGMEM = "Resume"; static const char PauseMenuOption_0[] PROGMEM = "Resume";
static const char PauseMenuOption_1[] PROGMEM = "Quit"; static const char PauseMenuOption_1[] PROGMEM = "Quit";
static TYPE_SPRITE MouseSpr; static TYPE_SPRITE MouseSpr;
static const char * const PauseMenuOptions[] PROGMEM = {PauseMenuOption_0, static const char* const PauseMenuOptions[] PROGMEM = {PauseMenuOption_0,
PauseMenuOption_1 }; PauseMenuOption_1 };
/* ************************************** /* **************************************
@ -75,50 +76,50 @@ const byte TowerSpr[] PROGMEM = {16,32,
void GameInit(void) void GameInit(void)
{ {
uint8_t i; uint8_t i;
for(i = 0; i < GAME_MAX_PLAYERS; i++) for (i = 0; i < GAME_MAX_PLAYERS; i++)
{ {
GamePlayers[i].Init(); GamePlayers[i].Init();
} }
MouseSpr.Data = MouseSprData; MouseSpr.Data = MouseSprData;
MouseSpr.color = GFX_INVERT; MouseSpr.color = GFX_INVERT;
MouseSpr.rotation = NOROT; MouseSpr.rotation = NOROT;
MouseSpr.flip = NOFLIP; MouseSpr.flip = NOFLIP;
MouseSpr.x = (X_SCREEN_RESOLUTION >> 1) - 4; MouseSpr.x = (X_SCREEN_RESOLUTION >> 1) - 4;
MouseSpr.y = (Y_SCREEN_RESOLUTION >> 1) - 4; MouseSpr.y = (Y_SCREEN_RESOLUTION >> 1) - 4;
GfxInit(); GfxInit();
GameLoop(); GameLoop();
} }
bool GamePause(void) bool GamePause(void)
{ {
if(PadButtonReleased(PAD_C) == true) if (PadButtonReleased(PAD_C) == true)
{ {
//int8_t menu(const char* const* items, uint8_t length); //int8_t menu(const char* const* items, uint8_t length);
uint8_t choice = gb.menu(PauseMenuOptions, 2); uint8_t choice = gb.menu(PauseMenuOptions, 2);
if(choice != 0) if (choice != 0)
{ {
return true; return true;
} }
} }
return false; return false;
} }
void GameCalculations(void) void GameCalculations(void)
{ {
uint8_t i; uint8_t i;
for(i = 0; i < GAME_MAX_PLAYERS; i++) for (i = 0; i < GAME_MAX_PLAYERS; i++)
{ {
GamePlayers[i].Handler(); GamePlayers[i].Handler();
} }
if(PadAnyKeyPressed() == true) if (PadAnyKeyPressed() == true)
{ {
SystemSetRandSeed(); SystemSetRandSeed();
} }
@ -127,32 +128,32 @@ void GameCalculations(void)
void GameGraphics(void) void GameGraphics(void)
{ {
uint8_t i; uint8_t i;
//GfxClearScreen(); //GfxClearScreen();
for(i = 0; i < GAME_MAX_PLAYERS; i++) for (i = 0; i < GAME_MAX_PLAYERS; i++)
{ {
GamePlayers[i].DrawHandler(); GamePlayers[i].DrawHandler();
} }
GfxDrawSprite(&MouseSpr); GfxDrawSprite(&MouseSpr);
} }
void GameLoop(void) void GameLoop(void)
{ {
while(1) while (1)
{ {
if(GamePause() == true) if (GamePause() == true)
{ {
return; return;
} }
GameCalculations(); GameCalculations();
while(GfxRefreshNeeded() == false); while (GfxRefreshNeeded() == false);
GameGraphics(); GameGraphics();
SystemIncreaseGlobalTimer(); SystemIncreaseGlobalTimer();
} }
} }

View File

@ -6,15 +6,12 @@
* **************************************/ * **************************************/
#include "Global_Inc.h" #include "Global_Inc.h"
#include "Gfx.h"
#include "Pad.h"
#include "Menu.h"
#include "Player.h" #include "Player.h"
/* ************************************** /* **************************************
* Defines * * Defines *
* **************************************/ * **************************************/
#define GAME_MAX_PLAYERS 1 #define GAME_MAX_PLAYERS 1
/* ************************************** /* **************************************
@ -26,7 +23,7 @@ void GameInit(void);
/* ************************************** /* **************************************
* Global variables * * Global variables *
* **************************************/ * **************************************/
extern Player GamePlayers[GAME_MAX_PLAYERS]; extern Player GamePlayers[GAME_MAX_PLAYERS];
#endif // __GAMEPLAY_H__ #endif // __GAMEPLAY_H__

120
Gfx.cpp
View File

@ -1,21 +1,21 @@
/* ************************************* /* *************************************
* Includes * Includes
* *************************************/ * *************************************/
#include "Gfx.h" #include "Gfx.h"
/* ************************************* /* *************************************
* Defines * Defines
* *************************************/ * *************************************/
/* ************************************* /* *************************************
* Structs and enums * Structs and enums
* *************************************/ * *************************************/
/* ************************************* /* *************************************
* Local variables * Local variables
* *************************************/ * *************************************/
static bool GfxIsInsideScreenArea(int8_t x, int8_t y, uint8_t w, uint8_t h); static bool GfxIsInsideScreenArea(int8_t x, int8_t y, uint8_t w, uint8_t h);
void GfxInit(void) void GfxInit(void)
@ -23,20 +23,20 @@ void GfxInit(void)
gb.display.persistence = false; // Clears screen automatically gb.display.persistence = false; // Clears screen automatically
gb.display.setFont(font3x5); gb.display.setFont(font3x5);
} }
void GfxDrawSprite(TYPE_SPRITE * ptrSprite) void GfxDrawSprite(TYPE_SPRITE * ptrSprite)
{ {
if(GfxIsSpriteInsideScreenArea(ptrSprite) == true) if (GfxIsSpriteInsideScreenArea(ptrSprite) == true)
{ {
int8_t orig_color = gb.display.getColor(); int8_t orig_color = gb.display.getColor();
gb.display.setColor(ptrSprite->color, GFX_WHITE); gb.display.setColor(ptrSprite->color, GFX_WHITE);
gb.display.drawBitmap( ptrSprite->x, gb.display.drawBitmap( ptrSprite->x,
ptrSprite->y, ptrSprite->y,
ptrSprite->Data, ptrSprite->Data,
ptrSprite->rotation, ptrSprite->rotation,
ptrSprite->flip ); ptrSprite->flip );
gb.display.setColor(orig_color); gb.display.setColor(orig_color);
} }
} }
@ -46,8 +46,8 @@ bool GfxRefreshNeeded(void)
return gb.update(); return gb.update();
} }
void GfxShowKeyboard(char * str, uint8_t length) void GfxShowKeyboard(char* str, uint8_t length)
{ {
gb.keyboard(str, length); gb.keyboard(str, length);
} }
@ -60,17 +60,17 @@ void GfxClearScreen(void)
bool GfxIsInsideScreenArea(int8_t x, int8_t y, uint8_t w, uint8_t h) bool GfxIsInsideScreenArea(int8_t x, int8_t y, uint8_t w, uint8_t h)
{ {
/*char strBuffer[16]; /*char strBuffer[16];
snprintf(strBuffer, 16, "%d", (int)(x + w)); snprintf(strBuffer, 16, "%d", (int)(x + w));
GfxPrintText(strBuffer,48,8); GfxPrintText(strBuffer,48,8);
snprintf(strBuffer, 16, "x = %d", (int)(x)); snprintf(strBuffer, 16, "x = %d", (int)(x));
GfxPrintText(strBuffer,48,16); GfxPrintText(strBuffer,48,16);
snprintf(strBuffer, 16, "w = %d", (int)(w)); snprintf(strBuffer, 16, "w = %d", (int)(w));
GfxPrintText(strBuffer,48,24);*/ GfxPrintText(strBuffer,48,24);*/
if( ( (x + w) >= 0) if ( ( (x + w) >= 0)
&& &&
(x < X_SCREEN_RESOLUTION) (x < X_SCREEN_RESOLUTION)
&& &&
@ -80,7 +80,7 @@ bool GfxIsInsideScreenArea(int8_t x, int8_t y, uint8_t w, uint8_t h)
{ {
return true; return true;
} }
return false; return false;
} }
@ -89,13 +89,13 @@ bool GfxIsSpriteInsideScreenArea(TYPE_SPRITE * spr)
return GfxIsInsideScreenArea(spr->x, spr->y, spr->w, spr->h); return GfxIsInsideScreenArea(spr->x, spr->y, spr->w, spr->h);
} }
uint8_t GfxGetWidthFromSpriteData(const uint8_t * sprData) uint8_t GfxGetWidthFromSpriteData(const uint8_t* sprData)
{ {
// On Gamebuino bitmaps, width is always stored on first byte. // On Gamebuino bitmaps, width is always stored on first byte.
return pgm_read_byte_near(&sprData[0]); return pgm_read_byte_near(&sprData[0]);
} }
uint8_t GfxGetHeightFromSpriteData(const uint8_t * sprData) uint8_t GfxGetHeightFromSpriteData(const uint8_t* sprData)
{ {
// On Gamebuino bitmaps, height is always stored on second byte. // On Gamebuino bitmaps, height is always stored on second byte.
return pgm_read_byte_near(&sprData[1]); return pgm_read_byte_near(&sprData[1]);
@ -106,61 +106,61 @@ void GfxPrintText_Flash(const __FlashStringHelper * str)
gb.popup(str, 20 * 3 /* 3 seconds */); gb.popup(str, 20 * 3 /* 3 seconds */);
} }
void GfxPrintTextFont(const char * str, const uint8_t * font, uint8_t x, uint8_t y) void GfxPrintTextFont(const char* str, const uint8_t* font, uint8_t x, uint8_t y)
{ {
uint8_t * orig_font = gb.display.getFont(); uint8_t* orig_font = gb.display.getFont();
gb.display.cursorX = x; gb.display.cursorX = x;
gb.display.cursorY = y; gb.display.cursorY = y;
gb.display.setFont(font); gb.display.setFont(font);
gb.display.setColor(GFX_BLACK, GFX_WHITE); gb.display.setColor(GFX_BLACK, GFX_WHITE);
gb.display.print(str); gb.display.print(str);
if(orig_font != NULL) if (orig_font != NULL)
{ {
gb.display.setFont(orig_font); gb.display.setFont(orig_font);
} }
} }
void GfxRenderTiles(TYPE_CAMERA * ptrCamera) void GfxRenderTiles(TYPE_CAMERA* ptrCamera)
{ {
gb.display.setColor(GFX_GRAY); gb.display.setColor(GFX_GRAY);
if(ptrCamera == NULL) if (ptrCamera == NULL)
{ {
return; return;
} }
for(int i = 0; i < Y_SCREEN_RESOLUTION; i+=8) for (int i = 0; i < Y_SCREEN_RESOLUTION; i+=8)
{ {
for(int j = 0; j < X_SCREEN_RESOLUTION; j++) for (int j = 0; j < X_SCREEN_RESOLUTION; j++)
{ {
//if(j & 1) //if (j & 1)
//{ //{
int x = j + ptrCamera->X_Offset; int x = j + ptrCamera->X_Offset;
int y = i + ptrCamera->Y_Offset; int y = i + ptrCamera->Y_Offset;
if((x >= 0) && (y >= 0)) if ((x >= 0) && (y >= 0))
{ {
gb.display.drawPixel(j + ptrCamera->X_Offset, i + ptrCamera->Y_Offset); gb.display.drawPixel(j + ptrCamera->X_Offset, i + ptrCamera->Y_Offset);
} }
//} //}
} }
} }
for(int i = 0; i < X_SCREEN_RESOLUTION; i+=8) for (int i = 0; i < X_SCREEN_RESOLUTION; i+=8)
{ {
for(int j = 0; j < Y_SCREEN_RESOLUTION; j++) for (int j = 0; j < Y_SCREEN_RESOLUTION; j++)
{ {
//if(j & 1) //if (j & 1)
//{ //{
int x = j + ptrCamera->X_Offset; int x = j + ptrCamera->X_Offset;
int y = i + ptrCamera->Y_Offset; int y = i + ptrCamera->Y_Offset;
if((x >= 0) && (y >= 0)) if ((x >= 0) && (y >= 0))
{ {
gb.display.drawPixel(i + ptrCamera->X_Offset, j + ptrCamera->Y_Offset); gb.display.drawPixel(i + ptrCamera->X_Offset, j + ptrCamera->Y_Offset);
} }
@ -169,14 +169,14 @@ void GfxRenderTiles(TYPE_CAMERA * ptrCamera)
} }
} }
void GfxPrintText(const char * str, uint8_t x, uint8_t y) void GfxPrintText(const char* str, uint8_t x, uint8_t y)
{ {
GfxPrintTextFont(str, font3x5, x, y); GfxPrintTextFont(str, font3x5, x, y);
} }
void GfxDrawCircle(uint16_t x, uint16_t y, uint8_t radius, int8_t color) void GfxDrawCircle(uint16_t x, uint16_t y, uint8_t radius, int8_t color)
{ {
if(GfxIsInsideScreenArea(x, y, radius, radius) == true) if (GfxIsInsideScreenArea(x, y, radius, radius) == true)
{ {
int8_t orig_color = gb.display.getColor(); int8_t orig_color = gb.display.getColor();
gb.display.setColor(color); gb.display.setColor(color);
@ -187,44 +187,50 @@ void GfxDrawCircle(uint16_t x, uint16_t y, uint8_t radius, int8_t color)
void GfxDrawRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, int8_t color) void GfxDrawRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, int8_t color)
{ {
if(GfxIsInsideScreenArea(x, y, w, h) == true) if (GfxIsInsideScreenArea(x, y, w, h) == true)
{ {
int8_t orig_color = gb.display.getColor(); int8_t orig_color = gb.display.getColor();
gb.display.setColor(color); gb.display.setColor(color);
gb.display.drawRect(x, y, w, h); gb.display.drawRect(x, y, w, h);
gb.display.setColor(orig_color); gb.display.setColor(orig_color);
} }
} }
void GfxDrawLine(uint8_t x0, uint8_t x1, uint8_t y0, uint8_t y1, uint8_t color)
{
gb.display.setColor(color);
gb.display.drawLine(x0, y0, x1, y1);
}
void GfxFillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, int8_t color) void GfxFillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, int8_t color)
{ {
if(GfxIsInsideScreenArea(x, y, w, h) == true) if (GfxIsInsideScreenArea(x, y, w, h) == true)
{ {
int8_t orig_color = gb.display.getColor(); int8_t orig_color = gb.display.getColor();
gb.display.setColor(color); gb.display.setColor(color);
gb.display.fillRect(x, y, w, h); gb.display.fillRect(x, y, w, h);
gb.display.setColor(orig_color); gb.display.setColor(orig_color);
} }
} }
void GfxShowResources(TYPE_RESOURCES * ptrResources) void GfxShowResources(TYPE_RESOURCES* ptrResources)
{ {
char str[8]; char str[8];
gb.display.setColor(GFX_WHITE); gb.display.setColor(GFX_WHITE);
gb.display.fillRect(0, 0, X_SCREEN_RESOLUTION, 5); gb.display.fillRect(0, 0, X_SCREEN_RESOLUTION, 5);
snprintf(str, 8, "W=%d", ptrResources->Wood); snprintf(str, 8, "W=%d", ptrResources->Wood);
GfxPrintTextFont(str, font3x3, 4, 1); GfxPrintTextFont(str, font3x3, 4, 1);
snprintf(str, 8, "G=%d", ptrResources->Gold); snprintf(str, 8, "G=%d", ptrResources->Gold);
GfxPrintTextFont(str, font3x3, 24, 1); GfxPrintTextFont(str, font3x3, 24, 1);
snprintf(str, 8, "F=%d", ptrResources->Food); snprintf(str, 8, "F=%d", ptrResources->Food);
GfxPrintTextFont(str, font3x3, 48, 1); GfxPrintTextFont(str, font3x3, 48, 1);
} }

53
Gfx.h
View File

@ -6,7 +6,9 @@
* *************************************/ * *************************************/
#include "Global_Inc.h" #include "Global_Inc.h"
#include "GameStructures.h" #include "System.h"
#include "Player.h"
#include "Camera.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
@ -16,7 +18,7 @@ extern "C"
/* ************************************* /* *************************************
* Defines * Defines
* *************************************/ * *************************************/
#define X_SCREEN_RESOLUTION 84 #define X_SCREEN_RESOLUTION 84
#define Y_SCREEN_RESOLUTION 48 #define Y_SCREEN_RESOLUTION 48
@ -28,16 +30,17 @@ extern "C"
#define GFX_NOFLIP 0 #define GFX_NOFLIP 0
#define GFX_FLIPH 1 #define GFX_FLIPH 1
#define GFX_FLIPV 2 #define GFX_FLIPV 2
#define GFX_FLIPHV 3 #define GFX_FLIPHV (GFX_FLIPH | GFX_FLIPV)
#define GFX_NOROT 0
#define GFX_ROTCCW 1 #define GFX_ROTCCW 1
#define GFX_ROTCW 3 #define GFX_ROTCW 3
/* ************************************* /* *************************************
* Structs and enums * Structs and enums
* *************************************/ * *************************************/
typedef struct typedef struct t_sprite
{ {
int8_t x; int8_t x;
int8_t y; int8_t y;
@ -46,31 +49,35 @@ typedef struct
uint8_t rotation; uint8_t rotation;
uint8_t flip; uint8_t flip;
uint8_t color; uint8_t color;
const uint8_t * Data; const uint8_t* Data;
}TYPE_SPRITE; }TYPE_SPRITE;
typedef struct t_Resource TYPE_RESOURCES;
typedef struct t_Camera TYPE_CAMERA;
/* ************************************* /* *************************************
* Global prototypes * Global prototypes
* *************************************/ * *************************************/
void GfxDrawSprite(TYPE_SPRITE * ptrSprite); void GfxDrawSprite(TYPE_SPRITE * ptrSprite);
void GfxDrawCircle(uint16_t x, uint16_t y, uint8_t radius, int8_t color); void GfxDrawCircle(uint16_t x, uint16_t y, uint8_t radius, int8_t color);
void GfxDrawRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, int8_t color); void GfxDrawRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, int8_t color);
void GfxFillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, int8_t color); void GfxFillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, int8_t color);
bool GfxRefreshNeeded(void); void GfxDrawLine(uint8_t x0, uint8_t x1, uint8_t y0, uint8_t y1, uint8_t color);
void GfxShowKeyboard(char * str, uint8_t length); bool GfxRefreshNeeded(void);
uint8_t GfxGetWidthFromSpriteData(const uint8_t * sprData); void GfxShowKeyboard(char* str, uint8_t length);
uint8_t GfxGetHeightFromSpriteData(const uint8_t * sprData); uint8_t GfxGetWidthFromSpriteData(const uint8_t* sprData);
bool GfxIsSpriteInsideScreenArea(TYPE_SPRITE * spr); uint8_t GfxGetHeightFromSpriteData(const uint8_t* sprData);
void GfxClearScreen(void); bool GfxIsSpriteInsideScreenArea(TYPE_SPRITE * spr);
void GfxClearScreen(void);
#ifdef __cplusplus #ifdef __cplusplus
void GfxPrintText_Flash(const __FlashStringHelper * str); void GfxPrintText_Flash(const __FlashStringHelper * str);
#endif // __cplusplus #endif // __cplusplus
void GfxPrintText(const char * str, uint8_t x, uint8_t y); 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 GfxPrintTextFont(const char* str, const uint8_t* font, uint8_t x, uint8_t y);
void GfxShowResources(TYPE_RESOURCES * ptrResources); void GfxShowResources(TYPE_RESOURCES* ptrResources);
void GfxInit(void); void GfxInit(void);
void GfxRenderTiles(TYPE_CAMERA * ptrCamera); void GfxRenderTiles(TYPE_CAMERA* ptrCamera);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -12,9 +12,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include "System.h"
#include "settings.c" #include "settings.c"
#include "GameStructures.h"
#ifdef __cplusplus #ifdef __cplusplus
#include <Gamebuino.h> #include <Gamebuino.h>
@ -25,6 +23,10 @@
* Defines * 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)
/* ************************************* /* *************************************
* Structs and enums * Structs and enums
* *************************************/ * *************************************/

View File

@ -2,28 +2,31 @@ include ../../Makefile.cfg
PROJECT=arduino PROJECT=arduino
LIBNAME=lib$(PROJECT).a LIBNAME=lib$(PROJECT).a
INCDIR=../../../include/$(PROJECT)
INCLUDE_FOLDER=../../../include/$(PROJECT)
LIBS_FOLDER=../../../lib LIBS_FOLDER=../../../lib
OBJECTS= wiring.o wiring_analog.o wiring_digital.o \ OBJECTS= wiring.o wiring_analog.o wiring_digital.o \
wiring_pulse.o wiring_shift.o HardwareSerial.o Print.o \ wiring_pulse.o wiring_shift.o HardwareSerial.o Print.o \
Tone.o WMath.o WString.o WInterrupts.o forward.o SPI.o Tone.o WMath.o WString.o WInterrupts.o forward.o SPI.o
default: $(OBJECTS) default: $(LIBNAME)
avr-ar rcs $(LIBNAME) $^ cp *.h $(INCLUDE_FOLDER)/$(PROJECT)
mkdir -p $(INCDIR)
cp *.h $(INCDIR)/
mkdir -p $(LIBS_FOLDER)
mv $(LIBNAME) $(LIBS_FOLDER)
avr-size $(LIBS_FOLDER)/$(LIBNAME)
$(LIBNAME): $(OBJECTS)
avr-ar rcs $@ $^
mkdir -p $(INCLUDE_FOLDER)/$(PROJECT)
mv $@ $(LIBS_FOLDER)
avr-size $(LIBS_FOLDER)/$@
%.o: %.cpp %.o: %.cpp
$(CXX) $< -o $@ $(DEFINE) $(INCLUDE) $(CC_FLAGS) $(CXX) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
%.o: %.c %.o: %.c
$(CC) $< -o $@ $(DEFINE) $(INCLUDE) $(CC_FLAGS) $(CC) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
clean: clean:
rm -f *.o rm -f $(OBJECTS)
rm -f $(LIBS_FOLDER)/$(LIBNAME)
.PHONY: default .PHONY: default clean

View File

@ -12,12 +12,14 @@ CFLAGS=-mmcu=$(MCU) $(CPU_SPEED) -Wall -Os -c -ffunction-sections -fdata-section
OBJECTS= Backlight.o Battery.o Buttons.o Display.o font3x3.o \ OBJECTS= Backlight.o Battery.o Buttons.o Display.o font3x3.o \
font3x5.o font5x7.o Gamebuino.o settings.o Sound.o font3x5.o font5x7.o Gamebuino.o settings.o Sound.o
default: $(OBJECTS) default: $(LIBNAME)
avr-ar rcs $(LIBNAME).a $^
mkdir -p $(INCLUDE_FOLDER)/$(PROJECT)
cp *.h $(INCLUDE_FOLDER)/$(PROJECT) cp *.h $(INCLUDE_FOLDER)/$(PROJECT)
cp settings.c $(INCLUDE_FOLDER)/$(PROJECT)
mv $(LIBNAME).a $(LIBS_FOLDER) $(LIBNAME): $(OBJECTS)
avr-ar rcs $@ $^
mkdir -p $(INCLUDE_FOLDER)/$(PROJECT)
mv $@ $(LIBS_FOLDER)
avr-size $(LIBS_FOLDER)/$@
%.o: %.cpp %.o: %.cpp
$(CXX) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS) $(CXX) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
@ -25,7 +27,8 @@ default: $(OBJECTS)
%.o: %.c %.o: %.c
$(CC) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS) $(CC) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
clean: clean:
rm -f *.o rm -f $(OBJECTS)
rm -f $(LIBS_FOLDER)/$(LIBNAME).a rm -f $(LIBS_FOLDER)/$(LIBNAME)
.PHONY: default clean

View File

@ -11,12 +11,14 @@ LIBNAME=lib$(PROJECT).a
OBJECTS=mmc.o petit_fatfs.o pff.o OBJECTS=mmc.o petit_fatfs.o pff.o
default: $(OBJECTS) default: $(LIBNAME)
avr-ar rcs $(LIBNAME) $^ cp *.h $(INCLUDE_FOLDER)/$(PROJECT)
$(LIBNAME): $(OBJECTS)
avr-ar rcs $@ $^
mkdir -p $(INCLUDE_FOLDER)/$(PROJECT) mkdir -p $(INCLUDE_FOLDER)/$(PROJECT)
cp *.h $(INCLUDE_FOLDER)//$(PROJECT) mv $@ $(LIBS_FOLDER)
mv $(LIBNAME) $(LIBS_FOLDER) avr-size $(LIBS_FOLDER)/$@
avr-size $(LIBS_FOLDER)/$(LIBNAME)
%.o: %.cpp %.o: %.cpp
$(CXX) $< -o $@ $(DEFINE) $(INCLUDE) $(CC_FLAGS) $(CXX) $< -o $@ $(DEFINE) $(INCLUDE) $(CC_FLAGS)
@ -27,3 +29,5 @@ default: $(OBJECTS)
clean: clean:
rm -f $(OBJECTS) rm -f $(OBJECTS)
rm -f $(LIBS_FOLDER)/$(LIBNAME) rm -f $(LIBS_FOLDER)/$(LIBNAME)
.PHONY: default clean

View File

@ -10,12 +10,14 @@ LIBNAME=lib$(PROJECT).a
OBJECTS=mmc.o tinyFAT.o OBJECTS=mmc.o tinyFAT.o
default: $(OBJECTS) default: $(LIBNAME)
avr-ar rcs $(LIBNAME) $^
mkdir -p $(INCLUDE_FOLDER)/$(PROJECT)
cp *.h $(INCLUDE_FOLDER)/$(PROJECT) cp *.h $(INCLUDE_FOLDER)/$(PROJECT)
mv $(LIBNAME) $(LIBS_FOLDER)
avr-size $(LIBS_FOLDER)/$(LIBNAME) $(LIBNAME): $(OBJECTS)
avr-ar rcs $@ $^
mkdir -p $(INCLUDE_FOLDER)/$(PROJECT)
mv $@ $(LIBS_FOLDER)
avr-size $(LIBS_FOLDER)/$@
%.o: %.cpp %.o: %.cpp
$(CXX) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS) $(CXX) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
@ -26,3 +28,5 @@ default: $(OBJECTS)
clean: clean:
rm -f $(OBJECTS) rm -f $(OBJECTS)
rm -f $(LIBS_FOLDER)/$(LIBNAME) rm -f $(LIBS_FOLDER)/$(LIBNAME)
.PHONY: default clean

View File

@ -13,11 +13,13 @@ OBJ_DIR = Obj
SRC_DIR = . SRC_DIR = .
OBJECTS=$(addprefix $(OBJ_DIR)/,main.o Gameplay.o System.o Gfx.o Pad.o \ OBJECTS=$(addprefix $(OBJ_DIR)/,main.o Gameplay.o System.o Gfx.o Pad.o \
Player.o Camera.o Unit.o Menu.o Building.o) Player.o Camera.o Unit.o Menu.o )
DEPS = $(OBJECTS:.o=.d)
build: libs $(EXE_DIR)/$(PROJECT).HEX build: libs $(EXE_DIR)/$(PROJECT).HEX
avr-size $(EXE_DIR)/$(PROJECT).ELF avr-size $(EXE_DIR)/$(PROJECT).ELF
rebuild: rebuild:
make clean make clean
make build make build
@ -28,25 +30,38 @@ libs:
make -C Libs/petit_fatfs make -C Libs/petit_fatfs
make -C Libs/tinyFAT make -C Libs/tinyFAT
run: $(EXE_DIR)/$(PROJECT).ELF
$(GBSIM) $^
depend: $(DEPS)
$(OBJ_DIR)/%.d: $(SRC_DIR)/%.c
$(CC) $< $(DEFINE) $(INCLUDE) -M -MF $@
$(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: clean:
rm *.elf -f rm *.elf -f
rm $(OBJ_DIR)/*.o -f rm $(OBJ_DIR)/*.o $(OBJ_DIR)/*.d -f
make -C $$PWD/Libs/libarduino clean make -C $$PWD/Libs/libarduino clean
make -C $$PWD/Libs/libgamebuino clean make -C $$PWD/Libs/libgamebuino clean
make -C $$PWD/Libs/petit_fatfs clean make -C $$PWD/Libs/petit_fatfs clean
make -C $$PWD/Libs/tinyFAT clean make -C $$PWD/Libs/tinyFAT clean
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
$(CC) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
$(EXE_DIR)/$(PROJECT).ELF: $(OBJECTS) $(EXE_DIR)/$(PROJECT).ELF: $(OBJECTS)
mkdir -p $(EXE_DIR) mkdir -p $(EXE_DIR)
$(LINKER) $^ -o $@ -g $(DEFINE) $(LIBS) $(INCLUDE) -flto -Xlinker -Map=$(EXE_DIR)/POCKET.MAP -Wl,-emain -Wl,--gc-sections $(LINKER) $^ -o $@ -g $(DEFINE) $(LIBS) $(INCLUDE) -flto -Xlinker -Map=$(EXE_DIR)/POCKET.MAP -Wl,-emain -Wl,--gc-sections
$(EXE_DIR)/$(PROJECT).HEX: $(EXE_DIR)/$(PROJECT).ELF $(EXE_DIR)/$(PROJECT).HEX: $(EXE_DIR)/$(PROJECT).ELF
avr-objcopy -j.text -j.data -j.bss -O ihex $^ $@ avr-objcopy -j.text -j.data -j.bss -O ihex $^ $@
.PHONY: all libs clean .PHONY: all libs clean run depend

View File

@ -2,12 +2,15 @@ CC=avr-gcc
CXX=avr-g++ CXX=avr-g++
LINKER=$(CXX) LINKER=$(CXX)
MCU=atmega328p MCU=atmega328p
CPU_SPEED=16000000UL CPU_SPEED=16000000UL
ARDUINO_VERSION=150 ARDUINO_VERSION=150
AVR_TOOLCHAIN_PATH=/usr/local/avr/ AVR_TOOLCHAIN_PATH=/usr/local/avr/
GBSIM_PATH = ~/gbsim/build
PATH := $(PATH):$(AVR_TOOLCHAIN_PATH)/bin/ PATH := $(PATH):$(AVR_TOOLCHAIN_PATH)/bin/
GBSIM = $(GBSIM_PATH)/gbsim
DEFINE = -DARDUINO=$(ARDUINO_VERSION) -mmcu=$(MCU) -DF_CPU=$(CPU_SPEED) DEFINE = -DARDUINO=$(ARDUINO_VERSION) -mmcu=$(MCU) -DF_CPU=$(CPU_SPEED)

View File

@ -1,39 +1,41 @@
/* ************************************** /* **************************************
* Includes * * Includes *
* **************************************/ * **************************************/
#include "Menu.h" #include "Menu.h"
#include "Player.h"
#include "Gameplay.h"
/* ************************************** /* **************************************
* Defines * * Defines *
* **************************************/ * **************************************/
/* ************************************** /* **************************************
* Local variables * * Local variables *
* **************************************/ * **************************************/
static const char MainMenuOption_0[] PROGMEM = "Single player game"; static const char MainMenuOption_0[] PROGMEM = "Single player game";
static const char MainMenuOption_1[] PROGMEM = "Multiplayer game"; static const char MainMenuOption_1[] PROGMEM = "Multiplayer game";
static const char MainMenuOption_2[] PROGMEM = "Options"; static const char MainMenuOption_2[] PROGMEM = "Options";
static const char MainMenuOption_3[] PROGMEM = "Quit"; static const char MainMenuOption_3[] PROGMEM = "Quit";
static const char * const MainMenuOptions[] PROGMEM = { MainMenuOption_0, static const char* const MainMenuOptions[] PROGMEM = { MainMenuOption_0,
MainMenuOption_1, MainMenuOption_1,
MainMenuOption_2, MainMenuOption_2,
MainMenuOption_3 }; MainMenuOption_3 };
void MenuGetPlayerName(Player * ptrPlayer) void MenuGetPlayerName(Player * ptrPlayer)
{ {
memset(ptrPlayer->getName(), 0, PLAYER_NAME_LENGTH); memset(ptrPlayer->getName(), 0, PLAYER_NAME_LENGTH);
gb.getDefaultName(ptrPlayer->getName()); gb.getDefaultName(ptrPlayer->getName());
} }
void MainMenu(void) void MainMenu(void)
{ {
//int8_t menu(const char* const* items, uint8_t length); //int8_t menu(const char* const* items, uint8_t length);
uint8_t choice = gb.menu(MainMenuOptions, 3); uint8_t choice = gb.menu(MainMenuOptions, 3);
switch(choice) switch(choice)
{ {
case 0: case 0:
@ -44,9 +46,9 @@ void MainMenu(void)
break; break;
break; break;
case 2: case 2:
break; break;
default: default:
break; break;
} }

8
Menu.h
View File

@ -4,15 +4,13 @@
/* ************************************** /* **************************************
* Includes * * Includes *
* **************************************/ * **************************************/
#include "Global_Inc.h" #include "Global_Inc.h"
#include "Player.h"
#include "Gameplay.h"
/* ************************************** /* **************************************
* Global prototypes * * Global prototypes *
* **************************************/ * **************************************/
void MainMenu(void); void MainMenu(void);
#endif // __MENU_HEADER__ #endif // __MENU_HEADER__

View File

@ -1,13 +1,13 @@
const uint8_t PROGMEM MouseSprData[] = const uint8_t PROGMEM MouseSprData[] =
{ {
8,8, //width and height 8,8, //width and height
B11111100, B11111100,
B10000100, B10000100,
B10001000, B10001000,
B10000100, B10000100,
B10100010, B10100010,
B11010001, B11010001,
B00001010, B00001010,
B00000100, B00000100,
}; };

View File

@ -7,11 +7,11 @@
/* ************************************* /* *************************************
* Defines * Defines
* *************************************/ * *************************************/
/* ************************************* /* *************************************
* Structs and enums * Structs and enums
* *************************************/ * *************************************/
bool PadButtonReleased(PAD_BUTTONS btn) bool PadButtonReleased(PAD_BUTTONS btn)
{ {
return gb.buttons.released(btn); return gb.buttons.released(btn);

10
Pad.h
View File

@ -4,18 +4,18 @@
/* ************************************** /* **************************************
* Includes * * Includes *
* **************************************/ * **************************************/
#include "Global_Inc.h" #include "Global_Inc.h"
/* ************************************** /* **************************************
* Defines * * Defines *
* **************************************/ * **************************************/
/* ************************************** /* **************************************
* Structs and enums * * Structs and enums *
* **************************************/ * **************************************/
//BTN_A, BTN_B, BTN_C, BTN_UP, BTN_RIGHT, BTN_DOWN, BTN_LEFT //BTN_A, BTN_B, BTN_C, BTN_UP, BTN_RIGHT, BTN_DOWN, BTN_LEFT
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
@ -32,11 +32,11 @@ typedef enum t_padbuttons
PAD_UP = BTN_UP, PAD_UP = BTN_UP,
PAD_DOWN = BTN_DOWN, PAD_DOWN = BTN_DOWN,
}PAD_BUTTONS; }PAD_BUTTONS;
/* ************************************** /* **************************************
* Global prototypes * * Global prototypes *
* **************************************/ * **************************************/
bool PadButtonReleased(PAD_BUTTONS btn); bool PadButtonReleased(PAD_BUTTONS btn);
bool PadButtonPressed(PAD_BUTTONS btn); bool PadButtonPressed(PAD_BUTTONS btn);
bool PadButtonPressedFrames(PAD_BUTTONS btn, uint8_t frames); bool PadButtonPressedFrames(PAD_BUTTONS btn, uint8_t frames);

View File

@ -4,27 +4,27 @@
const uint8_t PROGMEM Peasant_Walking_SprData[] = const uint8_t PROGMEM Peasant_Walking_SprData[] =
{ {
8,8, //width and height 8,8, //width and height
0x60, 0x60,
0xBC, 0xBC,
0xC2, 0xC2,
0x99, 0x99,
0xA5, 0xA5,
0x67, 0x67,
0x1D, 0x1D,
0x02, 0x02,
}; };
const uint8_t PROGMEM Peasant_SprData[] = const uint8_t PROGMEM Peasant_SprData[] =
{ {
8,8, //width and height 8,8, //width and height
0x00, 0x00,
0x3C, 0x3C,
0x42, 0x42,
0x99, 0x99,
0xA5, 0xA5,
0x66, 0x66,
0x18, 0x18,
0x00, 0x00,
}; };

View File

@ -1,17 +1,19 @@
/* ************************************** /* **************************************
* Includes * * Includes *
* **************************************/ * **************************************/
#include "Player.h" #include "Player.h"
#include "Pad.h"
#include <limits.h>
/* ************************************** /* **************************************
* Defines * * Defines *
* **************************************/ * **************************************/
#define CANCEL_SELECTION_NO_FRAMES 5
#define ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES 5 #define ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES 5
#define MAX_SELECTION_DIST 400 #define MAX_SELECTION_DIST 400
#define NO_SELECTION -1
/* ************************************** /* **************************************
* Local variables * * Local variables *
* **************************************/ * **************************************/
@ -22,55 +24,50 @@ Player::Player(void)
Player::~Player(void) Player::~Player(void)
{ {
} }
void Player::Init(void) void Player::Init(void)
{ {
uint8_t i; uint8_t i;
unit_i = 0; unit_i = 0;
bldg_i = 0;
selectedUnitCandidate = NO_SELECTION;
selectedUnit = NULL;
selectedBuilding = NULL;
CameraInit(&Camera); CameraInit(&Camera);
BuildingInit();
UnitInit(); UnitInit();
for(i = 0; i < PLAYER_MAX_BUILDINGS; i++) for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
memset(&buildings[i], 0, sizeof(TYPE_BUILDING));
}
for(i = 0; i < PLAYER_MAX_UNITS; i++)
{ {
memset(&units[i], 0, sizeof(TYPE_UNIT)); memset(&units[i], 0, sizeof(TYPE_UNIT));
} }
TYPE_COLLISION_BLOCK cl; TYPE_COLLISION_BLOCK cb;
Resources.Wood = 25; Resources.Wood = 25;
Resources.Gold = 50; Resources.Gold = 50;
Resources.Food = 75; Resources.Food = 75;
cl.x = SystemRand(0, 20); cb.x = SystemRand(0, 20);
cl.y = SystemRand(0, 20); cb.y = SystemRand(0, 20);
cl.w = BuildingGetWidthFromID(BARRACKS);
cl.h = BuildingGetHeightFromID(BARRACKS); if (createUnit(BARRACKS, cb) == false)
if(createBuilding(BARRACKS, cl) == false)
{ {
GfxPrintText_Flash(F("Failed to create building!")); GfxPrintText_Flash(F("Failed to create building!"));
} }
cl.x = SystemRand(48, 56); cb.x = SystemRand(48, 56);
cl.y = SystemRand(48, 56); cb.y = SystemRand(48, 56);
cl.w = UnitGetWidthFromID(PEASANT); cb.w = UnitGetWidthFromID(PEASANT);
cl.h = UnitGetHeightFromID(PEASANT); cb.h = UnitGetHeightFromID(PEASANT);
if(createUnit(PEASANT, cl) == false) showActionsMenu_counter = 0;
showActionsMenu_index = 0;
showActionsMenu = false;
anyUnitSelected = false;
if (createUnit(PEASANT, cb) == false)
{ {
GfxPrintText_Flash(F("Failed to create unit!")); GfxPrintText_Flash(F("Failed to create unit!"));
} }
@ -78,68 +75,71 @@ void Player::Init(void)
void Player::showHealth(uint8_t hp) void Player::showHealth(uint8_t hp)
{ {
enum
{
HP_TEXT_X = 4,
HP_TEXT_Y = Y_SCREEN_RESOLUTION - 4,
};
char str[8]; char str[8];
GfxFillRectangle(0, Y_SCREEN_RESOLUTION - 5, X_SCREEN_RESOLUTION, 8, GFX_WHITE); GfxFillRectangle(0, Y_SCREEN_RESOLUTION - 5, X_SCREEN_RESOLUTION, 8, GFX_WHITE);
snprintf(str, 8, "HP=%u", hp); snprintf(str, sizeof(str), "HP=%u", hp);
GfxPrintTextFont(str, font3x3, 4, Y_SCREEN_RESOLUTION - 4); GfxPrintTextFont(str, font3x3, HP_TEXT_X, HP_TEXT_Y);
} }
void Player::DrawHandler(void) void Player::DrawHandler(void)
{ {
enum
{
PROGRESS_BAR_X = X_SCREEN_RESOLUTION - 16,
PROGRESS_BAR_Y = Y_SCREEN_RESOLUTION - 4,
PROGRESS_BAR_W = ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES << 1,
PROGRESS_BAR_H = 3,
};
uint8_t i; uint8_t i;
bool bAnyoneSelected = false; bool bAnyoneSelected = false;
//GfxRenderTiles(&Camera); //GfxRenderTiles(&Camera);
for(i = 0; i < PLAYER_MAX_BUILDINGS; i++) for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{ {
TYPE_BUILDING * b = &buildings[i]; TYPE_UNIT* u = &units[i];
if(b->built == false) if (u->alive == false)
{ {
continue; continue;
} }
bool selected = (b == selectedBuilding); bool selected = false;
BuildingDraw(&Camera, b, selected);
if (selectedUnitCandidate != NO_SELECTION)
if( (b->selected == true) && (bAnyoneSelected == false) ) {
selected = (i == selectedUnitCandidate);
}
UnitDraw(u, &Camera, selected);
if ( (u->selected == true) && (bAnyoneSelected == false) )
{ {
bAnyoneSelected = true; bAnyoneSelected = true;
showHealth(b->hp);
BuildingSelectedOptions(b);
}
}
for(i = 0; i < PLAYER_MAX_UNITS; i++)
{
TYPE_UNIT * u = &units[i];
if(u->alive == false)
{
continue;
}
bool selected = (u == selectedUnit);
UnitDraw(&Camera, u, selected);
if( (u->selected == true) && (bAnyoneSelected == false) )
{
bAnyoneSelected = true;
showHealth(u->hp); showHealth(u->hp);
UnitSelectedOptions(u);
} }
} }
if(progress_bar != 0) if (bAnyoneSelected == true)
{ {
GfxDrawRectangle(X_SCREEN_RESOLUTION - 16, Y_SCREEN_RESOLUTION - 4, progress_bar, 2, 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);
}
}
} }
bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb) bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb)
@ -148,22 +148,23 @@ bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb)
TYPE_COLLISION_BLOCK bldgCB; TYPE_COLLISION_BLOCK bldgCB;
bool success; bool success;
static uint8_t max_tries = 0; static uint8_t max_tries = 0;
for(i = 0; i < PLAYER_MAX_BUILDINGS; i++) for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{ {
TYPE_UNIT* ptrUnit = &units[i];
success = false; success = false;
if(buildings[i].built == false) if (ptrUnit->building == false)
{ {
continue; continue;
} }
bldgCB.x = buildings[i].x; bldgCB.x = ptrUnit->x;
bldgCB.y = buildings[i].y; bldgCB.y = ptrUnit->y;
bldgCB.w = BuildingGetWidthFromID(buildings[i].id); bldgCB.w = UnitGetWidthFromID(ptrUnit->id);
bldgCB.h = BuildingGetHeightFromID(buildings[i].id); bldgCB.h = UnitGetHeightFromID(ptrUnit->id);
if(SystemCollisionCheck(*cb, bldgCB) == true) if (SystemCollisionCheck(*cb, bldgCB) == true)
{ {
success = false; success = false;
} }
@ -171,15 +172,15 @@ bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb)
{ {
success = true; success = true;
} }
if(success == false) if (success == false)
{ {
cb->x = SystemRand(0, 128); cb->x = SystemRand(0, 128);
cb->y = SystemRand(0, 128); cb->y = SystemRand(0, 128);
if(++max_tries < 16) if (++max_tries < 16)
{ {
if(checkNewBuildingPosition(cb) == false) if (checkNewBuildingPosition(cb) == false)
{ {
return false; return false;
} }
@ -194,332 +195,366 @@ bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb)
} }
} }
} }
max_tries = 0; max_tries = 0;
return true; return true;
} }
bool Player::createBuilding(TYPE_BUILDING_ID id, TYPE_COLLISION_BLOCK cb) bool Player::createUnit(TYPE_UNIT_ID id, TYPE_COLLISION_BLOCK cb)
{ {
if(checkNewBuildingPosition(&cb) == false) if (unit_i < PLAYER_MAX_UNITS_BUILDINGS)
{ {
return false; TYPE_UNIT* ptrNewUnit = &units[unit_i];
} ptrNewUnit->id = id;
ptrNewUnit->x = cb.x;
ptrNewUnit->y = cb.y;
ptrNewUnit->hp = UnitGetHpFromID(id);
ptrNewUnit->alive = true;
if(bldg_i < PLAYER_MAX_BUILDINGS) ptrNewUnit->building = (id > MAX_UNIT_ID);
{
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;
}
bool Player::createUnit(uint8_t id, TYPE_COLLISION_BLOCK cb)
{
if(unit_i < PLAYER_MAX_UNITS)
{
units[unit_i].id = id;
units[unit_i].x = cb.x;
units[unit_i].y = cb.y;
units[unit_i].hp = UnitGetHpFromID(id);
units[unit_i].alive = true;
unit_i++; unit_i++;
return true; return true;
} }
else else
{ {
return false; return false;
} }
return false; return false;
} }
TYPE_COLLISION_BLOCK Player::GetCursorPos(void) TYPE_COLLISION_BLOCK Player::GetCursorPos(void)
{ {
TYPE_COLLISION_BLOCK cb; TYPE_COLLISION_BLOCK cb;
cb.x = (X_SCREEN_RESOLUTION >> 1) - 4 - Camera.X_Offset; cb.x = (X_SCREEN_RESOLUTION >> 1) - 4 - Camera.X_Offset;
cb.y = (Y_SCREEN_RESOLUTION >> 1) - 4 - Camera.Y_Offset; cb.y = (Y_SCREEN_RESOLUTION >> 1) - 4 - Camera.Y_Offset;
cb.w = 8; cb.w = 8;
cb.h = 8; cb.h = 8;
return cb; return cb;
} }
void Player::UnitBuildingSelection(void) void Player::UnitBuildingSelection(void)
{ {
uint16_t i; uint16_t i;
TYPE_UNIT * nearest_unit = NULL;
uint32_t nearest_unit_dist = 0xFFFFFFFF; // Set maximum value uint32_t nearest_unit_dist = 0xFFFFFFFF; // Set maximum value
uint32_t dist; uint32_t dist;
for(i = 0; i < PLAYER_MAX_UNITS; i++) int8_t nearest_unit = NO_SELECTION;
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{ {
TYPE_UNIT * u = &units[i]; TYPE_UNIT* u = &units[i];
if( (u->alive == false) || (u->selected == true) ) if ( (u->alive == false) || (u->selected == true) )
{ {
continue; continue;
} }
TYPE_COLLISION_BLOCK cursor_cb = GetCursorPos(); TYPE_COLLISION_BLOCK cursor_cb = GetCursorPos();
TYPE_COLLISION_BLOCK u_cb = {u->x, u->y, UnitGetWidthFromID(u->id), UnitGetHeightFromID(u->id) }; TYPE_COLLISION_BLOCK u_cb = {u->x, u->y, UnitGetWidthFromID(u->id), UnitGetHeightFromID(u->id) };
uint16_t dist_x = (u_cb.x - cursor_cb.x); uint16_t dist_x = (u_cb.x + (u_cb.w >> 1) - cursor_cb.x);
uint16_t dist_y = (u_cb.y - cursor_cb.y); uint16_t dist_y = (u_cb.y + (u_cb.h >> 1) - cursor_cb.y);
dist = (dist_x * dist_x) + (dist_y * dist_y); dist = (dist_x * dist_x) + (dist_y * dist_y);
if(dist < nearest_unit_dist) if (dist < nearest_unit_dist)
{ {
nearest_unit_dist = dist; nearest_unit_dist = dist;
nearest_unit = u; nearest_unit = (int8_t)i;
} }
} }
TYPE_BUILDING * nearest_building = NULL; selectedUnitCandidate = nearest_unit;
uint32_t nearest_building_dist = 0xFFFFFFFF; // Set maximum value
for(i = 0; i < PLAYER_MAX_BUILDINGS; i++)
{
TYPE_BUILDING * b = &buildings[i];
if( (b->built == false) || (b->selected == true) )
{
continue;
}
TYPE_COLLISION_BLOCK cursor_cb = GetCursorPos();
TYPE_COLLISION_BLOCK u_cb = {b->x, b->y, BuildingGetWidthFromID(b->id), BuildingGetHeightFromID(b->id) };
uint16_t dist_x = (u_cb.x + (u_cb.w >> 1) - cursor_cb.x + 4);
uint16_t dist_y = (u_cb.y + (u_cb.h >> 1) - cursor_cb.y + 4);
dist = (dist_x * dist_x) + (dist_y * dist_y);
if(dist < nearest_building_dist)
{
nearest_building_dist = dist;
nearest_building = b;
}
}
if(nearest_building_dist <= nearest_unit_dist)
{
selectedUnit = NULL;
selectedBuilding = nearest_building;
}
else
{
selectedUnit = nearest_unit;
selectedBuilding = NULL;
}
if( (nearest_unit_dist > MAX_SELECTION_DIST)
&&
(nearest_building_dist > MAX_SELECTION_DIST) )
{
selectedUnit = NULL;
selectedBuilding = NULL;
}
/*char buf[8];
snprintf(buf, 8, "%lu", nearest_building_dist);
GfxPrintText(buf, X_SCREEN_RESOLUTION - 32, 16);
snprintf(buf, 8, "%lu", nearest_unit_dist);
GfxPrintText(buf, X_SCREEN_RESOLUTION - 32, 24);*/
} }
void Player::BuildingUnitActions(void) void Player::ActionsMenu(void)
{ {
const char* pActionStr = NULL; if (showActionsMenu == true)
{
for(uint8_t i = 0; i < PLAYER_MAX_BUILDINGS; i++) for (uint8_t i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{ {
if(buildings[i].selected == true) TYPE_UNIT* ptrUnit = &units[i];
{
BuildingSelectedOptions(selectedBuilding); if (ptrUnit->selected == true)
break; {
} uint8_t availableActions = UnitGetAvailableActions(ptrUnit);
}
if (!(availableActions & (1 << showActionsMenu_index) ) )
if(pActionStr == NULL) {
{ IncreaseShowActionsMenuIndex();
for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++) }
{
if(units[i].selected == true) UNIT_ACTION action = (UNIT_ACTION) (showActionsMenu_index);
{
pActionStr = UnitSelectedOptions(selectedUnit); const char* str = UnitGetActionString(action);
break;
} GfxPrintText(str, 40, Y_SCREEN_RESOLUTION - 4);
}
} break;
}
if(pActionStr != NULL)
{ }
GfxPrintTextFont(pActionStr, font3x3, 48, Y_SCREEN_RESOLUTION - 4); }
}
} }
void Player::Handler(void) void Player::Handler(void)
{ {
CameraSetLock(&Camera, showActionsMenu);
CameraHandler(&Camera); CameraHandler(&Camera);
UnitBuildingSelection(); UnitBuildingSelection();
for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++) for (uint8_t i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{ {
TYPE_UNIT * ptrUnit = &units[i]; TYPE_UNIT* ptrUnit = &units[i];
UnitHandler(ptrUnit); UnitHandler(ptrUnit);
} }
ActionsMenu();
ButtonHandler(); ButtonHandler();
BuildingUnitActions();
GfxShowResources(&Resources); GfxShowResources(&Resources);
} }
void Player::ButtonHandler(void) void Player::ButtonHandler(void)
{ {
static bool bCancelSelection = false; if (PadButtonPressed(PAD_A) == true)
{
if(PadButtonPressed(PAD_A) == true) ButtonAPressed();
{ }
if(progress_bar < ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES) else if (PadButtonReleased(PAD_A) == true)
{ {
progress_bar++; ButtonAReleased();
} }
} else if (PadButtonPressed(PAD_B) == true)
{
if(PadButtonReleased(PAD_A) == true) ButtonBPressed();
{ }
/*TYPE_COLLISION_BLOCK cl; else if (PadButtonReleased(PAD_B) == true)
{
cl.x = SystemRand(0, 32); ButtonBReleased();
cl.y = SystemRand(0, 32); }
cl.w = BuildingGetWidthFromID(BARRACKS); else if (PadButtonReleased(PAD_LEFT) == true)
cl.h = BuildingGetHeightFromID(BARRACKS); {
ButtonLeftReleased();
if(createBuilding(BARRACKS, cl) == false) }
{ else if (PadButtonReleased(PAD_RIGHT) == true)
GfxPrintText_Flash(F("Failed!")); {
} ButtonRightReleased();
else }
{
GfxPrintText_Flash(F("Building built!\0"));
}*/
for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++)
{
TYPE_UNIT * u = &units[i];
if(selectedUnit == u)
{
u->selected = true;
}
}
for(uint8_t i = 0; i < PLAYER_MAX_BUILDINGS; i++)
{
TYPE_BUILDING * b = &buildings[i];
if(selectedBuilding == b)
{
b->selected = true;
}
}
}
else if(PadButtonPressedFrames(PAD_A, ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES) == true)
{
for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++)
{
TYPE_UNIT* u = &units[i];
if(u->selected == true)
{
UnitAcceptAction(u);
break;
}
}
}
else if( (PadButtonPressedFrames(PAD_B, CANCEL_SELECTION_NO_FRAMES) == true)
&&
(bCancelSelection == false) )
{
for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++)
{
TYPE_UNIT * u = &units[i];
u->selected = false;
}
for(uint8_t i = 0; i < PLAYER_MAX_BUILDINGS; i++)
{
TYPE_BUILDING * b = &buildings[i];
b->selected = false;
}
selectedUnit = NULL;
selectedBuilding = NULL;
bCancelSelection = true;
showUnitBuildingOptions = true;
}
else
{
// Button A is not pressed. Reset progress bar.
progress_bar = 0;
}
if( (PadButtonReleased(PAD_B) == true) }
&&
(bCancelSelection == false) ) void Player::ButtonAPressed(void)
{ {
if(showUnitBuildingOptions == false) // Only increase progress bar when any unit has been previously selected
{ if (anyUnitSelected == true)
TYPE_COLLISION_BLOCK cursor = GetCursorPos(); {
if (showActionsMenu == false)
for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++) {
{ if (showActionsMenu_counter < ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES)
TYPE_UNIT * u = &units[i]; {
showActionsMenu_counter++;
if(u->selected == true) }
{ }
UnitMoveTo(u, cursor.x, cursor.y); }
} }
}
}
else void Player::ButtonAReleased(void)
{ {
showUnitBuildingOptions = false; if (showActionsMenu_counter < ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES)
} {
} if (selectedUnitCandidate != NO_SELECTION)
else if( (PadButtonReleased(PAD_B) == true) {
&& // When actions menu is not active, select unit if
(bCancelSelection == true) ) // a candidate is present
{ TYPE_UNIT* ptrUnit = &units[selectedUnitCandidate];
bCancelSelection = false;
UnitResetMenuLevel(); ptrUnit->selected = true;
} anyUnitSelected = true;
showActionsMenu_index = 0;
}
showActionsMenu_counter = 0;
}
else if (showActionsMenu == true)
{
uint8_t i = 0;
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
TYPE_UNIT* ptrUnit = &units[i];
if (ptrUnit->selected == true)
{
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);
break;
}
}
}
}
showActionsMenu = (showActionsMenu_counter < ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES)? false: true;
}
void Player::ButtonBPressed(void)
{
enum
{
CANCEL_SELECTION_FRAMES = 5
};
if (anyUnitSelected == true)
{
if (unselectUnits_counter < CANCEL_SELECTION_FRAMES)
{
unselectUnits_counter++;
}
else
{
if (anyUnitSelected == true)
{
uint8_t i;
// Cancel selection of all units
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
TYPE_UNIT* ptrUnit = &units[i];
if (ptrUnit->selected == true)
{
ptrUnit->selected = false;
}
}
// Reset accumulated counter and flags
unselectUnits_counter = 0;
anyUnitSelected = false;
showActionsMenu = false;
showActionsMenu_counter = 0;
}
}
}
}
void Player::ButtonBReleased(void)
{
if (anyUnitSelected == true)
{
TYPE_COLLISION_BLOCK cursor = GetCursorPos();
uint8_t i;
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
TYPE_UNIT* ptrUnit = &units[i];
if (ptrUnit->selected == true)
{
UnitMoveTo(ptrUnit, cursor.x, cursor.y);
}
}
}
// Reset accumulated counter
unselectUnits_counter = 0;
}
void Player::ButtonLeftReleased(void)
{
if (showActionsMenu == true)
{
uint8_t i;
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
TYPE_UNIT* ptrUnit = &units[i];
if (ptrUnit->selected == true)
{
// We need to iterate over all available actions
// 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))
{
// Maximum index: 7
j = (sizeof(uint8_t) << 3) - 1;
}
if (availableActions & (1 << j))
{
showActionsMenu_index = j;
break;
}
}
}
}
}
}
void Player::ButtonRightReleased(void)
{
IncreaseShowActionsMenuIndex();
}
void Player::IncreaseShowActionsMenuIndex(void)
{
if (showActionsMenu == true)
{
uint8_t i;
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
TYPE_UNIT* ptrUnit = &units[i];
if (ptrUnit->selected == true)
{
// We need to iterate over all available actions
// 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) )
{
j = 0;
}
if (availableActions & (1 << j))
{
showActionsMenu_index = j;
break;
}
}
}
}
}
} }

View File

@ -4,29 +4,35 @@
/* ************************************** /* **************************************
* Includes * * Includes *
* **************************************/ * **************************************/
#include "Global_Inc.h" #include "Global_Inc.h"
#include "GameStructures.h"
#include "Camera.h" #include "Camera.h"
#include "Building.h"
#include "Unit.h" #include "Unit.h"
/* ************************************** /* **************************************
* Defines * * Defines *
* **************************************/ * **************************************/
#define PLAYER_NAME_LENGTH 16 #define PLAYER_NAME_LENGTH 16
#define PLAYER_MAX_UNITS 32 #define PLAYER_MAX_UNITS_BUILDINGS 32
#define PLAYER_MAX_BUILDINGS 32
/* ************************************** /* **************************************
* Structs and enums * * Structs and enums *
* **************************************/ * **************************************/
typedef struct t_Resource
{
uint8_t Wood;
uint8_t Gold;
uint8_t Food;
}TYPE_RESOURCES;
/* ************************************** /* **************************************
* Class definition * * Class definition *
* **************************************/ * **************************************/
#ifdef __cplusplus
class Player class Player
{ {
public: public:
@ -35,36 +41,52 @@ class Player
void Init(void); void Init(void);
void Handler(void); void Handler(void);
void DrawHandler(void); void DrawHandler(void);
char * getName(void) {return name;} char* getName(void) {return name;}
void setHuman(bool value) { human = value; } void setHuman(bool value) { human = value; }
bool isHuman(void) {return human;} bool isHuman(void) {return human;}
bool createUnit(uint8_t id, TYPE_COLLISION_BLOCK cb); bool createUnit(TYPE_UNIT_ID id, TYPE_COLLISION_BLOCK cb);
bool createBuilding(TYPE_BUILDING_ID id, TYPE_COLLISION_BLOCK cb);
uint8_t getPopulation(void) {return (unit_i + 1);} uint8_t getPopulation(void) {return (unit_i + 1);}
uint8_t getBuildings(void) {return (bldg_i + 1);}
private: private:
// Player definition
bool human;
char name[PLAYER_NAME_LENGTH];
TYPE_RESOURCES Resources;
// Unit handling
TYPE_UNIT units[PLAYER_MAX_UNITS_BUILDINGS];
uint8_t unit_i;
// Camera handling
TYPE_CAMERA Camera;
// Button pressed/released events
void ButtonLeftReleased(void);
void ButtonRightReleased(void);
void ButtonAPressed(void);
void ButtonAReleased(void);
void ButtonBPressed(void);
void ButtonBReleased(void);
bool checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb); bool checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb);
void UnitBuildingSelection(void); void UnitBuildingSelection(void);
void showHealth(uint8_t hp); void showHealth(uint8_t hp);
void ButtonHandler(void); void ButtonHandler(void);
void BuildingUnitActions(void); void ActionsMenu(void);
TYPE_COLLISION_BLOCK GetCursorPos(void); TYPE_COLLISION_BLOCK GetCursorPos(void);
char name[PLAYER_NAME_LENGTH]; // Unit selection
TYPE_UNIT units[PLAYER_MAX_UNITS]; bool anyUnitSelected;
TYPE_BUILDING buildings[PLAYER_MAX_BUILDINGS]; int8_t selectedUnitCandidate;
TYPE_UNIT* selectedUnit; uint8_t unselectUnits_counter;
TYPE_BUILDING* selectedBuilding;
uint8_t id; // Actions selection
uint8_t unit_i; bool showActionsMenu;
uint8_t bldg_i; uint8_t showActionsMenu_counter;
bool human; uint8_t showActionsMenu_index;
bool showUnitBuildingOptions; void IncreaseShowActionsMenuIndex();
TYPE_CAMERA Camera;
TYPE_RESOURCES Resources;
uint8_t progress_bar;
//Print _serial;
}; };
#endif // __cplusplus
#endif //PLAYER_HEADER__ #endif //PLAYER_HEADER__

View File

@ -15,7 +15,7 @@
/* ************************************* /* *************************************
* Local Prototypes * Local Prototypes
* *************************************/ * *************************************/
//static void SystemCheckTimer(bool * timer, uint64_t * last_timer, uint8_t step); //static void SystemCheckTimer(bool * timer, uint64_t * last_timer, uint8_t step);
/* ************************************* /* *************************************
@ -51,7 +51,7 @@ void SystemInit(void)
SystemResetTimers(); SystemResetTimers();
//Initial value for system_busy //Initial value for system_busy
system_busy = false; system_busy = false;
#if defined(USBCON) #if defined(USBCON)
USBDevice.attach(); USBDevice.attach();
#endif #endif
@ -59,7 +59,7 @@ void SystemInit(void)
void SystemSetRandSeed(void) void SystemSetRandSeed(void)
{ {
if(rand_seed == false) if (rand_seed == false)
{ {
rand_seed = true; rand_seed = true;
//Set random seed using global timer as reference //Set random seed using global timer as reference
@ -96,7 +96,7 @@ void SystemRunTimers(void)
{ {
/* static uint64_t last_one_second_tick; /* static uint64_t last_one_second_tick;
static uint64_t last_100_ms_tick; static uint64_t last_100_ms_tick;
SystemCheckTimer(&one_second_timer, &last_one_second_tick, REFRESH_FREQUENCY); SystemCheckTimer(&one_second_timer, &last_one_second_tick, REFRESH_FREQUENCY);
SystemCheckTimer(&hundred_ms_timer, &last_100_ms_tick, 2); SystemCheckTimer(&hundred_ms_timer, &last_100_ms_tick, 2);
* */ * */
@ -104,23 +104,23 @@ void SystemRunTimers(void)
void SystemCheckTimer(bool * timer, uint64_t * last_timer, uint8_t step) void SystemCheckTimer(bool * timer, uint64_t * last_timer, uint8_t step)
{ {
if(*timer == true) if (*timer == true)
{ {
*timer = false; *timer = false;
*last_timer = global_timer; *last_timer = global_timer;
} }
if(global_timer >= (*last_timer + step) ) if (global_timer >= (*last_timer + step) )
{ {
*timer = true; *timer = true;
} }
} }
void SystemWaitCycles(uint32_t cycles) void SystemWaitCycles(uint32_t cycles)
{ {
uint64_t currentTime = global_timer; uint64_t currentTime = global_timer;
while(global_timer < (currentTime + cycles) ); while (global_timer < (currentTime + cycles) );
} }
uint32_t SystemRand(uint32_t min, uint32_t max) uint32_t SystemRand(uint32_t min, uint32_t max)
@ -133,33 +133,33 @@ bool SystemIsBusy(void)
return system_busy; return system_busy;
} }
bool SystemContains_u8(uint8_t value, uint8_t * buffer, size_t sz) bool SystemContains_u8(uint8_t value, uint8_t* buffer, size_t sz)
{ {
size_t i = 0; size_t i = 0;
for(i = 0; i < sz; i++) for (i = 0; i < sz; i++)
{ {
if(buffer[i] == value) if (buffer[i] == value)
{ {
return true; return true;
} }
} }
return false; return false;
} }
bool SystemContains_u16(uint16_t value, uint16_t * buffer, size_t sz) bool SystemContains_u16(uint16_t value, uint16_t * buffer, size_t sz)
{ {
size_t i = 0; size_t i = 0;
for(i = 0; i < sz; i++) for (i = 0; i < sz; i++)
{ {
if(buffer[i] == value) if (buffer[i] == value)
{ {
return true; return true;
} }
} }
return false; return false;
} }
@ -167,15 +167,15 @@ TYPE_TIMER * SystemCreateTimer(uint32_t seconds, bool rf, void (*timer_callback)
{ {
bool success = false; bool success = false;
uint8_t i; uint8_t i;
if(seconds == 0) if (seconds == 0)
{ {
return NULL; return NULL;
} }
for(i = 0; i < SYSTEM_MAX_TIMERS; i++) for (i = 0; i < SYSTEM_MAX_TIMERS; i++)
{ {
if(timer_array[i].busy == false) if (timer_array[i].busy == false)
{ {
timer_array[i].Timeout_Callback = timer_callback; timer_array[i].Timeout_Callback = timer_callback;
timer_array[i].time = seconds; timer_array[i].time = seconds;
@ -186,20 +186,20 @@ TYPE_TIMER * SystemCreateTimer(uint32_t seconds, bool rf, void (*timer_callback)
break; break;
} }
} }
if(success == false) if (success == false)
{ {
return NULL; return NULL;
} }
return &timer_array[i]; return &timer_array[i];
} }
void SystemResetTimers(void) void SystemResetTimers(void)
{ {
uint8_t i; uint8_t i;
for(i = 0; i < SYSTEM_MAX_TIMERS; i++) for (i = 0; i < SYSTEM_MAX_TIMERS; i++)
{ {
timer_array[i].Timeout_Callback = NULL; timer_array[i].Timeout_Callback = NULL;
timer_array[i].busy = false; timer_array[i].busy = false;
@ -212,20 +212,20 @@ void SystemResetTimers(void)
void SystemUserTimersHandler(void) void SystemUserTimersHandler(void)
{ {
uint8_t i; uint8_t i;
for(i = 0; i < SYSTEM_MAX_TIMERS; i++) for (i = 0; i < SYSTEM_MAX_TIMERS; i++)
{ {
if(timer_array[i].busy == true) if (timer_array[i].busy == true)
{ {
if(System1SecondTick() == true) if (System1SecondTick() == true)
{ {
timer_array[i].time--; timer_array[i].time--;
if(timer_array[i].time == 0) if (timer_array[i].time == 0)
{ {
timer_array[i].Timeout_Callback(); timer_array[i].Timeout_Callback();
if(timer_array[i].repeat_flag == true) if (timer_array[i].repeat_flag == true)
{ {
timer_array[i].time = timer_array[i].orig_time; timer_array[i].time = timer_array[i].orig_time;
} }
@ -259,15 +259,15 @@ void SystemTimerRemove(TYPE_TIMER * timer)
bool SystemArrayCompare(unsigned short * arr1, unsigned short * arr2, size_t sz) bool SystemArrayCompare(unsigned short * arr1, unsigned short * arr2, size_t sz)
{ {
size_t i; size_t i;
for(i = 0; i < sz; i++) for (i = 0; i < sz; i++)
{ {
if(arr1[i] != arr2[i]) if (arr1[i] != arr2[i])
{ {
return false; return false;
} }
} }
return true; return true;
} }

View File

@ -11,7 +11,6 @@ extern "C"
* **************************************/ * **************************************/
#include "Global_Inc.h" #include "Global_Inc.h"
#include "GameStructures.h"
/* ************************************** /* **************************************
* Defines * * Defines *
@ -33,6 +32,14 @@ typedef struct t_Timer
void (*Timeout_Callback)(void); void (*Timeout_Callback)(void);
}TYPE_TIMER; }TYPE_TIMER;
typedef struct t_CollisionBlock
{
uint16_t x;
uint16_t y;
uint8_t w;
uint8_t h;
}TYPE_COLLISION_BLOCK;
/* ************************************** /* **************************************
* Global Prototypes * * Global Prototypes *
* **************************************/ * **************************************/
@ -60,7 +67,7 @@ uint64_t SystemGetGlobalTimer(void);
// Returns whether critical section of code is being entered // Returns whether critical section of code is being entered
bool SystemIsBusy(void); bool SystemIsBusy(void);
// Returns whether indicated value is contained inside buffer // Returns whether indicated value is contained inside buffer
bool SystemContains_u8(uint8_t value, uint8_t * buffer, size_t sz); bool SystemContains_u8(uint8_t value, uint8_t* buffer, size_t sz);
// Overload for uint16_t // Overload for uint16_t
bool SystemContains_u16(uint16_t value, uint16_t * buffer, size_t sz); bool SystemContains_u16(uint16_t value, uint16_t * buffer, size_t sz);
// Creates a timer instance wiht a determined value and associates it to a callback // Creates a timer instance wiht a determined value and associates it to a callback
@ -80,9 +87,9 @@ bool SystemArrayCompare(unsigned short * arr1, unsigned short * arr2, size_t sz)
bool SystemCollisionCheck(TYPE_COLLISION_BLOCK c1, TYPE_COLLISION_BLOCK c2); bool SystemCollisionCheck(TYPE_COLLISION_BLOCK c1, TYPE_COLLISION_BLOCK c2);
/* ************************************** /* **************************************
* Global Variables * * Global Variables *
* **************************************/ * **************************************/
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

405
Unit.c
View File

@ -1,174 +1,221 @@
/* ************************************** /* **************************************
* Includes * * Includes *
* **************************************/ * **************************************/
#include "Unit.h" #include "Unit.h"
#include "PeasantSpr.c" #include "PeasantSpr.c"
#include "BarracksSpr.c"
/* ************************************** /* **************************************
* Defines * * Defines *
* **************************************/ * **************************************/
#define MAX_ACTION_LEVELS 2 #define MAX_ACTIONS 3
/* **************************************
* Structs and enums *
* **************************************/
struct t_coordinates
{
int8_t x;
int8_t y;
};
/* ************************************** /* **************************************
* Local prototypes * * Local prototypes *
* **************************************/ * **************************************/
static void UnitIncreaseMenuLevel(void); static void UnitBuildAccepted(TYPE_UNIT* ptrUnit);
static void UnitBuildAccept(TYPE_UNIT* ptrUnit); static void UnitAttackAccepted(TYPE_UNIT* ptrUnit);
/* ************************************** /* **************************************
* Local variables * * Local variables *
* **************************************/ * **************************************/
static uint8_t menuLevel; /* Tables */
static uint8_t const UnitHPTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = 25 ,
[BARRACKS] = 100 };
static uint8_t const UnitSpeedTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = 1 ,
[BARRACKS] = 0 };
typedef struct static TYPE_UNIT_ACTION const UnitActionsTable_Level0[MAX_ACTIONS] = { [ACTION_BUILD] = {"BUILD", &UnitBuildAccepted} ,
{ [ACTION_ATTACK] = {"ATTACK", &UnitAttackAccepted} ,
const char* str; [ACTION_CREATE_UNIT] = {"CREATE", NULL} };
void (*pAction)(TYPE_UNIT*);
}TYPE_UNIT_ACTION;
typedef enum t_availableactions static uint8_t const UnitActionsTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = ((1 << ACTION_BUILD) | (1 << ACTION_ATTACK)),
{ [BARRACKS] = (1 << ACTION_CREATE_UNIT) };
ACTION_BUILD = 0,
ACTION_ATTACK,
MAX_ACTIONS
}UNIT_AVAILABLE_ACTIONS;
/* Sprites */ // **************
static TYPE_SPRITE PeasantSpr; // Sprite tables
static TYPE_SPRITE PeasantWalkingSpr; // **************
static TYPE_SPRITE UnitSprTable[MAX_UNITS_BUILDINGS];
/* Tables */ static TYPE_SPRITE UnitWalkingShadowSprTable[MAX_UNITS_BUILDINGS];
static uint8_t const UnitHPTable[] = { [PEASANT] = 25 }; static const struct t_coordinates UnitShadowOffsetTable[MAX_BUILDING_ID - FIRST_BUILDING_ID] = { [BARRACKS - FIRST_BUILDING_ID] = {.x = -6, .y = 0} };
static uint8_t const UnitSpeedTable[] = { [PEASANT] = 1 };
static TYPE_UNIT_ACTION const UnitActionsTable_Level0[MAX_ACTIONS] = { [ACTION_BUILD] = {"Build", NULL},
[ACTION_ATTACK] = {"Attack", NULL} };
static TYPE_UNIT_ACTION const UnitActionsTable_Level1[MAX_ACTIONS] = { {"Barracks", &UnitBuildAccept} };
static TYPE_UNIT_ACTION* const UnitActionsTable_LevelX[MAX_ACTION_LEVELS] = { &UnitActionsTable_Level0 ,
&UnitActionsTable_Level1 };
static TYPE_SPRITE * const UnitSprTable[] = {&PeasantSpr};
static TYPE_SPRITE * const UnitWalkingSprTable[] = {&PeasantWalkingSpr};
void UnitInit(void) void UnitInit(void)
{ {
PeasantSpr.Data = Peasant_SprData; enum
PeasantSpr.w = GfxGetWidthFromSpriteData(Peasant_SprData); {
PeasantSpr.h = GfxGetHeightFromSpriteData(Peasant_SprData); BARRACKS_SHADOW_OFFSET_X = -8,
PeasantSpr.flip = 0; BARRACKS_SHADOW_OFFSET_Y = 0
PeasantSpr.rotation = 0; };
PeasantSpr.color = GFX_BLACK;
UnitSprTable[PEASANT].Data = Peasant_SprData;
PeasantWalkingSpr.Data = Peasant_Walking_SprData; UnitSprTable[PEASANT].w = GfxGetWidthFromSpriteData(Peasant_SprData);
PeasantWalkingSpr.w = GfxGetWidthFromSpriteData(Peasant_Walking_SprData); UnitSprTable[PEASANT].h = GfxGetHeightFromSpriteData(Peasant_SprData);
PeasantWalkingSpr.h = GfxGetHeightFromSpriteData(Peasant_Walking_SprData); UnitSprTable[PEASANT].flip = 0;
PeasantWalkingSpr.flip = 0; UnitSprTable[PEASANT].rotation = 0;
PeasantWalkingSpr.rotation = 0; UnitSprTable[PEASANT].color = GFX_BLACK;
PeasantWalkingSpr.color = GFX_BLACK;
UnitWalkingShadowSprTable[PEASANT].Data = Peasant_Walking_SprData;
UnitWalkingShadowSprTable[PEASANT].w = GfxGetWidthFromSpriteData(Peasant_Walking_SprData);
UnitWalkingShadowSprTable[PEASANT].h = GfxGetHeightFromSpriteData(Peasant_Walking_SprData);
UnitWalkingShadowSprTable[PEASANT].flip = 0;
UnitWalkingShadowSprTable[PEASANT].rotation = 0;
UnitWalkingShadowSprTable[PEASANT].color = GFX_BLACK;
UnitSprTable[BARRACKS].Data = BarracksSpr_Data;
UnitSprTable[BARRACKS].w = GfxGetWidthFromSpriteData(BarracksSpr_Data);
UnitSprTable[BARRACKS].h = GfxGetHeightFromSpriteData(BarracksSpr_Data);
UnitSprTable[BARRACKS].flip = 0;
UnitSprTable[BARRACKS].rotation = 0;
UnitSprTable[BARRACKS].color = GFX_BLACK;
UnitWalkingShadowSprTable[BARRACKS].Data = BarracksShadowSpr_Data;
UnitWalkingShadowSprTable[BARRACKS].w = GfxGetWidthFromSpriteData(BarracksShadowSpr_Data);
UnitWalkingShadowSprTable[BARRACKS].h = GfxGetHeightFromSpriteData(BarracksShadowSpr_Data);
UnitWalkingShadowSprTable[BARRACKS].flip = 0;
UnitWalkingShadowSprTable[BARRACKS].rotation = 0;
UnitWalkingShadowSprTable[BARRACKS].color = GFX_GRAY;
} }
void UnitDraw(TYPE_CAMERA * ptrCamera, TYPE_UNIT * ptrUnit, bool bSelected) void UnitDraw(TYPE_UNIT* ptrUnit, TYPE_CAMERA* ptrCamera, bool bHighlighted)
{ {
uint8_t id = ptrUnit->id; uint8_t id = ptrUnit->id;
TYPE_SPRITE * ptrSpr; TYPE_SPRITE * ptrSpr;
static uint8_t walk_counter = 0;
static bool mirror = false; if (ptrUnit->alive == false)
if(ptrUnit->alive == false)
{ {
return; return;
} }
ptrSpr = ptrUnit->walking ? UnitWalkingSprTable[id] : UnitSprTable[id]; if (ptrUnit->building == false)
{
ptrSpr->rotation = ptrUnit->dir ? ROTCCW : NOROT; enum
ptrSpr->flip = GFX_NOFLIP; {
WALK_FRAMES = 4
if(ptrUnit->walking == true) };
{
if(mirror == true) // ***************
{ // Units
if(ptrUnit->dir == false) // ***************
{
ptrSpr->flip = GFX_FLIPH; ptrSpr = ptrUnit->walking ? &UnitWalkingShadowSprTable[id] : &UnitSprTable[id];
}
else ptrSpr->rotation = GFX_NOROT;
{ ptrSpr->flip = GFX_NOFLIP;
ptrSpr->flip = GFX_FLIPV;
} if (ptrUnit->walking == true)
} {
else if (++ptrUnit->walk_counter > WALK_FRAMES)
{ {
ptrSpr->flip = NOROT; ptrUnit->walk_counter = 0;
} ptrUnit->mirror = ptrUnit->mirror ? false : true;
} }
}
if(ptrUnit->mirror == false)
{ switch (ptrUnit->dir)
if(ptrUnit->dir == false) {
{ case DIRECTION_UP:
ptrSpr->flip |= GFX_FLIPV; ptrSpr->flip |= GFX_FLIPV;
}
else if (ptrUnit->mirror == true)
{ {
ptrSpr->flip |= GFX_FLIPH; ptrSpr->flip |= GFX_FLIPH;
} }
} break;
case DIRECTION_DOWN:
if (ptrUnit->mirror == true)
{
ptrSpr->flip |= GFX_FLIPH;
}
break;
case DIRECTION_LEFT:
ptrSpr->rotation = GFX_ROTCCW;
ptrSpr->flip |= GFX_FLIPH;
if (ptrUnit->mirror == true)
{
ptrSpr->flip |= GFX_FLIPV;
}
break;
case DIRECTION_RIGHT:
ptrSpr->rotation = GFX_ROTCCW;
if (ptrUnit->mirror == true)
{
ptrSpr->flip |= GFX_FLIPV;
}
break;
default:
// Invalid direction
break;
}
}
else
{
// *******************
// Buildings
// *******************
CameraApplyCoordinatesToSprite( ptrCamera,
&UnitWalkingShadowSprTable[id],
ptrUnit->x + UnitShadowOffsetTable[id - FIRST_BUILDING_ID].x,
ptrUnit->y + UnitShadowOffsetTable[id - FIRST_BUILDING_ID].y );
GfxDrawSprite(&UnitWalkingShadowSprTable[id]);
ptrSpr = &UnitSprTable[id];
}
CameraApplyCoordinatesToSprite( ptrCamera, CameraApplyCoordinatesToSprite( ptrCamera,
ptrSpr, ptrSpr,
ptrUnit->x, ptrUnit->x,
ptrUnit->y ); ptrUnit->y );
GfxDrawSprite(ptrSpr); GfxDrawSprite(ptrSpr);
if( (bSelected == true) && (ptrUnit->selected == false) ) if ( (bHighlighted == true) || (ptrUnit->selected == true) )
{ {
TYPE_COLLISION_BLOCK cb; TYPE_COLLISION_BLOCK cb = CameraApplyCoordinatesToCoordinates(ptrCamera, ptrUnit->x, ptrUnit->y);
int8_t colour = ptrUnit->selected? GFX_BLACK : GFX_GRAY;
cb = CameraApplyCoordinatesToCoordinates(ptrCamera, ptrUnit->x, ptrUnit->y); uint8_t w = UnitGetWidthFromID(id);
uint8_t h = UnitGetHeightFromID(id);
GfxDrawCircle(cb.x + 3, cb.y + 3, UnitGetWidthFromID(ptrUnit->id), GFX_GRAY);
} if (ptrUnit->building == true)
else if(ptrUnit->selected == true) {
{ GfxDrawRectangle(cb.x - (w >> 3), cb.y - (h >> 3), w + (w >> 2), h + (h >> 2), colour);
TYPE_COLLISION_BLOCK cb; }
else
cb = CameraApplyCoordinatesToCoordinates(ptrCamera, ptrUnit->x, ptrUnit->y); {
GfxDrawCircle(cb.x + (w >> 1), cb.y + (h >> 1), w, colour);
GfxDrawCircle(cb.x + 3, cb.y + 3, UnitGetWidthFromID(ptrUnit->id), GFX_BLACK); }
} }
/*char str[2];
snprintf(str, 2, "%u", walk_counter);
GfxPrintText(str, 60, 20);
snprintf(str, 2, "%u", mirror);
GfxPrintText(str, 60, 30);*/
if(++walk_counter > 4)
{
walk_counter = 0;
mirror = mirror ? false : true;
}
} }
uint8_t UnitGetWidthFromID(uint8_t id) uint8_t UnitGetWidthFromID(uint8_t id)
{ {
return GfxGetWidthFromSpriteData(UnitSprTable[id]->Data); return GfxGetWidthFromSpriteData(UnitSprTable[id].Data);
} }
uint8_t UnitGetHeightFromID(uint8_t id) uint8_t UnitGetHeightFromID(uint8_t id)
{ {
return GfxGetHeightFromSpriteData(UnitSprTable[id]->Data); return GfxGetHeightFromSpriteData(UnitSprTable[id].Data);
} }
uint8_t UnitGetHpFromID(uint8_t id) uint8_t UnitGetHpFromID(uint8_t id)
@ -176,120 +223,70 @@ uint8_t UnitGetHpFromID(uint8_t id)
return UnitHPTable[id]; return UnitHPTable[id];
} }
void UnitMoveTo(TYPE_UNIT * ptrUnit, uint16_t x, uint16_t y) void UnitMoveTo(TYPE_UNIT* ptrUnit, uint16_t x, uint16_t y)
{ {
ptrUnit->target_x = x; ptrUnit->target_x = x;
ptrUnit->target_y = y; ptrUnit->target_y = y;
ptrUnit->walking = true; ptrUnit->walking = true;
} }
void UnitHandler(TYPE_UNIT * ptrUnit) void UnitAttackAccepted(TYPE_UNIT* ptrUnit)
{
ptrUnit->selecting_attack = true;
}
void UnitHandler(TYPE_UNIT* ptrUnit)
{ {
bool bMoving = false; bool bMoving = false;
if(ptrUnit->walking == true) if (ptrUnit->walking == true)
{ {
if( (ptrUnit->x - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_x) if ( (ptrUnit->x - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_x)
{ {
ptrUnit->dir = DIRECTION_LEFT;
ptrUnit->x -= UnitSpeedTable[ptrUnit->id]; ptrUnit->x -= UnitSpeedTable[ptrUnit->id];
//ptrUnit->rotation = GFX_ROTCCW;
bMoving = true; bMoving = true;
ptrUnit->dir = true;
ptrUnit->mirror = false;
} }
else if( (ptrUnit->x + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_x) else if ( (ptrUnit->x + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_x)
{ {
ptrUnit->dir = DIRECTION_RIGHT;
ptrUnit->x += UnitSpeedTable[ptrUnit->id]; ptrUnit->x += UnitSpeedTable[ptrUnit->id];
//ptrUnit->rotation = GFX_ROTCCW;
bMoving = true; bMoving = true;
ptrUnit->dir = true;
ptrUnit->mirror = true;
} }
if( (ptrUnit->y - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_y) if ( (ptrUnit->y - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_y)
{ {
ptrUnit->dir = DIRECTION_UP;
ptrUnit->y -= UnitSpeedTable[ptrUnit->id]; ptrUnit->y -= UnitSpeedTable[ptrUnit->id];
//ptrUnit->rotation = 0;
bMoving = true; bMoving = true;
ptrUnit->dir = false;
ptrUnit->mirror = false;
} }
else if( (ptrUnit->y + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_y) else if ( (ptrUnit->y + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_y)
{ {
ptrUnit->dir = DIRECTION_DOWN;
ptrUnit->y += UnitSpeedTable[ptrUnit->id]; ptrUnit->y += UnitSpeedTable[ptrUnit->id];
//ptrUnit->rotation = 0;
bMoving = true; bMoving = true;
ptrUnit->dir = false;
ptrUnit->mirror = true;
} }
ptrUnit->walking = bMoving; ptrUnit->walking = bMoving;
} }
} }
const char* UnitSelectedOptions(TYPE_UNIT* ptrUnit)
{
const TYPE_UNIT_ACTION* tUnitAction = NULL;
switch(menuLevel)
{
case 0:
tUnitAction = &UnitActionsTable_Level0[ptrUnit->id];
break;
case 1:
tUnitAction = &UnitActionsTable_Level1[ptrUnit->id];
break;
default:
return NULL;
break;
}
return tUnitAction->str;
}
void UnitAcceptAction(TYPE_UNIT* ptrUnit) void UnitAcceptAction(TYPE_UNIT* ptrUnit)
{ {
const TYPE_UNIT_ACTION* tUnitAction = NULL;
switch(menuLevel)
{
case 0:
tUnitAction = &UnitActionsTable_Level0[ptrUnit->id];
break;
case 1:
tUnitAction = &UnitActionsTable_Level1[ptrUnit->id];
break;
default:
return;
break;
}
if(tUnitAction->pAction == NULL)
{
UnitIncreaseMenuLevel();
}
else
{
tUnitAction->pAction(ptrUnit);
}
} }
void UnitIncreaseMenuLevel(void) uint8_t UnitGetAvailableActions(TYPE_UNIT* ptrUnit)
{ {
if(menuLevel < 1) return UnitActionsTable[ptrUnit->id];
{
menuLevel++;
}
} }
void UnitBuildAccept(TYPE_UNIT* ptrUnit) void UnitBuildAccepted(TYPE_UNIT* ptrUnit)
{ {
} }
void UnitResetMenuLevel(void) const char* UnitGetActionString(UNIT_ACTION action)
{ {
menuLevel = 0; return UnitActionsTable_Level0[action].str;
} }

104
Unit.h
View File

@ -4,47 +4,111 @@
/* ************************************** /* **************************************
* Includes * * Includes *
* **************************************/ * **************************************/
#include "Global_Inc.h" #include "Global_Inc.h"
#include "Gfx.h"
#include "GameStructures.h"
#include "Camera.h" #include "Camera.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
#endif //__cplusplus #endif //__cplusplus
/* ************************************** /* **************************************
* Defines * * Defines *
* **************************************/ * **************************************/
/* ************************************** /* **************************************
* Structs and enums * * Structs and enums *
* **************************************/ * **************************************/
typedef enum t_unitdirection
{
DIRECTION_LEFT = 0,
DIRECTION_RIGHT,
DIRECTION_UP,
DIRECTION_DOWN
}UNIT_DIRECTION;
typedef struct t_Unit
{
uint16_t x;
uint16_t y;
uint16_t target_x;
uint16_t target_y;
uint8_t hp;
uint8_t id;
UNIT_DIRECTION dir;
bool mirror; // True = down or left; true = up or right
bool building;
bool walking;
bool alive;
bool selected;
bool selecting_attack;
bool attacking;
uint8_t walk_counter;
}TYPE_UNIT;
typedef enum t_unitid typedef enum t_unitid
{ {
PEASANT = 0 // Walking units
PEASANT = 0,
MAX_UNIT_ID,
// Buildings
BARRACKS,
TOWER,
MAX_BUILDING_ID,
MAX_UNITS_BUILDINGS,
FIRST_UNIT_ID = PEASANT,
FIRST_BUILDING_ID = BARRACKS
}TYPE_UNIT_ID; }TYPE_UNIT_ID;
typedef struct
{
const char* str;
void (*pAction)(TYPE_UNIT*);
}TYPE_UNIT_ACTION;
typedef enum t_availableactions
{
ACTION_BUILD,
ACTION_ATTACK,
ACTION_CREATE_UNIT
}UNIT_ACTION;
typedef struct t_Camera TYPE_CAMERA;
/* ************************************** /* **************************************
* Global prototypes * * Global prototypes *
* **************************************/ * **************************************/
void UnitInit(void); // Initialization and handling
uint8_t UnitGetHpFromID(uint8_t id); void UnitInit(void);
uint8_t UnitGetWidthFromID(uint8_t id); void UnitHandler(TYPE_UNIT* ptrUnit);
uint8_t UnitGetHeightFromID(uint8_t id);
void UnitDraw(TYPE_CAMERA * ptrCamera, TYPE_UNIT * ptrUnit, bool bSelected); // Unit information
const char* UnitSelectedOptions(TYPE_UNIT* ptrUnit); uint8_t UnitGetHpFromID(uint8_t id);
void UnitMoveTo(TYPE_UNIT * ptrUnit, uint16_t x, uint16_t y); uint8_t UnitGetWidthFromID(uint8_t id);
void UnitHandler(TYPE_UNIT * ptrUnit); uint8_t UnitGetHeightFromID(uint8_t id);
void UnitAcceptAction(TYPE_UNIT* ptrUnit); uint8_t UnitGetAvailableActions(TYPE_UNIT* ptrUnit);
void UnitResetMenuLevel(void);
// Rendering
void UnitDraw(TYPE_UNIT* ptrUnit, TYPE_CAMERA* ptrCamera, bool bHighlighted);
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);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif //__cplusplus #endif //__cplusplus
#endif //__UNIT_HEADER__ #endif //__UNIT_HEADER__

View File

@ -4,6 +4,7 @@
#include "Global_Inc.h" #include "Global_Inc.h"
#include "Gameplay.h" #include "Gameplay.h"
#include "Menu.h"
/* ************************************** /* **************************************
* Global variables * * Global variables *
@ -12,22 +13,22 @@
Gamebuino gb; Gamebuino gb;
int main(){ int main(){
init(); init();
SystemInit(); SystemInit();
gb.begin(); gb.begin();
// Main loop // Main loop
while(1) while (1)
{ {
gb.titleScreen(F("Pocket Empires")); gb.titleScreen(F("Pocket Empires"));
MainMenu(); MainMenu();
} }
return 0; return 0;
} }