Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
304 lines (264 loc) · 8.42 KB
/
Copy pathmain.rs
File metadata and controls
304 lines (264 loc) · 8.42 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use macroquad::prelude::*;
const JUMP_VELOCITY: f32 = 900.0;
const DOUBLE_JUMP_VELOCITY: f32 = 1000.0;
const DASH_VELOCITY: f32 = 2500.0;
const RUN_SPEED: f32 = 800.0;
const WALK_SPEED: f32 = 500.0;
const GRAVITY: f32 = 20.0;
const FRICTION: f32 = 0.9;
const AIR_CONTROL: f32 = 0.3;
fn close_to_zero(val: f32) -> bool {
val.abs() < 0.01
}
struct Movement {
left: bool,
right: bool,
run: bool,
}
impl Movement {
fn moving(&self) -> bool {
self.left || self.right
}
}
enum Action {
Jump,
Dash,
}
struct InputHandler {}
impl InputHandler {
fn new() -> Self {
Self {}
}
fn get_movement(&self) -> Movement {
let keys_down = get_keys_down();
Movement {
left: keys_down.contains(&KeyCode::Left),
right: keys_down.contains(&KeyCode::Right),
run: keys_down.contains(&KeyCode::LeftShift),
}
}
fn get_action(&self) -> Option<Action> {
let keys_pressed = get_keys_pressed();
if keys_pressed.contains(&KeyCode::Space) {
Some(Action::Jump)
} else if keys_pressed.contains(&KeyCode::Z) {
Some(Action::Dash)
} else {
None
}
}
}
#[derive(Debug)]
enum PlayerDirection {
Left,
Right,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
enum PlayerState {
Idle,
Walking,
Running,
Jumping,
DoubleJumping,
Dashing,
}
#[derive(Debug)]
struct Player {
state: PlayerState,
direction: PlayerDirection,
x: f32,
y: f32,
width: f32,
height: f32,
velocity_x: f32,
velocity_y: f32,
ground_speed: f32,
}
impl Player {
fn new() -> Self {
Self {
state: PlayerState::Idle,
direction: PlayerDirection::Right,
x: screen_width() / 2.0,
y: screen_height() - 20.0,
width: 40.0,
height: 40.0,
velocity_x: 0.0,
velocity_y: 0.0,
ground_speed: WALK_SPEED,
}
}
fn update(&mut self, movement: &Movement, action: &Option<Action>, dt: f32) {
// Process action inputs first
match action {
Some(Action::Jump) => self.jump(),
Some(Action::Dash) => self.dash(),
None => {}
}
// Update direction based on movement input
if movement.left {
self.direction = PlayerDirection::Left;
} else if movement.right {
self.direction = PlayerDirection::Right;
}
// Apply physics based on current state
self.apply_movement(movement);
self.apply_physics(dt);
self.check_state_transitions();
}
fn jump(&mut self) {
match self.state {
PlayerState::Idle | PlayerState::Walking | PlayerState::Running => {
self.state = PlayerState::Jumping;
self.velocity_y = -JUMP_VELOCITY;
// Set ground speed based on current state and movement
self.ground_speed = match self.state {
PlayerState::Running => RUN_SPEED,
PlayerState::Walking => WALK_SPEED,
_ => WALK_SPEED * 0.8, // Even when idle, allow some control
};
}
PlayerState::Jumping => {
self.state = PlayerState::DoubleJumping;
self.velocity_y = -DOUBLE_JUMP_VELOCITY;
}
_ => {}
}
}
fn dash(&mut self) {
if matches!(
self.state,
PlayerState::Jumping | PlayerState::DoubleJumping
) {
self.velocity_y = 0.0;
self.state = PlayerState::Dashing;
match self.direction {
PlayerDirection::Left => self.velocity_x = -DASH_VELOCITY,
PlayerDirection::Right => self.velocity_x = DASH_VELOCITY,
}
}
}
fn apply_movement(&mut self, movement: &Movement) {
match self.state {
PlayerState::Idle | PlayerState::Walking | PlayerState::Running => {
// Ground movement
if movement.moving() {
let speed = if movement.run { RUN_SPEED } else { WALK_SPEED };
self.ground_speed = speed;
if movement.left {
self.velocity_x = -speed;
self.state = if movement.run {
PlayerState::Running
} else {
PlayerState::Walking
};
} else if movement.right {
self.velocity_x = speed;
self.state = if movement.run {
PlayerState::Running
} else {
PlayerState::Walking
};
}
} else {
self.velocity_x = 0.0;
self.state = PlayerState::Idle;
}
}
PlayerState::Jumping | PlayerState::DoubleJumping => {
// Air control - use saved ground speed
if movement.left || movement.right {
let base_speed = if movement.run {
self.ground_speed.max(RUN_SPEED * 0.8)
} else {
self.ground_speed
};
let target_speed = if movement.left {
-base_speed
} else if movement.right {
base_speed
} else {
0.0
};
self.velocity_x =
self.velocity_x * (1.0 - AIR_CONTROL) + target_speed * AIR_CONTROL;
}
}
PlayerState::Dashing => {
// Dashing - no movement control, just let physics slow it down
}
}
}
fn apply_physics(&mut self, dt: f32) {
// Apply velocities to position
self.x += self.velocity_x * dt;
self.y += self.velocity_y * dt;
// Apply gravity
self.velocity_y += GRAVITY;
// Apply friction for non-walking/running states
if !matches!(self.state, PlayerState::Walking | PlayerState::Running) {
self.velocity_x *= FRICTION;
if close_to_zero(self.velocity_x) {
self.velocity_x = 0.0;
}
}
}
fn check_state_transitions(&mut self) {
// Check for landing
if self.y >= screen_height() - self.height / 2.0 {
self.y = screen_height() - self.height / 2.0;
self.velocity_y = 0.0;
if matches!(
self.state,
PlayerState::Jumping | PlayerState::DoubleJumping
) {
if self.velocity_x.abs() > 0.1 {
// Still has horizontal momentum, so walking
self.state = PlayerState::Walking;
} else {
self.state = PlayerState::Idle;
}
}
}
// End dashing state after velocity slows down
if self.state == PlayerState::Dashing && self.velocity_x.abs() < WALK_SPEED {
if self.velocity_x.abs() > 0.1 {
self.state = PlayerState::Walking;
} else {
self.state = PlayerState::Idle;
}
}
}
fn draw(&self) {
let color = match self.direction {
PlayerDirection::Left => BLUE,
PlayerDirection::Right => RED,
};
draw_rectangle(
self.x - self.width / 2.0,
self.y - self.height / 2.0,
self.width,
self.height,
color,
);
// Maybe add some visual indicator of the current state
let state_text = format!("{:?}", self.state);
draw_text(&state_text, self.x - 30.0, self.y - 20.0, 15.0, WHITE);
}
}
#[macroquad::main("Game")]
async fn main() {
let mut player = Player::new();
let input_handler = InputHandler::new();
clear_background(BLACK);
loop {
// Get inputs - only once per frame
let movement = input_handler.get_movement();
let action = input_handler.get_action();
// Update player with both movement and action inputs
player.update(&movement, &action, get_frame_time());
// Draw
player.draw();
next_frame().await
}
}