summaryrefslogtreecommitdiff
path: root/src/player.c
diff options
context:
space:
mode:
authorBoredGuy <osome3717@gmail.com>2026-03-23 17:11:28 +0300
committerBoredGuy <osome3717@gmail.com>2026-03-23 17:11:28 +0300
commit7c438c2a0e25d22323b5def545f32e97eee689f0 (patch)
treea9e2cf29a967c88d022ea8e6c20a522a9c482610 /src/player.c
parent6dbd16cd920b51bc24b60d0561bd707ff8862cc5 (diff)
Asset system
- Multiple entity updates
Diffstat (limited to 'src/player.c')
-rw-r--r--src/player.c172
1 files changed, 124 insertions, 48 deletions
diff --git a/src/player.c b/src/player.c
index 0ab53cd..9e1a782 100644
--- a/src/player.c
+++ b/src/player.c
@@ -1,93 +1,169 @@
#include <raylib.h>
#include <raymath.h>
#include <math.h>
+#include <stdio.h>
+#include "utils.h"
#include "physics.h"
#include "player.h"
-#include <stdio.h>
+
+#define PLAYER_WIDTH 128
+#define PLAYER_HEIGHT 128
+
+#define COLLIDER_WIDTH 77
+#define COLLIDER_HEIGHT 98
+#define COLLIDER_XOFF 0
+#define COLLIDER_YOFF 14
#define TOP_SPEEDX 100
#define TOP_SPEEDY 100
-#define ACCEL_X 40
+#define ACCEL_X 200
+
+extern Texture character_spritesheet;
+
+void add_player_animations(struct entity* player);
void add_player(float xpos, float ypos) {
struct entity player = {
- .flags = (ENTITY_ACTIVE | ENTITY_COLLISION_ACTIVE | ENTITY_VISIBLE),
+ .flags = (ENTITY_ACTIVE | ENTITY_COLLISION_ACTIVE | ENTITY_VISIBLE | ENTITY_ANIMATED),
.type = Player_Entity,
.position = (Vector2) {xpos, ypos},
- .texture = LoadTexture("assets/Graphics/spritesheet-characters-double.png"),
+ .texture = character_spritesheet,
.sprite_source_rect = (Rectangle) {
- .x = 50,
- .y = 57,
- .width = 160,
- .height = 200
+ .x = 0,
+ .y = 0,
+ .width = 128,
+ .height = 128
},
.collider = (Rectangle) {
- .x = -50,
- .y = -50,
- .width = 100,
- .height = 100
+ .x = -COLLIDER_WIDTH / 2 + COLLIDER_XOFF,
+ .y = -COLLIDER_HEIGHT / 2 + COLLIDER_YOFF,
+ .width = COLLIDER_WIDTH,
+ .height = COLLIDER_HEIGHT
},
.sprite_dest_rect = (Rectangle) {
- .x = -50,
- .y = -50,
- .width = 100,
- .height = 100
- }
+ .x = -PLAYER_WIDTH / 2,
+ .y = -PLAYER_HEIGHT / 2,
+ .width = PLAYER_WIDTH,
+ .height = PLAYER_HEIGHT
+ },
+
+ .player_state = Player_Idle,
+ .start_position = (Vector2) {xpos, ypos}
};
+ add_player_animations(&player);
+
add_entity(&player);
}
-float get_x_acceleration_direction(const struct entity* player) {
+void player_accelerate(struct entity* player, float step) {
+
if (IsKeyDown(KEY_A)) {
- return -1.0;
+
+ player->velocity.x =
+ fmaxf(player->velocity.x - step, -TOP_SPEEDX);
} else if (IsKeyDown(KEY_D)) {
- return 1.0;
+
+ player->velocity.x =
+ fminf(player->velocity.x + step, TOP_SPEEDX);
+ } else if (fabsf(player->velocity.x) > 0) {
+
+ player->velocity.x = move_to(player->velocity.x, 0, step);
}
- return 0.0;
}
void handle_movement(struct entity* player, float dt) {
- Vector2 accel = {
- .x = get_x_acceleration_direction(player) * ACCEL_X,
- .y = GRAVITY
- };
-
- if (accel.x != 0) {
- player->velocity.x += dt * accel.x * 0.5;
- } else if (player->velocity.x < 0) {
- player->velocity.x =
- fminf(player->velocity.x + ACCEL_X * 0.5 * dt, 0);
- } else if (player->velocity.x > 0) {
- player->velocity.x =
- fmaxf(player->velocity.x - ACCEL_X * 0.5 * dt, 0);
- }
- player->velocity.y += GRAVITY * dt * 0.5;
+ player_accelerate(player, ACCEL_X * dt * 0.5);
+ player->velocity.y += GRAVITY * 0.5 * dt;
move_and_collide(player, dt);
- if (accel.x != 0) {
- player->velocity.x += dt * accel.x * 0.5;
- } else if (player->velocity.x < 0) {
- player->velocity.x =
- fminf(player->velocity.x + ACCEL_X * 0.5 * dt, 0);
- } else if (player->velocity.x > 0) {
- player->velocity.x =
- fmaxf(player->velocity.x - ACCEL_X * 0.5 * dt, 0);
- }
- player->velocity.y += GRAVITY * dt * 0.5;
+ player_accelerate(player, ACCEL_X * dt * 0.5);
+ player->velocity.y += GRAVITY * 0.5 * dt;
+}
+
+void jump(struct entity* player) {
+ player->velocity.y = -200;
+
+ if (IsKeyDown(KEY_A) && player->velocity.x > 0)
+ player->velocity.x = 0;
+ else if (IsKeyDown(KEY_D) && player->velocity.x < 0)
+ player->velocity.x = 0;
}
void handle_input(struct entity* player) {
- if (IsKeyPressed(KEY_SPACE)) {
- player->velocity.y = -50;
+ if (IsKeyPressed(KEY_SPACE) && player->player_state == Player_Idle) {
+ jump(player);
+ }
+}
+
+void reset_position(struct entity* player) {
+ player->position = player->start_position;
+}
+
+void handle_fall(struct entity* player) {
+ if (player->position.y > WINDOW_HEIGHT + 100)
+ reset_position(player);
+}
+
+void handle_flip(struct entity* player) {
+ if (player->velocity.x < 0) {
+ player->flip = true;
+ } else {
+ player->flip = false;
}
}
void update_player(struct entity* player, float dt) {
+ //Collision unsets it I didn't find a better way to do this
+ //with the current setup
+ player->player_state = Player_Air;
+
handle_movement(player, dt);
handle_input(player);
+ handle_fall(player);
+ handle_flip(player);
+ update_animation(&player->animations[player->current_animation], dt);
+}
+
+void player_handle_collision
+(
+ struct entity* player,
+ struct entity* other,
+ enum direction collision_direction
+ ) {
+ switch (other->type) {
+ case Wall_Entity:
+ if (collision_direction == Direction_Left || collision_direction == Direction_Right) {
+ player->velocity.x = 0.0;
+ } else if (collision_direction == Direction_Down || collision_direction == Direction_Up) {
+ player->velocity.y = 0.0;
+ }
+
+ if (collision_direction == Direction_Down) {
+ player->player_state = Player_Idle;
+ }
+
+ break;
+
+ default:
+ break;
+ }
+}
+
+void add_player_animations(struct entity* player) {
+ //Walking animation
+ player->animations[0] = (struct animation) {
+ .frame_count = 2,
+ .is_looping = true,
+
+ .source_rects = {
+ {.x = 0, .y = 129, .width = 128, .height = 128},
+ {.x = 129, .y = 129, .width = 128, .height = 128}
+ },
+ .frame_times = {0.2, 0.2}
+ };
}