summaryrefslogtreecommitdiff
path: root/src/player.c
diff options
context:
space:
mode:
authorzlago2024-09-30 10:52:14 +0200
committerzlago2024-09-30 10:52:14 +0200
commit45512bbc85188e3adb4eda597d0d2fa5530de651 (patch)
tree3d159b6baad17c828e42f4594353599b879cb2c8 /src/player.c
parent5ead22e91fe1165f3a9208c0d1c965b3edd104be (diff)
player animations
Diffstat (limited to 'src/player.c')
-rw-r--r--src/player.c107
1 files changed, 92 insertions, 15 deletions
diff --git a/src/player.c b/src/player.c
index 4eba788..9eed3a8 100644
--- a/src/player.c
+++ b/src/player.c
@@ -1,25 +1,53 @@
#include "main.h"
+#include "entity.h"
#include "input.h"
-#include "loader.h"
#include "tilemap.h"
#include <stdbool.h>
#define from_fixed(a) (a / 16)
#define to_fixed(a) (a * 16)
+#define SIZE 8
+#define ACCELERATION 1
+#define FRICTION 2
+#define MAX_SPEED 12
+#define GRAVITY 1
+#define JUMP 30
+
enum {
+ PLAYER_NONE,
PLAYER_IDLE,
+ PLAYER_WALK,
PLAYER_JUMP,
PLAYER_FALL,
};
-#define SIZE 8
-#define ACCELERATION 1
-#define FRICTION 2
-#define MAX_SPEED 12
-#define GRAVITY 1
+enum {
+ PLAYER_A_IDLE,
+ PLAYER_A_IDLE2,
+ PLAYER_A_WALK,
+ PLAYER_A_WALK2,
+ PLAYER_A_WALK3,
+ PLAYER_A_WALK4,
+ PLAYER_A_JUMP,
+ PLAYER_A_FALL,
+ PLAYER_A_FALL2,
+};
+
+struct anim player_anims[] = {
+ {PLAYER_A_IDLE2, {0, 0, 16, 16}, 300},
+ {PLAYER_A_IDLE, {0, 32, 16, 16}, 2},
+ {PLAYER_A_WALK2, {0, 0, 16, 16}, 6},
+ {PLAYER_A_WALK3, {16, 0, 16, 16}, 6},
+ {PLAYER_A_WALK4, {32, 0, 16, 16}, 6},
+ {PLAYER_A_WALK, {48, 0, 16, 16}, 6},
+ {PLAYER_A_JUMP, {16, 0, 16, 16}, 300},
+ {PLAYER_A_FALL2, {32, 0, 16, 16}, 15},
+ {PLAYER_A_FALL2, {48, 0, 16, 16}, 15},
+};
static int move(struct entity *self) {
+ int on_ground = false;
int dx = (input_right(input_now) - input_left(input_now)) * ACCELERATION;
self->velocity.x += dx;
if (dx == 0) {
@@ -47,6 +75,11 @@ static int move(struct entity *self) {
}
self->velocity.x = 0;
}
+ if (self->velocity.x < 0) {
+ self->facing = -1;
+ } else if (self->velocity.x > 0) {
+ self->facing = +1;
+ }
self->velocity.y += GRAVITY;
self->y += self->velocity.y;
//self->y += (input_down(input_now) - input_up(input_now)) * 8;
@@ -59,44 +92,81 @@ static int move(struct entity *self) {
} else {
self->y -= ((self->y + to_fixed(SIZE)) % to_fixed(8)); // down
self->velocity.y = to_fixed(1); // crazy but it works
- return true;
+ on_ground = true;
}
}
- return false;
+ self->hitbox.left = self->x;
+ self->hitbox.right = self->x + SIZE;
+ self->hitbox.top = self->y;
+ self->hitbox.bottom = self->y + SIZE;
+ return on_ground;
+}
+
+void anim(struct entity *self, unsigned anim) {
+ self->anim = player_anims[anim];
}
static int player_update(struct entity *self) {
switch (self->state) {
case PLAYER_IDLE:
- if (move(self) == true) {
- if (input_a(input_now)) {
- self->velocity.y = -30;
- self->state = PLAYER_JUMP;
- }
- } else {
+ if (move(self) == false) {
+ anim(self, PLAYER_A_FALL);
self->state = PLAYER_FALL;
}
+ if (input_right(input_now) - input_left(input_now)) {
+ anim(self, PLAYER_A_WALK);
+ self->state = PLAYER_WALK;
+ }
+ if (input_a(input_now)) {
+ self->velocity.y = -JUMP;
+ anim(self, PLAYER_A_JUMP);
+ self->state = PLAYER_JUMP;
+ }
+ break;
+
+ case PLAYER_WALK:
+ if (move(self) == false) {
+ anim(self, PLAYER_A_FALL);
+ self->state = PLAYER_FALL;
+ }
+ if (!(input_right(input_now) - input_left(input_now))) {
+ anim(self, PLAYER_A_IDLE);
+ self->state = PLAYER_IDLE;
+ }
+ if (input_a(input_now)) {
+ self->velocity.y = -JUMP;
+ anim(self, PLAYER_A_JUMP);
+ self->state = PLAYER_JUMP;
+ }
break;
case PLAYER_JUMP:
if (!input_a(input_now)) {
self->velocity.y /= 2;
+ anim(self, PLAYER_A_FALL);
self->state = PLAYER_FALL;
}
if (self->velocity.y > 0) {
+ anim(self, PLAYER_A_FALL);
self->state = PLAYER_FALL;
}
if (move(self) == true) {
+ anim(self, PLAYER_A_IDLE);
self->state = PLAYER_IDLE;
}
break;
case PLAYER_FALL:
if (move(self) == true) {
+ anim(self, PLAYER_A_IDLE);
self->state = PLAYER_IDLE;
}
break;
}
+ self->anim.length--;
+ if (self->anim.length == 0) {
+ anim(self, self->anim.frame);
+ }
return 0;
}
@@ -106,7 +176,11 @@ static int player_hurt(struct entity *self, int damage) {
static int player_draw(struct entity *self, int camX, int camY) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
- SDL_RenderCopy(renderer, self->texture, &(SDL_Rect) {0, 0, 16, 16}, &(SDL_Rect) {from_fixed(self->x) - camX - 4, from_fixed(self->y) - camY - 4, 16, 16});
+ SDL_Rect rect = self->anim.rect;
+ if (self->facing == -1) {
+ rect.y += 16;
+ }
+ SDL_RenderCopy(renderer, self->texture, &rect, &(SDL_Rect) {from_fixed(self->x) - camX - 4, from_fixed(self->y) - camY - 4, 16, 16});
return 0;
}
@@ -123,9 +197,12 @@ struct entity *player_new(void) {
.state = PLAYER_IDLE,
.hp = 3,
.timer = 0,
+ .facing = 1,
+ .iframes = 0,
.texture = NULL,
.ext = NULL,
};
+ anim(player, PLAYER_A_IDLE);
player[0].texture = res_get_texture("fywi").data;
return player + 0;
}