Skip to content

Commit

Permalink
updated how x scrolling is incremented
Browse files Browse the repository at this point in the history
  • Loading branch information
sardap committed Oct 3, 2020
1 parent 3a3b8ee commit bfbc217
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
1 change: 0 additions & 1 deletion source/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void scene_update() {
current_scene.show();
}
current_scene.update();
_frame_count++;
}


Expand Down
7 changes: 7 additions & 0 deletions source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 8 additions & 15 deletions source/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -80,21 +78,16 @@ void update_player() {
break;
}

//if the player y wraps everything just fucks up
if(_player.y > 160 * FIX_SCALE) {
_player.y = PLAYER_SPAWN_Y;
}

// 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
}
18 changes: 14 additions & 4 deletions source/scenes/main_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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) {
Expand Down

0 comments on commit bfbc217

Please sign in to comment.