Skip to content

Commit

Permalink
Add Camera Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Oct 14, 2022
1 parent cb2cf8d commit d69c1d4
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 16 deletions.
87 changes: 86 additions & 1 deletion assets/game/camera_controller.ts
Original file line number Diff line number Diff line change
@@ -1 +1,86 @@
export default {};
type PlayerIdx = [usize];
const PlayerIdx: BevyType<PlayerIdx> = {
typeName: "jumpy::player::PlayerIdx",
};

const GameCamera: BevyType<unknown> = {
typeName: "jumpy::camera::GameCamera",
};
const MapMeta: BevyType<{
grid_size: UVec2;
tile_size: UVec2;
}> = {
typeName: "jumpy::metadata::map::MapMeta",
};

type KinematicBody = {
offset: Vec2;
size: Vec2;
velocity: Vec2;
is_on_ground: boolean;
was_on_ground: boolean;
has_mass: boolean;
has_friction: boolean;
bouncyness: f32;
is_deactivated: boolean;
gravity: f32;
};
const KinematicBody: BevyType<KinematicBody> = {
typeName: "jumpy::physics::KinematicBody",
};

const borderX = 150;
const borderY = 200;

export default {
postUpdateInGame() {
const aspect = 16 / 9; // TODO: Use screen aspect ratio
const [mapMeta] = world.query(MapMeta)[0].components;
const mapSize = [
mapMeta.tile_size.x * mapMeta.grid_size.x,
mapMeta.tile_size.y * mapMeta.grid_size.y,
];

const playerComponents = world
.query(PlayerIdx, Transform, KinematicBody)
.map((x) => x.components);
const [_, cameraTransform, projection] = world.query(
GameCamera,
Transform,
OrthographicProjection
)[0].components;

let middlePoint = { x: 0, y: 0 };
let min = { x: 100000, y: 100000 };
let max = { x: -100000, y: -100000 };

const player_count = playerComponents.length;

for (const [playerIdx, playerTransform, body] of playerComponents) {
const playerPos = playerTransform.translation;
middlePoint.x += playerPos.x;
middlePoint.y += playerPos.y;

min.x = Math.min(playerPos.x, min.x);
min.y = Math.min(playerPos.y, min.y);
max.x = Math.max(playerPos.x, max.x);
max.y = Math.max(playerPos.y, max.y);
}

middlePoint.x /= player_count;
middlePoint.y /= player_count;

const scaleX = Math.abs(max.x - min.x) + borderX * 2.0;
let scaleY = Math.abs(max.y - min.y) + borderY * 2.0;

if (scaleX > scaleY * aspect) {
scaleY = scaleX / aspect;
}

let zoom = scaleY / 500;

cameraTransform.translation.x = middlePoint.x;
cameraTransform.translation.y = middlePoint.y;
projection.scale = zoom;
},
};
10 changes: 7 additions & 3 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ pub struct CameraPlugin;

impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(bevy_parallax::ParallaxPlugin);
app.register_type::<GameCamera>()
.register_type::<EditorCamera>()
.add_plugin(bevy_parallax::ParallaxPlugin);
}
}

Expand All @@ -21,10 +23,12 @@ impl GameRenderLayers {
pub const EDITOR: u8 = 2;
}

#[derive(Component)]
#[derive(Reflect, Component, Default)]
#[reflect(Component, Default)]
pub struct GameCamera;

#[derive(Component)]
#[derive(Reflect, Component, Default)]
#[reflect(Component, Default)]
pub struct EditorCamera;

#[derive(Bundle)]
Expand Down
23 changes: 18 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,29 @@ mod physics;
mod platform;
mod player;
mod prelude;
mod run_criteria;
mod scripting;
mod ui;
mod utils;
mod workarounds;
mod run_criteria;

use crate::{
animation::AnimationPlugin, assets::AssetPlugin, camera::CameraPlugin, debug::DebugPlugin,
lines::LinesPlugin, loading::LoadingPlugin, localization::LocalizationPlugin, map::MapPlugin,
metadata::GameMeta, name::NamePlugin, physics::PhysicsPlugin, platform::PlatformPlugin,
player::PlayerPlugin, prelude::*, scripting::ScriptingPlugin, ui::UiPlugin,
animation::AnimationPlugin,
assets::AssetPlugin,
camera::CameraPlugin,
debug::DebugPlugin,
lines::LinesPlugin,
loading::LoadingPlugin,
localization::LocalizationPlugin,
map::MapPlugin,
metadata::{GameMeta, MetadataPlugin},
name::NamePlugin,
physics::PhysicsPlugin,
platform::PlatformPlugin,
player::PlayerPlugin,
prelude::*,
scripting::ScriptingPlugin,
ui::UiPlugin,
workarounds::WorkaroundsPlugin,
};

Expand Down Expand Up @@ -130,6 +142,7 @@ pub fn main() {

// Install game plugins
app.add_plugins(DefaultPlugins)
.add_plugin(MetadataPlugin)
.add_plugin(LinesPlugin)
.add_plugin(PlatformPlugin)
.add_plugin(LoadingPlugin)
Expand Down
8 changes: 8 additions & 0 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ pub use player::*;
pub use settings::*;
pub use ui::*;

pub struct MetadataPlugin;

impl Plugin for MetadataPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(MapMetadataPlugin);
}
}

#[derive(HasLoadProgress, TypeUuid, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
#[uuid = "b14f1630-64d0-4bb7-ba3d-e7b83f8a7f62"]
Expand Down
24 changes: 18 additions & 6 deletions src/metadata/map.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
use super::*;

use bevy::reflect::FromReflect;
use bevy_parallax::{LayerData as ParallaxLayerData, ParallaxResource};

#[derive(Component, HasLoadProgress, TypeUuid, Deserialize, Serialize, Clone, Debug, Default)]
pub struct MapMetadataPlugin;

impl Plugin for MapMetadataPlugin {
fn build(&self, app: &mut App) {
app.register_type::<MapMeta>();
}
}

#[derive(
Reflect, Component, HasLoadProgress, TypeUuid, Deserialize, Serialize, Clone, Debug, Default,
)]
#[reflect(Component, Default)]
#[serde(deny_unknown_fields)]
#[uuid = "8ede98c2-4f17-46f2-bcc5-ae0dc63b2137"]
pub struct MapMeta {
Expand Down Expand Up @@ -33,7 +45,7 @@ impl MapMeta {
}
}

#[derive(HasLoadProgress, Deserialize, Serialize, Clone, Debug)]
#[derive(Reflect, FromReflect, HasLoadProgress, Deserialize, Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct MapLayerMeta {
pub id: String,
Expand All @@ -42,7 +54,7 @@ pub struct MapLayerMeta {
pub entity: Option<Entity>,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(Reflect, FromReflect, Deserialize, Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "snake_case")]
pub enum MapLayerKind {
Expand All @@ -62,7 +74,7 @@ impl HasLoadProgress for MapLayerKind {
}
}

#[derive(HasLoadProgress, Deserialize, Serialize, Clone, Debug, Default)]
#[derive(Reflect, HasLoadProgress, Deserialize, Serialize, Clone, Debug, Default)]
#[serde(deny_unknown_fields)]
pub struct MapTileLayer {
pub tilemap: String,
Expand All @@ -87,14 +99,14 @@ pub struct MapElementSpawn {
pub element_handle: Handle<MapElementMeta>,
}

#[derive(HasLoadProgress, Deserialize, Serialize, Clone, Debug)]
#[derive(Reflect, FromReflect, HasLoadProgress, Deserialize, Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct MapTileMeta {
pub pos: UVec2,
pub idx: u32,
}

#[derive(HasLoadProgress, Deserialize, Serialize, Clone, Debug)]
#[derive(Reflect, FromReflect, HasLoadProgress, Deserialize, Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct ParallaxLayerMeta {
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub struct ButtonBordersMeta {
pub clicked: Option<BorderImageMeta>,
}

#[derive(HasLoadProgress, Default, Clone, Copy, Debug)]
#[derive(Reflect, HasLoadProgress, Default, Clone, Copy, Debug)]
#[has_load_progress(none)]
pub struct ColorMeta(pub Color);

Expand Down

0 comments on commit d69c1d4

Please sign in to comment.