~starkingdoms/starkingdoms

ref: e2c74ff0a4af609afb2869c59835838dd76f639c starkingdoms/crates/unified/src/client/zoom.rs -rw-r--r-- 8.4 KiB
e2c74ff0 — core Revert "aaa ??? ?? ? ?? ???????????????????????????????????????????? i would like to explosion" 10 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use bevy::{
    input::mouse::{MouseScrollUnit, MouseWheel},
    prelude::*,
};

use crate::{
    client::starfield::{BACK_STARFIELD_SIZE, FRONT_STARFIELD_SIZE, MID_STARFIELD_SIZE, StarfieldSize}, ecs::{MainCamera, Me, OrbitCamera, StarfieldBack, StarfieldFront, StarfieldMid}
};
use crate::ecs::{StarguideCamera, GameplayState};

pub fn zoom_plugin(app: &mut App) {
    app.add_systems(Update, on_scroll);
}

fn on_scroll(
    mut scroll_events: MessageReader<MouseWheel>,
    gameplay_state: Res<State<GameplayState>>,
    mut gameplay_next_state: ResMut<NextState<GameplayState>>,
    window: Single<&Window>,
    starfield_back: Single<
        (&mut Sprite, &mut Transform, &mut Visibility, Option<&StarfieldSize>),
        (
            With<StarfieldBack>,
            Without<StarfieldMid>,
            Without<StarfieldFront>,
        ),
    >,
    starfield_mid: Single<
        (&mut Sprite, &mut Transform, &mut Visibility, Option<&StarfieldSize>),
        (
            With<StarfieldMid>,
            Without<StarfieldBack>,
            Without<StarfieldFront>,
        ),
    >,
    starfield_front: Single<
        (&mut Sprite, &mut Transform, &mut Visibility, Option<&StarfieldSize>),
        (
            With<StarfieldFront>,
            Without<StarfieldBack>,
            Without<StarfieldMid>,
        ),
    >,
    mut starguide_camera: Single<
        (&mut Camera, &mut Projection, &mut Transform),
        (
            With<StarguideCamera>,
            Without<MainCamera>,
            Without<Me>,
            Without<StarfieldFront>,
            Without<StarfieldMid>,
            Without<StarfieldBack>,
        ),
    >,
    /*mut orbit_camera: Single<
        &mut Camera,
        (
            With<OrbitCamera>,
            Without<StarguideCamera>,
            Without<MainCamera>,
            Without<Me>,
            Without<StarfieldFront>,
            Without<StarfieldMid>,
            Without<StarfieldBack>,
        ),
    >,*/
    mut camera: Single<
        (&mut Camera, &mut Projection),
        (
            With<MainCamera>,
            Without<OrbitCamera>,
            Without<StarguideCamera>,
            Without<Me>,
            Without<StarfieldFront>,
            Without<StarfieldMid>,
            Without<StarfieldBack>,
        ),
    >,
    player: Single<
        &Transform,
        (
            With<Me>,
            Without<StarfieldFront>,
            Without<StarfieldMid>,
            Without<StarfieldBack>,
            Without<MainCamera>,
        ),
    >,
    /*mut starguide_orbit: Single<
        &mut Transform,
        (
            With<StarguideOrbit>,
            Without<Me>,
            Without<StarfieldFront>,
            Without<StarfieldMid>,
            Without<StarfieldBack>,
            Without<MainCamera>,
            Without<OrbitCamera>,
            Without<StarguideCamera>,
        ),
    >,*/
) {
    let (mut starfield_back, mut starfield_back_pos, mut visibility_back, size_back) =
        starfield_back.into_inner();
    let (mut starfield_mid, mut starfield_mid_pos, mut visibility_mid, size_mid) = starfield_mid.into_inner();
    let (mut starfield_front, mut starfield_front_pos, mut visibility_front, size_front) =
        starfield_front.into_inner();
    let Projection::Orthographic(ref mut camera_projection) = camera.1.clone() else { return };
    let Projection::Orthographic(ref mut starguide_projection) = starguide_camera.1.clone() else { return };
    for event in scroll_events.read() {
        match event.unit {
            MouseScrollUnit::Line | MouseScrollUnit::Pixel => {
                if event.y > 0.0 {
                    camera_projection.scale *= 0.97;
                    starguide_projection.scale *= 0.97;
                } else {
                    camera_projection.scale *= 1.03;
                    starguide_projection.scale *= 1.03;
                }
                //starguide_orbit.scale = Vec3::splat(starguide_projection.scale);

                if camera_projection.scale > 20.0 && matches!(gameplay_state.get(), GameplayState::Main) {
                    camera.0.is_active = false;
                    starguide_camera.0.is_active = true;
                    //orbit_camera.is_active = true;

                    starguide_camera.2.translation = player.translation;
                    //starguide_orbit.translation = player.translation;
                    gameplay_next_state.set(GameplayState::Starguide);

                    starfield_back.image_mode = SpriteImageMode::Auto;
                    starfield_mid.image_mode = SpriteImageMode::Auto;
                    starfield_front.image_mode = SpriteImageMode::Auto;
                    *visibility_back = Visibility::Hidden;
                    *visibility_mid = Visibility::Hidden;
                    *visibility_front = Visibility::Hidden;
                } else if camera_projection.scale <= 20.0 && matches!(gameplay_state.get(), GameplayState::Starguide) {
                    camera.0.is_active = true;
                    gameplay_next_state.set(GameplayState::Main);
                    starguide_camera.0.is_active = false;
                    //orbit_camera.is_active = false;

                    if matches!(*visibility_back, Visibility::Hidden) {
                        if let Some(size_back) = size_back {
                            starfield_back.image_mode = SpriteImageMode::Tiled {
                                tile_x: true,
                                tile_y: true,
                                stretch_value: size_back.0,
                            };
                        }
                        if let Some(size_mid) = size_mid {
                            starfield_mid.image_mode = SpriteImageMode::Tiled {
                                tile_x: true,
                                tile_y: true,
                                stretch_value: size_mid.0,
                            };
                        }
                        if let Some(size_front) = size_front{
                            starfield_front.image_mode = SpriteImageMode::Tiled {
                                tile_x: true,
                                tile_y: true,
                                stretch_value: size_front.0,
                            };
                        }
                    }
                    *visibility_back = Visibility::Inherited;
                    *visibility_mid = Visibility::Inherited;
                    *visibility_front = Visibility::Inherited;
                }
                starfield_back.custom_size =
                    Some(window.size() * camera_projection.scale + Vec2::splat(BACK_STARFIELD_SIZE * 2.0));
                starfield_mid.custom_size =
                    Some(window.size() * camera_projection.scale + Vec2::splat(MID_STARFIELD_SIZE * 2.0));
                starfield_front.custom_size =
                    Some(window.size() * camera_projection.scale + Vec2::splat(FRONT_STARFIELD_SIZE * 2.0));
                starfield_back_pos.translation = player.translation
                    + (-player.translation / 3.0) % BACK_STARFIELD_SIZE
                    + (Vec3::new(window.resolution.width(), -window.resolution.height(), 0.0)
                        * camera_projection.scale
                        / 2.0)
                        % BACK_STARFIELD_SIZE
                    + Vec3::new(0.0, BACK_STARFIELD_SIZE, 0.0)
                    - Vec3::new(0.0, 0.0, 5.0);
                starfield_mid_pos.translation = player.translation
                    + (-player.translation / 2.5) % MID_STARFIELD_SIZE
                    + (Vec3::new(window.resolution.width(), -window.resolution.height(), 0.0)
                        * camera_projection.scale
                        / 2.0)
                        % MID_STARFIELD_SIZE
                    + Vec3::new(0.0, MID_STARFIELD_SIZE, 0.0)
                    - Vec3::new(0.0, 0.0, 4.5);
                starfield_front_pos.translation = player.translation
                    + (-player.translation / 2.0) % FRONT_STARFIELD_SIZE
                    + (Vec3::new(window.resolution.width(), -window.resolution.height(), 0.0)
                        * camera_projection.scale
                        / 2.0)
                        % FRONT_STARFIELD_SIZE
                    + Vec3::new(0.0, FRONT_STARFIELD_SIZE, 0.0)
                    - Vec3::new(0.0, 0.0, 4.0);
            }
        }
    }
    *camera.1 = Projection::Orthographic(camera_projection.clone());
    *starguide_camera.1 = Projection::Orthographic(starguide_projection.clone());
}