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

View File

@ -7,7 +7,6 @@
#include "Global_Inc.h"
#include "Gfx.h"
#include "Pad.h"
#ifdef __cplusplus
extern "C"
@ -18,24 +17,37 @@ extern "C"
/* *************************************
* 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;
bool locked;
}TYPE_CAMERA;
typedef struct t_sprite TYPE_SPRITE;
/* *************************************
* Global prototypes
* *************************************/
void CameraInit(TYPE_CAMERA * ptrCamera);
void CameraHandler(TYPE_CAMERA * ptrCamera);
TYPE_COLLISION_BLOCK CameraApplyCoordinatesToCoordinates( TYPE_CAMERA * ptrCamera,
uint16_t x,
uint16_t y );
void CameraApplyCoordinatesToSprite(TYPE_CAMERA * ptrCamera,
TYPE_SPRITE * spr,
uint16_t x,
uint16_t y );
void CameraInit(TYPE_CAMERA* ptrCamera);
void CameraHandler(TYPE_CAMERA* ptrCamera);
void CameraSetLock(TYPE_CAMERA* ptrCamera, bool value);
void CameraApplyCoordinatesToSprite( TYPE_CAMERA* ptrCamera,
TYPE_SPRITE * spr,
uint16_t x,
uint16_t y );
TYPE_COLLISION_BLOCK CameraApplyCoordinatesToCoordinates( TYPE_CAMERA* ptrCamera,
uint16_t x,
uint16_t y );
#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 *
* **************************************/
#include "Gameplay.h"
#include "MouseSpr.c"
#include "Pad.h"
/* **************************************
* Defines *
* **************************************/
/* **************************************
* Global variables *
* **************************************/
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_1[] PROGMEM = "Quit";
static TYPE_SPRITE MouseSpr;
static const char * const PauseMenuOptions[] PROGMEM = {PauseMenuOption_0,
static const char* const PauseMenuOptions[] PROGMEM = {PauseMenuOption_0,
PauseMenuOption_1 };
/* **************************************
@ -75,50 +76,50 @@ const byte TowerSpr[] PROGMEM = {16,32,
void GameInit(void)
{
uint8_t i;
for(i = 0; i < GAME_MAX_PLAYERS; i++)
for (i = 0; i < GAME_MAX_PLAYERS; i++)
{
GamePlayers[i].Init();
}
MouseSpr.Data = MouseSprData;
MouseSpr.color = GFX_INVERT;
MouseSpr.rotation = NOROT;
MouseSpr.flip = NOFLIP;
MouseSpr.x = (X_SCREEN_RESOLUTION >> 1) - 4;
MouseSpr.y = (Y_SCREEN_RESOLUTION >> 1) - 4;
GfxInit();
GameLoop();
}
bool GamePause(void)
{
if(PadButtonReleased(PAD_C) == true)
if (PadButtonReleased(PAD_C) == true)
{
//int8_t menu(const char* const* items, uint8_t length);
uint8_t choice = gb.menu(PauseMenuOptions, 2);
if(choice != 0)
if (choice != 0)
{
return true;
}
}
return false;
}
void GameCalculations(void)
{
uint8_t i;
for(i = 0; i < GAME_MAX_PLAYERS; i++)
for (i = 0; i < GAME_MAX_PLAYERS; i++)
{
GamePlayers[i].Handler();
}
if(PadAnyKeyPressed() == true)
if (PadAnyKeyPressed() == true)
{
SystemSetRandSeed();
}
@ -127,32 +128,32 @@ void GameCalculations(void)
void GameGraphics(void)
{
uint8_t i;
//GfxClearScreen();
for(i = 0; i < GAME_MAX_PLAYERS; i++)
for (i = 0; i < GAME_MAX_PLAYERS; i++)
{
GamePlayers[i].DrawHandler();
}
GfxDrawSprite(&MouseSpr);
}
void GameLoop(void)
{
while(1)
while (1)
{
if(GamePause() == true)
if (GamePause() == true)
{
return;
}
GameCalculations();
while(GfxRefreshNeeded() == false);
while (GfxRefreshNeeded() == false);
GameGraphics();
SystemIncreaseGlobalTimer();
}
}

View File

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

120
Gfx.cpp
View File

@ -1,21 +1,21 @@
/* *************************************
* Includes
* *************************************/
#include "Gfx.h"
/* *************************************
* Defines
* *************************************/
/* *************************************
* Structs and enums
* *************************************/
/* *************************************
* Local variables
* *************************************/
static bool GfxIsInsideScreenArea(int8_t x, int8_t y, uint8_t w, uint8_t h);
void GfxInit(void)
@ -23,20 +23,20 @@ void GfxInit(void)
gb.display.persistence = false; // Clears screen automatically
gb.display.setFont(font3x5);
}
void GfxDrawSprite(TYPE_SPRITE * ptrSprite)
{
if(GfxIsSpriteInsideScreenArea(ptrSprite) == true)
if (GfxIsSpriteInsideScreenArea(ptrSprite) == true)
{
int8_t orig_color = gb.display.getColor();
gb.display.setColor(ptrSprite->color, GFX_WHITE);
gb.display.drawBitmap( ptrSprite->x,
ptrSprite->y,
ptrSprite->Data,
ptrSprite->rotation,
ptrSprite->flip );
gb.display.setColor(orig_color);
}
}
@ -46,8 +46,8 @@ bool GfxRefreshNeeded(void)
return gb.update();
}
void GfxShowKeyboard(char * str, uint8_t length)
{
void GfxShowKeyboard(char* str, uint8_t 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)
{
/*char strBuffer[16];
snprintf(strBuffer, 16, "%d", (int)(x + w));
GfxPrintText(strBuffer,48,8);
snprintf(strBuffer, 16, "x = %d", (int)(x));
GfxPrintText(strBuffer,48,16);
snprintf(strBuffer, 16, "w = %d", (int)(w));
GfxPrintText(strBuffer,48,24);*/
if( ( (x + w) >= 0)
if ( ( (x + w) >= 0)
&&
(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 false;
}
@ -89,13 +89,13 @@ bool GfxIsSpriteInsideScreenArea(TYPE_SPRITE * spr)
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]);
}
uint8_t GfxGetHeightFromSpriteData(const uint8_t * sprData)
uint8_t GfxGetHeightFromSpriteData(const uint8_t* sprData)
{
// On Gamebuino bitmaps, height is always stored on second byte.
return pgm_read_byte_near(&sprData[1]);
@ -106,61 +106,61 @@ void GfxPrintText_Flash(const __FlashStringHelper * str)
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.cursorY = y;
gb.display.setFont(font);
gb.display.setColor(GFX_BLACK, GFX_WHITE);
gb.display.print(str);
if(orig_font != NULL)
if (orig_font != NULL)
{
gb.display.setFont(orig_font);
}
}
void GfxRenderTiles(TYPE_CAMERA * ptrCamera)
void GfxRenderTiles(TYPE_CAMERA* ptrCamera)
{
gb.display.setColor(GFX_GRAY);
if(ptrCamera == NULL)
if (ptrCamera == NULL)
{
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 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);
}
//}
}
}
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 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);
}
@ -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);
}
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();
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)
{
if(GfxIsInsideScreenArea(x, y, w, h) == true)
if (GfxIsInsideScreenArea(x, y, w, h) == true)
{
int8_t orig_color = gb.display.getColor();
gb.display.setColor(color);
gb.display.drawRect(x, y, w, h);
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)
{
if(GfxIsInsideScreenArea(x, y, w, h) == true)
if (GfxIsInsideScreenArea(x, y, w, h) == true)
{
int8_t orig_color = gb.display.getColor();
gb.display.setColor(color);
gb.display.fillRect(x, y, w, h);
gb.display.setColor(orig_color);
}
}
void GfxShowResources(TYPE_RESOURCES * ptrResources)
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);
}

53
Gfx.h
View File

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

View File

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

View File

@ -2,28 +2,31 @@ include ../../Makefile.cfg
PROJECT=arduino
LIBNAME=lib$(PROJECT).a
INCDIR=../../../include/$(PROJECT)
INCLUDE_FOLDER=../../../include/$(PROJECT)
LIBS_FOLDER=../../../lib
OBJECTS= wiring.o wiring_analog.o wiring_digital.o \
wiring_pulse.o wiring_shift.o HardwareSerial.o Print.o \
Tone.o WMath.o WString.o WInterrupts.o forward.o SPI.o
default: $(OBJECTS)
avr-ar rcs $(LIBNAME) $^
mkdir -p $(INCDIR)
cp *.h $(INCDIR)/
mkdir -p $(LIBS_FOLDER)
mv $(LIBNAME) $(LIBS_FOLDER)
avr-size $(LIBS_FOLDER)/$(LIBNAME)
default: $(LIBNAME)
cp *.h $(INCLUDE_FOLDER)/$(PROJECT)
$(LIBNAME): $(OBJECTS)
avr-ar rcs $@ $^
mkdir -p $(INCLUDE_FOLDER)/$(PROJECT)
mv $@ $(LIBS_FOLDER)
avr-size $(LIBS_FOLDER)/$@
%.o: %.cpp
$(CXX) $< -o $@ $(DEFINE) $(INCLUDE) $(CC_FLAGS)
$(CXX) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
%.o: %.c
$(CC) $< -o $@ $(DEFINE) $(INCLUDE) $(CC_FLAGS)
$(CC) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
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 \
font3x5.o font5x7.o Gamebuino.o settings.o Sound.o
default: $(OBJECTS)
avr-ar rcs $(LIBNAME).a $^
mkdir -p $(INCLUDE_FOLDER)/$(PROJECT)
default: $(LIBNAME)
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
$(CXX) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
@ -25,7 +27,8 @@ default: $(OBJECTS)
%.o: %.c
$(CC) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
clean:
rm -f *.o
rm -f $(LIBS_FOLDER)/$(LIBNAME).a
rm -f $(OBJECTS)
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
default: $(OBJECTS)
avr-ar rcs $(LIBNAME) $^
default: $(LIBNAME)
cp *.h $(INCLUDE_FOLDER)/$(PROJECT)
$(LIBNAME): $(OBJECTS)
avr-ar rcs $@ $^
mkdir -p $(INCLUDE_FOLDER)/$(PROJECT)
cp *.h $(INCLUDE_FOLDER)//$(PROJECT)
mv $(LIBNAME) $(LIBS_FOLDER)
avr-size $(LIBS_FOLDER)/$(LIBNAME)
mv $@ $(LIBS_FOLDER)
avr-size $(LIBS_FOLDER)/$@
%.o: %.cpp
$(CXX) $< -o $@ $(DEFINE) $(INCLUDE) $(CC_FLAGS)
@ -27,3 +29,5 @@ default: $(OBJECTS)
clean:
rm -f $(OBJECTS)
rm -f $(LIBS_FOLDER)/$(LIBNAME)
.PHONY: default clean

View File

@ -10,12 +10,14 @@ LIBNAME=lib$(PROJECT).a
OBJECTS=mmc.o tinyFAT.o
default: $(OBJECTS)
avr-ar rcs $(LIBNAME) $^
mkdir -p $(INCLUDE_FOLDER)/$(PROJECT)
default: $(LIBNAME)
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
$(CXX) $< -o $@ $(INCLUDE) $(DEFINE) $(CC_FLAGS)
@ -26,3 +28,5 @@ default: $(OBJECTS)
clean:
rm -f $(OBJECTS)
rm -f $(LIBS_FOLDER)/$(LIBNAME)
.PHONY: default clean

View File

@ -13,11 +13,13 @@ OBJ_DIR = Obj
SRC_DIR = .
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
avr-size $(EXE_DIR)/$(PROJECT).ELF
rebuild:
make clean
make build
@ -28,25 +30,38 @@ libs:
make -C Libs/petit_fatfs
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:
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/libgamebuino clean
make -C $$PWD/Libs/libgamebuino clean
make -C $$PWD/Libs/petit_fatfs 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)
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
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++
LINKER=$(CXX)
MCU=atmega328p
MCU=atmega328p
CPU_SPEED=16000000UL
ARDUINO_VERSION=150
AVR_TOOLCHAIN_PATH=/usr/local/avr/
GBSIM_PATH = ~/gbsim/build
PATH := $(PATH):$(AVR_TOOLCHAIN_PATH)/bin/
GBSIM = $(GBSIM_PATH)/gbsim
DEFINE = -DARDUINO=$(ARDUINO_VERSION) -mmcu=$(MCU) -DF_CPU=$(CPU_SPEED)

View File

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

8
Menu.h
View File

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

View File

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

View File

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

10
Pad.h
View File

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

View File

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

View File

@ -1,17 +1,19 @@
/* **************************************
* Includes *
* **************************************/
#include "Player.h"
#include "Pad.h"
#include <limits.h>
/* **************************************
* Defines *
* **************************************/
#define CANCEL_SELECTION_NO_FRAMES 5
#define ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES 5
#define MAX_SELECTION_DIST 400
#define NO_SELECTION -1
/* **************************************
* Local variables *
* **************************************/
@ -22,55 +24,50 @@ Player::Player(void)
Player::~Player(void)
{
}
void Player::Init(void)
{
uint8_t i;
unit_i = 0;
bldg_i = 0;
selectedUnit = NULL;
selectedBuilding = NULL;
selectedUnitCandidate = NO_SELECTION;
CameraInit(&Camera);
BuildingInit();
UnitInit();
for(i = 0; i < PLAYER_MAX_BUILDINGS; i++)
{
memset(&buildings[i], 0, sizeof(TYPE_BUILDING));
}
for(i = 0; i < PLAYER_MAX_UNITS; i++)
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
memset(&units[i], 0, sizeof(TYPE_UNIT));
}
TYPE_COLLISION_BLOCK cl;
TYPE_COLLISION_BLOCK cb;
Resources.Wood = 25;
Resources.Gold = 50;
Resources.Food = 75;
cl.x = SystemRand(0, 20);
cl.y = SystemRand(0, 20);
cl.w = BuildingGetWidthFromID(BARRACKS);
cl.h = BuildingGetHeightFromID(BARRACKS);
if(createBuilding(BARRACKS, cl) == false)
cb.x = SystemRand(0, 20);
cb.y = SystemRand(0, 20);
if (createUnit(BARRACKS, cb) == false)
{
GfxPrintText_Flash(F("Failed to create building!"));
}
cl.x = SystemRand(48, 56);
cl.y = SystemRand(48, 56);
cl.w = UnitGetWidthFromID(PEASANT);
cl.h = UnitGetHeightFromID(PEASANT);
if(createUnit(PEASANT, cl) == false)
cb.x = SystemRand(48, 56);
cb.y = SystemRand(48, 56);
cb.w = UnitGetWidthFromID(PEASANT);
cb.h = UnitGetHeightFromID(PEASANT);
showActionsMenu_counter = 0;
showActionsMenu_index = 0;
showActionsMenu = false;
anyUnitSelected = false;
if (createUnit(PEASANT, cb) == false)
{
GfxPrintText_Flash(F("Failed to create unit!"));
}
@ -78,68 +75,71 @@ void Player::Init(void)
void Player::showHealth(uint8_t hp)
{
enum
{
HP_TEXT_X = 4,
HP_TEXT_Y = Y_SCREEN_RESOLUTION - 4,
};
char str[8];
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)
{
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;
bool bAnyoneSelected = false;
//GfxRenderTiles(&Camera);
for(i = 0; i < PLAYER_MAX_BUILDINGS; i++)
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
TYPE_BUILDING * b = &buildings[i];
if(b->built == false)
TYPE_UNIT* u = &units[i];
if (u->alive == false)
{
continue;
}
bool selected = (b == selectedBuilding);
BuildingDraw(&Camera, b, selected);
if( (b->selected == true) && (bAnyoneSelected == false) )
bool selected = false;
if (selectedUnitCandidate != NO_SELECTION)
{
selected = (i == selectedUnitCandidate);
}
UnitDraw(u, &Camera, selected);
if ( (u->selected == true) && (bAnyoneSelected == false) )
{
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);
UnitSelectedOptions(u);
}
}
if(progress_bar != 0)
{
GfxDrawRectangle(X_SCREEN_RESOLUTION - 16, Y_SCREEN_RESOLUTION - 4, progress_bar, 2, GFX_BLACK);
}
if (bAnyoneSelected == true)
{
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)
@ -148,22 +148,23 @@ bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb)
TYPE_COLLISION_BLOCK bldgCB;
bool success;
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;
if(buildings[i].built == false)
if (ptrUnit->building == false)
{
continue;
}
bldgCB.x = buildings[i].x;
bldgCB.y = buildings[i].y;
bldgCB.w = BuildingGetWidthFromID(buildings[i].id);
bldgCB.h = BuildingGetHeightFromID(buildings[i].id);
if(SystemCollisionCheck(*cb, bldgCB) == true)
bldgCB.x = ptrUnit->x;
bldgCB.y = ptrUnit->y;
bldgCB.w = UnitGetWidthFromID(ptrUnit->id);
bldgCB.h = UnitGetHeightFromID(ptrUnit->id);
if (SystemCollisionCheck(*cb, bldgCB) == true)
{
success = false;
}
@ -171,15 +172,15 @@ bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb)
{
success = true;
}
if(success == false)
if (success == false)
{
cb->x = SystemRand(0, 128);
cb->y = SystemRand(0, 128);
if(++max_tries < 16)
{
if(checkNewBuildingPosition(cb) == false)
if (++max_tries < 16)
{
if (checkNewBuildingPosition(cb) == false)
{
return false;
}
@ -194,332 +195,366 @@ bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb)
}
}
}
max_tries = 0;
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)
{
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;
}
ptrNewUnit->building = (id > MAX_UNIT_ID);
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++;
return true;
}
else
{
return false;
}
return false;
}
TYPE_COLLISION_BLOCK Player::GetCursorPos(void)
{
TYPE_COLLISION_BLOCK cb;
cb.x = (X_SCREEN_RESOLUTION >> 1) - 4 - Camera.X_Offset;
cb.y = (Y_SCREEN_RESOLUTION >> 1) - 4 - Camera.Y_Offset;
cb.w = 8;
cb.h = 8;
return cb;
}
void Player::UnitBuildingSelection(void)
{
uint16_t i;
TYPE_UNIT * nearest_unit = NULL;
uint32_t nearest_unit_dist = 0xFFFFFFFF; // Set maximum value
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];
if( (u->alive == false) || (u->selected == true) )
TYPE_UNIT* u = &units[i];
if ( (u->alive == false) || (u->selected == true) )
{
continue;
}
TYPE_COLLISION_BLOCK cursor_cb = GetCursorPos();
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_y = (u_cb.y - cursor_cb.y);
uint16_t dist_x = (u_cb.x + (u_cb.w >> 1) - cursor_cb.x);
uint16_t dist_y = (u_cb.y + (u_cb.h >> 1) - cursor_cb.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 = u;
nearest_unit = (int8_t)i;
}
}
TYPE_BUILDING * nearest_building = NULL;
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);*/
selectedUnitCandidate = nearest_unit;
}
void Player::BuildingUnitActions(void)
void Player::ActionsMenu(void)
{
const char* pActionStr = NULL;
for(uint8_t i = 0; i < PLAYER_MAX_BUILDINGS; i++)
{
if(buildings[i].selected == true)
{
BuildingSelectedOptions(selectedBuilding);
break;
}
}
if(pActionStr == NULL)
{
for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++)
{
if(units[i].selected == true)
{
pActionStr = UnitSelectedOptions(selectedUnit);
break;
}
}
}
if(pActionStr != NULL)
{
GfxPrintTextFont(pActionStr, font3x3, 48, Y_SCREEN_RESOLUTION - 4);
}
if (showActionsMenu == true)
{
for (uint8_t i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
TYPE_UNIT* ptrUnit = &units[i];
if (ptrUnit->selected == true)
{
uint8_t availableActions = UnitGetAvailableActions(ptrUnit);
if (!(availableActions & (1 << showActionsMenu_index) ) )
{
IncreaseShowActionsMenuIndex();
}
UNIT_ACTION action = (UNIT_ACTION) (showActionsMenu_index);
const char* str = UnitGetActionString(action);
GfxPrintText(str, 40, Y_SCREEN_RESOLUTION - 4);
break;
}
}
}
}
void Player::Handler(void)
{
{
CameraSetLock(&Camera, showActionsMenu);
CameraHandler(&Camera);
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);
}
ActionsMenu();
ButtonHandler();
BuildingUnitActions();
GfxShowResources(&Resources);
}
void Player::ButtonHandler(void)
{
static bool bCancelSelection = false;
if(PadButtonPressed(PAD_A) == true)
{
if(progress_bar < ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES)
{
progress_bar++;
}
}
if(PadButtonReleased(PAD_A) == true)
{
/*TYPE_COLLISION_BLOCK cl;
cl.x = SystemRand(0, 32);
cl.y = SystemRand(0, 32);
cl.w = BuildingGetWidthFromID(BARRACKS);
cl.h = BuildingGetHeightFromID(BARRACKS);
if(createBuilding(BARRACKS, cl) == false)
{
GfxPrintText_Flash(F("Failed!"));
}
else
{
GfxPrintText_Flash(F("Building built!\0"));
}*/
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 (PadButtonPressed(PAD_A) == true)
{
ButtonAPressed();
}
else if (PadButtonReleased(PAD_A) == true)
{
ButtonAReleased();
}
else if (PadButtonPressed(PAD_B) == true)
{
ButtonBPressed();
}
else if (PadButtonReleased(PAD_B) == true)
{
ButtonBReleased();
}
else if (PadButtonReleased(PAD_LEFT) == true)
{
ButtonLeftReleased();
}
else if (PadButtonReleased(PAD_RIGHT) == true)
{
ButtonRightReleased();
}
if( (PadButtonReleased(PAD_B) == true)
&&
(bCancelSelection == false) )
{
if(showUnitBuildingOptions == false)
{
TYPE_COLLISION_BLOCK cursor = GetCursorPos();
for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++)
{
TYPE_UNIT * u = &units[i];
if(u->selected == true)
{
UnitMoveTo(u, cursor.x, cursor.y);
}
}
}
else
{
showUnitBuildingOptions = false;
}
}
else if( (PadButtonReleased(PAD_B) == true)
&&
(bCancelSelection == true) )
{
bCancelSelection = false;
UnitResetMenuLevel();
}
}
void Player::ButtonAPressed(void)
{
// Only increase progress bar when any unit has been previously selected
if (anyUnitSelected == true)
{
if (showActionsMenu == false)
{
if (showActionsMenu_counter < ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES)
{
showActionsMenu_counter++;
}
}
}
}
void Player::ButtonAReleased(void)
{
if (showActionsMenu_counter < ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES)
{
if (selectedUnitCandidate != NO_SELECTION)
{
// When actions menu is not active, select unit if
// a candidate is present
TYPE_UNIT* ptrUnit = &units[selectedUnitCandidate];
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 *
* **************************************/
#include "Global_Inc.h"
#include "GameStructures.h"
#include "Camera.h"
#include "Building.h"
#include "Unit.h"
/* **************************************
* Defines *
* **************************************/
#define PLAYER_NAME_LENGTH 16
#define PLAYER_MAX_UNITS 32
#define PLAYER_MAX_BUILDINGS 32
#define PLAYER_MAX_UNITS_BUILDINGS 32
/* **************************************
* Structs and enums *
* **************************************/
typedef struct t_Resource
{
uint8_t Wood;
uint8_t Gold;
uint8_t Food;
}TYPE_RESOURCES;
/* **************************************
* Class definition *
* **************************************/
#ifdef __cplusplus
class Player
{
public:
@ -35,36 +41,52 @@ class Player
void Init(void);
void Handler(void);
void DrawHandler(void);
char * getName(void) {return name;}
char* getName(void) {return name;}
void setHuman(bool value) { human = value; }
bool isHuman(void) {return human;}
bool createUnit(uint8_t id, TYPE_COLLISION_BLOCK cb);
bool createBuilding(TYPE_BUILDING_ID id, TYPE_COLLISION_BLOCK cb);
bool createUnit(TYPE_UNIT_ID id, TYPE_COLLISION_BLOCK cb);
uint8_t getPopulation(void) {return (unit_i + 1);}
uint8_t getBuildings(void) {return (bldg_i + 1);}
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);
void UnitBuildingSelection(void);
void showHealth(uint8_t hp);
void ButtonHandler(void);
void BuildingUnitActions(void);
void ActionsMenu(void);
TYPE_COLLISION_BLOCK GetCursorPos(void);
char name[PLAYER_NAME_LENGTH];
TYPE_UNIT units[PLAYER_MAX_UNITS];
TYPE_BUILDING buildings[PLAYER_MAX_BUILDINGS];
TYPE_UNIT* selectedUnit;
TYPE_BUILDING* selectedBuilding;
uint8_t id;
uint8_t unit_i;
uint8_t bldg_i;
bool human;
bool showUnitBuildingOptions;
TYPE_CAMERA Camera;
TYPE_RESOURCES Resources;
uint8_t progress_bar;
//Print _serial;
// Unit selection
bool anyUnitSelected;
int8_t selectedUnitCandidate;
uint8_t unselectUnits_counter;
// Actions selection
bool showActionsMenu;
uint8_t showActionsMenu_counter;
uint8_t showActionsMenu_index;
void IncreaseShowActionsMenuIndex();
};
#endif // __cplusplus
#endif //PLAYER_HEADER__

View File

@ -15,7 +15,7 @@
/* *************************************
* Local Prototypes
* *************************************/
//static void SystemCheckTimer(bool * timer, uint64_t * last_timer, uint8_t step);
/* *************************************
@ -51,7 +51,7 @@ void SystemInit(void)
SystemResetTimers();
//Initial value for system_busy
system_busy = false;
#if defined(USBCON)
USBDevice.attach();
#endif
@ -59,7 +59,7 @@ void SystemInit(void)
void SystemSetRandSeed(void)
{
if(rand_seed == false)
if (rand_seed == false)
{
rand_seed = true;
//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_100_ms_tick;
SystemCheckTimer(&one_second_timer, &last_one_second_tick, REFRESH_FREQUENCY);
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)
{
if(*timer == true)
if (*timer == true)
{
*timer = false;
*last_timer = global_timer;
}
if(global_timer >= (*last_timer + step) )
if (global_timer >= (*last_timer + step) )
{
*timer = true;
}
}
}
void SystemWaitCycles(uint32_t cycles)
{
uint64_t currentTime = global_timer;
while(global_timer < (currentTime + cycles) );
while (global_timer < (currentTime + cycles) );
}
uint32_t SystemRand(uint32_t min, uint32_t max)
@ -133,33 +133,33 @@ bool SystemIsBusy(void)
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;
for(i = 0; i < sz; i++)
for (i = 0; i < sz; i++)
{
if(buffer[i] == value)
if (buffer[i] == value)
{
return true;
}
}
return false;
}
bool SystemContains_u16(uint16_t value, uint16_t * buffer, size_t sz)
{
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 false;
}
@ -167,15 +167,15 @@ TYPE_TIMER * SystemCreateTimer(uint32_t seconds, bool rf, void (*timer_callback)
{
bool success = false;
uint8_t i;
if(seconds == 0)
if (seconds == 0)
{
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].time = seconds;
@ -186,20 +186,20 @@ TYPE_TIMER * SystemCreateTimer(uint32_t seconds, bool rf, void (*timer_callback)
break;
}
}
if(success == false)
if (success == false)
{
return NULL;
}
return &timer_array[i];
}
void SystemResetTimers(void)
{
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].busy = false;
@ -212,20 +212,20 @@ void SystemResetTimers(void)
void SystemUserTimersHandler(void)
{
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--;
if(timer_array[i].time == 0)
if (timer_array[i].time == 0)
{
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;
}
@ -259,15 +259,15 @@ void SystemTimerRemove(TYPE_TIMER * timer)
bool SystemArrayCompare(unsigned short * arr1, unsigned short * arr2, size_t sz)
{
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 true;
}

View File

@ -11,7 +11,6 @@ extern "C"
* **************************************/
#include "Global_Inc.h"
#include "GameStructures.h"
/* **************************************
* Defines *
@ -33,6 +32,14 @@ typedef struct t_Timer
void (*Timeout_Callback)(void);
}TYPE_TIMER;
typedef struct t_CollisionBlock
{
uint16_t x;
uint16_t y;
uint8_t w;
uint8_t h;
}TYPE_COLLISION_BLOCK;
/* **************************************
* Global Prototypes *
* **************************************/
@ -60,7 +67,7 @@ uint64_t SystemGetGlobalTimer(void);
// Returns whether critical section of code is being entered
bool SystemIsBusy(void);
// 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
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
@ -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);
/* **************************************
* Global Variables *
* Global Variables *
* **************************************/
#ifdef __cplusplus
}
#endif

405
Unit.c
View File

@ -1,174 +1,221 @@
/* **************************************
* Includes *
* **************************************/
#include "Unit.h"
#include "PeasantSpr.c"
#include "BarracksSpr.c"
/* **************************************
* Defines *
* **************************************/
#define MAX_ACTION_LEVELS 2
#define MAX_ACTIONS 3
/* **************************************
* Structs and enums *
* **************************************/
struct t_coordinates
{
int8_t x;
int8_t y;
};
/* **************************************
* Local prototypes *
* **************************************/
static void UnitIncreaseMenuLevel(void);
static void UnitBuildAccept(TYPE_UNIT* ptrUnit);
static void UnitBuildAccepted(TYPE_UNIT* ptrUnit);
static void UnitAttackAccepted(TYPE_UNIT* ptrUnit);
/* **************************************
* 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
{
const char* str;
void (*pAction)(TYPE_UNIT*);
}TYPE_UNIT_ACTION;
static TYPE_UNIT_ACTION const UnitActionsTable_Level0[MAX_ACTIONS] = { [ACTION_BUILD] = {"BUILD", &UnitBuildAccepted} ,
[ACTION_ATTACK] = {"ATTACK", &UnitAttackAccepted} ,
[ACTION_CREATE_UNIT] = {"CREATE", NULL} };
typedef enum t_availableactions
{
ACTION_BUILD = 0,
ACTION_ATTACK,
MAX_ACTIONS
}UNIT_AVAILABLE_ACTIONS;
static uint8_t const UnitActionsTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = ((1 << ACTION_BUILD) | (1 << ACTION_ATTACK)),
[BARRACKS] = (1 << ACTION_CREATE_UNIT) };
/* Sprites */
static TYPE_SPRITE PeasantSpr;
static TYPE_SPRITE PeasantWalkingSpr;
/* Tables */
static uint8_t const UnitHPTable[] = { [PEASANT] = 25 };
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};
// **************
// Sprite tables
// **************
static TYPE_SPRITE UnitSprTable[MAX_UNITS_BUILDINGS];
static TYPE_SPRITE UnitWalkingShadowSprTable[MAX_UNITS_BUILDINGS];
static const struct t_coordinates UnitShadowOffsetTable[MAX_BUILDING_ID - FIRST_BUILDING_ID] = { [BARRACKS - FIRST_BUILDING_ID] = {.x = -6, .y = 0} };
void UnitInit(void)
{
PeasantSpr.Data = Peasant_SprData;
PeasantSpr.w = GfxGetWidthFromSpriteData(Peasant_SprData);
PeasantSpr.h = GfxGetHeightFromSpriteData(Peasant_SprData);
PeasantSpr.flip = 0;
PeasantSpr.rotation = 0;
PeasantSpr.color = GFX_BLACK;
PeasantWalkingSpr.Data = Peasant_Walking_SprData;
PeasantWalkingSpr.w = GfxGetWidthFromSpriteData(Peasant_Walking_SprData);
PeasantWalkingSpr.h = GfxGetHeightFromSpriteData(Peasant_Walking_SprData);
PeasantWalkingSpr.flip = 0;
PeasantWalkingSpr.rotation = 0;
PeasantWalkingSpr.color = GFX_BLACK;
enum
{
BARRACKS_SHADOW_OFFSET_X = -8,
BARRACKS_SHADOW_OFFSET_Y = 0
};
UnitSprTable[PEASANT].Data = Peasant_SprData;
UnitSprTable[PEASANT].w = GfxGetWidthFromSpriteData(Peasant_SprData);
UnitSprTable[PEASANT].h = GfxGetHeightFromSpriteData(Peasant_SprData);
UnitSprTable[PEASANT].flip = 0;
UnitSprTable[PEASANT].rotation = 0;
UnitSprTable[PEASANT].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;
TYPE_SPRITE * ptrSpr;
static uint8_t walk_counter = 0;
static bool mirror = false;
if(ptrUnit->alive == false)
if (ptrUnit->alive == false)
{
return;
}
ptrSpr = ptrUnit->walking ? UnitWalkingSprTable[id] : UnitSprTable[id];
ptrSpr->rotation = ptrUnit->dir ? ROTCCW : NOROT;
ptrSpr->flip = GFX_NOFLIP;
if(ptrUnit->walking == true)
{
if(mirror == true)
{
if(ptrUnit->dir == false)
{
ptrSpr->flip = GFX_FLIPH;
}
else
{
ptrSpr->flip = GFX_FLIPV;
}
}
else
{
ptrSpr->flip = NOROT;
}
}
if(ptrUnit->mirror == false)
{
if(ptrUnit->dir == false)
{
ptrSpr->flip |= GFX_FLIPV;
}
else
{
ptrSpr->flip |= GFX_FLIPH;
}
}
if (ptrUnit->building == false)
{
enum
{
WALK_FRAMES = 4
};
// ***************
// Units
// ***************
ptrSpr = ptrUnit->walking ? &UnitWalkingShadowSprTable[id] : &UnitSprTable[id];
ptrSpr->rotation = GFX_NOROT;
ptrSpr->flip = GFX_NOFLIP;
if (ptrUnit->walking == true)
{
if (++ptrUnit->walk_counter > WALK_FRAMES)
{
ptrUnit->walk_counter = 0;
ptrUnit->mirror = ptrUnit->mirror ? false : true;
}
}
switch (ptrUnit->dir)
{
case DIRECTION_UP:
ptrSpr->flip |= GFX_FLIPV;
if (ptrUnit->mirror == true)
{
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,
ptrSpr,
ptrUnit->x,
ptrUnit->y );
GfxDrawSprite(ptrSpr);
if( (bSelected == true) && (ptrUnit->selected == false) )
{
TYPE_COLLISION_BLOCK cb;
cb = CameraApplyCoordinatesToCoordinates(ptrCamera, ptrUnit->x, ptrUnit->y);
GfxDrawCircle(cb.x + 3, cb.y + 3, UnitGetWidthFromID(ptrUnit->id), GFX_GRAY);
}
else if(ptrUnit->selected == true)
{
TYPE_COLLISION_BLOCK cb;
cb = CameraApplyCoordinatesToCoordinates(ptrCamera, ptrUnit->x, ptrUnit->y);
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;
}
if ( (bHighlighted == true) || (ptrUnit->selected == true) )
{
TYPE_COLLISION_BLOCK cb = CameraApplyCoordinatesToCoordinates(ptrCamera, ptrUnit->x, ptrUnit->y);
int8_t colour = ptrUnit->selected? GFX_BLACK : GFX_GRAY;
uint8_t w = UnitGetWidthFromID(id);
uint8_t h = UnitGetHeightFromID(id);
if (ptrUnit->building == true)
{
GfxDrawRectangle(cb.x - (w >> 3), cb.y - (h >> 3), w + (w >> 2), h + (h >> 2), colour);
}
else
{
GfxDrawCircle(cb.x + (w >> 1), cb.y + (h >> 1), w, colour);
}
}
}
uint8_t UnitGetWidthFromID(uint8_t id)
{
return GfxGetWidthFromSpriteData(UnitSprTable[id]->Data);
return GfxGetWidthFromSpriteData(UnitSprTable[id].Data);
}
uint8_t UnitGetHeightFromID(uint8_t id)
{
return GfxGetHeightFromSpriteData(UnitSprTable[id]->Data);
return GfxGetHeightFromSpriteData(UnitSprTable[id].Data);
}
uint8_t UnitGetHpFromID(uint8_t id)
@ -176,120 +223,70 @@ uint8_t UnitGetHpFromID(uint8_t 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_y = y;
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;
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->rotation = GFX_ROTCCW;
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->rotation = GFX_ROTCCW;
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->rotation = 0;
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->rotation = 0;
bMoving = true;
ptrUnit->dir = false;
ptrUnit->mirror = true;
}
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)
{
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)
{
menuLevel++;
}
return UnitActionsTable[ptrUnit->id];
}
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 *
* **************************************/
#include "Global_Inc.h"
#include "Gfx.h"
#include "GameStructures.h"
#include "Camera.h"
#ifdef __cplusplus
extern "C"
{
#endif //__cplusplus
/* **************************************
* Defines *
* **************************************/
/* **************************************
* 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
{
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;
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 *
* **************************************/
void UnitInit(void);
uint8_t UnitGetHpFromID(uint8_t id);
uint8_t UnitGetWidthFromID(uint8_t id);
uint8_t UnitGetHeightFromID(uint8_t id);
void UnitDraw(TYPE_CAMERA * ptrCamera, TYPE_UNIT * ptrUnit, bool bSelected);
const char* UnitSelectedOptions(TYPE_UNIT* ptrUnit);
void UnitMoveTo(TYPE_UNIT * ptrUnit, uint16_t x, uint16_t y);
void UnitHandler(TYPE_UNIT * ptrUnit);
void UnitAcceptAction(TYPE_UNIT* ptrUnit);
void UnitResetMenuLevel(void);
// Initialization and handling
void UnitInit(void);
void UnitHandler(TYPE_UNIT* ptrUnit);
// Unit information
uint8_t UnitGetHpFromID(uint8_t id);
uint8_t UnitGetWidthFromID(uint8_t id);
uint8_t UnitGetHeightFromID(uint8_t id);
uint8_t UnitGetAvailableActions(TYPE_UNIT* ptrUnit);
// 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
}
#endif //__cplusplus
#endif //__UNIT_HEADER__

View File

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