Skip to content

Commit 630ed32

Browse files
committed
Refactoring to limit scope of guardCount
1 parent 5dd1de5 commit 630ed32

File tree

4 files changed

+50
-49
lines changed

4 files changed

+50
-49
lines changed

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ OBJ = $(SRC:%.c=%.o)
1313
# cl65 -t cx16 -I $(INCLUDE) -Oi $<
1414

1515
lode_runner: mainloop.c $(SRC)
16-
cl65 -t cx16 --listing test.txt $(INCLUDES) $(LIBRARIES) -Oi -o lode_runner.prg mainloop.c $(SRC) libX16.lib cx16.lib
16+
cl65 -t cx16 $(INCLUDES) $(LIBRARIES) -Oi -o lode_runner.prg mainloop.c $(SRC) libX16.lib cx16.lib
1717

1818
PALETTE.BIN: ../assets/palette.txt
1919
../scripts/ascii2bin.py --input ../assets/palette.txt --output PALETTE.BIN --addr 1000

src/guard.c

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct guard_t guard[MAX_GUARDS];
1010

1111
struct shakeGuard_t shake[MAX_GUARDS];
1212

13-
uint8_t guardCount;
13+
static uint8_t guardCount;
1414

1515
void addGuardToShakeQueue(uint8_t id);
1616

@@ -111,38 +111,43 @@ void clearGuards()
111111
moveId = 0;
112112
}
113113

114-
void initGuard(uint8_t x, uint8_t y)
114+
uint8_t initGuard(uint8_t x, uint8_t y)
115115
{
116-
uint8_t i = guardCount;
117-
uint16_t xPos = x * TILE_W;
118-
uint16_t yPos = y * TILE_H;
119-
120-
guard[i].x = x;
121-
guard[i].y = y;
122-
guard[i].xOffset = 0;
123-
guard[i].yOffset = 0;
124-
guard[i].holePosX = 0;
125-
guard[i].holePosY = 0;
126-
guard[i].action = ACT_UNKNOWN;
127-
guard[i].direction = ACT_RIGHT;
128-
guard[i].idx = 0;
129-
130-
guard[i].sequence = RUN_SEQUENCE;
131-
132-
// Sprite attribute settings
133-
vpoke((GUARD_1 >> 5) & 0xff, 0x1f5000 + 8 * (i + 1)); // Attr0
134-
VERA.data0 = (GUARD_1 >> 13) & 0xf; // Attr1
135-
VERA.data0 = xPos & 0xff; // Attr2
136-
VERA.data0 = xPos >> 8; // Attr3
137-
VERA.data0 = yPos & 0xff; // Attr4
138-
VERA.data0 = yPos >> 8; // Attr5
139-
VERA.data0 = (3 << 2); // Attr6
140-
VERA.data0 = 0; // Attr7
116+
if (guardCount < MAX_GUARDS) {
117+
uint8_t i = guardCount;
118+
uint16_t xPos = x * TILE_W;
119+
uint16_t yPos = y * TILE_H;
120+
121+
guard[i].x = x;
122+
guard[i].y = y;
123+
guard[i].xOffset = 0;
124+
guard[i].yOffset = 0;
125+
guard[i].holePosX = 0;
126+
guard[i].holePosY = 0;
127+
guard[i].action = ACT_UNKNOWN;
128+
guard[i].direction = ACT_RIGHT;
129+
guard[i].idx = 0;
130+
131+
guard[i].sequence = RUN_SEQUENCE;
132+
133+
// Sprite attribute settings
134+
vpoke((GUARD_1 >> 5) & 0xff, 0x1f5000 + 8 * (i + 1)); // Attr0
135+
VERA.data0 = (GUARD_1 >> 13) & 0xf; // Attr1
136+
VERA.data0 = xPos & 0xff; // Attr2
137+
VERA.data0 = xPos >> 8; // Attr3
138+
VERA.data0 = yPos & 0xff; // Attr4
139+
VERA.data0 = yPos >> 8; // Attr5
140+
VERA.data0 = (3 << 2); // Attr6
141+
VERA.data0 = 0; // Attr7
141142

142143
#ifdef DEBUG
143-
// displayGuard(guardCount);
144+
// displayGuard(guardCount);
144145
#endif
145-
guardCount++;
146+
guardCount++;
147+
return 1;
148+
} else {
149+
return 0;
150+
}
146151
}
147152

148153
int8_t guardId(uint8_t x, uint8_t y)

src/guard.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,16 @@ extern struct guard_t guard[MAX_GUARDS];
5050

5151
extern struct shakeGuard_t shake[MAX_GUARDS];
5252

53-
extern uint8_t guardCount;
54-
55-
void clearGuards(void);
56-
void initGuard(uint8_t x, uint8_t y);
57-
int8_t guardId(uint8_t x, uint8_t y);
58-
int8_t guardAlive(uint8_t x, uint8_t y);
59-
void guardReborn(uint8_t x, uint8_t y);
60-
void removeFromShake(uint8_t id);
53+
extern void clearGuards(void);
54+
extern uint8_t initGuard(uint8_t x, uint8_t y);
55+
extern int8_t guardId(uint8_t x, uint8_t y);
56+
extern int8_t guardAlive(uint8_t x, uint8_t y);
57+
extern void guardReborn(uint8_t x, uint8_t y);
58+
extern void removeFromShake(uint8_t id);
6159

6260
// Entry point from main loop to shake guards prior to
6361
// them climbing out of holes
64-
void processGuardShake(void);
62+
extern void processGuardShake(void);
6563

6664
// Entry point from main loop to move guards
6765
extern void moveGuard(void);

src/levels.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,14 @@ int loadLevel(uint8_t world, uint8_t level)
167167

168168
switch (tile1) {
169169
case TILE_GUARD:
170-
// TODO: Initiaze guard and set tile as blank
171-
map[idx * 2][row].act = TILE_GUARD;
172-
map[idx * 2][row].base = TILE_BLANK;
173-
if (guardCount == MAX_GUARDS) {
170+
if (initGuard(idx*2,row)) {
171+
map[idx * 2][row].act = TILE_GUARD;
172+
map[idx * 2][row].base = TILE_BLANK;
173+
} else {
174174
// Too many guards
175175
map[idx * 2][row].act = TILE_BLANK;
176176
continue;
177177
}
178-
initGuard(idx*2,row);
179178
break;
180179
case TILE_RUNNER:
181180
// If runner hasn't been found yet, then initialize
@@ -200,15 +199,14 @@ int loadLevel(uint8_t world, uint8_t level)
200199
}
201200
switch (tile2) {
202201
case TILE_GUARD:
203-
// TODO: Initiaze guard and set tile as blank
204-
map[idx * 2 + 1][row].act = TILE_GUARD;
205-
map[idx * 2 + 1][row].base = TILE_BLANK;
206-
if (guardCount == MAX_GUARDS) {
202+
if (initGuard(idx*2+1,row)) {
203+
map[idx * 2 + 1][row].act = TILE_GUARD;
204+
map[idx * 2 + 1][row].base = TILE_BLANK;
205+
} else {
207206
// Too many guards
208207
map[idx * 2 + 1][row].act = TILE_BLANK;
209208
continue;
210209
}
211-
initGuard(idx*2+1,row);
212210
break;
213211
case TILE_RUNNER:
214212
// If runner hasn't been found yet, then initialize

0 commit comments

Comments
 (0)