Skip to content

Commit

Permalink
Declare LUM_TAG in a c file, extern in a header
Browse files Browse the repository at this point in the history
Hook up lumberjackAudioCallback(), lumberjackExitGameMode()
Add TODO headers for unused functions
Declare arrays const where appropriate
Remove duplicate set of enemy->type
Only declare functions static when used in a single file
Use ARRAY_SIZE() macro for accurate loop bounds
Declare restartLevel() as restartLevel(void)
Uniquely name loop variables to not shadow others
Call lumberjackUpdatePlayerCollision() with appropriate arg
Fixed types and duplicate in lumberjackDetectBump()
  • Loading branch information
AEFeinstein committed Sep 5, 2023
1 parent 8952365 commit 67d1688
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 68 deletions.
17 changes: 13 additions & 4 deletions main/modes/lumberjack/lumberjack.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static const char lumberjackPanic[] = "Panic";
static const char lumberjackAttack[] = "Attack";
static const char lumberjackBack[] = "Back";


const char* LUM_TAG = "LUM";

swadgeMode_t lumberjackMode = {
.modeName = lumberjackName,
Expand All @@ -44,7 +44,7 @@ swadgeMode_t lumberjackMode = {
.fnEnterMode = lumberjackEnterMode,
.fnExitMode = lumberjackExitMode,
.fnMainLoop = lumberjackMainLoop,
.fnAudioCallback = NULL,
.fnAudioCallback = lumberjackAudioCallback,
.fnBackgroundDrawCallback = lumberjackBackgroundDrawCallback,
.fnEspNowRecvCb = lumberjackEspNowRecvCb,
.fnEspNowSendCb = lumberjackEspNowSendCb,
Expand Down Expand Up @@ -91,6 +91,7 @@ static void lumberjackEnterMode(void)

static void lumberjackExitMode(void)
{
lumberjackExitGameMode();
p2pDeinit(&lumberjack->p2p);
freeFont(&lumberjack->ibm);
freeFont(&lumberjack->logbook);
Expand Down Expand Up @@ -165,6 +166,14 @@ static void lumberjackMsgRxCb(p2pInfo* p2p, const uint8_t* payload, uint8_t len)
//Do anything
}

/**
* @brief TODO use this somewhere
*
* @param p2p
* @param status
* @param data
* @param len
*/
static void lumberjackMsgTxCbFn(p2pInfo* p2p, messageStatus_t status, const uint8_t* data, uint8_t len)
{

Expand All @@ -173,12 +182,12 @@ static void lumberjackMsgTxCbFn(p2pInfo* p2p, messageStatus_t status, const uint

static void lumberjackMenuCb(const char* label, bool selected, uint32_t settingVal)
{
ESP_LOGI("LUM", "Info ");
ESP_LOGI(LUM_TAG, "Info ");
if (selected)
{
if (label == lumberjackPanic)
{
ESP_LOGI("LUM", "Panic");
ESP_LOGI(LUM_TAG, "Panic");
lumberjack->screen = LUMBERJACK_A;
lumberjackStartGameMode(LUMBERJACK_PANIC);
}
Expand Down
2 changes: 1 addition & 1 deletion main/modes/lumberjack/lumberjack.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "lumberjackEntity.h"
#include "lumberjackPlayer.h"

const static char* LUM_TAG = "LUM";
extern const char* LUM_TAG;
extern swadgeMode_t lumberjackMode;

typedef enum{
Expand Down
10 changes: 3 additions & 7 deletions main/modes/lumberjack/lumberjackEntity.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ void lumberjackUpdateEnemy(lumberjackEntity_t* enemy, int newIndex)
enemy->cH = 15;

}


enemy->type = newIndex;

}

void lumberjackDoEnemyControls(lumberjackEntity_t* enemy)
Expand Down Expand Up @@ -162,20 +158,20 @@ uint8_t lumberjackGetEnemyAnimation(lumberjackEntity_t* enemy)

if (animation == LUMBERJACK_RUN)
{
int anim[] = {0, 1, 2, 3};
const int anim[] = {0, 1, 2, 3};
return anim[enemy->currentFrame % 4];
}

if (animation == LUMBERJACK_BUMPED)
{
int anim[] = {4};
const int anim[] = {4};
return anim[enemy->currentFrame % 2];
}


if (animation == LUMBERJACK_BUMPED_IDLE)
{
int anim[] = {5, 6};
const int anim[] = {5, 6};
return anim[enemy->currentFrame % 2];
}

Expand Down
73 changes: 40 additions & 33 deletions main/modes/lumberjack/lumberjackGame.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include "lumberjackEntity.h"
#include "lumberjackPlayer.h"


static lumberjackTile_t* lumberjackGetTile(int x, int y);
static void lumberjackUpdateEntity(lumberjackEntity_t* entity, int64_t elapsedUs);
static bool lumberjackIsCollisionTile(int index);

lumberjackVars_t* lumv;
lumberjackTile_t lumberjackCollisionDebugTiles[32] = {};
Expand Down Expand Up @@ -178,7 +180,7 @@ void lumberjackSetupLevel(int index)

}

uint8_t level[] = {
const uint8_t level[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand All @@ -202,7 +204,7 @@ void lumberjackSetupLevel(int index)
0, 0, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 10, 0, 0, 0,
};

uint8_t ani[] = {
const uint8_t ani[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand All @@ -226,7 +228,7 @@ void lumberjackSetupLevel(int index)
2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2,
};

for (int i = 0; i < 378; i++)
for (int i = 0; i < ARRAY_SIZE(ani); i++)
{
lumv->anim[i] = ani[i];

Expand All @@ -238,7 +240,10 @@ void lumberjackSetupLevel(int index)
ESP_LOGI(LUM_TAG, "LOADED");
}

static void restartLevel()
/**
* @brief TODO use this somewhere
*/
void restartLevel(void)
{
lumberjackRespawn(lumv->localPlayer);
}
Expand All @@ -259,15 +264,15 @@ void baseMode(int64_t elapsedUs)
lumv->btnState = evt.state;
}

//areturn;
// return;

//Check Controls
if (lumv->localPlayer->state != LUMBERJACK_DEAD && lumv->localPlayer->state != LUMBERJACK_OFFSCREEN)
{
lumberjackDoControls();
}

for (int i = 0; i < 8; i++)
for (int i = 0; i < ARRAY_SIZE(lumv->enemy); i++)
{
if (lumv->enemy[i] == NULL) continue;

Expand All @@ -280,7 +285,7 @@ void baseMode(int64_t elapsedUs)
//Check spawn
lumberjackSpawnCheck(elapsedUs);

for (int i = 0; i < 32; i++)
for (int i = 0; i < ARRAY_SIZE(lumberjackCollisionDebugTiles); i++)
{
lumberjackCollisionDebugTiles[i].type = -1;
lumberjackCollisionDebugTiles[i].x = -1;
Expand All @@ -302,9 +307,9 @@ void baseMode(int64_t elapsedUs)
//Check physics

//Enemy
for (int i = 0; i < 8; i++)
for (int eIdx = 0; eIdx < ARRAY_SIZE(lumv->enemy); eIdx++)
{
lumberjackEntity_t* enemy = lumv->enemy[i];
lumberjackEntity_t* enemy = lumv->enemy[eIdx];
if (enemy == NULL || enemy->ready == true) continue;

enemy->showAlert = false;
Expand Down Expand Up @@ -337,14 +342,14 @@ void baseMode(int64_t elapsedUs)
}


lumberjackUpdateEntity(lumv->enemy[i],elapsedUs);
lumberjackUpdatePlayerCollision(lumv->playerSprites);
lumberjackUpdateEntity(enemy,elapsedUs);
lumberjackUpdatePlayerCollision(lumv->localPlayer);

This comment has been minimized.

Copy link
@AEFeinstein

AEFeinstein Sep 5, 2023

Author Owner

@MrTroyD This fix was a biggie


for (int i = 0; i < 8; i++)
for (int oeIdx = 0; oeIdx < ARRAY_SIZE(lumv->enemy); oeIdx++)
{
if (lumv->enemy[i] == NULL) continue;
if (lumv->enemy[oeIdx] == NULL) continue;

lumberjackUpdateEnemyCollision(lumv->enemy[i]);
lumberjackUpdateEnemyCollision(lumv->enemy[oeIdx]);
}
}

Expand All @@ -366,7 +371,7 @@ void baseMode(int64_t elapsedUs)
lumberjackUpdateEntity(lumv->localPlayer, elapsedUs);

//
for (int i = 0; i < 8; i++)
for (int i = 0; i < ARRAY_SIZE(lumv->enemy); i++)
{
lumberjackEntity_t* enemy = lumv->enemy[i];

Expand Down Expand Up @@ -423,7 +428,7 @@ void baseMode(int64_t elapsedUs)

//Update animation
//Enemy Animation
for (int i = 0; i < 8; i++)
for (int i = 0; i < ARRAY_SIZE(lumv->enemy); i++)
{
if (lumv->enemy[i] == NULL) continue;

Expand Down Expand Up @@ -456,7 +461,7 @@ void baseMode(int64_t elapsedUs)
//Draw enemies


for (int i = 0; i < 8; i++)
for (int i = 0; i < ARRAY_SIZE(lumv->enemy); i++)
{
if (lumv->enemy[i] == NULL || lumv->enemy[i]->ready) continue;
lumberjackEntity_t* enemy = lumv->enemy[i];
Expand All @@ -483,7 +488,7 @@ void baseMode(int64_t elapsedUs)

if (lumv->localPlayer->state == LUMBERJACK_DEAD)
{
//ESP_LOGI("LLL", "DEAD %d", currentFrame);
//ESP_LOGI(LUM_TAG, "DEAD %d", currentFrame);
}


Expand Down Expand Up @@ -530,7 +535,7 @@ void lumberjackSpawnCheck(int64_t elapseUs)

if (lumv->spawnTimer < 0)
{
for(int i = 0; i < 8; i++)
for(int i = 0; i < ARRAY_SIZE(lumv->enemy); i++)
{
if (lumv->enemy[i] == NULL) continue;

Expand Down Expand Up @@ -745,8 +750,9 @@ static void lumberjackUpdateEntity(lumberjackEntity_t* entity, int64_t elapsedUs
return;
}
}
else if (entity != lumv->localPlayer)
else
{
// Entity is not local player
}
}

Expand All @@ -769,9 +775,9 @@ static void lumberjackUpdateEntity(lumberjackEntity_t* entity, int64_t elapsedUs
}
}

static void lumberjackUpdate(int64_t elapseUs)
void lumberjackUpdate(int64_t elapseUs)
{
for (int i = 0; i < 400; i++)
for (int i = 0; i < ARRAY_SIZE(lumv->tile); i++)
{
if (lumv->tile[i].offset > 0)
{
Expand All @@ -787,7 +793,7 @@ static void lumberjackUpdate(int64_t elapseUs)
}
}

static void lumberjackTileMap()
void lumberjackTileMap(void)
{
for (int y = 0; y < 21; y++)
{
Expand Down Expand Up @@ -896,7 +902,7 @@ static lumberjackTile_t* lumberjackGetTile(int x, int y)
if (ty < 0) ty = 0;
if (ty > lumv->currentMapHeight) ty = lumv->currentMapHeight;

int test = -1;
// int test = -1;
for (int i = 0; i < 32; i++ )
{
if (lumberjackCollisionDebugTiles[i].type == -1)
Expand All @@ -911,10 +917,10 @@ static lumberjackTile_t* lumberjackGetTile(int x, int y)

return &lumberjackCollisionDebugTiles[i];
}
test = i;
// test = i;
}

//ESP_LOGI("TU","NO TILE at %d %d!", test, ty);
//ESP_LOGI(LUM_TAG,"NO TILE at %d %d!", test, ty);
return NULL;
}

Expand All @@ -929,12 +935,13 @@ static bool lumberjackIsCollisionTile(int index)

void lumberjackDetectBump(lumberjackTile_t* tile)
{
if (&lumv->localPlayer->state != LUMBERJACK_BUMPED && &lumv->localPlayer->state != LUMBERJACK_DEAD && &lumv->localPlayer->state != LUMBERJACK_BUMPED)
if (lumv->localPlayer->state != LUMBERJACK_BUMPED &&
lumv->localPlayer->state != LUMBERJACK_DEAD)

This comment has been minimized.

Copy link
@AEFeinstein

AEFeinstein Sep 5, 2023

Author Owner

@MrTroyD this one made a big difference too, note the removal of ampersands.

{
//TODO put in bump the player
}

for(int i = 0; i < 8; i++)
for(int i = 0; i < ARRAY_SIZE(lumv->enemy); i++)
{
lumberjackEntity_t* enemy = lumv->enemy[i];
if (enemy == NULL) continue;
Expand Down Expand Up @@ -993,26 +1000,26 @@ void lumberjackDetectBump(lumberjackTile_t* tile)
}
}

static void lumberjackExitGameMode(void)
void lumberjackExitGameMode(void)
{
printf("FREE");
p2pDeinit(&lumv->p2p);
//** FREE THE SPRITES **//
//Free the enemies
for (int i =0; i < 21; i++)
for (int i =0; i < ARRAY_SIZE(lumv->enemySprites); i++)
{
freeWsg(&lumv->enemySprites[i]);
}


//Free the players
for (int i =0; i < 63; i++)
for (int i =0; i < ARRAY_SIZE(lumv->playerSprites); i++)
{
freeWsg(&lumv->playerSprites[i]);
}

//Free the tiles
for (int i = 0; i < 12; i++)
for (int i = 0; i < ARRAY_SIZE(lumv->animationTiles); i++)
{
freeWsg(&lumv->animationTiles[i]);
}
Expand Down
12 changes: 4 additions & 8 deletions main/modes/lumberjack/lumberjackGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@


void lumberjackStartGameMode(lumberjackGameType_t type);
void lumberjackExitGameMode(void);
void lumberjackSetupLevel(int index);
void lumberjackDoControls(void);
static void lumberjackTileMap(void);
static void lumberjackUpdate(int64_t elapseUs);
void lumberjackTileMap(void);
void lumberjackUpdate(int64_t elapseUs);

void lumberjackGameLoop(int64_t elapsedUs);

static void restartLevel();

static void lumberjackUpdateEntity(lumberjackEntity_t* entity, int64_t elapsedUs);

static lumberjackTile_t* lumberjackGetTile(int x, int y);
static bool lumberjackIsCollisionTile(int index);
void restartLevel(void);

void lumberjackGameDebugLoop(int64_t elapsedUs);

Expand Down
Loading

0 comments on commit 67d1688

Please sign in to comment.