feat: camera boundaries

fix/collision-refresh
hheik 2022-12-09 05:07:42 +02:00
parent 7e127948ac
commit a8a0afcbeb
4 changed files with 42 additions and 20 deletions

View File

@ -8,7 +8,7 @@ use crate::{
}; };
use self::{ use self::{
camera::{CameraFollow, GameCameraPlugin}, camera::{CameraFollow, GameCameraPlugin, WORLD_WIDTH},
kinematic::KinematicPlugin, kinematic::KinematicPlugin,
player::PlayerPlugin, player::PlayerPlugin,
}; };
@ -27,9 +27,9 @@ pub fn init() {
.add_plugin(KinematicPlugin) .add_plugin(KinematicPlugin)
.add_plugin(GameCameraPlugin) .add_plugin(GameCameraPlugin)
.add_plugin(Terrain2DPlugin) .add_plugin(Terrain2DPlugin)
// .add_plugin(PlayerPlugin) .add_plugin(PlayerPlugin)
.add_startup_system(setup_debug_terrain) .add_startup_system(setup_debug_terrain)
.add_startup_system(setup_debug_camera) // .add_startup_system(setup_debug_camera)
.add_system(debug_controls) .add_system(debug_controls)
.run(); .run();
} }
@ -57,8 +57,8 @@ fn setup_debug_camera(mut commands: Commands) {
fn setup_debug_terrain(mut terrain: ResMut<Terrain2D>) { fn setup_debug_terrain(mut terrain: ResMut<Terrain2D>) {
let terrain_gen = TerrainGen2D::new(432678); let terrain_gen = TerrainGen2D::new(432678);
for y in 0..(512 / Chunk2D::SIZE_Y as i32) { for y in 0..(WORLD_WIDTH / Chunk2D::SIZE_Y as i32) {
for x in 0..(512 / Chunk2D::SIZE_X as i32) { for x in 0..(WORLD_WIDTH / Chunk2D::SIZE_X as i32) {
let position = Vector2I { x, y }; let position = Vector2I { x, y };
terrain.add_chunk(position, terrain_gen.gen_chunk(&position)); terrain.add_chunk(position, terrain_gen.gen_chunk(&position));
} }

View File

@ -6,6 +6,8 @@ use bevy_inspector_egui::{Inspectable, RegisterInspectable};
use crate::util::{move_towards_vec3, vec3_lerp}; use crate::util::{move_towards_vec3, vec3_lerp};
pub const WORLD_WIDTH: i32 = 512;
pub struct GameCameraPlugin; pub struct GameCameraPlugin;
impl Plugin for GameCameraPlugin { impl Plugin for GameCameraPlugin {
@ -41,8 +43,9 @@ fn camera_setup(mut commands: Commands) {
Name::new("Camera"), Name::new("Camera"),
Camera2dBundle { Camera2dBundle {
projection: OrthographicProjection { projection: OrthographicProjection {
scaling_mode: ScalingMode::FixedHorizontal(512.0), scaling_mode: ScalingMode::FixedHorizontal(WORLD_WIDTH as f32),
window_origin: WindowOrigin::BottomLeft, window_origin: WindowOrigin::Center,
scale: 0.5,
..default() ..default()
}, },
camera_2d: Camera2d { camera_2d: Camera2d {
@ -57,7 +60,7 @@ fn camera_setup(mut commands: Commands) {
fn camera_system( fn camera_system(
time: Res<Time>, time: Res<Time>,
mut camera_query: Query<&mut Transform, With<Camera2d>>, mut camera_query: Query<(&mut Transform, &OrthographicProjection), With<Camera2d>>,
follow_query: Query<(&Transform, &CameraFollow), Without<Camera2d>>, follow_query: Query<(&Transform, &CameraFollow), Without<Camera2d>>,
) { ) {
let (target, follow) = match follow_query let (target, follow) = match follow_query
@ -68,25 +71,41 @@ fn camera_system(
None => return, None => return,
}; };
for mut camera_transform in camera_query.iter_mut() { // let offset = Vec3::new(WORLD_WIDTH as f32 / 2.0, 0.0, 999.9);
for (mut camera_transform, projection) in camera_query.iter_mut() {
let left_limit = 0.0;
let right_limit = WORLD_WIDTH as f32;
let offset = Vec3::new(0.0, 0.0, 999.9);
match follow.movement { match follow.movement {
FollowMovement::Instant => { FollowMovement::Instant => {
camera_transform.translation = target.translation * Vec3::new(0.0, 1.0, 1.0) camera_transform.translation = target.translation + offset;
} }
FollowMovement::Linear(speed) => { FollowMovement::Linear(speed) => {
camera_transform.translation = move_towards_vec3( camera_transform.translation = move_towards_vec3(
camera_transform.translation, camera_transform.translation,
target.translation * Vec3::new(0.0, 1.0, 1.0), target.translation + offset,
speed * time.delta_seconds(), speed * time.delta_seconds(),
) );
} }
FollowMovement::Smooth(speed) => { FollowMovement::Smooth(speed) => {
camera_transform.translation = vec3_lerp( camera_transform.translation = vec3_lerp(
camera_transform.translation, camera_transform.translation,
target.translation * Vec3::new(0.0, 1.0, 1.0), target.translation + offset,
speed * time.delta_seconds(), speed * time.delta_seconds(),
) );
} }
} }
let camera_x = camera_transform.translation.x;
camera_transform.translation += Vec3::new(
(left_limit - (projection.left * projection.scale + camera_x)).max(0.0),
0.0,
0.0,
);
let camera_x = camera_transform.translation.x;
camera_transform.translation += Vec3::new(
(right_limit - (projection.right * projection.scale + camera_x)).min(0.0),
0.0,
0.0,
);
} }
} }

View File

@ -43,7 +43,7 @@ pub fn player_system(
}; };
kinematic_input.movement = movement; kinematic_input.movement = movement;
kinematic_input.want_jump = input.just_pressed(KeyCode::Space) kinematic_input.want_jump = input.pressed(KeyCode::Space)
} }
fn input_to_axis(negative: bool, positive: bool) -> f32 { fn input_to_axis(negative: bool, positive: bool) -> f32 {
@ -59,8 +59,10 @@ fn input_to_axis(negative: bool, positive: bool) -> f32 {
pub fn player_spawn(mut commands: Commands) { pub fn player_spawn(mut commands: Commands) {
let kinematic = KinematicBundle { let kinematic = KinematicBundle {
collider: Collider::cuboid(8.0, 16.0), collider: Collider::round_cuboid(4.0, 8.0, 1.0),
transform: TransformBundle::default(), transform: TransformBundle::from_transform(Transform::from_translation(Vec3::new(
256.0, 128.0, 0.0,
))),
..default() ..default()
}; };
@ -70,7 +72,7 @@ pub fn player_spawn(mut commands: Commands) {
.insert(SpriteBundle { .insert(SpriteBundle {
sprite: Sprite { sprite: Sprite {
color: Color::rgb(0.75, 0.25, 0.25), color: Color::rgb(0.75, 0.25, 0.25),
custom_size: Some(Vec2 { x: 16.0, y: 32.0 }), custom_size: Some(Vec2 { x: 8.0, y: 16.0 }),
..default() ..default()
}, },
..default() ..default()
@ -81,7 +83,7 @@ pub fn player_spawn(mut commands: Commands) {
}) })
.insert(KinematicInput::default()) .insert(KinematicInput::default())
.insert(CameraFollow { .insert(CameraFollow {
priority: 0, priority: 1,
movement: FollowMovement::Smooth(7.0), movement: FollowMovement::Smooth(7.0),
}); });
} }

View File

@ -17,7 +17,8 @@ type Island = VecDeque<Segment2I>;
lazy_static! { lazy_static! {
pub static ref COLOR_MAP: HashMap<TexelID, [u8; 4]> = { pub static ref COLOR_MAP: HashMap<TexelID, [u8; 4]> = {
let mut map = HashMap::new(); let mut map = HashMap::new();
map.insert(0, [0x03, 0x03, 0x03, 0xff]); map.insert(0, [0x00, 0x00, 0x00, 0x00]);
// map.insert(0, [0x03, 0x03, 0x03, 0xff]);
// map.insert(1, [0x47, 0x8e, 0x48, 0xff]); // map.insert(1, [0x47, 0x8e, 0x48, 0xff]);
map.insert(1, [0x9e, 0x7f, 0x63, 0xff]); map.insert(1, [0x9e, 0x7f, 0x63, 0xff]);
map.insert(2, [0x38, 0x32, 0x2d, 0xff]); map.insert(2, [0x38, 0x32, 0x2d, 0xff]);