From bfbc217c3010a2890ce96853af377284baa835c9 Mon Sep 17 00:00:00 2001 From: sardap Date: Sat, 3 Oct 2020 23:54:51 +1000 Subject: [PATCH] updated how x scrolling is incremented --- source/common.c | 1 - source/common.h | 7 +++++++ source/player.c | 23 ++++++++--------------- source/scenes/main_game.c | 18 ++++++++++++++---- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/source/common.c b/source/common.c index 5044c7c..965ef34 100644 --- a/source/common.c +++ b/source/common.c @@ -42,7 +42,6 @@ void scene_update() { current_scene.show(); } current_scene.update(); - _frame_count++; } diff --git a/source/common.h b/source/common.h index a026eab..7a41dd6 100644 --- a/source/common.h +++ b/source/common.h @@ -10,10 +10,17 @@ #define BUILDING_Y_SPAWN 136 #define BUILDING_Y_TILE_SPAWN BUILDING_Y_SPAWN / 8 #define MAX_JUMP_WIDTH_TILES 6 +#define MIN_JUMP_WIDTH_TILES 3 #define GRAVITY (int)(1.5f * (FIX_SHIFT)) #define TERMINAL_VY GRAVITY * 20 +#define GBA_WIDTH 240 + +#define X_SCROLL_GAIN (FIXED)(0.01f * (FIX_SCALEF)) +#define X_SCROLL_MAX (FIXED)(3.5f * (FIX_SCALEF)) +#define X_SCROLL_RATE 120 + typedef struct scene_t { void (*show)(void); void (*update)(void); diff --git a/source/player.c b/source/player.c index 5b0e557..6c61435 100644 --- a/source/player.c +++ b/source/player.c @@ -5,8 +5,8 @@ #include "ent.h" #include "debug.h" -static const int SPEED = (int)(2.0f * (FIX_SCALE)); -static const int JUMP_POWER = (int)(1.2f * (FIX_SCALE)); +static const FIXED SPEED = (int)(2.0f * (FIX_SCALE)); +static const FIXED JUMP_POWER = (int)(1.2f * (FIX_SCALE)); ent_t _player = {}; static OBJ_ATTR* _player_obj = &_obj_buffer[0]; @@ -29,6 +29,7 @@ void init_player() { } void update_player() { + //Handles fliping the sprite if facing the other direction if(_player.facing == FACING_RIGHT && key_hit(KEY_LEFT)) { _player.facing = FACING_LEFT; _player_obj->attr1 ^= ATTR1_HFLIP; @@ -37,28 +38,25 @@ void update_player() { _player_obj->attr1 ^= ATTR1_HFLIP; } + // Player movement if(key_held(KEY_LEFT)) { _player.vx = -SPEED; } else if(key_held(KEY_RIGHT)) { _player.vx = SPEED; } - if(fx2int(_player.x) > 220) { + // Stops player from going offscreen to the right + if(fx2int(_player.x) > GBA_WIDTH - 20) { _player.vx += -SPEED; } -#ifdef DEBUG - // char str[50]; - // sprintf(str, "X: %d, VX: %f", fx2int(_player.x), fx2float(_player.vx)); - // write_to_log(LOG_LEVEL_INFO, str); -#endif - // _player.x += _player.vx; _player.vx += -_scroll_x; ent_move_x(&_player, _player.vx); _player.vx = 0; bool hit_y = ent_move_y(&_player, _player.vy); + // Applies gravity if(!hit_y) { if(_player.vy < TERMINAL_VY) { _player.vy += GRAVITY; @@ -80,6 +78,7 @@ void update_player() { break; } + //if the player y wraps everything just fucks up if(_player.y > 160 * FIX_SCALE) { _player.y = PLAYER_SPAWN_Y; } @@ -87,14 +86,8 @@ void update_player() { // increment/decrement starting tile with R/L _player.tid += bit_tribool(key_hit(KEY_START), KI_R, KI_L); - // toggle mapping mode - if(key_hit(KEY_START)) - REG_DISPCNT ^= DCNT_OBJ_1D; - // Hey look, it's one of them build macros! - // metr->attr2 = ATTR2_BUILD(player.tid, player.pb, 0); - obj_set_pos(_player_obj, fx2int(_player.x), fx2int(_player.y)); oam_copy(oam_mem, _obj_buffer, 1); // only need to update one } diff --git a/source/scenes/main_game.c b/source/scenes/main_game.c index 2622aa1..f64d89d 100644 --- a/source/scenes/main_game.c +++ b/source/scenes/main_game.c @@ -24,6 +24,7 @@ static const int building_sb = 26; // entries static FIXED _next_cloud_spawn; static FIXED _next_building_spawn; static int _building_spawn_x; +static int _tmp; static FIXED wrap_x(FIXED x) { if(x > bg_x_pix) { @@ -113,7 +114,7 @@ static void spawn_buildings() { width = spawn_building_0(start_x); } - width += gba_rand_range(3, MAX_JUMP_WIDTH_TILES); + width += gba_rand_range(MIN_JUMP_WIDTH_TILES, MAX_JUMP_WIDTH_TILES); _building_spawn_x = level_wrap_x(start_x + width); @@ -181,7 +182,7 @@ static void show(void) { _next_cloud_spawn = 0; _next_building_spawn = 0; - _scroll_x = 0;//(int)(0.25f * FIX_SCALE); + _scroll_x = (int)(0.25f * FIX_SCALE); _building_spawn_x = 0; @@ -233,8 +234,9 @@ static void update(void) { if(key_hit(KEY_B)) { if(_scroll_x == 0) { - _scroll_x = (int)(0.25f * FIX_SCALE); + _scroll_x = _tmp; } else { + _tmp = _scroll_x; _scroll_x = 0; } } @@ -254,7 +256,15 @@ static void update(void) { update_player(); - _scroll_x += 0.001f * FIX_SCALE; + if(frame_count() % X_SCROLL_RATE == 0) { + _scroll_x += X_SCROLL_GAIN; + //This is better than checking if it's greater prior to adding + //Because it handles the edge case where the gain will put it much + //over the limit + if(_scroll_x > X_SCROLL_MAX) { + _scroll_x = X_SCROLL_MAX; + } + } } static void hide(void) {