Reformat to remove some hardocded variables

This commit is contained in:
David Volovskiy 2024-07-19 08:43:15 -04:00
parent defd01f9f0
commit 6f3f09c5ba

View File

@ -27,12 +27,9 @@
#include "endless_runner_face.h" #include "endless_runner_face.h"
typedef enum { typedef enum {
NOT_JUMPING = 0, JUMPING_FINAL_FRAME = 0,
JUMP, NOT_JUMPING,
JUMPING_1, JUMPING_START,
JUMPING_2,
JUMPING_3,
JUMP_COUNT
} RunnerJumpState; } RunnerJumpState;
typedef enum { typedef enum {
@ -53,12 +50,16 @@ typedef enum {
#define NUM_GRID 12 #define NUM_GRID 12
#define FREQ 8 #define FREQ 8
#define FREQ_SLOW 4 #define FREQ_SLOW 4
#define JUMP_FRAMES 2
#define JUMP_FRAMES_EASY 20
#define MIN_ZEROES 4
#define MIN_ZEROES_HARD 3
#define MAX_DISP_SCORE 39 // The top-right digits can't properly display above 39 #define MAX_DISP_SCORE 39 // The top-right digits can't properly display above 39
typedef struct { typedef struct {
uint32_t obst_pattern; uint32_t obst_pattern;
uint16_t obst_indx : 8; uint16_t obst_indx : 8;
uint16_t jump_state : 3; uint16_t jump_state : 5;
uint16_t sec_before_moves : 3; uint16_t sec_before_moves : 3;
bool loc_2_on; bool loc_2_on;
bool loc_3_on; bool loc_3_on;
@ -81,7 +82,7 @@ static uint32_t get_random_legal(uint32_t prev_val, uint16_t difficulty) {
* @param difficulty To dictate how spread apart the obsticles must be * @param difficulty To dictate how spread apart the obsticles must be
* @return the new random value, where it's first NUM_GRID MSBs are the same as prev_val * @return the new random value, where it's first NUM_GRID MSBs are the same as prev_val
*/ */
uint8_t min_zeros = (difficulty == DIFF_HARD) ? 3 : 4; uint8_t min_zeros = (difficulty == DIFF_HARD) ? MIN_ZEROES_HARD : MIN_ZEROES;
uint32_t max = (1 << (_num_bits_obst_pattern - NUM_GRID)) - 1; uint32_t max = (1 << (_num_bits_obst_pattern - NUM_GRID)) - 1;
uint32_t rand = get_random(max); uint32_t rand = get_random(max);
uint32_t rand_legal = 0; uint32_t rand_legal = 0;
@ -110,7 +111,7 @@ static uint32_t get_random_legal(uint32_t prev_val, uint16_t difficulty) {
} }
static void display_ball(bool jumping) { static void display_ball(bool jumping) {
if (jumping == NOT_JUMPING) { if (!jumping) {
watch_set_pixel(0, 21); watch_set_pixel(0, 21);
watch_set_pixel(1, 21); watch_set_pixel(1, 21);
watch_set_pixel(0, 20); watch_set_pixel(0, 20);
@ -168,6 +169,24 @@ static void display_title(endless_runner_state_t *state) {
display_difficulty(state -> difficulty); display_difficulty(state -> difficulty);
} }
static void begin_playing(endless_runner_state_t *state) {
state -> curr_screen = SCREEN_PLAYING;
movement_request_tick_frequency((state -> difficulty == DIFF_BABY) ? FREQ_SLOW : FREQ);
watch_display_string("ER ", 0);
game_state.jump_state = NOT_JUMPING;
display_ball(game_state.jump_state != NOT_JUMPING);
do // Avoid the first array of obstacles being a boring line of 0s
{
game_state.obst_pattern = get_random_legal(0, state -> difficulty);
} while (game_state.obst_pattern == 0);
display_score(state -> curr_score);
if (state -> soundOn){
watch_buzzer_play_note(BUZZER_NOTE_C5, 200);
watch_buzzer_play_note(BUZZER_NOTE_E5, 200);
watch_buzzer_play_note(BUZZER_NOTE_G5, 200);
}
}
static void display_lose_screen(endless_runner_state_t *state) { static void display_lose_screen(endless_runner_state_t *state) {
movement_request_tick_frequency(1); movement_request_tick_frequency(1);
state -> curr_screen = SCREEN_LOSE; state -> curr_screen = SCREEN_LOSE;
@ -294,6 +313,7 @@ void endless_runner_face_activate(movement_settings_t *settings, void *context)
bool endless_runner_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { bool endless_runner_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
endless_runner_state_t *state = (endless_runner_state_t *)context; endless_runner_state_t *state = (endless_runner_state_t *)context;
bool success_jump = false; bool success_jump = false;
uint8_t curr_jump_frame = 0;
switch (event.event_type) { switch (event.event_type) {
case EVENT_ACTIVATE: case EVENT_ACTIVATE:
@ -314,18 +334,11 @@ bool endless_runner_face_loop(movement_event_t event, movement_settings_t *setti
success_jump = display_obstacles(state); success_jump = display_obstacles(state);
switch (game_state.jump_state) switch (game_state.jump_state)
{ {
case JUMP: case NOT_JUMPING:
game_state.jump_state = JUMPING_1;
break; break;
case JUMPING_1: case JUMPING_FINAL_FRAME:
game_state.jump_state = (state -> difficulty >= DIFF_NORM) ? JUMPING_3 : JUMPING_2;
break;
case JUMPING_2:
game_state.jump_state = JUMPING_3;
break;
case JUMPING_3:
game_state.jump_state = NOT_JUMPING; game_state.jump_state = NOT_JUMPING;
display_ball(game_state.jump_state); display_ball(game_state.jump_state != NOT_JUMPING);
if (state -> soundOn){ if (state -> soundOn){
if (success_jump) if (success_jump)
watch_buzzer_play_note(BUZZER_NOTE_C5, 60); watch_buzzer_play_note(BUZZER_NOTE_C5, 60);
@ -334,6 +347,12 @@ bool endless_runner_face_loop(movement_event_t event, movement_settings_t *setti
} }
break; break;
default: default:
curr_jump_frame = game_state.jump_state - NOT_JUMPING;
if (curr_jump_frame >= JUMP_FRAMES_EASY || (state -> difficulty >= DIFF_NORM && curr_jump_frame >= JUMP_FRAMES))
game_state.jump_state = JUMPING_FINAL_FRAME;
else
game_state.jump_state++;
//if (!watch_get_pin_level(BTN_ALARM) && !watch_get_pin_level(BTN_LIGHT)) game_state.jump_state = JUMPING_FINAL_FRAME; // Uncomment to have depressing the buttons cause the ball to drop regardless of its current frame.
break; break;
} }
if (game_state.jump_state == NOT_JUMPING && (game_state.loc_2_on || game_state.loc_3_on)) { if (game_state.jump_state == NOT_JUMPING && (game_state.loc_2_on || game_state.loc_3_on)) {
@ -345,25 +364,10 @@ bool endless_runner_face_loop(movement_event_t event, movement_settings_t *setti
break; break;
case EVENT_LIGHT_BUTTON_UP: case EVENT_LIGHT_BUTTON_UP:
case EVENT_ALARM_BUTTON_UP: case EVENT_ALARM_BUTTON_UP:
if (state -> curr_screen == SCREEN_TITLE) { if (state -> curr_screen == SCREEN_TITLE)
state -> curr_screen = SCREEN_PLAYING; begin_playing(state);
movement_request_tick_frequency((state -> difficulty == DIFF_BABY) ? FREQ_SLOW : FREQ); else if (state -> curr_screen == SCREEN_LOSE)
watch_display_string(" ", 4);
display_ball(false);
do // Avoid the first array of obstacles being a boring line of 0s
{
game_state.obst_pattern = get_random_legal(0, state -> difficulty);
} while (game_state.obst_pattern == 0);
display_score(state -> curr_score);
if (state -> soundOn){
watch_buzzer_play_note(BUZZER_NOTE_C5, 200);
watch_buzzer_play_note(BUZZER_NOTE_E5, 200);
watch_buzzer_play_note(BUZZER_NOTE_G5, 200);
}
}
else if (state -> curr_screen == SCREEN_LOSE) {
display_title(state); display_title(state);
}
break; break;
case EVENT_LIGHT_LONG_PRESS: case EVENT_LIGHT_LONG_PRESS:
if (state -> curr_screen == SCREEN_TITLE) { if (state -> curr_screen == SCREEN_TITLE) {
@ -378,8 +382,8 @@ bool endless_runner_face_loop(movement_event_t event, movement_settings_t *setti
case EVENT_LIGHT_BUTTON_DOWN: case EVENT_LIGHT_BUTTON_DOWN:
case EVENT_ALARM_BUTTON_DOWN: case EVENT_ALARM_BUTTON_DOWN:
if (state -> curr_screen == SCREEN_PLAYING && game_state.jump_state == NOT_JUMPING){ if (state -> curr_screen == SCREEN_PLAYING && game_state.jump_state == NOT_JUMPING){
game_state.jump_state = JUMP; game_state.jump_state = JUMPING_START;
display_ball(game_state.jump_state); display_ball(game_state.jump_state != NOT_JUMPING);
} }
break; break;
case EVENT_ALARM_LONG_PRESS: case EVENT_ALARM_LONG_PRESS: