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 /player.c | |
parent | 65fa1051585f5345c3270db73ab53b7a0dbbaa50 (diff) |
Basic player movement.
Diffstat (limited to 'player.c')
-rw-r--r-- | player.c | 56 |
1 files changed, 56 insertions, 0 deletions
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; + } +} |