summary refs log tree commit diff
path: root/src/player.c
diff options
context:
space:
mode:
authorzlago2024-10-26 20:21:15 +0200
committerzlago2024-10-26 20:22:08 +0200
commitee316a07cdfb01e52694edef2cc998e672e2885b (patch)
tree99ad4155267146e8b727905de539ddcaf1d0bcca /src/player.c
parentd44d411e0eb3800ed883374b29c2863e1a863735 (diff)
redo the damage system
Diffstat (limited to 'src/player.c')
-rw-r--r--src/player.c66
1 files changed, 37 insertions, 29 deletions
diff --git a/src/player.c b/src/player.c
index 4844b96..e7dfd98 100644
--- a/src/player.c
+++ b/src/player.c
@@ -5,6 +5,7 @@
 #include "tilemap.h"
 #include <stdbool.h>
 #include <math.h>
+#include "particles.h"
 
 #define SIZE 4
 #define ACCELERATION 1
@@ -51,6 +52,12 @@ struct anim player_anims[] = {
 	{PLAYER_A_FALL2, {48, 0, 16, 16}, 15},
 };
 
+enum slash_type {
+	SLASH_NORMAL,
+	SLASH_STRONG,
+	SLASH_WEAK,
+};
+
 static int slash_update(struct projectile *self) {
 	self->x += self->velocity.x;
 	self->y += self->velocity.y;
@@ -74,14 +81,14 @@ static int slash_update(struct projectile *self) {
 			continue;
 		}
 		if (hitbox_overlap(self->hitbox, entities.enemy[i].hitbox)) {
-			entities.enemy[i].hurt(entities.enemy + i, 1);
+			entities.enemy[i].hurt(entities.enemy + i, (struct damage) {self->ext == SLASH_STRONG? 2: 1, self->ext == SLASH_NORMAL? 10: 60});
 		}
 	}
 	struct particle *part = entities.particle + entities.particles;
 	part->x = to_fixed(x - 1);
 	part->y = to_fixed(y - 1);
 	part->velocity = (struct vec2) {self->velocity.x + x - xx, self->velocity.y + y - yy};
-	part->rect = (SDL_Rect) {8, 0, 4, 4};
+	part->rect = particle_orange;
 	part->acceleration = (struct vec2) {0, 0};
 	part->hp = 10 - (self->hp >= 0? self->hp: -self->hp - 8);
 	entities.particles++;
@@ -179,7 +186,7 @@ static int move(struct entity *self) {
 	}
 	
 	if (collision_hazard(cx | cy)) {
-		self->hurt(self, 1);
+		self->hurt(self, (struct damage) {1, 60});
 	}
 	
 	self->hitbox.left = from_fixed(self->x) - SIZE / 2;
@@ -189,7 +196,7 @@ static int move(struct entity *self) {
 	return on_ground;
 }
 
-static void attack(struct entity *self, int a) {
+static void attack(struct entity *self, int a, enum slash_type type) {
 	if (!input_s(input_pressed)) {
 		return;
 	}
@@ -209,7 +216,7 @@ static void attack(struct entity *self, int a) {
 		.faction = FACTION_PLAYER,
 		.iframes = 0,
 		.texture = NULL,
-		.ext = NULL,
+		.ext = (void *) type,
 	};
 	//anim(entities->projectile + entities->projectiles, ENEMY_A_IDLE);
 	//entities->projectile[entities->projectiles].texture = res_get_texture("fywi").data;
@@ -244,7 +251,7 @@ static int player_update(struct entity *self) {
 			part->x = self->x - to_fixed(-3) - to_fixed(1);
 			part->y = self->y - 2;
 			part->velocity = (struct vec2) {12, -8};
-			part->rect = (SDL_Rect) {0, 0, 4, 4};
+			part->rect = particle_gray;
 			part->acceleration = (struct vec2) {0, 1};
 			part->hp = 5;
 			entities.particles++;
@@ -252,12 +259,12 @@ static int player_update(struct entity *self) {
 			part->x = self->x - to_fixed(3) - to_fixed(1);
 			part->y = self->y - 2;
 			part->velocity = (struct vec2) {-12, -8};
-			part->rect = (SDL_Rect) {0, 0, 4, 4};
+			part->rect = particle_gray;
 			part->acceleration = (struct vec2) {0, 1};
 			part->hp = 5;
 			entities.particles++;
 			}
-			attack(self, 1);
+			attack(self, 1, SLASH_NORMAL);
 			if (input_s(input_pressed)) {
 				self->state = PLAYER_ATTACK1;
 				self->timer = 30;
@@ -279,7 +286,7 @@ static int player_update(struct entity *self) {
 				anim(self, PLAYER_A_JUMP);
 				self->state = PLAYER_JUMP;
 			}
-			attack(self, 0);
+			attack(self, 0, SLASH_NORMAL);
 			if (input_s(input_pressed)) {
 				self->state = PLAYER_ATTACK2;
 				self->timer = 30;
@@ -304,7 +311,7 @@ static int player_update(struct entity *self) {
 				anim(self, PLAYER_A_JUMP);
 				self->state = PLAYER_JUMP;
 			}
-			attack(self, 1);
+			attack(self, 1, SLASH_STRONG);
 			if (input_s(input_pressed)) {
 				self->state = PLAYER_ATTACK3;
 				self->timer = 60;
@@ -339,7 +346,7 @@ static int player_update(struct entity *self) {
 			part->x = self->x - to_fixed(self->facing) * 3 - to_fixed(1);
 			part->y = self->y - 2;
 			part->velocity = (struct vec2) {-self->velocity.x, -8};
-			part->rect = (SDL_Rect) {0, 0, 4, 4};
+			part->rect = particle_gray;
 			part->acceleration = (struct vec2) {0, 1};
 			part->hp = 5;
 			entities.particles++;
@@ -358,7 +365,7 @@ static int player_update(struct entity *self) {
 				anim(self, PLAYER_A_JUMP);
 				self->state = PLAYER_JUMP;
 			}
-			attack(self, 1);
+			attack(self, 1, SLASH_WEAK);
 			break;
 		
 		case PLAYER_JUMP:
@@ -387,7 +394,7 @@ static int player_update(struct entity *self) {
 					part->x = self->x;
 					part->y = self->y;
 					part->velocity = (struct vec2) {x * 5, -yy / 4 - 8};
-					part->rect = (SDL_Rect) {0, 0, 4, 4};
+					part->rect = particle_gray;
 					part->acceleration = (struct vec2) {0, 1};
 					part->hp = yy / 2;
 					entities.particles++;
@@ -405,24 +412,25 @@ static int player_update(struct entity *self) {
 	return 0;
 }
 
-static int player_hurt(struct entity *self, int damage) {
-	if (self->iframes == 0) {
-		self->hp -= damage;
-		self->iframes = 60;
-		for (int x = -1; x <= 1; x += 2) {
-			for (int y = -1; y <= 1; y += 2) {
-				struct particle *part = entities.particle + entities.particles;
-				part->x = self->x;
-				part->y = self->y;
-				part->velocity = (struct vec2) {to_fixed(x), to_fixed(y) - 8};
-				part->rect = (SDL_Rect) {4, 0, 4, 4};
-				part->acceleration = (struct vec2) {0, 1};
-				part->hp = 30;
-				entities.particles++;
-			}
+static int player_hurt(struct entity *self, struct damage damage) {
+	if (self->iframes > 0) {
+		return 0;
+	}
+	self->hp -= damage.damage;
+	self->iframes = damage.iframes;
+	for (int x = -1; x <= 1; x += 2) {
+		for (int y = -1; y <= 1; y += 2) {
+			struct particle *part = entities.particle + entities.particles;
+			part->x = self->x;
+			part->y = self->y;
+			part->velocity = (struct vec2) {to_fixed(x), to_fixed(y) - 8};
+			part->rect = particle_red;
+			part->acceleration = (struct vec2) {0, 1};
+			part->hp = 30;
+			entities.particles++;
 		}
 	}
-	return 0;
+	return 1;
 }
 
 static int player_draw(struct entity *self, int camX, int camY) {