Bounding boxes: origins
This commit is contained in:
parent
b0f4106d51
commit
d6db2f3e5f
@ -34,6 +34,9 @@ void game_update() {
|
|||||||
void game_draw() {
|
void game_draw() {
|
||||||
sys_cls(9);
|
sys_cls(9);
|
||||||
|
|
||||||
sys_map_draw(map_game_map, spr_game_tiles, 0, 0, 0, 0, 32, 18);
|
game_player_set_camera();
|
||||||
|
sys_map_draw(map_game_map, spr_game_tiles, 0, 0, 0, 0, map_game_map.width, map_game_map.height);
|
||||||
game_player_draw();
|
game_player_draw();
|
||||||
|
|
||||||
|
sys_camera_reset();
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,8 @@ bool game_collision_is_occlusive(sys_maptile tile) {
|
|||||||
|
|
||||||
bool game_collision_can_move(game_bbox body, sys_i32 dx, sys_i32 dy) {
|
bool game_collision_can_move(game_bbox body, sys_i32 dx, sys_i32 dy) {
|
||||||
// First: place the body in space.
|
// First: place the body in space.
|
||||||
// Align its top-left corner to the pixel boundary
|
sys_i32 x = body.x - body.x_origin + dx;
|
||||||
sys_i32 x = body.x + dx;
|
sys_i32 y = body.y - body.y_origin + dy;
|
||||||
sys_i32 y = body.y + dy;
|
|
||||||
|
|
||||||
// this is extremely similar to the logic in move_to_contact
|
// this is extremely similar to the logic in move_to_contact
|
||||||
// but has different rounding behavior
|
// but has different rounding behavior
|
||||||
@ -80,10 +79,10 @@ game_collision game_collision_move_to_contact(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// figure out where we are
|
// figure out where we are
|
||||||
sys_i32 body_x0 = body->x;
|
sys_i32 body_x0 = body->x - body->x_origin;
|
||||||
sys_i32 body_y0 = body->y;
|
sys_i32 body_y0 = body->y - body->y_origin;
|
||||||
sys_i32 body_x1 = body->x + body->w;
|
sys_i32 body_x1 = body_x0 + body->w;
|
||||||
sys_i32 body_y1 = body->y + body->h;
|
sys_i32 body_y1 = body_y0 + body->h;
|
||||||
|
|
||||||
// figure out what tile we're still in
|
// figure out what tile we're still in
|
||||||
sys_i32 tile_x0 = round_down(body_x0, TILE_SZ_MICROPIXEL);
|
sys_i32 tile_x0 = round_down(body_x0, TILE_SZ_MICROPIXEL);
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
#define PIXEL_SZ_MICROPIXEL 0x100
|
#define PIXEL_SZ_MICROPIXEL 0x100
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
sys_i32 x_origin;
|
||||||
|
sys_i32 y_origin;
|
||||||
sys_i32 x;
|
sys_i32 x;
|
||||||
sys_i32 y;
|
sys_i32 y;
|
||||||
sys_i32 w;
|
sys_i32 w;
|
||||||
|
@ -5,13 +5,11 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "sys/sys.h"
|
#include "sys/sys.h"
|
||||||
|
|
||||||
#define GAME_PLAYER_ORIGIN_X 3
|
|
||||||
#define GAME_PLAYER_ORIGIN_Y 4
|
|
||||||
|
|
||||||
game_bbox game_player_bbox = {
|
game_bbox game_player_bbox = {
|
||||||
|
.x_origin=0x0500,
|
||||||
|
.y_origin=0x0c00,
|
||||||
.x=0x9000,
|
.x=0x9000,
|
||||||
// .y=0x5800,
|
.y=0x1000,
|
||||||
.y=0x0000,
|
|
||||||
.w=0x0a00,
|
.w=0x0a00,
|
||||||
.h=0x0c00,
|
.h=0x0c00,
|
||||||
};
|
};
|
||||||
@ -129,6 +127,20 @@ void game_player_update() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void game_player_set_camera() {
|
||||||
|
sys_i32 x = game_player_bbox.x;
|
||||||
|
sys_i32 y = game_player_bbox.y;
|
||||||
|
|
||||||
|
// TODO: Use round_down et al
|
||||||
|
sys_i32 x_pixel = x / PIXEL_SZ_MICROPIXEL;
|
||||||
|
sys_i32 y_pixel = y / PIXEL_SZ_MICROPIXEL;
|
||||||
|
|
||||||
|
sys_i32 x_topcorner = x_pixel - x_pixel % DEVICE_W;
|
||||||
|
sys_i32 y_topcorner = y_pixel - y_pixel % DEVICE_H;
|
||||||
|
|
||||||
|
sys_camera_set(x_topcorner, y_topcorner);
|
||||||
|
}
|
||||||
|
|
||||||
void game_player_draw() {
|
void game_player_draw() {
|
||||||
int game_player_image;
|
int game_player_image;
|
||||||
switch (game_player_anim_state) {
|
switch (game_player_anim_state) {
|
||||||
@ -147,8 +159,8 @@ void game_player_draw() {
|
|||||||
sys_sprite_draw_ext(
|
sys_sprite_draw_ext(
|
||||||
spr_game_player,
|
spr_game_player,
|
||||||
game_player_image,
|
game_player_image,
|
||||||
game_player_bbox.x / 0x100 - GAME_PLAYER_ORIGIN_X,
|
game_player_bbox.x / 0x100 - 8,
|
||||||
game_player_bbox.y / 0x100 - GAME_PLAYER_ORIGIN_Y,
|
game_player_bbox.y / 0x100 - 16,
|
||||||
2, 2,
|
2, 2,
|
||||||
game_player_faceleft, false
|
game_player_faceleft, false
|
||||||
);
|
);
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
void game_player_init();
|
void game_player_init();
|
||||||
void game_player_update();
|
void game_player_update();
|
||||||
|
void game_player_set_camera();
|
||||||
void game_player_draw();
|
void game_player_draw();
|
||||||
|
|
||||||
#endif // GAME_PLAYER_H
|
#endif // GAME_PLAYER_H
|
@ -3815,7 +3815,7 @@
|
|||||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,
|
||||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,
|
||||||
0,0,0,0,0,0,0,0,0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,
|
0,0,0,0,0,0,0,0,0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,
|
||||||
0,0,0,0,0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
|
0,0,3,0,0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
|
||||||
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
|
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
|
||||||
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
|
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
|
||||||
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
|
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
|
||||||
@ -4053,6 +4053,7 @@
|
|||||||
{ "px": [120,72], "src": [8,104], "f": 0, "t": 79, "d": [77,303], "a": 1 },
|
{ "px": [120,72], "src": [8,104], "f": 0, "t": 79, "d": [77,303], "a": 1 },
|
||||||
{ "px": [144,80], "src": [8,104], "f": 0, "t": 79, "d": [77,338], "a": 1 },
|
{ "px": [144,80], "src": [8,104], "f": 0, "t": 79, "d": [77,338], "a": 1 },
|
||||||
{ "px": [168,88], "src": [8,104], "f": 0, "t": 79, "d": [77,373], "a": 1 },
|
{ "px": [168,88], "src": [8,104], "f": 0, "t": 79, "d": [77,373], "a": 1 },
|
||||||
|
{ "px": [0,96], "src": [8,104], "f": 0, "t": 79, "d": [77,384], "a": 1 },
|
||||||
{ "px": [88,64], "src": [0,104], "f": 0, "t": 78, "d": [76,267], "a": 1 },
|
{ "px": [88,64], "src": [0,104], "f": 0, "t": 78, "d": [76,267], "a": 1 },
|
||||||
{ "px": [48,80], "src": [0,104], "f": 0, "t": 78, "d": [76,326], "a": 1 },
|
{ "px": [48,80], "src": [0,104], "f": 0, "t": 78, "d": [76,326], "a": 1 },
|
||||||
{ "px": [32,88], "src": [0,104], "f": 0, "t": 78, "d": [76,356], "a": 1 },
|
{ "px": [32,88], "src": [0,104], "f": 0, "t": 78, "d": [76,356], "a": 1 },
|
||||||
@ -4081,12 +4082,12 @@
|
|||||||
{ "px": [232,88], "src": [8,80], "f": 0, "t": 61, "d": [72,381], "a": 1 },
|
{ "px": [232,88], "src": [8,80], "f": 0, "t": 61, "d": [72,381], "a": 1 },
|
||||||
{ "px": [240,88], "src": [8,80], "f": 0, "t": 61, "d": [72,382], "a": 1 },
|
{ "px": [240,88], "src": [8,80], "f": 0, "t": 61, "d": [72,382], "a": 1 },
|
||||||
{ "px": [248,88], "src": [8,80], "f": 0, "t": 61, "d": [72,383], "a": 1 },
|
{ "px": [248,88], "src": [8,80], "f": 0, "t": 61, "d": [72,383], "a": 1 },
|
||||||
{ "px": [0,96], "src": [8,80], "f": 0, "t": 61, "d": [72,384], "a": 1 },
|
|
||||||
{ "px": [8,96], "src": [8,80], "f": 0, "t": 61, "d": [72,385], "a": 1 },
|
{ "px": [8,96], "src": [8,80], "f": 0, "t": 61, "d": [72,385], "a": 1 },
|
||||||
{ "px": [16,96], "src": [8,80], "f": 0, "t": 61, "d": [72,386], "a": 1 },
|
{ "px": [16,96], "src": [8,80], "f": 0, "t": 61, "d": [72,386], "a": 1 },
|
||||||
{ "px": [120,56], "src": [16,80], "f": 0, "t": 62, "d": [69,239], "a": 1 },
|
{ "px": [120,56], "src": [16,80], "f": 0, "t": 62, "d": [69,239], "a": 1 },
|
||||||
{ "px": [144,72], "src": [16,80], "f": 0, "t": 62, "d": [69,306], "a": 1 },
|
{ "px": [144,72], "src": [16,80], "f": 0, "t": 62, "d": [69,306], "a": 1 },
|
||||||
{ "px": [168,80], "src": [16,80], "f": 0, "t": 62, "d": [69,341], "a": 1 },
|
{ "px": [168,80], "src": [16,80], "f": 0, "t": 62, "d": [69,341], "a": 1 },
|
||||||
|
{ "px": [0,88], "src": [16,80], "f": 0, "t": 62, "d": [69,352], "a": 1 },
|
||||||
{ "px": [88,56], "src": [0,80], "f": 0, "t": 60, "d": [68,235], "a": 1 },
|
{ "px": [88,56], "src": [0,80], "f": 0, "t": 60, "d": [68,235], "a": 1 },
|
||||||
{ "px": [48,64], "src": [0,80], "f": 0, "t": 60, "d": [68,262], "a": 1 },
|
{ "px": [48,64], "src": [0,80], "f": 0, "t": 60, "d": [68,262], "a": 1 },
|
||||||
{ "px": [32,80], "src": [0,80], "f": 0, "t": 60, "d": [68,324], "a": 1 },
|
{ "px": [32,80], "src": [0,80], "f": 0, "t": 60, "d": [68,324], "a": 1 },
|
||||||
|
@ -58,6 +58,9 @@ sys_color sys_pixel_get(sys_i32 x, sys_i32 y) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void sys_print(char* str, sys_i32 x, sys_i32 y, sys_color col) {
|
void sys_print(char* str, sys_i32 x, sys_i32 y, sys_color col) {
|
||||||
|
x += sys_cam_dx;
|
||||||
|
y += sys_cam_dy;
|
||||||
|
|
||||||
sys_i32 x_orig = x;
|
sys_i32 x_orig = x;
|
||||||
for (sys_i32 i = 0;; i++) {
|
for (sys_i32 i = 0;; i++) {
|
||||||
uint8_t c = str[i];
|
uint8_t c = str[i];
|
||||||
@ -135,6 +138,11 @@ void sys_oval_fill(
|
|||||||
void sys_oval_draw_ext(
|
void sys_oval_draw_ext(
|
||||||
sys_i32 x0, sys_i32 y0, sys_i32 x1, sys_i32 y1, sys_color c, bool fill
|
sys_i32 x0, sys_i32 y0, sys_i32 x1, sys_i32 y1, sys_color c, bool fill
|
||||||
) {
|
) {
|
||||||
|
x0 += sys_cam_dx;
|
||||||
|
y0 += sys_cam_dy;
|
||||||
|
x1 += sys_cam_dx;
|
||||||
|
y1 += sys_cam_dy;
|
||||||
|
|
||||||
assert(sys_get_initialized());
|
assert(sys_get_initialized());
|
||||||
|
|
||||||
if (x0 == x1 || y0 == y1) { return; }
|
if (x0 == x1 || y0 == y1) { return; }
|
||||||
@ -186,6 +194,11 @@ void sys_oval_draw_ext(
|
|||||||
void sys_line_draw(
|
void sys_line_draw(
|
||||||
sys_i32 x0, sys_i32 y0, sys_i32 x1, sys_i32 y1, sys_color c
|
sys_i32 x0, sys_i32 y0, sys_i32 x1, sys_i32 y1, sys_color c
|
||||||
) {
|
) {
|
||||||
|
x0 += sys_cam_dx;
|
||||||
|
y0 += sys_cam_dy;
|
||||||
|
x1 += sys_cam_dx;
|
||||||
|
y1 += sys_cam_dy;
|
||||||
|
|
||||||
assert(sys_get_initialized());
|
assert(sys_get_initialized());
|
||||||
|
|
||||||
if (x0 == x1 || y0 == y1) { return; }
|
if (x0 == x1 || y0 == y1) { return; }
|
||||||
@ -254,6 +267,9 @@ void sys_sprite_draw_ext(
|
|||||||
sys_i32 w, sys_i32 h,
|
sys_i32 w, sys_i32 h,
|
||||||
bool flip_x, bool flip_y
|
bool flip_x, bool flip_y
|
||||||
) {
|
) {
|
||||||
|
x += sys_cam_dx;
|
||||||
|
y += sys_cam_dy;
|
||||||
|
|
||||||
// map n to a specific entity on the spritesheet
|
// map n to a specific entity on the spritesheet
|
||||||
// (this is necessary for w and h)
|
// (this is necessary for w and h)
|
||||||
for (int sy = 0; sy < h; sy++) {
|
for (int sy = 0; sy < h; sy++) {
|
||||||
@ -283,6 +299,10 @@ void sys_map_draw(
|
|||||||
sys_i32 tile_x, sys_i32 tile_y,
|
sys_i32 tile_x, sys_i32 tile_y,
|
||||||
sys_i32 tile_w, sys_i32 tile_h
|
sys_i32 tile_w, sys_i32 tile_h
|
||||||
) {
|
) {
|
||||||
|
// no need to do this, sys_sprite_draw does it
|
||||||
|
// sx += sys_cam_dx;
|
||||||
|
// sy += sys_cam_dy;
|
||||||
|
|
||||||
for (sys_i32 ty = 0; ty < tile_h; ty++) {
|
for (sys_i32 ty = 0; ty < tile_h; ty++) {
|
||||||
for (sys_i32 tx = 0; tx < tile_w; tx++) {
|
for (sys_i32 tx = 0; tx < tile_w; tx++) {
|
||||||
sys_i32 real_tx = tx + tile_x;
|
sys_i32 real_tx = tx + tile_x;
|
||||||
|
Loading…
Reference in New Issue
Block a user