summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorzlago2024-10-01 20:17:17 +0200
committerzlago2024-10-01 20:17:17 +0200
commitb687574d705dd6643501e37455932b6e504dd232 (patch)
tree1c44c26c502af7372258a693f822e7414db7ffa6 /src
parente4ad2c9362254ab3213c4cb7c743b6bbd72b6346 (diff)
move player origin to bottom center
Diffstat (limited to 'src')
-rw-r--r--src/player.c22
-rw-r--r--src/tilemap.c8
2 files changed, 20 insertions, 10 deletions
diff --git a/src/player.c b/src/player.c
index 2e0346d..ed7dcbb 100644
--- a/src/player.c
+++ b/src/player.c
@@ -4,7 +4,7 @@
 #include "tilemap.h"
 #include <stdbool.h>
 
-#define SIZE 8
+#define SIZE 4
 #define ACCELERATION 1
 #define FRICTION 2
 #define MAX_SPEED 12
@@ -43,6 +43,10 @@ struct anim player_anims[] = {
 	{PLAYER_A_FALL2, {48, 0, 16, 16}, 15},
 };
 
+static collision_T collide(struct entity *self) {
+	return tilemap_area(tilemap, from_fixed(self->x) - SIZE / 2, from_fixed(self->y) - SIZE, from_fixed(self->x) + SIZE / 2, from_fixed(self->y));
+}
+
 static int move(struct entity *self) {
 	int on_ground = false;
 	int dx = (input_right(input_now) - input_left(input_now)) * ACCELERATION;
@@ -62,13 +66,13 @@ static int move(struct entity *self) {
 		self->velocity.x = -MAX_SPEED;
 	}
 	self->x += self->velocity.x;
-	if (collision_solid(tilemap_area(tilemap, from_fixed(self->x), from_fixed(self->y), from_fixed(self->x) + SIZE, from_fixed(self->y) + SIZE))) {
+	if (collision_solid(collide(self))) {
 		if (self->velocity.x < 0) {
-			self->x += to_fixed(8) - ((self->x) % to_fixed(8)); // left
+			self->x += to_fixed(8) - ((self->x - to_fixed(SIZE / 2)) % to_fixed(8)); // left
 		} else if (self->velocity.x == 0) {
 			//fputs("what?\n", stderr);
 		} else {
-			self->x -= ((self->x + to_fixed(SIZE)) % to_fixed(8)); // right
+			self->x -= ((self->x + to_fixed(SIZE / 2)) % to_fixed(8)); // right
 		}
 		self->velocity.x = 0;
 	}
@@ -80,14 +84,14 @@ static int move(struct entity *self) {
 	self->velocity.y += GRAVITY;
 	self->y += self->velocity.y;
 	//self->y += (input_down(input_now) - input_up(input_now)) * 8;
-	if (collision_solid(tilemap_area(tilemap, from_fixed(self->x), from_fixed(self->y), from_fixed(self->x) + SIZE, from_fixed(self->y) + SIZE))) {
+	if (collision_solid(collide(self))) {
 		if (self->velocity.y < 0) {
-			self->y += to_fixed(8) - ((self->y) % to_fixed(8)); // up
+			self->y += to_fixed(8) - ((self->y - to_fixed(SIZE)) % to_fixed(8)); // up
 			self->velocity.y = 0;
 		} else if (self->velocity.y == 0) {
 			//fputs("what?\n", stderr);
 		} else {
-			self->y -= ((self->y + to_fixed(SIZE)) % to_fixed(8)); // down
+			self->y -= ((self->y) % to_fixed(8)); // down
 			self->velocity.y = to_fixed(1); // crazy but it works
 			on_ground = true;
 		}
@@ -107,6 +111,7 @@ static int player_update(struct entity *self) {
 	switch (self->state) {
 		case PLAYER_IDLE:
 			if (move(self) == false) {
+				self->velocity.y = 0;
 				anim(self, PLAYER_A_FALL);
 				self->state = PLAYER_FALL;
 			}
@@ -123,6 +128,7 @@ static int player_update(struct entity *self) {
 
 		case PLAYER_WALK:
 			if (move(self) == false) {
+				self->velocity.y = 0;
 				anim(self, PLAYER_A_FALL);
 				self->state = PLAYER_FALL;
 			}
@@ -177,7 +183,7 @@ static int player_draw(struct entity *self, int camX, int camY) {
 	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});
+	SDL_RenderCopy(renderer, self->texture, &rect, &(SDL_Rect) {from_fixed(self->x) - camX - 8, from_fixed(self->y) - camY - 12, 16, 16});
 	return 0;
 }
 
diff --git a/src/tilemap.c b/src/tilemap.c
index ea53e2f..e7ee1d4 100644
--- a/src/tilemap.c
+++ b/src/tilemap.c
@@ -71,13 +71,17 @@ collision_T tilemap_area(struct tilemap *tilemap, int x1, int y1, int x2, int y2
 	if (x1 < 0) {
 		x1 = 0;
 	}
-	if (x2 >= tilemap->width) {
+	if (x2 < 0) {
+		x2 = 0;
+	} else if (x2 >= tilemap->width) {
 		x2 = tilemap->width - 1;
 	}
 	if (y1 < 0) {
 		y1 = 0;
 	}
-	if (y2 >= tilemap->height) {
+	if (y2 < 0) { // for some reason you can bonk your head on nothing without this
+		y2 = 0;
+	} else if (y2 >= tilemap->height) {
 		y2 = tilemap->height - 1;
 	}
 	return tilemap_area_raw(tilemap, x1, y1, x2, y2);