From 7664fdafb9a6d6f4aa3339fe38958b24b234218e Mon Sep 17 00:00:00 2001 From: quou Date: Mon, 30 Sep 2024 19:01:53 +1000 Subject: player slashing --- 1bitjam.c | 16 +++++++--------- Makefile | 7 ++++++- animation.c | 8 ++++++-- animation.h | 2 +- intermediate/arms.bmp | Bin 0 -> 262282 bytes intermediate/slash_left.anm | 6 ++++++ intermediate/slash_right.anm | 6 ++++++ obj.h | 20 +++++++++++++++++--- particle.c | 28 ++++++++++++++++++++++++++++ player.c | 31 +++++++++++++++++++++++++++---- world.c | 37 +++++++++++++++++++++++++++++++++++++ world.h | 29 +++++++++++++++++++++++++++++ 12 files changed, 170 insertions(+), 20 deletions(-) create mode 100644 intermediate/arms.bmp create mode 100644 intermediate/slash_left.anm create mode 100644 intermediate/slash_right.anm create mode 100644 particle.c create mode 100644 world.c create mode 100644 world.h diff --git a/1bitjam.c b/1bitjam.c index 619876e..8c63aa5 100644 --- a/1bitjam.c +++ b/1bitjam.c @@ -1,20 +1,18 @@ #include "asset.h" #include "config.h" -#include "map.h" #include "maths.h" #include "memory.h" -#include "obj.h" #include "plat.h" #include "rect.h" #include "render.h" +#include "world.h" int entrypoint(int argc, const char** argv, Arena* m) { Heap h; App* a; FPS f; Renderer r; - Player player; - Map map; + World* world; (void)argc; (void)argv; init_maths(); @@ -26,19 +24,19 @@ int entrypoint(int argc, const char** argv, Arena* m) { a = new_app(&h, game_name); init_audio(); init_fps(&f, default_mpf); - generate_floor(&map, 0); - init_player(&player); + world = arena_alloc(m, sizeof world); + init_world(world); + generate_floor(&world->map, 0); while (a->o) { fps_begin(&f); while (f.now >= f.next && a->o) { app_begin(a); - update_player(&player, a, &map); + update_world(world, a); ren_begin(&r, a->fb, viewport_w, viewport_h); ren_clear(&r); - render_map(&map, &r); - ren_player(&player, &r); + ren_world(world, &r); ren_end(&r); app_end(a); fps_update(&f); diff --git a/Makefile b/Makefile index 4c755b9..74ad6e3 100644 --- a/Makefile +++ b/Makefile @@ -49,16 +49,19 @@ sources = \ map.c \ maths.c \ memory.c \ + particle.c \ physics.c \ plat.c \ player.c \ rect.c \ render.c \ + world.c \ image_sources = \ + $(int_dir)/arms.bmp \ $(int_dir)/guy.bmp \ $(int_dir)/map.bmp \ - $(int_dir)/mask.bmp + $(int_dir)/mask.bmp \ anim_sources = \ $(int_dir)/guy_fall_left.anm \ @@ -69,6 +72,8 @@ anim_sources = \ $(int_dir)/guy_jump_right.anm \ $(int_dir)/guy_run_left.anm \ $(int_dir)/guy_run_right.anm \ + $(int_dir)/slash_right.anm \ + $(int_dir)/slash_left.anm \ objects = $(sources:%.c=%.o) images = $(image_sources:$(int_dir)/%.bmp=$(data_dir)/%.img) diff --git a/animation.c b/animation.c index 388e29d..2010301 100644 --- a/animation.c +++ b/animation.c @@ -1,9 +1,13 @@ #include "animation.h" #include "rect.h" -void update_anim(const Animation* a, int* f, Rect* r) { +int update_anim(const Animation* a, int* f, Rect* r) { + int done = 0; f[0]++; - if (f[0] >= a->fc * a->s) + if (f[0] >= a->fc * a->s) { f[0] = 0; + done = 1; + } *r = ((const Rect*)&a[1])[f[0] / a->s]; + return done; } diff --git a/animation.h b/animation.h index 983fed8..e894396 100644 --- a/animation.h +++ b/animation.h @@ -7,6 +7,6 @@ typedef struct Animation { int fc, s; } Animation; -void update_anim(const Animation* a, int* f, struct Rect* r); +int update_anim(const Animation* a, int* f, struct Rect* r); #endif diff --git a/intermediate/arms.bmp b/intermediate/arms.bmp new file mode 100644 index 0000000..0b686f3 Binary files /dev/null and b/intermediate/arms.bmp differ diff --git a/intermediate/slash_left.anm b/intermediate/slash_left.anm new file mode 100644 index 0000000..b95a86d --- /dev/null +++ b/intermediate/slash_left.anm @@ -0,0 +1,6 @@ +1 +{ 0, 17, 45, 17 } +{ 45, 17, 45, 17 } +{ 90, 17, 45, 17 } +{ 135, 17, 45, 17 } +{ 180, 17, 45, 17 } diff --git a/intermediate/slash_right.anm b/intermediate/slash_right.anm new file mode 100644 index 0000000..9e00005 --- /dev/null +++ b/intermediate/slash_right.anm @@ -0,0 +1,6 @@ +1 +{ 0, 0, 45, 17 } +{ 45, 0, 45, 17 } +{ 90, 0, 45, 17 } +{ 135, 0, 45, 17 } +{ 180, 0, 45, 17 } diff --git a/obj.h b/obj.h index 9a331b7..7f93ecc 100644 --- a/obj.h +++ b/obj.h @@ -4,8 +4,9 @@ #include "rect.h" struct App; -struct Renderer; struct Map; +struct Renderer; +struct World; typedef enum { face_left, @@ -17,6 +18,7 @@ typedef struct { int frame; int anim; int grounded, headbutted, on_ramp, jumping; + int cooldown; Face face; Rect rect; } Player; @@ -24,9 +26,21 @@ typedef struct { void init_player(Player* p); void update_player( Player* p, - struct App* app, + struct World* w, + const struct App* app, const struct Map* map ); -void ren_player(Player* p, struct Renderer* r); +void ren_player(const Player* p, struct Renderer* r); + +typedef struct { + int x, y; + int frame, anim; + int bmp; + Rect rect; +} Particle; + +void init_particle(Particle* p, int x, int y, int anim, int bmp); +int update_particle(Particle* p); +void ren_particle(const Particle* p, struct Renderer* r); #endif diff --git a/particle.c b/particle.c new file mode 100644 index 0000000..c10e3aa --- /dev/null +++ b/particle.c @@ -0,0 +1,28 @@ +#include "animation.h" +#include "asset.h" +#include "obj.h" +#include "render.h" + +void init_particle(Particle* p, int x, int y, int anim, int bmp) { + p->x = x; + p->y = y; + p->frame = 0; + p->anim = anim; + p->bmp = bmp; +} + +int update_particle(Particle* p) { + const Animation* a = get_animation(p->anim); + return update_anim(a, &p->frame, &p->rect); +} + +void ren_particle(const Particle* p, Renderer* r) { + const Bitmap* b = get_bitmap(p->bmp); + ren_map( + r, + p->x, + p->y, + &p->rect, + b + ); +} diff --git a/player.c b/player.c index a99f40b..77d0b01 100644 --- a/player.c +++ b/player.c @@ -6,6 +6,7 @@ #include "physics.h" #include "plat.h" #include "render.h" +#include "world.h" void init_player(Player* p) { p->anim = asset_id_guy_run_right_anm; @@ -18,6 +19,7 @@ void init_player(Player* p) { p->headbutted = 0; p->on_ramp = 0; p->jumping = 0; + p->cooldown = 0; p->face = face_right; } @@ -46,12 +48,13 @@ void update_player_phys(Player* p, const Map* map) { ); } -void update_player_move(Player* p, const App* a) { +void update_player_move(Player* p, const App* a, World* w) { int grounded = p->grounded < 3; int mf = player_move_force; int nanim, moving, t; int jumping = p->jumping; Face* face = &p->face; + p->cooldown--; if (!grounded) { mf = player_air_move_force; } @@ -89,6 +92,21 @@ void update_player_move(Player* p, const App* a) { p->jumping = 1; play_sound(500); } + if (p->cooldown <= 0 && btn_just_pressed(a, btn_shoot)) { + int sx, sy; + int anim; + if (p->face == face_left) { + anim = asset_id_slash_left_anm; + sx = (p->x >> fbits) - 45; + sy = (p->y >> fbits); + } else { + anim = asset_id_slash_right_anm; + sx = (p->x >> fbits) + 16; + sy = (p->y >> fbits); + } + inst_particle(w, sx, sy, anim, asset_id_arms_img); + p->cooldown = 10; + } jumping = p->jumping; nanim = p->anim; switch (p->face) { @@ -120,13 +138,18 @@ void update_player_move(Player* p, const App* a) { } } -void update_player(Player* p, App* app, const Map* map) { - update_player_move(p, app); +void update_player( + Player* p, + World* w, + const App* app, + const Map* map +) { + update_player_move(p, app, w); update_player_phys(p, map); update_player_anim(p); } -void ren_player(Player* p, struct Renderer* r) { +void ren_player(const Player* p, struct Renderer* r) { const Bitmap* b = get_bitmap(asset_id_guy_img); ren_map(r, p->x >> fbits, p->y >> fbits, &p->rect, b); } diff --git a/world.c b/world.c new file mode 100644 index 0000000..e7777fd --- /dev/null +++ b/world.c @@ -0,0 +1,37 @@ +#include "plat.h" +#include "world.h" + +void init_world(World* w) { + w->particle_count = 0; + init_player(&w->player); +} + +Particle* inst_particle( + World* w, + int x, + int y, + int anim, + int bmp +) { + Particle* p; + assert(w->particle_count < max_particles); + p = &w->particles[w->particle_count++]; + init_particle(p, x, y, anim, bmp); + return p; +} + +void update_world(World* w, const App* a) { + int i; + update_player(&w->player, w, a, &w->map); + for (i = w->particle_count - 1; i >= 0; i--) + if (update_particle(&w->particles[i])) + w->particles[i] = w->particles[--w->particle_count]; +} + +void ren_world(const World* w, struct Renderer* r) { + int i; + ren_player(&w->player, r); + render_map(&w->map, r); + for (i = 0; i < w->particle_count; i++) + ren_particle(&w->particles[i], r); +} diff --git a/world.h b/world.h new file mode 100644 index 0000000..bd3df31 --- /dev/null +++ b/world.h @@ -0,0 +1,29 @@ +#ifndef world_h +#define world_h + +#include "map.h" +#include "obj.h" + +#define max_particles 32 + +struct Renderer; + +typedef struct World { + Particle particles[max_particles]; + int particle_count; + Player player; + Map map; +} World; + +void init_world(World* w); +Particle* inst_particle( + World* w, + int x, + int y, + int anim, + int bmp +); +void update_world(World* w, const App* a); +void ren_world(const World* w, struct Renderer* r); + +#endif -- cgit v1.2.3-54-g00ecf