Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb62c5f2

Browse files
committed
add top down camera experiment
1 parent74cf662 commitb62c5f2

File tree

8 files changed

+111
-39
lines changed

8 files changed

+111
-39
lines changed

‎assets/config.ron‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
(
22
camera: (
3-
speed:0.5,
3+
// top down config
4+
height:20.0,
5+
speed:20.0,
6+
edge_margin:20.0,
7+
zoom_speed:5.0,
8+
min_height:5.0,
9+
max_height:50.0,
410
),
511
player:(
612
spawn_pos: (1.5,60,0.5),

‎crates/game/Cargo.toml‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ edition.workspace = true
77

88
# TODO: figure out the best way to enable stuff like inspector or ui debug option only in dev builds
99
[features]
10-
default = ["dev_native","top_down_camera"]
10+
default = ["dev_native","third_person"]
1111
dev_native = ["bevy-inspector-egui" ]
12-
top_down_camera = []
12+
third_person = ["bevy_third_person_camera"]
13+
top_down = []
1314

1415
[dependencies]
1516
audio = {path ="../audio" }
@@ -19,13 +20,13 @@ models = { path = "../models" }
1920
scene = {path ="../scene" }
2021
ui = {path ="../ui" }
2122

22-
bevy-inspector-egui = {version ="0.31.0",optional =true }
23+
bevy_third_person_camera = {workspace =true,optional =true }
2324

25+
bevy-inspector-egui = {version ="0.31.0",optional =true }
2426
avian3d.workspace =true
2527
bevy.workspace =true
2628
bevy_seedling.workspace =true
2729
bevy_enhanced_input.workspace =true
28-
bevy_third_person_camera.workspace =true
2930
rand.workspace =true
3031

3132
[lints]

‎crates/game/src/camera.rs‎

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,32 @@ use bevy::core_pipeline::{
33
bloom::Bloom,
44
tonemapping::{DebandDither,Tonemapping},
55
};
6-
#[cfg(feature ="top_down_camera")]
7-
use bevy::input::mouse::{MouseButtonInput,MouseMotion};
6+
#[cfg(feature ="top_down")]
7+
use bevy_enhanced_input::prelude::*;
8+
#[cfg(feature ="third_person")]
89
use bevy_third_person_camera::*;
910

1011
pubfnplugin(app:&mutApp){
1112
app.add_systems(Startup, spawn_camera)
12-
.add_systems(OnEnter(Screen::Title), add_skybox_to_camera)
13-
.add_systems(OnEnter(Screen::Gameplay), add_tpv_cam)
13+
.add_systems(OnEnter(Screen::Title), add_skybox_to_camera);
14+
15+
#[cfg(feature ="third_person")]
16+
app.add_systems(OnEnter(Screen::Gameplay), add_tpv_cam)
1417
.add_systems(OnExit(Screen::Gameplay), rm_tpv_cam)
1518
.add_observer(toggle_cam_cursor);
1619

17-
#[cfg(feature ="top_down_camera")]
18-
app.add_systems(Update, camera_mouse_pan);
20+
#[cfg(feature ="top_down")]
21+
app.add_systems(OnEnter(Screen::Gameplay), move_camera_to_top_down)
22+
.add_observer(camera_mouse_pan)
23+
.add_observer(camera_zoom);
1924
}
2025

21-
pubfnspawn_camera(mutcommands:Commands,#[cfg(feature ="top_down_camera")]cfg:Res<Config>){
26+
pubfnspawn_camera(mutcommands:Commands){
2227
commands.spawn((
2328
SceneCamera,
2429
Camera3d::default(),
2530
Msaa::Sample4,
2631
IsDefaultUiCamera,
27-
#[cfg(feature ="top_down_camera")]
28-
Transform::from_xyz(0.0, cfg.player.spawn_pos.1 +50.0,0.0)
29-
.looking_at(cfg.player.spawn_pos.into(),Vec3::X),// X is 'up' in this case
30-
#[cfg(not(feature ="top_down_camera"))]
3132
Transform::from_xyz(100.,50.,100.).looking_at(Vec3::ZERO,Vec3::Y),
3233
Camera{
3334
hdr:true,
@@ -42,6 +43,7 @@ pub fn spawn_camera(mut commands: Commands, #[cfg(feature = "top_down_camera")]
4243
}
4344

4445
// TODO: add top down camera as well
46+
#[cfg(feature ="third_person")]
4547
fnadd_tpv_cam(
4648
cfg:Res<Config>,
4749
mutcommands:Commands,
@@ -81,6 +83,7 @@ fn add_tpv_cam(
8183
Ok(())
8284
}
8385

86+
#[cfg(feature ="third_person")]
8487
fnrm_tpv_cam(mutcommands:Commands,mutcamera:Query<Entity,With<SceneCamera>>){
8588
ifletOk(camera) = camera.single_mut(){
8689
commands
@@ -90,33 +93,85 @@ fn rm_tpv_cam(mut commands: Commands, mut camera: Query<Entity, With<SceneCamera
9093
}
9194
}
9295

96+
#[cfg(feature ="third_person")]
9397
fntoggle_cam_cursor(_:Trigger<CamCursorToggle>,mutcam:Query<&mutThirdPersonCamera>){
9498
letOk(mut cam) = cam.single_mut()else{
9599
return;
96100
};
97101
cam.cursor_lock_active = !cam.cursor_lock_active;
98102
}
99103

100-
/// Pans the camera using right mouse drag
101-
#[cfg(feature ="top_down_camera")]
104+
#[cfg(feature ="top_down")]
105+
fnmove_camera_to_top_down(cfg:Res<Config>,mutcamera:Query<&mutTransform,With<SceneCamera>>){
106+
letOk(mut cam) = camera.single_mut()else{
107+
return;
108+
};
109+
110+
// *cam = Transform::from_xyz(0.0, cfg.player.spawn_pos.1 + 10.0, 0.0)
111+
// .looking_at(cfg.player.spawn_pos.into(), Vec3::Y);
112+
cam.rotate_y(-0.6);
113+
}
114+
115+
/// Pans the camera using mouse drag
116+
#[cfg(feature ="top_down")]
102117
fncamera_mouse_pan(
103118
on:Trigger<Fired<Pan>>,
104-
config:Res<Config>,
105-
mutquery:Query<&mutTransform,With<SceneCamera>>,
119+
time:Res<Time>,
120+
cfg:Res<Config>,
121+
windows:Query<&Window>,
122+
mutcam:Query<&mutTransform,With<SceneCamera>>,
123+
){
124+
letOk(window) = windows.single()else{
125+
return;
126+
};
127+
letSome(cursor_pos) = window.cursor_position()else{
128+
return;
129+
};
130+
letOk(mut cam) = cam.single_mut()else{
131+
return;
132+
};
133+
134+
letmut movement =Vec3::ZERO;
135+
136+
// Check screen borders and set movement direction
137+
if cursor_pos.x <= cfg.camera.edge_margin{
138+
movement +=Vec3::Z;// left edge → move camera left (Z+)
139+
}elseif cursor_pos.x >= window.width() - cfg.camera.edge_margin{
140+
movement += -Vec3::Z;// right edge → move right (Z-)
141+
}
142+
143+
if cursor_pos.y <= cfg.camera.edge_margin{
144+
movement += -Vec3::X;// bottom edge → move down (X-)
145+
}elseif cursor_pos.y >= window.height() - cfg.camera.edge_margin{
146+
movement +=Vec3::X;// top edge → move up (X+)
147+
}
148+
149+
if movement !=Vec3::ZERO{
150+
cam.translation += movement.normalize()* cfg.camera.speed* time.delta_secs();
151+
}
152+
}
153+
154+
#[cfg(feature ="top_down")]
155+
fncamera_zoom(
156+
on:Trigger<Fired<ScrollZoom>>,
157+
cfg:Res<Config>,
158+
player:Query<&mutTransform,(With<Player>,Without<SceneCamera>)>,
159+
mutcam:Query<&mutTransform,With<SceneCamera>>,
106160
){
107-
// let mut delta = Vec2::ZERO;
108-
//
109-
// delta += on.delta;
110-
//
111-
// if delta == Vec2::ZERO {
112-
// return;
113-
// }
114-
//
115-
// let mut transform = query.single_mut();
116-
//
117-
// // Move in XZ plane, based on mouse drag direction
118-
// let right = Vec3::Z * delta.x * config.camera.speed;
119-
// let forward = Vec3::X * -delta.y * config.camera.speed;
120-
//
121-
// transform.translation += right + forward;
161+
letOk(mut cam) = cam.single_mut()else{
162+
return;
163+
};
164+
letOk(player) = player.single()else{
165+
return;
166+
};
167+
168+
info!("scroll: {}", on.value);
169+
170+
let new_height =(cam.translation.y - on.value.y* cfg.camera.zoom_speed)
171+
.clamp(cfg.camera.min_height, cfg.camera.max_height);
172+
173+
cam.translation.y = new_height;
174+
let(x, z) =(cam.translation.x, cam.translation.z);
175+
176+
// cam.look_at(Vec3::new(x, player.translation.y, z), Vec3::X);
122177
}

‎crates/game/src/lib.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use asset_loading::*;
22
use audio::*;
33
use avian3d::prelude::*;
44
use bevy::prelude::*;
5-
use bevy_enhanced_input::prelude::*;
65
use bevy_seedling::prelude::*;
76
use models::*;
87
use scene::*;

‎crates/game/src/mood.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
usesuper::*;
33
use rand::prelude::*;
44

5-
constFADE_TIME:f32 =2.0;
5+
//const FADE_TIME: f32 = 2.0;
66

77
pubfnplugin(app:&mutApp){
88
app.add_systems(OnExit(Screen::Gameplay), stop_soundtrack)

‎crates/models/src/input.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ pub struct Navigate;
9595
#[input_action(output =Vec2, require_reset =true)]
9696
pubstructPan;
9797

98+
#[derive(Debug,InputAction)]
99+
#[input_action(output =Vec2, require_reset =true)]
100+
pubstructScrollZoom;
101+
98102
#[derive(Debug,InputAction)]
99103
#[input_action(output =bool)]
100104
pubstructAttack;
@@ -171,6 +175,8 @@ fn bind_gameplay(
171175
Axial::right_stick().with_modifiers_each((Scale::splat(2.0),Negate::x())),
172176
));
173177

178+
actions.bind::<ScrollZoom>().to(Input::mouse_wheel());
179+
174180
actions.bind::<Pause>().to(KeyCode::KeyP);
175181
actions.bind::<Mute>().to(KeyCode::KeyM);
176182
actions

‎crates/models/src/pre_load.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ pub struct Timers {
8080
#[derive(Clone,Debug,Default,Serialize,Deserialize,Reflect)]
8181
pubstructCamera{
8282
pubspeed:f32,
83+
pubheight:f32,
84+
pubzoom_speed:f32,
85+
pubmin_height:f32,
86+
pubmax_height:f32,
87+
pubedge_margin:f32,
8388
}
8489

8590
#[derive(Clone,Debug,Default,Serialize,Deserialize,Reflect,Asset,Resource)]

‎crates/screens/src/gameplay.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pub(super) fn plugin(app: &mut App) {
1313
.add_observer(clear_modals);
1414
}
1515

16-
fnspawn_gameplay_ui(mutcmds:Commands,textures:Res<Textures>,settings:Res<Settings>){
17-
info!("settings on gameplay enter:{settings:?}");
16+
fnspawn_gameplay_ui(mutcmds:Commands,textures:Res<Textures>,_settings:Res<Settings>){
17+
//info!("settings on gameplay enter:{settings:?}");
1818
let opts =Opts::default().hidden().width(Vw(5.0)).height(Vw(5.0));
1919
cmds.spawn((
2020
StateScoped(Screen::Gameplay),

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp