summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/collision.h6
-rw-r--r--src/main.c6
-rw-r--r--src/player.c45
3 files changed, 40 insertions, 17 deletions
diff --git a/src/collision.h b/src/collision.h
index 3b7a457..856d5f5 100644
--- a/src/collision.h
+++ b/src/collision.h
@@ -10,6 +10,6 @@ enum _collisions {
 	COLLISION_LENGTH // no. of checked inputs
 };
 
-#define collision_solid(a)   (1 & a >> COLLISION_SOLID)
-#define collision_hazard(a)  (1 & a >> COLLISION_HAZARD)
-#define collision_floor(a)   (1 & a >> COLLISION_FLOOR)
+#define collision_solid(a)   (1 & (a) >> COLLISION_SOLID)
+#define collision_hazard(a)  (1 & (a) >> COLLISION_HAZARD)
+#define collision_floor(a)   (1 & (a) >> COLLISION_FLOOR)
diff --git a/src/main.c b/src/main.c
index b2244d2..42baa75 100644
--- a/src/main.c
+++ b/src/main.c
@@ -200,7 +200,7 @@ int main(int const argc, char *const *const argv) {
 	int x = 0, y = 0;
 	player_new();
 	player_property(player, "x", "40");
-	player_property(player, "y", "40");
+	player_property(player, "y", "64");
 	while (1) {
 		SDL_Event evt;
 		while (SDL_PollEvent(&evt)) {
@@ -302,7 +302,9 @@ int main(int const argc, char *const *const argv) {
 		SDL_RenderClear(renderer);
 
 		//SDL_RenderCopy(renderer, tilemap->wang_tileset, &(SDL_Rect) {0, 0, 128, 90}, &(SDL_Rect) {0, 0, 128, 90});
-		player[0].update(player);
+		if (player[0].update(player)) {
+			break; // TODO: game over state
+		}
 		
 		//x += input_right(input_now) - input_left(input_now);
 		//y += input_down(input_now) - input_up(input_now);
diff --git a/src/player.c b/src/player.c
index ed7dcbb..9efd0ac 100644
--- a/src/player.c
+++ b/src/player.c
@@ -51,6 +51,7 @@ 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;
+	// deaccel
 	if (dx == 0) {
 		if (self->velocity.x < -FRICTION) {
 			self->velocity.x += FRICTION;
@@ -60,13 +61,16 @@ static int move(struct entity *self) {
 			self->velocity.x = 0;
 		}
 	}
+	// speed cap
 	if (self->velocity.x > MAX_SPEED) {
 		self->velocity.x = MAX_SPEED;
 	} else if (self->velocity.x < -MAX_SPEED) {
 		self->velocity.x = -MAX_SPEED;
 	}
+	// x collision
 	self->x += self->velocity.x;
-	if (collision_solid(collide(self))) {
+	collision_T const cx = collide(self);
+	if (collision_solid(cx)) {
 		if (self->velocity.x < 0) {
 			self->x += to_fixed(8) - ((self->x - to_fixed(SIZE / 2)) % to_fixed(8)); // left
 		} else if (self->velocity.x == 0) {
@@ -82,9 +86,10 @@ static int move(struct entity *self) {
 		self->facing = +1;
 	}
 	self->velocity.y += GRAVITY;
+	// y collision
 	self->y += self->velocity.y;
-	//self->y += (input_down(input_now) - input_up(input_now)) * 8;
-	if (collision_solid(collide(self))) {
+	collision_T const cy = collide(self);
+	if (collision_solid(cy)) {
 		if (self->velocity.y < 0) {
 			self->y += to_fixed(8) - ((self->y - to_fixed(SIZE)) % to_fixed(8)); // up
 			self->velocity.y = 0;
@@ -96,10 +101,15 @@ static int move(struct entity *self) {
 			on_ground = true;
 		}
 	}
-	self->hitbox.left = self->x;
-	self->hitbox.right = self->x + SIZE;
-	self->hitbox.top = self->y;
-	self->hitbox.bottom = self->y + SIZE;
+	
+	if (collision_hazard(cx | cy)) {
+		self->hurt(self, 1);
+	}
+	
+	self->hitbox.left = from_fixed(self->x) - SIZE / 2;
+	self->hitbox.right = from_fixed(self->x) + SIZE / 2;
+	self->hitbox.top = from_fixed(self->y) - SIZE;
+	self->hitbox.bottom = from_fixed(self->y);
 	return on_ground;
 }
 
@@ -108,6 +118,9 @@ void anim(struct entity *self, unsigned anim) {
 }
 
 static int player_update(struct entity *self) {
+	if (self->hp <= 0) {
+		return 1;
+	}
 	switch (self->state) {
 		case PLAYER_IDLE:
 			if (move(self) == false) {
@@ -166,6 +179,9 @@ static int player_update(struct entity *self) {
 			}
 			break;
 	}
+	if (self->iframes > 0) {
+		self->iframes--;
+	}
 	self->anim.length--;
 	if (self->anim.length == 0) {
 		anim(self, self->anim.frame);
@@ -174,16 +190,21 @@ static int player_update(struct entity *self) {
 }
 
 static int player_hurt(struct entity *self, int damage) {
+	if (self->iframes == 0) {
+		self->hp -= damage;
+		self->iframes = 60;
+	}
 	return 0;
 }
 
 static int player_draw(struct entity *self, int camX, int camY) {
-	SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
-	SDL_Rect rect = self->anim.rect;
-	if (self->facing == -1) {
-		rect.y += 16;
+	if (!(self->iframes & 0x4)) {
+		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 - 8, from_fixed(self->y) - camY - 12, 16, 16});
 	}
-	SDL_RenderCopy(renderer, self->texture, &rect, &(SDL_Rect) {from_fixed(self->x) - camX - 8, from_fixed(self->y) - camY - 12, 16, 16});
 	return 0;
 }