From 5787e539b2d007bf9a4b214c5f1fa255214f44c5 Mon Sep 17 00:00:00 2001 From: Si Bartha Date: Sat, 2 Apr 2022 00:06:46 -0400 Subject: [PATCH 1/3] Drawing a Box --- game/dist/project-constants.json | 3 ++- game/game_logic/src/model/player.rs | 4 +++- game/game_logic/src/project_constants.rs | 3 +++ game/game_logic/src/scenes/player_interaction.rs | 10 ++++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/game/dist/project-constants.json b/game/dist/project-constants.json index 3064e0eb..6f9303c7 100644 --- a/game/dist/project-constants.json +++ b/game/dist/project-constants.json @@ -5,6 +5,7 @@ 720 ], "target_fps": 60, + "tile_size": 128, "discord": { "app_id": 954413081918857276, "artwork": { @@ -16,4 +17,4 @@ "details.main_menu": "In the main menu" } } -} \ No newline at end of file +} diff --git a/game/game_logic/src/model/player.rs b/game/game_logic/src/model/player.rs index 2bb87f3e..69a7fe9f 100644 --- a/game/game_logic/src/model/player.rs +++ b/game/game_logic/src/model/player.rs @@ -5,6 +5,7 @@ use nalgebra as na; pub struct Player { pub position: na::Vector2, pub velocity: na::Vector2, + pub size: f32, } impl Player { @@ -14,7 +15,8 @@ impl Player { Self { position, velocity: na::Vector2::zeros(), + size: 1.0, } } -} \ No newline at end of file +} diff --git a/game/game_logic/src/project_constants.rs b/game/game_logic/src/project_constants.rs index 4350a297..b0a32061 100644 --- a/game/game_logic/src/project_constants.rs +++ b/game/game_logic/src/project_constants.rs @@ -42,4 +42,7 @@ pub struct ProjectConstants { /// The target framerate of the game pub target_fps: u32, + + /// The size of the game tiles + pub tile_size: u8, } diff --git a/game/game_logic/src/scenes/player_interaction.rs b/game/game_logic/src/scenes/player_interaction.rs index db34e0bd..fb7e25e9 100644 --- a/game/game_logic/src/scenes/player_interaction.rs +++ b/game/game_logic/src/scenes/player_interaction.rs @@ -61,7 +61,17 @@ impl PlayableScene { // Clear the screen draw.clear_background(Color::WHITE); + draw.draw_rectangle_lines( + 0, + 0, + (constants.tile_size as f32 * self.player.size) as i32, + (constants.tile_size as f32 * self.player.size) as i32, + Color::GREEN + ); + // TODO: Render stuff // self.player. } } + + From e55b54a3138379f723c6db1192e415e950e10532 Mon Sep 17 00:00:00 2001 From: Si Bartha Date: Sat, 2 Apr 2022 12:29:26 -0400 Subject: [PATCH 2/3] Funny slippery guyy --- game/game_logic/src/project_constants.rs | 2 +- game/game_logic/src/scenes/mod.rs | 3 + .../src/scenes/player_interaction.rs | 67 ++++++++++++++++--- 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/game/game_logic/src/project_constants.rs b/game/game_logic/src/project_constants.rs index b0a32061..bd821f43 100644 --- a/game/game_logic/src/project_constants.rs +++ b/game/game_logic/src/project_constants.rs @@ -44,5 +44,5 @@ pub struct ProjectConstants { pub target_fps: u32, /// The size of the game tiles - pub tile_size: u8, + pub tile_size: u32, } diff --git a/game/game_logic/src/scenes/mod.rs b/game/game_logic/src/scenes/mod.rs index 940ccafd..bd0d0280 100644 --- a/game/game_logic/src/scenes/mod.rs +++ b/game/game_logic/src/scenes/mod.rs @@ -56,6 +56,9 @@ impl SceneRenderDelegate { self.scene_playable .render_frame(raylib, rl_thread, &discord, global_resources, constants) .await; + self.scene_playable + .update_physics(raylib) + .await; } } diff --git a/game/game_logic/src/scenes/player_interaction.rs b/game/game_logic/src/scenes/player_interaction.rs index fb7e25e9..cd1d269d 100644 --- a/game/game_logic/src/scenes/player_interaction.rs +++ b/game/game_logic/src/scenes/player_interaction.rs @@ -2,6 +2,7 @@ use nalgebra as na; use raylib::prelude::*; +use std::time::SystemTime; use crate::{ discord::{DiscordChannel, DiscordRpcSignal}, @@ -15,26 +16,41 @@ use crate::{ pub struct PlayableScene { has_updated_discord_rpc: bool, player: Player, + camera: raylib::camera::Camera2D, + last_update: SystemTime, } impl PlayableScene { /// Construct a new `PlayableScene` pub fn new( - raylib_handle: &mut RaylibHandle, - thread: &RaylibThread, + raylib_handle: &mut raylib::RaylibHandle, + thread: & raylib::RaylibThread, constants: &ProjectConstants, ) -> Self { Self { has_updated_discord_rpc: false, player: Player::new(na::Vector2::new(10.0, 10.0)), + 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 } } /// Handler for each frame pub async fn render_frame( &mut self, - raylib: &mut RaylibHandle, - rl_thread: &RaylibThread, + raylib: &mut raylib::RaylibHandle, + rl_thread: &raylib::RaylibThread, discord: &DiscordChannel, global_resources: &GlobalResources, constants: &ProjectConstants, @@ -61,16 +77,49 @@ impl PlayableScene { // Clear the screen draw.clear_background(Color::WHITE); - draw.draw_rectangle_lines( - 0, - 0, + 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 ); + } + + // Physics + pub async fn update_physics( + &mut self, + raylib: & raylib::RaylibHandle, + ) { + let time = SystemTime::now(); + if time + .duration_since(self.last_update) + .expect("Time Appears to Have Moved Backwards!") + .as_millis() < 16 + { + return + } + + self.last_update = time; - // TODO: Render stuff - // self.player. + 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; + let direction = na::Vector2::new(h_axis as f32, v_axis as f32); + player.velocity += &direction; + player.position += &player.velocity; } } From 84633f00a0c942d7644383e83ee29dd16bc61f91 Mon Sep 17 00:00:00 2001 From: Si Bartha Date: Sat, 2 Apr 2022 14:24:40 -0400 Subject: [PATCH 3/3] Phunny Fysics --- game/dist/project-constants.json | 5 +++ game/game_logic/src/project_constants.rs | 17 ++++++++ game/game_logic/src/scenes/mod.rs | 2 +- .../src/scenes/player_interaction.rs | 43 ++++++++++++++----- 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/game/dist/project-constants.json b/game/dist/project-constants.json index 6f9303c7..cd2d0e59 100644 --- a/game/dist/project-constants.json +++ b/game/dist/project-constants.json @@ -16,5 +16,10 @@ "details.sm_failure": "Game went FUBAR", "details.main_menu": "In the main menu" } + }, + "player": { + "max_velocity": 3, + "acceleration": 2, + "deceleration": 1 } } diff --git a/game/game_logic/src/project_constants.rs b/game/game_logic/src/project_constants.rs index bd821f43..0797baf2 100644 --- a/game/game_logic/src/project_constants.rs +++ b/game/game_logic/src/project_constants.rs @@ -28,6 +28,20 @@ pub struct DiscordConstants { pub strings: HashMap, } +/// Constants relating to the Player +#[derive(Debug, Deserialize)] +pub struct PlayerConstants { + + /// Maximum velocity, tiles per second + pub max_velocity: u32, + + /// Acceleration, tiles per second per second + pub acceleration: u32, + + /// Deceleration, tiles per second per second + pub deceleration: u32, +} + /// This structure is filled with the contents of `dist/project-constants.json` at runtime #[derive(Debug, Deserialize)] pub struct ProjectConstants { @@ -40,6 +54,9 @@ pub struct ProjectConstants { /// The Discord constants pub discord: DiscordConstants, + /// The Player constants + pub player: PlayerConstants, + /// The target framerate of the game pub target_fps: u32, diff --git a/game/game_logic/src/scenes/mod.rs b/game/game_logic/src/scenes/mod.rs index bd0d0280..c9d77ab2 100644 --- a/game/game_logic/src/scenes/mod.rs +++ b/game/game_logic/src/scenes/mod.rs @@ -57,7 +57,7 @@ impl SceneRenderDelegate { .render_frame(raylib, rl_thread, &discord, global_resources, constants) .await; self.scene_playable - .update_physics(raylib) + .update_physics(raylib, constants) .await; } } diff --git a/game/game_logic/src/scenes/player_interaction.rs b/game/game_logic/src/scenes/player_interaction.rs index cd1d269d..30128dc4 100644 --- a/game/game_logic/src/scenes/player_interaction.rs +++ b/game/game_logic/src/scenes/player_interaction.rs @@ -102,24 +102,45 @@ impl PlayableScene { pub async fn update_physics( &mut self, raylib: & raylib::RaylibHandle, + constants: &ProjectConstants, ) { + + // Get time since last physics update let time = SystemTime::now(); - if time - .duration_since(self.last_update) - .expect("Time Appears to Have Moved Backwards!") - .as_millis() < 16 - { - return - } - + 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; // Physics will be scaled by this value let player = &mut self.player; + + // Get input direction components 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; - let direction = na::Vector2::new(h_axis as f32, v_axis as f32); - player.velocity += &direction; - player.position += &player.velocity; + if h_axis != 0 || v_axis != 0 { + // Normalize input and accelerate in desired direction + 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; } }