summaryrefslogtreecommitdiff
path: root/src/player.c
diff options
context:
space:
mode:
authorBoredGuy <osome3717@gmail.com>2026-03-19 09:43:11 +0300
committerBoredGuy <osome3717@gmail.com>2026-03-19 09:43:11 +0300
commit6dbd16cd920b51bc24b60d0561bd707ff8862cc5 (patch)
tree34add659f1ddb15be4a817b66dbf7c9c194c1576 /src/player.c
parent9b63c2572ee6d8b73fd0d2f298dc339335875607 (diff)
Added Walls
- And more player mechanics
Diffstat (limited to 'src/player.c')
-rw-r--r--src/player.c52
1 files changed, 40 insertions, 12 deletions
diff --git a/src/player.c b/src/player.c
index dd758a0..0ab53cd 100644
--- a/src/player.c
+++ b/src/player.c
@@ -1,7 +1,9 @@
#include <raylib.h>
#include <raymath.h>
+#include <math.h>
#include "physics.h"
#include "player.h"
+#include <stdio.h>
#define TOP_SPEEDX 100
#define TOP_SPEEDY 100
@@ -9,17 +11,23 @@
void add_player(float xpos, float ypos) {
struct entity player = {
- .flags = (ENTITY_ACTIVE | ENTITY_VISIBLE),
+ .flags = (ENTITY_ACTIVE | ENTITY_COLLISION_ACTIVE | ENTITY_VISIBLE),
.type = Player_Entity,
.position = (Vector2) {xpos, ypos},
.texture = LoadTexture("assets/Graphics/spritesheet-characters-double.png"),
.sprite_source_rect = (Rectangle) {
- .x = 0,
- .y = 0,
- .width = 257,
- .height = 257
+ .x = 50,
+ .y = 57,
+ .width = 160,
+ .height = 200
+ },
+ .collider = (Rectangle) {
+ .x = -50,
+ .y = -50,
+ .width = 100,
+ .height = 100
},
.sprite_dest_rect = (Rectangle) {
.x = -50,
@@ -32,7 +40,7 @@ void add_player(float xpos, float ypos) {
add_entity(&player);
}
-float get_x_acceleration_direction() {
+float get_x_acceleration_direction(const struct entity* player) {
if (IsKeyDown(KEY_A)) {
return -1.0;
} else if (IsKeyDown(KEY_D)) {
@@ -44,19 +52,39 @@ float get_x_acceleration_direction() {
void handle_movement(struct entity* player, float dt) {
Vector2 accel = {
- .x = get_x_acceleration_direction() * ACCEL_X,
+ .x = get_x_acceleration_direction(player) * ACCEL_X,
.y = GRAVITY
};
- player->velocity =
- Vector2Add(player->velocity, Vector2Scale(accel, dt * 0.5));
+ 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;
+
move_and_collide(player, dt);
- player->velocity =
- Vector2Add(player->velocity, Vector2Scale(accel, dt * 0.5));
+
+ 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;
}
void handle_input(struct entity* player) {
-
+ if (IsKeyPressed(KEY_SPACE)) {
+ player->velocity.y = -50;
+ }
}
void update_player(struct entity* player, float dt) {