Skip to content

Commit

Permalink
feat: Now the camera follows the player
Browse files Browse the repository at this point in the history
  • Loading branch information
uggla committed May 5, 2024
1 parent 8e370dd commit 1160a7d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
28 changes: 27 additions & 1 deletion src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bevy::prelude::*;

use crate::{
level::{CurrentLevel, Level},
player::Player,
state::AppState,
};
pub struct CameraPlugin;
Expand All @@ -13,7 +14,11 @@ impl Plugin for CameraPlugin {
OnEnter(AppState::GameCreate),
move_camera_to_level_start_screen,
)
.add_systems(OnEnter(AppState::StartMenu), move_camera_to_center);
.add_systems(OnEnter(AppState::StartMenu), move_camera_to_center)
.add_systems(
Update,
camera_follows_player.run_if(in_state(AppState::GameRunning)),
);
}
}

Expand Down Expand Up @@ -43,3 +48,24 @@ fn move_camera_to_level_start_screen(
}
}
}

fn camera_follows_player(
player_query: Query<&Transform, With<Player>>,
mut camera_query: Query<&mut Transform, (With<Camera2d>, Without<Player>)>,
current_level: Res<CurrentLevel>,
levels: Query<&Level, With<Level>>,
) {
let mut camera = camera_query.single_mut();
let player = player_query.single();

for level in levels.iter() {
if level.id == current_level.id {
dbg!(&player.translation);
camera.translation = dbg!(level
.map
.move_camera(camera.translation.xy(), player.translation.xy())
.extend(0.0));
break;
}
}
}
25 changes: 14 additions & 11 deletions src/screen_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Map {
vec![p0, p1, p2, p3]
}

fn move_camera(&self, old_pos: Vec2, new_pos: Vec2) -> Vec2 {
pub fn move_camera(&self, old_pos: Vec2, new_pos: Vec2) -> Vec2 {
let mut camera_pos = old_pos;
let direction = new_pos - old_pos;

Expand All @@ -162,8 +162,11 @@ impl Map {

let camera_points = self.get_camera_points_coords(Vec2::new(new_pos.x, old_pos.y));

if direction.x == 0.0 {
// Do nothing because we're not moving horizontally
} else if
// move to right
if direction.x > 0.0
direction.x > 0.0
&& self.check_points(
&camera_points,
Direction::get_points(Direction::Right),
Expand Down Expand Up @@ -219,19 +222,19 @@ impl Map {
(p1, p2): (usize, usize),
old_pos: Vec2,
) -> bool {
dbg!(self.get_screen(camera_points[p1]).is_some())
&& dbg!(self.get_screen(camera_points[p2]).is_some())
&& dbg!(self.get_screen(camera_points[p1]).unwrap().allowed_screen)
&& dbg!(self.get_screen(camera_points[p2]).unwrap().allowed_screen)
&& dbg!(self.get_screen(old_pos).is_some())
&& dbg!(!self
self.get_screen(camera_points[p1]).is_some()
&& self.get_screen(camera_points[p2]).is_some()
&& self.get_screen(camera_points[p1]).unwrap().allowed_screen
&& self.get_screen(camera_points[p2]).unwrap().allowed_screen
&& self.get_screen(old_pos).is_some()
&& !self
.get_screen(old_pos)
.unwrap()
.contains(&camera_points[p1]))
&& dbg!(!self
.contains(&camera_points[p1])
&& !self
.get_screen(old_pos)
.unwrap()
.contains(&camera_points[p2]))
.contains(&camera_points[p2])
}

pub fn get_start_screen(&self) -> &Screen {
Expand Down

0 comments on commit 1160a7d

Please sign in to comment.