1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#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
#define ACCEL_X 40
void add_player(float xpos, float ypos) {
struct entity player = {
.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 = 50,
.y = 57,
.width = 160,
.height = 200
},
.collider = (Rectangle) {
.x = -50,
.y = -50,
.width = 100,
.height = 100
},
.sprite_dest_rect = (Rectangle) {
.x = -50,
.y = -50,
.width = 100,
.height = 100
}
};
add_entity(&player);
}
float get_x_acceleration_direction(const struct entity* player) {
if (IsKeyDown(KEY_A)) {
return -1.0;
} else if (IsKeyDown(KEY_D)) {
return 1.0;
}
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;
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;
}
void handle_input(struct entity* player) {
if (IsKeyPressed(KEY_SPACE)) {
player->velocity.y = -50;
}
}
void update_player(struct entity* player, float dt) {
handle_movement(player, dt);
handle_input(player);
}
|