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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use chrono::{DateTime, Utc};
use nalgebra as na;
use raylib::prelude::*;
use std::time::SystemTime;
use crate::{
asset_manager::{load_music_from_internal_data, load_sound_from_internal_data},
discord::{DiscordChannel, DiscordRpcSignal},
global_resource_package::GlobalResources,
model::{
player::Player,
world_object::{ObjectCollider, WorldSpaceObjectCollider},
},
project_constants::ProjectConstants,
rendering::utilities::{anim_texture::AnimatedTexture, map_render::MapRenderer},
};
use super::main_menu::MenuStateSignal;
#[derive(Debug)]
pub struct PlayableScene {
pub has_updated_discord_rpc: bool,
player: Player,
world_map: MapRenderer,
camera: raylib::camera::Camera2D,
last_update: SystemTime,
game_soundtrack: Music,
world_colliders: Vec<WorldSpaceObjectCollider>,
show_debug_info: bool,
play_start_time: DateTime<Utc>,
player_start_position: na::Vector2<f32>,
}
impl PlayableScene {
pub fn new(
raylib_handle: &mut raylib::RaylibHandle,
thread: &raylib::RaylibThread,
constants: &ProjectConstants,
) -> Self {
let map_renderer = MapRenderer::new(
"map_gameMap.tmx",
"map_gameMap.objects.json",
"map_gameMap.end.json",
raylib_handle,
thread,
)
.unwrap();
let world_colliders = map_renderer.get_world_colliders();
let player_start_position = na::Vector2::new(
10.0 * constants.tile_size as f32,
-10.0 * constants.tile_size as f32,
);
let game_soundtrack =
load_music_from_internal_data(thread, "assets/audio/gameSoundtrack.mp3").unwrap();
let player = Player::new(
raylib_handle,
thread,
player_start_position,
);
Self {
has_updated_discord_rpc: false,
player_start_position,
world_map: map_renderer,
player,
camera: raylib::camera::Camera2D {
target: raylib::math::Vector2 { x: 0.0, y: 0.0 },
offset: raylib::math::Vector2 { x: 0.0, y: 0.0 },
rotation: 0.0,
zoom: 1.0,
},
last_update: SystemTime::now(),
game_soundtrack,
world_colliders,
show_debug_info: false,
play_start_time: Utc::now(),
}
}
pub async fn render_frame(
&mut self,
raylib: &mut raylib::RaylibHandle,
rl_thread: &raylib::RaylibThread,
discord: &DiscordChannel,
global_resources: &GlobalResources,
constants: &ProjectConstants,
audio_subsystem: &mut RaylibAudio,
) -> MenuStateSignal {
if !self.has_updated_discord_rpc {
discord
.send(DiscordRpcSignal::BeginGameTimer)
.await
.unwrap();
discord
.send(DiscordRpcSignal::ChangeDetails {
details: "Playing the game".to_string(),
party_status: None,
})
.await
.unwrap();
self.has_updated_discord_rpc = true;
self.play_start_time = Utc::now();
self.player.position = self.player_start_position;
self.player.velocity = na::Vector2::new(0.0, 0.0);
}
if !audio_subsystem.is_music_playing(&self.game_soundtrack) {
debug!("Playing game soundtrack");
audio_subsystem.play_music_stream(&mut self.game_soundtrack);
} else {
audio_subsystem.update_music_stream(&mut self.game_soundtrack);
}
let mut draw = raylib.begin_drawing(rl_thread);
draw.clear_background(Color::WHITE);
self.draw_world(&mut draw, constants);
self.draw_ui(&mut draw, constants);
if self.player.size < 0.15 {
return MenuStateSignal::DoMeltedDeathCutscene {
playtime: Utc::now().signed_duration_since(self.play_start_time),
};
}
if self
.world_map
.is_point_inside_win_zone(self.player.position)
{
return MenuStateSignal::DoFinishedCutscene {
playtime: Utc::now().signed_duration_since(self.play_start_time),
};
}
if draw.is_key_pressed(KeyboardKey::KEY_ESCAPE) {
return MenuStateSignal::DoPauseMenu;
} else {
return MenuStateSignal::StartGame;
}
}
pub fn draw_world(&mut self, draw: &mut RaylibDrawHandle, constants: &ProjectConstants) {
let mut ctx2d = draw.begin_mode2D(self.camera);
self.world_map.render_map(
&mut ctx2d,
&self.camera,
self.show_debug_info,
self.player.position,
);
let player_size =
(constants.tile_size as f32 * constants.player.start_size * self.player.size) as i32;
ctx2d.draw_rectangle(
self.player.position[0] as i32 - player_size / 2,
self.player.position[1] as i32 * -1 - player_size / 2,
player_size,
player_size,
Color::LIGHTBLUE,
);
}
pub fn draw_ui(&mut self, draw: &mut RaylibDrawHandle, constants: &ProjectConstants) {
let mouse_x = draw.get_mouse_x();
let mouse_y = draw.get_mouse_y();
let current_temperature = self.world_map.sample_temperature_at(
self.player.position.component_mul(&na::Vector2::new(1.0, -1.0))
);
let mut current_temperature_val: f32 = -247.51879;
if let Some(val) = current_temperature {
current_temperature_val = val;
}
if draw.is_key_pressed(KeyboardKey::KEY_F3) {
self.show_debug_info = !self.show_debug_info;
}
if self.show_debug_info {
draw.draw_fps(10, 10);
draw.draw_text(
format!("Mouse position: ({}, {})", mouse_x, mouse_y).as_str(),
10,
30,
20,
Color::GREEN,
);
draw.draw_text(
format!(
"player: ({}, {}) size: {} map: ({}, {})",
self.player.position.x,
self.player.position.y,
self.player.size,
self.world_map.get_map_size().x,
self.world_map.get_map_size().y
)
.as_str(),
10,
50,
20,
Color::GREEN,
);
}
self.world_map
.render_hud_endgoal_arrow(draw, self.player.position, &self.camera);
let melt_amount = (current_temperature_val)/(-247.51879);
draw.draw_text(
format!("Funny Temperature: ({})[{}]", current_temperature_val, melt_amount).as_str(),
10, 10, 20, Color::PAPAYAWHIP
);
}
pub async fn update_physics(
&mut self,
raylib: &raylib::RaylibHandle,
constants: &ProjectConstants,
) {
let time = SystemTime::now();
let elapsed = time
.duration_since(self.last_update)
.expect("Time Appears to Have Moved Backwards!");
self.last_update = time;
let delta_time = elapsed.as_millis() as f32 / 1000.0;
let player = &mut self.player;
let current_friction = self.world_map.sample_friction_at(player.position);
let current_temperature = self.world_map.sample_temperature_at(
player.position.component_mul(&na::Vector2::new(1.0, -1.0))
);
let map_size = self.world_map.get_map_size();
let h_axis = raylib.is_key_down(KeyboardKey::KEY_D) as i8
- raylib.is_key_down(KeyboardKey::KEY_A) as i8;
let v_axis = raylib.is_key_down(KeyboardKey::KEY_W) as i8
- raylib.is_key_down(KeyboardKey::KEY_S) as i8;
if h_axis != 0 || v_axis != 0 {
let direction = na::Vector2::new(h_axis as f32, v_axis as f32).normalize();
player.velocity += &direction.xy()
* constants.player.acceleration as f32
* constants.tile_size as f32
* delta_time;
}
if player.velocity.magnitude() != 0.0 {
player.velocity -= player.velocity.normalize()
* constants.player.deceleration as f32
* constants.tile_size as f32
* delta_time;
if player.velocity.magnitude() < 1.0 {
player.velocity.set_magnitude(0.0);
}
}
if ((constants.player.max_velocity * constants.tile_size) as f32)
< player.velocity.magnitude()
{
player
.velocity
.set_magnitude((constants.player.max_velocity * constants.tile_size) as f32);
}
let velocity_modifier = &player.velocity * delta_time;
let player_size =
(constants.tile_size as f32 * constants.player.start_size * player.size / 2.0) as f32;
player.position.x += velocity_modifier.x;
for i in &self.world_colliders {
if player.position.x - player_size <= i.position.x + i.size.x / 2.0
&& player.position.x + player_size >= i.position.x - i.size.x / 2.0
&& player.position.y - player_size <= i.position.y + i.size.y / 2.0
&& player.position.y + player_size >= i.position.y - i.size.y / 2.0
{
player.position.x -= velocity_modifier.x;
player.velocity.x = 0.0;
break;
}
}
if player.position.x - player_size < 0.0
|| player.position.x + player_size > self.world_map.get_map_size().x
{
if player.velocity.x < 0.0 {
player.position.x = player_size;
} else if player.velocity.x > 0.0 {
player.position.x = self.world_map.get_map_size().x - player_size;
}
player.velocity.x = 0.0;
}
player.position.y += velocity_modifier.y;
for i in &self.world_colliders {
if player.position.x - player_size <= i.position.x + i.size.x / 2.0
&& player.position.x + player_size >= i.position.x - i.size.x / 2.0
&& player.position.y - player_size <= i.position.y + i.size.y / 2.0
&& player.position.y + player_size >= i.position.y - i.size.y / 2.0
{
player.position.y -= velocity_modifier.y;
player.velocity.y = 0.0;
break;
}
}
if player.position.y + player_size > 0.0
|| player.position.y - player_size < -self.world_map.get_map_size().y
{
if player.velocity.y > 0.0 {
player.position.y = -player_size;
} else if player.velocity.y < 0.0 {
player.position.y = -self.world_map.get_map_size().y + player_size;
}
player.velocity.y = 0.0;
}
let mut current_temperature_val: f32 = -247.51879;
if let Some(val) = current_temperature {
current_temperature_val = val - 273.15;
}
let melt_amount = constants.player.melt_speed * (current_temperature_val)/(-247.51879);
player.size -= melt_amount * delta_time;
self.update_camera(raylib);
}
pub fn update_camera(&mut self, raylib: &raylib::RaylibHandle) {
let bbox = na::Vector2::new(0.2, 0.2);
let bbox_screen_min: raylib::math::Vector2 = (((na::Vector2::new(1.0, 1.0) - bbox) * 0.5)
.component_mul(&na::Vector2::new(
raylib.get_screen_width() as f32,
raylib.get_screen_height() as f32,
)))
.into();
let bbox_screen_max: raylib::math::Vector2 = (((na::Vector2::new(1.0, 1.0) + bbox) * 0.5)
.component_mul(&na::Vector2::new(
raylib.get_screen_width() as f32,
raylib.get_screen_height() as f32,
)))
.into();
let mut bbox_world_min = raylib.get_screen_to_world2D(bbox_screen_min, self.camera);
let mut bbox_world_max = raylib.get_screen_to_world2D(bbox_screen_max, self.camera);
bbox_world_min.y *= -1.0;
bbox_world_max.y *= -1.0;
self.camera.offset = bbox_screen_min;
if self.player.position.x < bbox_world_min.x {
self.camera.target.x = self.player.position.x;
}
if self.player.position.y > bbox_world_min.y {
self.camera.target.y = -self.player.position.y;
}
if self.player.position.x > bbox_world_max.x {
self.camera.target.x = bbox_world_min.x + (self.player.position.x - bbox_world_max.x);
}
if self.player.position.y < bbox_world_max.y {
self.camera.target.y = bbox_world_max.y - (self.player.position.y + bbox_world_min.y);
}
}
}