Module game_logic::scenes::player_interaction [−][src]
Expand description
This scene encompasses all of the game where the player can walk around.
+Module player_interaction
Module game_logic::scenes::player_interaction [−][src]
Expand description
This scene encompasses all of the game where the player can walk around.
Structs
Fields
has_updated_discord_rpc: bool
player: Player
world_map: MapRenderer
camera: Camera2D
last_update: SystemTime
game_soundtrack: Music
Implementations
pub fn new(
raylib_handle: &mut RaylibHandle,
thread: &RaylibThread,
constants: &ProjectConstants
) -> Self
pub fn new(
raylib_handle: &mut RaylibHandle,
thread: &RaylibThread,
constants: &ProjectConstants
) -> Self
Construct a new PlayableScene
pub async fn render_frame(
&mut self,
raylib: &mut RaylibHandle,
rl_thread: &RaylibThread,
discord: &Sender<DiscordRpcSignal>,
global_resources: &GlobalResources,
constants: &ProjectConstants,
audio_subsystem: &mut RaylibAudio
)
pub async fn render_frame(
&mut self,
raylib: &mut RaylibHandle,
rl_thread: &RaylibThread,
discord: &Sender<DiscordRpcSignal>,
global_resources: &GlobalResources,
constants: &ProjectConstants,
audio_subsystem: &mut RaylibAudio
)
Handler for each frame
-Trait Implementations
Formats the value using the given formatter. Read more
+}Fields
has_updated_discord_rpc: bool
player: Player
world_map: MapRenderer
camera: Camera2D
last_update: SystemTime
game_soundtrack: Music
Implementations
pub fn new(
raylib_handle: &mut RaylibHandle,
thread: &RaylibThread,
constants: &ProjectConstants
) -> Self
pub fn new(
raylib_handle: &mut RaylibHandle,
thread: &RaylibThread,
constants: &ProjectConstants
) -> Self
Construct a new PlayableScene
pub async fn render_frame(
&mut self,
raylib: &mut RaylibHandle,
rl_thread: &RaylibThread,
discord: &Sender<DiscordRpcSignal>,
global_resources: &GlobalResources,
constants: &ProjectConstants,
audio_subsystem: &mut RaylibAudio
)
pub async fn render_frame(
&mut self,
raylib: &mut RaylibHandle,
rl_thread: &RaylibThread,
discord: &Sender<DiscordRpcSignal>,
global_resources: &GlobalResources,
constants: &ProjectConstants,
audio_subsystem: &mut RaylibAudio
)
Handler for each frame
+Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for PlayableScene
impl !Send for PlayableScene
impl !Sync for PlayableScene
impl Unpin for PlayableScene
impl UnwindSafe for PlayableScene
Blanket Implementations
Mutably borrows from an owned value. Read more
diff --git a/rustdoc/src/game_logic/scenes/player_interaction.rs.html b/rustdoc/src/game_logic/scenes/player_interaction.rs.html index e69507b3..902fed6e 100644 --- a/rustdoc/src/game_logic/scenes/player_interaction.rs.html +++ b/rustdoc/src/game_logic/scenes/player_interaction.rs.html @@ -202,6 +202,46 @@ 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//! This scene encompasses all of the game where the player can walk around.
use nalgebra as na;
@@ -248,13 +288,16 @@
Self {
has_updated_discord_rpc: false,
- player: Player::new(na::Vector2::new(10.0, 10.0)),
+ player: Player::new(na::Vector2::new(10.0 * constants.tile_size as f32, -10.0 * constants.tile_size as f32)),
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),
+ 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,
@@ -397,11 +440,48 @@
self.update_camera(raylib);
}
- pub fn update_camera(&mut self, raylib: &raylib::RaylibHandle) {
- self.camera.target = self.player.position.into();
- self.camera.target.y *= -1.0;
- self.camera.offset.x = raylib.get_screen_width() as f32 / 2.0;
- self.camera.offset.y = raylib.get_screen_height() as f32 / 2.0;
+ // Update the camera
+ pub fn update_camera(
+ &mut self,
+ raylib: & raylib::RaylibHandle,
+ ) {
+
+ // Bounding box
+ let bbox = na::Vector2::new(0.2, 0.2);
+
+ // Get bounding box dimensions on the screen
+ 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();
+
+ // Get bounding box in world space
+ 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);
+
+ // Invert y
+ 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);
+ }
}
}