Skip to content

Commit 6035f12

Browse files
committed
fix: bugfixes
1 parent 39f5b7d commit 6035f12

File tree

4 files changed

+62
-36
lines changed

4 files changed

+62
-36
lines changed

examples/simple/src/main.rs

+44-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use bevy::{color::palettes::css, prelude::*};
1+
use std::ops::Deref;
2+
3+
use bevy::{color::palettes::css, prelude::*, transform::commands};
24
use bevy_egui::{egui, EguiContexts, EguiPlugin};
35
use bevy_ogle::{prelude::*, OglePlugin};
46
use rand::random;
@@ -11,42 +13,40 @@ fn main() {
1113
.add_plugins(DefaultPlugins)
1214
.add_plugins(EguiPlugin)
1315
.add_plugins(OglePlugin)
14-
.add_systems(Startup, setup_background)
16+
.add_systems(Startup, setup_scene)
1517
.add_systems(Startup, |mut commands: Commands| {
1618
commands.spawn(Camera2dBundle::default());
17-
// Create target, begin following it.
18-
let entity = commands
19-
.spawn((ThingToFollow, SpatialBundle::default()))
20-
.id();
21-
commands.ogle_change_mode(OgleMode::Frozen);
22-
commands.ogle_target_entity(entity);
2319
})
2420
.add_systems(Update, move_target)
2521
.add_systems(Update, control_camera_ui)
2622
.run();
2723
}
2824

29-
fn setup_background(mut commands: Commands) {
30-
let n = 20;
31-
let spacing = 50.;
32-
let offset = spacing * n as f32 / 2.;
33-
let custom_size = Some(Vec2::new(spacing, spacing));
34-
for x in 0..n {
35-
for y in 0..n {
36-
let x = x as f32 * spacing - offset;
37-
let y = y as f32 * spacing - offset;
38-
let color = Color::hsl(240., random::<f32>() * 0.3, random::<f32>() * 0.3);
39-
commands.spawn(SpriteBundle {
40-
sprite: Sprite {
41-
color,
42-
custom_size,
43-
..default()
44-
},
45-
transform: Transform::from_xyz(x, y, 0.),
25+
fn setup_scene(mut commands: Commands) {
26+
// Background
27+
commands.spawn(SpriteBundle {
28+
sprite: Sprite {
29+
color: css::LIME.into(),
30+
custom_size: Some(Vec2::new(500.0, 500.0)),
31+
..default()
32+
},
33+
transform: Transform::from_xyz(0.0, 0.0, 0.),
34+
..default()
35+
});
36+
37+
// Moving thing for the camera to follow
38+
commands.spawn((
39+
ThingToFollow,
40+
SpriteBundle {
41+
sprite: Sprite {
42+
color: css::RED.into(),
43+
custom_size: Some(Vec2::new(5.0, 5.0)),
4644
..default()
47-
});
48-
}
49-
}
45+
},
46+
transform: Transform::from_xyz(0.0, 0.0, 0.),
47+
..default()
48+
},
49+
));
5050
}
5151

5252
fn move_target(
@@ -61,8 +61,10 @@ fn move_target(
6161
}
6262

6363
fn control_camera_ui(
64+
mut commands: Commands,
6465
mut contexts: EguiContexts,
6566
query_thing: Query<Entity, With<ThingToFollow>>,
67+
target: Res<OgleTarget>,
6668
mode: Res<State<OgleMode>>,
6769
mut next_mode: ResMut<NextState<OgleMode>>,
6870
) {
@@ -71,6 +73,7 @@ fn control_camera_ui(
7173
.resizable(false)
7274
.title_bar(true);
7375
window.show(contexts.ctx_mut(), |ui| {
76+
ui.heading("Mode");
7477
let mut set_mode = mode.clone();
7578
if ui
7679
.radio_value(&mut set_mode, OgleMode::Frozen, "Frozen")
@@ -87,5 +90,18 @@ fn control_camera_ui(
8790
{
8891
next_mode.set(set_mode);
8992
}
93+
94+
ui.separator();
95+
ui.heading("Mode");
96+
let target_entity = query_thing.single();
97+
if ui.radio(*target == OgleTarget::None, "None").clicked() {
98+
commands.ogle_clear_target();
99+
}
100+
if ui
101+
.radio(*target == OgleTarget::Entity(target_entity), "Entity")
102+
.clicked()
103+
{
104+
commands.ogle_target_entity(target_entity);
105+
}
90106
});
91107
}

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use commands::OgleCommandExt;
88
mod plugin;
99
pub use plugin::OglePlugin;
1010

11-
#[derive(Resource, Debug)]
11+
#[derive(Resource, Debug, PartialEq)]
1212
pub enum OgleTarget {
1313
Position(Vec2),
1414
Entity(Entity),
@@ -17,7 +17,7 @@ pub enum OgleTarget {
1717

1818
impl Default for OgleTarget {
1919
fn default() -> Self {
20-
Self::Position(Vec2 { x: 0.0, y: 0.0 })
20+
Self::None
2121
}
2222
}
2323

src/plugin.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use bevy_pancam::{PanCam, PanCamPlugin};
44
use dolly::prelude::*;
55

66
// TODO: These should be configurable and play well with pancam
7-
const MIN_ZOOM: f32 = 0.01;
8-
const MAX_ZOOM: f32 = 3.0;
7+
const MIN_ZOOM: f32 = -0.99;
8+
const MAX_ZOOM: f32 = 1000.0;
99
const ZOOM_SPEED: f32 = 2.0;
1010

1111
#[derive(Default)]
@@ -24,7 +24,9 @@ impl Plugin for OglePlugin {
2424
choreograph_target.run_if(in_state(OgleMode::Choreographed)),
2525
)
2626
.add_systems(OnEnter(OgleMode::Pancam), on_enter_pancam)
27-
.add_systems(OnExit(OgleMode::Pancam), on_exit_pancam);
27+
.add_systems(OnExit(OgleMode::Pancam), on_exit_pancam)
28+
.add_systems(OnEnter(OgleMode::Following), on_enter_following)
29+
.add_systems(OnExit(OgleMode::Following), on_exit_following);
2830
}
2931
}
3032

@@ -49,6 +51,14 @@ fn choreograph_target() {
4951
todo!("handle camera choreographs, like following a spline")
5052
}
5153

54+
fn on_enter_following() {
55+
info!("Enabling following");
56+
}
57+
58+
fn on_exit_following() {
59+
info!("Disabling following");
60+
}
61+
5262
fn follow_target(
5363
time: Res<Time>,
5464
target: Res<OgleTarget>,
@@ -72,7 +82,7 @@ fn follow_target(
7282
rig.driver_mut::<Position>().position = mint::Point3 {
7383
x: pos.x,
7484
y: pos.y,
75-
z: 0.0,
85+
z: 1.0,
7686
};
7787
}
7888
OgleTarget::Entity(e) => {
@@ -83,7 +93,7 @@ fn follow_target(
8393
rig.driver_mut::<Position>().position = mint::Point3 {
8494
x: transform.translation.x,
8595
y: transform.translation.y,
86-
z: 0.0,
96+
z: 1.0,
8797
};
8898
}
8999
OgleTarget::None => {}

src/rig.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl Default for OgleRig {
99
let pos = mint::Point3 {
1010
x: 0.0,
1111
y: 0.0,
12-
z: 0.0,
12+
z: 1.0,
1313
};
1414
Self(
1515
CameraRig::builder()

0 commit comments

Comments
 (0)