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
use nalgebra as na;
use raylib::prelude::*;
use std::time::SystemTime;
use crate::{
discord::{DiscordChannel, DiscordRpcSignal},
global_resource_package::GlobalResources,
model::player::Player,
project_constants::ProjectConstants,
rendering::utilities::{anim_texture::AnimatedTexture, map_render::MapRenderer},
};
#[derive(Debug)]
pub struct PlayableScene {
has_updated_discord_rpc: bool,
player: Player,
world_map: MapRenderer,
camera: raylib::camera::Camera2D,
last_update: SystemTime,
}
impl PlayableScene {
pub fn new(
raylib_handle: &mut raylib::RaylibHandle,
thread: & raylib::RaylibThread,
constants: &ProjectConstants,
) -> Self {
let map_renderer = MapRenderer::new("map_gameMap.tmx", raylib_handle, thread).unwrap();
Self {
has_updated_discord_rpc: false,
player: Player::new(na::Vector2::new(10.0, 10.0)),
world_map: map_renderer,
camera: raylib::camera::Camera2D {
target: raylib::math::Vector2 {
x: 0.0,
y: 0.0,
},
offset: raylib::math::Vector2 {
x: (constants.base_window_size.0 as f32 / 2.0),
y: (constants.base_window_size.1 as f32 / 2.0)
},
rotation: 0.0,
zoom: 1.0
},
last_update: SystemTime::UNIX_EPOCH
}
}
pub async fn render_frame(
&mut self,
raylib: &mut raylib::RaylibHandle,
rl_thread: &raylib::RaylibThread,
discord: &DiscordChannel,
global_resources: &GlobalResources,
constants: &ProjectConstants,
) {
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;
}
let mut draw = raylib.begin_drawing(rl_thread);
draw.clear_background(Color::WHITE);
{
let mut ctx2d = draw.begin_mode2D(self.camera);
self.world_map.render_map(&mut ctx2d, &self.camera, true);
}
for i in 0..100 {
for j in 0..100 {
draw.draw_rectangle(
constants.tile_size as i32 * (i * 2),
constants.tile_size as i32 * (j * 2) * -1,
constants.tile_size as i32,
constants.tile_size as i32,
Color::RED
)
}
}
draw.draw_rectangle(
self.player.position[0] as i32,
self.player.position[1] as i32 * -1,
(constants.tile_size as f32 * self.player.size) as i32,
(constants.tile_size as f32 * self.player.size) as i32,
Color::GREEN
);
}
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 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() < 0.01 {
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);
}
player.position += &player.velocity * delta_time;
}
}