Added basic highscore tracking for scrolly-tile
@@ -67,6 +67,11 @@ uint32_t current_row = 0;
Timer state_update;
Point tile_offset(0, 0);
+struct SaveData {
+ uint32_t highscore;
+} saveData;
+
+uint32_t highscore = 0;
Vec2 player_position(80.0f, SCREEN_H - PLAYER_H);
Vec2 player_velocity(0.0f, 0.0f);
@@ -107,6 +112,10 @@ using tile_callback = uint8_t (*)(uint8_t tile, uint8_t x, uint8_t y, void *args
uint32_t prng_lfsr = 0;
const uint16_t prng_tap = 0x74b8;
+uint32_t max(uint32_t a, uint32_t b) {
+ return a > b ? a : b;
+}
+
uint32_t get_random_number() {
switch(current_random_source) {
default:
@@ -396,6 +405,17 @@ void new_game() {
void init() {
set_screen_mode(lores);
+
+ // Attempt to load the first save slot.
+ if (read_save(saveData)) {
+ // Loaded sucessfully!
+ highscore = saveData.highscore;
+ }
+ else {
+ // No save file or it failed to load.
+ saveData.highscore = 0;
+ }
+
#ifdef __AUDIO__
channels[0].waveforms = Waveform::NOISE;
channels[0].frequency = 4200;
@@ -654,10 +674,11 @@ void update(uint32_t time_ms) {
// Useful for debug since you can position the player directly
//player_position.y += movement.y;
- if(player_position.y + PLAYER_H > SCREEN_H) {
- game_state = enum_state::dead;
- } else if(player_position.y > SCREEN_H - water_level) {
+ if(player_position.y + PLAYER_H > SCREEN_H || player_position.y > SCREEN_H - water_level) {
game_state = enum_state::dead;
+ highscore = max(highscore, player_progress);
+ saveData.highscore = highscore;
+ write_save(saveData); // save saveData
}
for_each_tile(collide_player_ud, (void *)&tile_offset);
}
@@ -682,6 +703,11 @@ void render_summary() {
}
text = "Press B";
+ screen.text(text, minimal_font, Point(10, (SCREEN_H / 2) + 50));
+
+ text = "Highscore: ";
+ text.append(std::to_string(highscore));
+ text.append("cm");
screen.text(text, minimal_font, Point(10, (SCREEN_H / 2) + 40));
}
