Merge pull request #22 from Ewpratten/camera-movement

Camera movement
This commit is contained in:
Evan Pratten 2022-04-02 18:46:12 -04:00 committed by GitHub
commit 928dea652e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 24 deletions

View File

@ -20,6 +20,7 @@
"player": { "player": {
"max_velocity": 3, "max_velocity": 3,
"acceleration": 2, "acceleration": 2,
"deceleration": 1 "deceleration": 1,
"start_size": 0.8
} }
} }

View File

@ -40,6 +40,9 @@ pub struct PlayerConstants {
/// Deceleration, tiles per second per second /// Deceleration, tiles per second per second
pub deceleration: u32, pub deceleration: u32,
/// Starting size of player in tiles
pub start_size: f32,
} }
/// This structure is filled with the contents of `dist/project-constants.json` at runtime /// This structure is filled with the contents of `dist/project-constants.json` at runtime

View File

@ -81,33 +81,48 @@ impl PlayableScene {
// Clear the screen // Clear the screen
draw.clear_background(Color::WHITE); draw.clear_background(Color::WHITE);
self.draw_world(&mut draw, constants);
{ self.draw_ui(&mut draw, constants);
// Begin camera mode }
let mut ctx2d = draw.begin_mode2D(self.camera);
// Render the map pub fn draw_world(
self.world_map.render_map(&mut ctx2d, &self.camera, true); &mut self,
} draw: &mut RaylibDrawHandle,
constants: &ProjectConstants,
) {
// Begin camera mode
let mut ctx2d = draw.begin_mode2D(self.camera);
for i in 0..100 { // Render the map
for j in 0..100 { self.world_map.render_map(&mut ctx2d, &self.camera, true);
draw.draw_rectangle(
constants.tile_size as i32 * (i * 2), let player_size = (constants.tile_size as f32 * constants.player.start_size * self.player.size) as i32;
constants.tile_size as i32 * (j * 2) * -1,
constants.tile_size as i32,
constants.tile_size as i32,
Color::RED
)
}
}
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,
) {
draw.draw_rectangle( draw.draw_rectangle(
self.player.position[0] as i32, draw.get_screen_width() / 2 - 225, 0,
self.player.position[1] as i32 * -1, 450, 40,
(constants.tile_size as f32 * self.player.size) as i32, Color::WHITE
(constants.tile_size as f32 * self.player.size) as i32, );
Color::GREEN draw.draw_text(
"Unregistered HyperCam 2",
draw.get_screen_width() / 2 - 215, 0,
32, Color::BLACK
); );
} }
@ -143,7 +158,7 @@ impl PlayableScene {
* constants.player.deceleration as f32 * constants.player.deceleration as f32
* constants.tile_size as f32 * constants.tile_size as f32
* delta_time; * delta_time;
if player.velocity.magnitude() < 0.01 { if player.velocity.magnitude() < 1.0 {
player.velocity.set_magnitude(0.0); player.velocity.set_magnitude(0.0);
} }
} }
@ -154,6 +169,18 @@ impl PlayableScene {
} }
player.position += &player.velocity * delta_time; player.position += &player.velocity * delta_time;
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;
} }
} }