diff options
author | quou <quou@disroot.org> | 2023-05-03 07:32:34 +1000 |
---|---|---|
committer | quou <quou@disroot.org> | 2023-05-03 07:32:34 +1000 |
commit | 199a5e6e9ddb13af0bb557b6ebbb2c4b8f4ce873 (patch) | |
tree | 2253af9926b3d0d1e3065d910cf16db957b2eedf | |
parent | 65fa1051585f5345c3270db73ab53b7a0dbbaa50 (diff) |
Basic player movement.
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | main.c | 17 | ||||
-rw-r--r-- | player.c | 56 | ||||
-rw-r--r-- | player.h | 13 | ||||
-rw-r--r-- | sprite_system.c | 3 | ||||
-rw-r--r-- | standard.c | 38 | ||||
-rw-r--r-- | standard.h | 8 | ||||
-rw-r--r-- | world.h | 9 |
8 files changed, 131 insertions, 14 deletions
@@ -89,6 +89,7 @@ sources = \ main.c \ malware.c \ platform.c \ + player.c \ rect.c \ render.c \ sprite_system.c \ @@ -1,14 +1,14 @@ #include "asset.h" +#include "player.h" #include "render.h" +#include "standard.h" #include "systems.h" #include "world.h" World world; -Entity player; void on_init(int argc, char** argv) { - CSprite* sprite; - CPosition* pos; + seed_rng(500); init_renderer(); @@ -16,17 +16,12 @@ void on_init(int argc, char** argv) { init_world(&world); - player = new_entity(&world); - add_components(&world, player, ctype_sprite | ctype_position); - sprite = &world.sprites[player]; - pos = &world.positions[player]; - pos->x = 32; - pos->y = 70; - sprite->id = asset_id_char; - sprite->rect = make_rect(0, 16, 16, 16); + init_player(&world.player, &world); } void on_update() { + renderer_begin_frame(); + update_player(&world.player, &world); sprite_system(&world); } diff --git a/player.c b/player.c new file mode 100644 index 0000000..e5a0853 --- /dev/null +++ b/player.c @@ -0,0 +1,56 @@ +#include "components.h" +#include "input.h" +#include "player.h" +#include "standard.h" +#include "world.h" + +void init_player(Player* player, World* world) { + CSprite* sprite; + CPosition* pos; + Entity e; + + e = new_entity(world); + player->entity = e; + add_components(world, e, ctype_sprite | ctype_position); + sprite = &world->sprites[e]; + + pos = &world->positions[e]; + pos->x = 32; + pos->y = 70; + + sprite->id = asset_id_char; + sprite->rect = make_rect(0, 16, 16, 16); +} + +void update_player(Player* player, World* world) { + int dx, dy; + Entity e; + CPosition* pos; + + e = player->entity; + pos = &world->positions[e]; + + dx = dy = 0; + if (button_pressed(btn_dpad_left)) { + dx -= 1 << fbits; + } + + if (button_pressed(btn_dpad_right)) { + dx += 1 << fbits; + } + + if (button_pressed(btn_dpad_up)) { + dy -= 1 << fbits; + } + + if (button_pressed(btn_dpad_down)) { + dy += 1 << fbits; + } + + if (dx != 0 || dy != 0) { + vec_nrmise(&dx, &dy); + + pos->x += dx; + pos->y += dy; + } +} diff --git a/player.h b/player.h new file mode 100644 index 0000000..37a7f2a --- /dev/null +++ b/player.h @@ -0,0 +1,13 @@ +#ifndef player_h +#define player_h + +struct World; + +typedef struct { + int entity; +} Player; + +void init_player(Player* player, struct World* world); +void update_player(Player* player, struct World* world); + +#endif diff --git a/sprite_system.c b/sprite_system.c index 99360fc..526c097 100644 --- a/sprite_system.c +++ b/sprite_system.c @@ -1,6 +1,7 @@ #include "asset.h" #include "components.h" #include "render.h" +#include "standard.h" #include "world.h" void sprite_system(const World* world) { @@ -17,7 +18,7 @@ void sprite_system(const World* world) { sprite = &world->sprites[i]; b = get_bitmap(sprite->id); - render_bitmap(b, pos->x, pos->y, &sprite->rect); + render_bitmap(b, pos->x >> fbits, pos->y >> fbits, &sprite->rect); } } } @@ -135,6 +135,44 @@ int flerp(int a, int b, int t) { return a + ((t * (b - a)) >> fbits); } +void vec_nrmise(int* x, int* y) { + int l; + + l = ((*x * *x) >> fbits) + ((*y * *y) >> fbits); + *x = (*x << fbits) / l; + *y = (*y << fbits) / l; +} + +static unsigned rng_seed, n; + +void seed_rng(unsigned seed) { + rng_seed = seed; + n = seed; +} + +unsigned get_seed() { + return rng_seed; +} + +unsigned rng() { + int a; + unsigned r; + + n++; + + a = n * 15485863; + r = (a * a * a % 2038074743); + return r; +} + +int rand_range(int a, int b) { + return a + (int)(rng() % ((unsigned)(b - a) + 1)); +} + +int randf() { + return (int)(rng() % ((1 << fbits) + 1)); +} + int int_to_buf(int n, char* buf) { int i, sign, t; unsigned n1; @@ -15,6 +15,14 @@ int ftan(int t); int flerp(int a, int b, int t); int fsqrt(int v); +void vec_nrmise(int* x, int* y); + +void seed_rng(unsigned seed); +unsigned get_seed(); +unsigned rng(); +int rand_range(int a, int b); +int randf(); + /* Not safe! */ int f_to_buf(int f, char* buf); int int_to_buf(int n, char* buf); @@ -2,19 +2,24 @@ #define world_h #include "components.h" +#include "player.h" #define max_entities 256 typedef int Entity; -typedef struct { +typedef struct World World; + +struct World { int entity_count; unsigned char bitmask[max_entities]; CSprite sprites[max_entities]; CPosition positions[max_entities]; -} World; + + Player player; +}; void init_world(World* world); Entity new_entity(World* world); |