diff --git a/assets/worlds/mainworld.json b/assets/worlds/mainworld.json index 45a78a5..b9a4ce8 100644 --- a/assets/worlds/mainworld.json +++ b/assets/worlds/mainworld.json @@ -3,6 +3,10 @@ "x": 10000.0, "y": 10000.0 }, + "player_spawn": { + "x": 200.0, + "y": 100.0 + }, "fish": [ { "x": 500.0, diff --git a/src/gamecore.rs b/src/gamecore.rs index a29e2fa..d8216b7 100644 --- a/src/gamecore.rs +++ b/src/gamecore.rs @@ -107,6 +107,7 @@ impl GameCore { world: World, progress: GameProgress, ) -> Self { + let player = Player::new(&world.player_spawn); Self { state: GameState::Loading, last_state: GameState::Loading, @@ -117,13 +118,13 @@ impl GameCore { .expect("Failed to load game assets. Can not launch!"), master_camera: Camera2D { offset: Vector2::zero(), - target: Vector2::zero(), + target: world.player_spawn, rotation: 0.0, zoom: 2.0, }, show_simple_debug_info: false, world: world, - player: Player::new(), + player, progress: progress, } } diff --git a/src/logic/ingame/mod.rs b/src/logic/ingame/mod.rs index 910faa7..4f81a8b 100644 --- a/src/logic/ingame/mod.rs +++ b/src/logic/ingame/mod.rs @@ -36,8 +36,8 @@ impl InGameScreen { height: game_core.resources.cave_mid_layer.height as f32, }; let world_bounds = Rectangle { - x: -200.0, - y: -100.0, + x: 0.0, + y: 0.0, width: game_core.resources.cave_mid_layer.width as f32, height: game_core.resources.cave_mid_layer.height as f32, }; @@ -56,6 +56,18 @@ impl InGameScreen { Color::WHITE, ); } + + fn render_colliders( + &mut self, + context_2d: &mut RaylibMode2D, + game_core: &mut GameCore, + ) { + + // Render every collider + for collider in game_core.world.colliders.iter() { + context_2d.draw_rectangle_lines_ex(collider, 2, Color::RED); + } + } } impl Screen for InGameScreen { @@ -95,6 +107,7 @@ impl Screen for InGameScreen { // Render the world self.render_world(&mut context_2d, game_core); + self.render_colliders(&mut context_2d, game_core); // Render entities let mut fish = &mut game_core.world.fish; diff --git a/src/logic/ingame/playerlogic.rs b/src/logic/ingame/playerlogic.rs index ff7855f..4e8059e 100644 --- a/src/logic/ingame/playerlogic.rs +++ b/src/logic/ingame/playerlogic.rs @@ -129,8 +129,20 @@ pub fn update_player_movement( // Only do this if the mouse is far enough away let player_real_movement = game_core.player.direction * speed_multiplier; if raw_movement_direction.distance_to(Vector2::zero()) > game_core.player.size.y / 2.0 { - game_core.player.position += player_real_movement; game_core.player.is_moving = true; + + // Check for any collisions + for collider in game_core.world.colliders.iter() { + if collider.check_collision_point_rec(game_core.player.position + player_real_movement) + { + // game_core.player.is_moving = false; + break; + } + } + + if game_core.player.is_moving { + game_core.player.position += player_real_movement; + } } else { game_core.player.is_moving = false; } @@ -146,12 +158,15 @@ pub fn update_player_movement( game_core.master_camera.target += player_real_movement; } + // If the player is not on screen, snap the camera to them + if player_screen_position.distance_to(window_center).abs() > window_center.y { + game_core.master_camera.target = game_core.player.position - (window_center / 2.0); + } + // // Clamp camera target y to 0 // if game_core.master_camera.target.y < -100.0 { // game_core.master_camera.target.y = -100.0; // } - - } pub fn render_player(context_2d: &mut RaylibMode2D, game_core: &mut GameCore) { diff --git a/src/player.rs b/src/player.rs index 95c34a2..b541685 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,7 +1,5 @@ use raylib::math::Vector2; - - #[derive(Debug, Default)] pub struct Player { pub position: Vector2, @@ -12,20 +10,17 @@ pub struct Player { pub breath_percent: f32, pub is_moving: bool, pub is_boosting: bool, - pub is_boost_charging: bool + pub is_boost_charging: bool, } impl Player { - pub fn new() -> Self { + pub fn new(spawn: &Vector2) -> Self { Self { boost_percent: 1.0, - size: Vector2 { - x: 11.0, - y: 21.0 - }, + size: Vector2 { x: 11.0, y: 21.0 }, breath_percent: 1.0, + position: spawn.clone(), ..Default::default() - } } -} \ No newline at end of file +} diff --git a/src/world.rs b/src/world.rs index 2df41fd..29cbf70 100644 --- a/src/world.rs +++ b/src/world.rs @@ -10,6 +10,7 @@ use crate::entities::fish::FishEntity; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct World { pub end_position: Vector2, + pub player_spawn: Vector2, #[serde(rename = "fish")] pub fish_positions: Vec,