Collectibles 1

This commit is contained in:
Pyrex 2024-02-28 16:10:09 -08:00
parent d084e4dba3
commit 7649a2dc7e
13 changed files with 2824 additions and 325 deletions

View File

@ -1,8 +1,10 @@
load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
load("helpers.bzl", "add_sprites")
cc_library(
name = "game",
srcs = glob(["*.c"]) + [
"art/game_collectibles.c",
"art/game_player.c",
"art/game_tiles.c",
"map/game_map.c",
@ -13,33 +15,9 @@ cc_library(
deps = ["//device:device", "//sys:sys"]
)
run_binary(
name = "game_player",
args = [
"game_player",
"96", # n sprites
"0", # key color
"$(location :art/game_player.png)",
"$(location :art/game_player.c)"
],
srcs = [":art/game_player.png"],
outs = [":art/game_player.c"],
tool = "//pytools:spritesheet",
)
run_binary(
name = "game_tiles",
args = [
"game_tiles",
"120", # n sprites
"0", # key color
"$(location :art/game_tiles.png)",
"$(location :art/game_tiles.c)"
],
srcs = [":art/game_tiles.png"],
outs = [":art/game_tiles.c"],
tool = "//pytools:spritesheet",
)
add_sprites(name="game_collectibles", n=24)
add_sprites(name="game_player", n=96)
add_sprites(name="game_tiles", n=120)
run_binary(
name = "game_map",

Binary file not shown.

View File

@ -0,0 +1,8 @@
#ifndef GAME_COLLECTIBLES_H
#define GAME_COLLECTIBLES_H
#include "sys/sys.h"
extern sys_spritesheet spr_game_collectibles;
#endif // GAME_COLLECTIBLES_H

Binary file not shown.

After

Width:  |  Height:  |  Size: 419 B

View File

@ -15,8 +15,11 @@ const char* game_title() {
void game_init() {
sys_init();
game_collectibles_preinit();
map_game_map_create_entities();
game_collectibles_init();
game_palette_init();
game_player_init();
}
@ -29,6 +32,7 @@ void game_update() {
sys_update();
game_frame += 1;
game_collectibles_update();
game_player_update();
}
@ -38,6 +42,8 @@ void game_draw() {
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_collectibles_draw();
game_player_draw();
sys_camera_reset();

View File

@ -1,7 +1,9 @@
#ifndef CROCPARTY_GAME_H
#define CROCPARTY_GAME_H
#include "game_collectible.h"
#include "game_collision.h"
#include "game_math.h"
#include "game_palette.h"
#include "game_player.h"

57
game/game_collectible.c Normal file
View File

@ -0,0 +1,57 @@
#include <assert.h>
#include "game.h"
#include "art/game_collectibles.h"
// If this isn't enough, raise the number
#define GAME_COLLECTIBLES_N 32
sys_i32 game_collectible_next = 0;
game_collectible game_collectibles[GAME_COLLECTIBLES_N];
sys_i32 game_collectible_wobb_frame = 0;
void game_collectibles_preinit() {
}
void game_collectible_create(sys_i32 x, sys_i32 y, game_collectible_type type) {
assert(game_collectible_next < GAME_COLLECTIBLES_N && "too many collectibles");
sys_i32 id = game_collectible_next++;
game_collectibles[id].x = x;
game_collectibles[id].y = y;
game_collectibles[id].type = type;
}
void game_collectibles_init() {
}
void game_collectibles_update() {
game_collectible_wobb_frame += 1;
}
void game_collectibles_draw() {
for (sys_i32 i = 0; i < game_collectible_next; i++) {
game_collectible c = game_collectibles[i];
sys_i32 x = c.x / PIXEL_SZ_MICROPIXEL;
sys_i32 y = c.y / PIXEL_SZ_MICROPIXEL;
y += game_wobb3(game_collectible_wobb_frame & 0xff);
switch(c.type) {
case GAME_COLLECTIBLE_TYPE_CAKE:
sys_sprite_draw_ext(
spr_game_collectibles,0,x-16,y-16,4,4,false,false);
break;
case GAME_COLLECTIBLE_TYPE_MONEY_BIG:
sys_sprite_draw_ext(
spr_game_collectibles,4,x-8,y-8,2,2,false,false);
break;
case GAME_COLLECTIBLE_TYPE_MONEY_SMALL:
sys_sprite_draw_ext(
spr_game_collectibles,16,x-8,y-8,2,2,false,false);
break;
default:
assert(false&&"invalid collectible");
}
}
}

26
game/game_collectible.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef GAME_COLLECTIBLE_H
#define GAME_COLLECTIBLE_H
#include "sys/sys.h"
typedef enum {
GAME_COLLECTIBLE_TYPE_CAKE,
GAME_COLLECTIBLE_TYPE_MONEY_BIG,
GAME_COLLECTIBLE_TYPE_MONEY_SMALL,
GAME_COLLECTIBLE_TYPE_N
} game_collectible_type;
typedef struct {
sys_i32 x;
sys_i32 y;
game_collectible_type type;
} game_collectible;
void game_collectibles_preinit();
void game_collectible_create(sys_i32 x, sys_i32 y, game_collectible_type type);
void game_collectibles_init();
void game_collectibles_update();
void game_collectibles_draw();
#endif // GAME_COLLECTIBLE_H

36
game/game_math.c Normal file
View File

@ -0,0 +1,36 @@
#include "game_math.h"
#include "sys/sys.h"
// prog runs from 0 to 255
// except for game_wobb1, all of these are
// generated with list(enumerate([round(sin(i/256 * pi * 2)*k) for i in range(256)]))
// for k = various values
sys_i32 game_wobb1(uint8_t prog) {
// return 0 or 1 in a roughly sinusoidal pattern
if (prog<128) return 0;
return 1;
}
sys_i32 game_wobb2(uint8_t prog) {
// return -1, 0, 1 in a roughly sinusoidal pattern
if (prog<22) return 0;
if (prog<107) return 1;
if (prog<150) return 0;
if (prog<235) return -1;
return 0;
}
sys_i32 game_wobb3(uint8_t prog) {
// return -2, -1, 0, 1, 2 in a roughly sinusoidal pattern
if (prog<11) return 0;
if (prog<35) return 1;
if (prog<94) return 2;
if (prog<118) return 1;
if (prog<139) return 0;
if (prog<163) return -1;
if (prog<222) return -2;
if (prog<246) return -1;
return 0;
}

11
game/game_math.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef GAME_MATH_H
#define GAME_MATH_H
#include <stdint.h>
#include "sys/sys.h"
sys_i32 game_wobb1(uint8_t prog);
sys_i32 game_wobb2(uint8_t prog);
sys_i32 game_wobb3(uint8_t prog);
#endif // GAME_MATH_H

16
game/helpers.bzl Normal file
View File

@ -0,0 +1,16 @@
load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
def add_sprites(name, n):
run_binary(
name = name,
args = [
name,
"{}".format(n), # n sprites
"0", # key color
"$(location :art/{}.png)".format(name),
"$(location :art/{}.c)".format(name)
],
srcs = [":art/{}.png".format(name)],
outs = [":art/{}.c".format(name)],
tool = "//pytools:spritesheet",
)

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,5 @@
#include "sys/sys.h"
#include "game/game.h"
#include "game_map.h"
sys_i32 game_map_player_start_x;
@ -10,3 +11,27 @@ void map_game_map_player_spawn_create(sys_i32 x, sys_i32 y) {
game_map_player_start_x = x * TILE_SZ_MICROPIXEL + PIXEL_SZ_MICROPIXEL * 8;
game_map_player_start_y = y * TILE_SZ_MICROPIXEL + PIXEL_SZ_MICROPIXEL * 16;
}
void map_game_map_collectible_cake_create(sys_i32 x, sys_i32 y) {
game_collectible_create(
(x + 2) * TILE_SZ_MICROPIXEL,
(y + 2) * TILE_SZ_MICROPIXEL,
GAME_COLLECTIBLE_TYPE_CAKE
);
}
void map_game_map_collectible_money_big_create(sys_i32 x, sys_i32 y) {
game_collectible_create(
(x + 1) * TILE_SZ_MICROPIXEL,
(y + 1) * TILE_SZ_MICROPIXEL,
GAME_COLLECTIBLE_TYPE_MONEY_BIG
);
}
void map_game_map_collectible_money_small_create(sys_i32 x, sys_i32 y) {
game_collectible_create(
(x + 1) * TILE_SZ_MICROPIXEL,
(y + 1) * TILE_SZ_MICROPIXEL,
GAME_COLLECTIBLE_TYPE_MONEY_SMALL
);
}