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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
#include "animation.h"
#include "asset.h"
#include "map.h"
#include "maths.h"
#include "obj.h"
#include "physics.h"
#include "plat.h"
#include "render.h"
void init_player(Player* p) {
p->anim = asset_id_guy_run_right_anm;
p->x = 0;
p->y = 0;
p->vx = 0;
p->vy = 0;
p->frame = 0;
p->grounded = 0;
p->headbutted = 0;
p->on_ramp = 0;
p->jumping = 0;
p->face = face_right;
}
void update_player_anim(Player* p) {
const Animation* anim = get_animation(p->anim);
update_anim(anim, &p->frame, &p->rect);
}
void update_player_phys(Player* p, const Map* map) {
const Rect r = {
5 << fbits,
4 << fbits,
5 << fbits,
12 << fbits
};
update_body(
map,
&p->x,
&p->y,
&p->vx,
&p->vy,
&p->grounded,
&p->headbutted,
&p->on_ramp,
&r
);
}
void update_player_move(Player* p, const App* a) {
int grounded = p->grounded < 3;
int mf = player_move_force;
int nanim, moving, t;
int jumping = p->jumping;
Face* face = &p->face;
if (!grounded) {
mf = player_air_move_force;
}
if (btn_pressed(a, btn_left)) {
p->vx -= mf;
*face = face_left;
}
if (btn_pressed(a, btn_right)) {
p->vx += mf;
*face = face_right;
}
p->vx =
p->vx < -player_max_vel ? -player_max_vel:
p->vx > player_max_vel ? player_max_vel:
p->vx;
moving = absolute(p->vx);
moving = moving > player_stop_thresh;
if (p->grounded == 0 || p->headbutted) {
p->jumping = 0;
}
jumping = p->jumping;
if (jumping) {
if (p->grounded < 10 && btn_pressed(a, btn_jump)) {
t = absolute(p->vy + 1);
t = (f1 << fbits) / t;
p->vy -= (player_jump_power_air * t) >> fbits;
}
if (p->vy < 0 && btn_just_released(a, btn_jump)) {
p->vy += f1;
p->jumping = 0;
}
}
if (grounded && btn_just_pressed(a, btn_jump)) {
p->vy = -player_jump_power;
p->jumping = 1;
play_sound(500);
}
jumping = p->jumping;
nanim = p->anim;
switch (p->face) {
case face_left:
nanim =
grounded ?
moving ?
asset_id_guy_run_left_anm:
asset_id_guy_idle_left_anm:
p->vy < 0 ?
asset_id_guy_jump_left_anm:
asset_id_guy_fall_left_anm;
break;
case face_right:
nanim =
grounded ?
moving ?
asset_id_guy_run_right_anm:
asset_id_guy_idle_right_anm:
p->vy < 0 ?
asset_id_guy_jump_right_anm:
asset_id_guy_fall_right_anm;
break;
default: break;
}
if (nanim != p->anim) {
p->frame = 0;
p->anim = nanim;
}
}
void update_player(Player* p, App* app, const Map* map) {
update_player_move(p, app);
update_player_phys(p, map);
update_player_anim(p);
}
void ren_player(Player* p, struct Renderer* r) {
const Bitmap* b = get_bitmap(asset_id_guy_img);
ren_map(r, p->x >> fbits, p->y >> fbits, &p->rect, b);
}
|