aboutsummaryrefslogtreecommitdiff
path: root/player.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2023-05-04 10:15:19 +1000
committerquou <quou@disroot.org>2023-05-04 10:15:19 +1000
commitc4ac81cffcf925963acb0c02584ab22626427a73 (patch)
tree7d6421405e53a92baada909355c5398f21e91864 /player.c
parentc4ee81da673208fe7b3e3638692fd466acf61c3f (diff)
Add an animation system.
Diffstat (limited to 'player.c')
-rw-r--r--player.c52
1 files changed, 47 insertions, 5 deletions
diff --git a/player.c b/player.c
index e5a0853..801dc33 100644
--- a/player.c
+++ b/player.c
@@ -1,4 +1,6 @@
+#include "animation.h"
#include "components.h"
+#include "game_config.h"
#include "input.h"
#include "player.h"
#include "standard.h"
@@ -7,28 +9,45 @@
void init_player(Player* player, World* world) {
CSprite* sprite;
CPosition* pos;
+ CAnimated* animated;
Entity e;
e = new_entity(world);
player->entity = e;
- add_components(world, e, ctype_sprite | ctype_position);
+ add_components(
+ world,
+ e,
+ ctype_sprite |
+ ctype_position |
+ ctype_animated
+ );
+ pos = &world->positions[e];
sprite = &world->sprites[e];
+ animated = &world->animateds[e];
- pos = &world->positions[e];
pos->x = 32;
pos->y = 70;
sprite->id = asset_id_char;
sprite->rect = make_rect(0, 16, 16, 16);
+
+ animated->id = animation_player_walk_left;
+ animated->frame = 0;
+ animated->timer = 0;
+
+ player->face = 0;
}
void update_player(Player* player, World* world) {
int dx, dy;
+ int face, moving = 0;
Entity e;
CPosition* pos;
+ CAnimated* animated;
e = player->entity;
pos = &world->positions[e];
+ animated = &world->animateds[e];
dx = dy = 0;
if (button_pressed(btn_dpad_left)) {
@@ -47,10 +66,33 @@ void update_player(Player* player, World* world) {
dy += 1 << fbits;
}
- if (dx != 0 || dy != 0) {
+ if (dx || dy) {
vec_nrmise(&dx, &dy);
- pos->x += dx;
- pos->y += dy;
+ if (dx) {
+ face = dx < 0 ? 0 : 1;
+ } else {
+ face = player->face;
+ }
+
+ pos->x += (dx * player_move_speed) >> fbits;
+ pos->y += (dy * player_move_speed) >> fbits;
+
+ moving = 1;
+ }
+
+ if (!moving) {
+ animated->frame = 0;
+ animated->id =
+ player->face ?
+ animation_player_idle_right :
+ animation_player_idle_left;
+ } else {
+ player->face = face;
+
+ animated->id =
+ player->face ?
+ animation_player_walk_right :
+ animation_player_walk_left;
}
}