Skip to content

Commit

Permalink
fixed debug printing args fixed colsion while scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
sardap committed Oct 3, 2020
1 parent cb4558b commit 3a3b8ee
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"files.associations": {
"*.bf": "plaintext",
"cstdlib": "c",
"algorithm": "c"
"algorithm": "c",
"sstream": "c"
},
"C_Cpp.errorSquiggles": "Disabled"
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# ECS GBA

Taking a huge amount of "inspiration" from https://github.com/exelotl/goodboy-advance
3 changes: 2 additions & 1 deletion source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

#define BUILDING_Y_SPAWN 136
#define BUILDING_Y_TILE_SPAWN BUILDING_Y_SPAWN / 8
#define MAX_JUMP_WIDTH_TILES 5
#define MAX_JUMP_WIDTH_TILES 6

#define GRAVITY (int)(1.5f * (FIX_SHIFT))
#define TERMINAL_VY GRAVITY * 20

typedef struct scene_t {
void (*show)(void);
Expand Down
11 changes: 9 additions & 2 deletions source/debug.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#ifndef DEBUG_H
#define DEBUG_H

#include <stdarg.h>
#include <tonc_types.h>
#include <string.h>
#include <stdio.h>

#include "ent.h"

#define DEBUG 1

Expand All @@ -17,8 +21,11 @@ typedef enum log_level_e {

void init_debug();

inline static void write_to_log(log_level_e level, char* vs,...) {
strcpy(REG_DEBUG_STRING, vs);
inline static void write_to_log(log_level_e level, const char* ptr,...) {
va_list args;
va_start(args, ptr);
vsnprintf(REG_DEBUG_STRING, 0x100, ptr, args);
va_end(args);
*REG_DEBUG_FLAGS = (u16)level | 0x100;
}

Expand Down
86 changes: 76 additions & 10 deletions source/ent.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <tonc.h>
#include "common.h"
#include "level.h"
#include "debug.h"

OBJ_ATTR _obj_buffer[128] = {};
FIXED _bg_pos_x = 0;
Expand All @@ -19,17 +20,82 @@ static inline FIXED level_to_screen(int l) {
return int2fx(l * TILE_WIDTH + l % TILE_WIDTH);
}

void apply_gravity(ent_t *e) {
// FIXED level_x = translate_x(e);
// FIXED level_y = translate_y(e);
// STOLEN from https://github.com/exelotl/goodboy-advance
bool did_hit_x(ent_t *e, FIXED dx) {
int flags = ent_level_collision_at(e, dx, 0);
return flags & (LEVEL_COL_GROUND);
}

// STOLEN from https://github.com/exelotl/goodboy-advance
bool ent_move_x(ent_t *e, FIXED dx) {
if(did_hit_x(e, dx)) {
int sign = dx >= 0 ? 1 : -1;
while(fx2int(dx) != 0 && !did_hit_x(e, sign)) {
e->x += sign;
dx -= sign;
}

//NOT STOLEN MY OWN SHIT CODE
//Failed to push out
if(did_hit_x(e, 0)) {
int start = (translate_x(e) / FIX_SCALE) / TILE_WIDTH;
int y = (translate_y(e) / FIX_SCALE) / TILE_WIDTH;

//Failed to push out but will only check all tiles
for(int i = 0; i < 32; i++) {
//Left
if(!tile_to_collision(at_level(level_wrap_x(start - i), y))) {
while(did_hit_x(e, 0)) {
e->x--;
}
break;
}

//Right
if(!tile_to_collision(at_level(level_wrap_x(start + i), y))) {
while(did_hit_x(e, 0)) {
e->x++;
}
break;
}
}
}
return true;
}

e->x += e->vx;
return false;
}

// STOLEN from https://github.com/exelotl/goodboy-advance
bool did_hit_y(ent_t *e, FIXED dy) {
int flags = ent_level_collision_at(e, 0, dy);
return flags & (LEVEL_COL_GROUND);
}

// STOLEN from https://github.com/exelotl/goodboy-advance
bool ent_move_y(ent_t *e, FIXED dy) {
if(did_hit_y(e, dy)) {
int sign = dy >= 0 ? 1 : -1;
while(fx2int(dy) != 0 && !did_hit_y(e, sign)) {
e->y += sign;
dy -= sign;
}
return true;
}

e->y += e->vy;
return false;
}

int flags = level_collision_at(e, 0, GRAVITY);
bool apply_gravity(ent_t *e) {
int flags = ent_level_collision_at(e, 0, GRAVITY);

se_mem[30][1] = flags;
if(flags & (LEVEL_COL_GROUND)) {
_player.vy = 0;
return true;
}

// if(flags & (LEVEL_COL_GROUND)) {
// _player.vy = 0;
// } else {
// _player.vy += GRAVITY;
// }
_player.vy += GRAVITY;
return false;
}
27 changes: 22 additions & 5 deletions source/ent.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "level.h"

#include <tonc.h>
#include <tonc_types.h>

#ifdef ISMAIN
#define def
Expand Down Expand Up @@ -40,15 +40,32 @@ def ent_t _player;
FIXED translate_x(ent_t *e);
FIXED translate_y(ent_t *e);

inline int level_collision_at(ent_t *e, FIXED vx, FIXED vy) {
inline int ent_level_collision(ent_t *e) {
return level_collision_rect(
(translate_x(e) + vx) * FIX_SCALE,
(translate_y(e) + vy) * FIX_SCALE,
translate_x(e) / FIX_SCALE,
translate_y(e) / FIX_SCALE,
e->w,
e->h
);
}

inline int ent_level_collision_at(ent_t *e, FIXED vx, FIXED vy) {
return level_collision_rect(
(translate_x(e) + vx) / FIX_SCALE,
(translate_y(e) + vy) / FIX_SCALE,
e->w,
e->h
);
}

void apply_gravity(ent_t *e);
void push_up_from_ground(ent_t *e);

bool did_hit_x(ent_t *e, FIXED dx);
bool ent_move_x(ent_t *e, FIXED dx);

bool did_hit_y(ent_t *e, FIXED dy);
bool ent_move_y(ent_t *e, FIXED dy);

bool apply_gravity(ent_t *e);

#endif
4 changes: 2 additions & 2 deletions source/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ int tile_to_collision(u16 tile) {

int level_collision_rect(int x, int y, int w, int h) {
int tile_left = x / TILE_WIDTH;
int tile_right = tile_left + w / TILE_WIDTH - 1;
int tile_right = tile_left + w / TILE_WIDTH;
int tile_top = y / TILE_WIDTH;
int tile_bot = tile_top + h / TILE_WIDTH - 1;
int tile_bot = tile_top + h / TILE_WIDTH;

int res = 0;

Expand Down
45 changes: 26 additions & 19 deletions source/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <tonc.h>
#include "common.h"
#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 int GROUND_Y = (int)(120.0f * (FIX_SCALE));

ent_t _player = {};
static OBJ_ATTR* _player_obj = &_obj_buffer[0];
Expand All @@ -20,7 +20,7 @@ void init_player() {
_player.h = 16;

_player.x = 20 << FIX_SHIFT;
_player.y = GROUND_Y;
_player.y = PLAYER_SPAWN_Y;

obj_set_attr(_player_obj,
ATTR0_SQUARE, ATTR1_SIZE_16x16,
Expand All @@ -43,17 +43,28 @@ void update_player() {
_player.vx = SPEED;
}

_player.vx += -_scroll_x;

//Push player backwards in wind
if(_player.facing == FACING_LEFT) {
_player.vx += -_scroll_x;
}

if(fx2int(_player.x) > 220) {
_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);

if(!hit_y) {
if(_player.vy < TERMINAL_VY) {
_player.vy += GRAVITY;
}
}

switch (_player.move_state)
{
case MOVEMENT_GROUNDED:
Expand All @@ -63,19 +74,15 @@ void update_player() {
}
break;
case MOVEMENT_AIR:
// if(_player.y >= GROUND_Y) {
// _player.y = GROUND_Y;
// _player.vy = 0;
// _player.move_state = MOVEMENT_GROUNDED;
// }
if(hit_y) {
_player.move_state = MOVEMENT_GROUNDED;
}
break;
}

apply_gravity(&_player);
_player.x += _player.vx;
_player.y += _player.vy;

_player.vx = 0;
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);
Expand Down
2 changes: 2 additions & 0 deletions source/player.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef PLAYER_H
#define PLAYER_H

#define PLAYER_SPAWN_Y 0

void init_player();

void update_player();
Expand Down
9 changes: 6 additions & 3 deletions source/scenes/main_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static void spawn_buildings() {
width = spawn_building_0(start_x);
}

width += gba_rand_range(2, MAX_JUMP_WIDTH_TILES);
width += gba_rand_range(3, MAX_JUMP_WIDTH_TILES);

_building_spawn_x = level_wrap_x(start_x + width);

Expand Down Expand Up @@ -232,8 +232,11 @@ static void update(void) {
}

if(key_hit(KEY_B)) {
// _scroll_x += (int)(0.25f * FIX_SCALE);
write_to_log(LOG_LEVEL_INFO, "test\0");
if(_scroll_x == 0) {
_scroll_x = (int)(0.25f * FIX_SCALE);
} else {
_scroll_x = 0;
}
}

for(int x = 0; x < LEVEL_WIDTH; x++) {
Expand Down

0 comments on commit 3a3b8ee

Please sign in to comment.