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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * librePvZ: game logic implementation.
 * Copyright (c) 2023  Ruifeng Xie
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

//! Seed bank and seed packets.

use std::ops::Deref;
use bevy::prelude::*;
use bevy::asset::Handle;
use bevy_asset_loader::prelude::*;
use crate::scene::loading::AssetState;

/// Assets for the seed bank and seed packets.
#[derive(Debug, AssetCollection, Resource)]
pub struct SeedBankAssets {
    /// Background of various seed packets.
    #[asset(path = "seeds.png")]
    pub seed_packet_background: Handle<TextureAtlasLayout>,
    /// Larger packet background, used in "card bonus" at the end of levels.
    ///
    /// Currently used in the seed bank, because we cannot yet use a [`TextureAtlas`] in
    /// [`ImageBundle`]s. Waiting for [bevyengine/bevy#5103] & [bevyengine/bevy#5070]
    /// (expected in 0.10.0).
    ///
    /// [bevyengine/bevy#5103]: https://github.com/bevyengine/bevy/pull/5103
    /// [bevyengine/bevy#5070]: https://github.com/bevyengine/bevy/pull/5070
    #[asset(path = "SeedPacket_Larger.png")]
    pub seed_packet_large: Handle<Image>,
    /// Silhouette of seed packets.
    #[asset(path = "SeedPacketSilhouette.png")]
    pub seed_packet_silhouette: Handle<Image>,
    /// Background of the seed bank.
    #[asset(path = "SeedBank.png")]
    pub seed_bank_background: Handle<Image>,
}

/// Plugin for the seed bank.
#[derive(Default, Debug, Copy, Clone)]
pub struct SeedBankPlugin;

impl Plugin for SeedBankPlugin {
    fn build(&self, app: &mut App) {
        app.insert_resource(GridInfo::default())
            .add_loading_state(LoadingState::new(AssetState::AssetLoading)
                .continue_to_state(AssetState::AssetReady)
                .on_failure_continue_to_state(AssetState::LoadFailure)
                .load_collection::<SeedBankAssets>())
            .add_systems(OnEnter(AssetState::AssetReady), spawn_seed_bank)
            .add_systems(Update, update_seed_bank.run_if(in_state(AssetState::AssetReady)));
    }
}

/// Description of a seed bank.
#[derive(Debug, Copy, Clone, Component)]
pub struct SeedBank {
    packet_number: usize,
}

/// Marker of the rectangular area for seed packets in a seed bank.
#[derive(Debug, Copy, Clone, Component)]
pub struct SeedPacketArea;

/// Index of a seed packet.
#[derive(Debug, Copy, Clone, Component)]
pub struct SeedPacketIndex(pub usize);

#[derive(Copy, Clone, PartialEq, Resource)]
struct GridInfo {
    position: Vec2,
    bank_size: Vec2,
    packet_size: Vec2,
    packet_separator: f32,
    seed_area_top_left: Vec2,
    natural_packet_count: usize,
    extension_packet_count: usize,
    extension_left_padding: f32,
}

impl Default for GridInfo {
    fn default() -> Self {
        GridInfo {
            position: Vec2::new(230.0, 0.0),
            bank_size: Vec2::new(446.0, 87.0),
            packet_size: Vec2::new(50.0, 70.0),
            packet_separator: 1.0,
            seed_area_top_left: Vec2::new(79.0, 7.0),
            natural_packet_count: 7,
            extension_packet_count: 6,
            extension_left_padding: 4.0,
        }
    }
}

impl GridInfo {
    fn packet_area_width(&self, packet_count: usize) -> f32 {
        packet_count as f32 * (self.packet_size.x + self.packet_separator)
    }

    fn extension_offset(&self, packet_count: usize) -> f32 {
        assert!(packet_count <= self.extension_packet_count);
        let left_cut = self.natural_packet_count - packet_count;
        let left_cut_width = left_cut as f32 * (self.packet_size.x + self.packet_separator);
        self.seed_area_top_left.x + left_cut_width - self.extension_left_padding
    }

    fn extension_width(&self, packet_count: usize) -> f32 {
        self.bank_size.x - self.extension_offset(packet_count)
    }

    fn background_at(&self, index: usize) -> f32 {
        assert_ne!(index, 0);
        self.seed_area_top_left.x + self.packet_area_width(self.natural_packet_count)
            + (index - 1) as f32 * self.packet_area_width(self.extension_packet_count)
            - self.extension_left_padding
    }
}

/// Seed packet content.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SeedPacket {
    /// Gray packet (generated by imitator).
    Gray,
    /// Purple packet (planted on other plants).
    Purple,
    /// Green packet (normal plants).
    Green,
    /// Crater recovery.
    Crater,
    /// Refresh game.
    Refresh,
    /// Sun packet.
    Sun,
    /// Diamond packet.
    Diamond,
    /// Snorkel zombie (in aquarium level).
    Snorkel,
    /// Trophy (goal in mini games).
    Trophy,
}

fn spawn_seed_bank(
    seed_bank_assets: Res<SeedBankAssets>,
    grid_info: Res<GridInfo>,
    mut commands: Commands,
) {
    let bank = commands.spawn((
        SeedBank { packet_number: 10 },
        NodeBundle {
            style: Style {
                left: Val::Px(grid_info.position.x),
                top: Val::Px(grid_info.position.y),
                ..default()
            },
            ..default()
        },
    )).id();
    let container = commands.spawn(NodeBundle {
        background_color: Color::NONE.into(),
        // `BackgroundIndex` uses `u8` internally, 129 guarantees foreground
        z_index: ZIndex::Local(129),
        style: Style {
            flex_direction: FlexDirection::Row,
            position_type: PositionType::Absolute,
            left: Val::Px(grid_info.seed_area_top_left.x),
            top: Val::Px(grid_info.seed_area_top_left.y),
            ..default()
        },
        ..default()
    }).id();
    commands.entity(bank).add_child(container);
    commands.entity(container).with_children(|parent| for i in 0..10 {
        parent.spawn((
            ImageBundle {
                image: UiImage::new(seed_bank_assets.seed_packet_large.clone()),
                style: Style {
                    margin: UiRect {
                        right: Val::Px(grid_info.packet_separator),
                        ..default()
                    },
                    width: Val::Px(grid_info.packet_size.x),
                    height: Val::Px(grid_info.packet_size.y),
                    ..default()
                },
                ..default()
            },
            SeedPacketIndex(i),
        ));
    });
}

#[derive(Copy, Clone, Component)]
struct Clipped;

#[derive(Copy, Clone, Component)]
struct BackgroundIndex(u8);

fn update_seed_bank(
    seed_bank_assets: Res<SeedBankAssets>,
    grid_info: Res<GridInfo>,
    seed_bank: Query<(Entity, &SeedBank, &Children), Changed<SeedBank>>,
    mut background: Query<(Entity, &Children, &mut Style, &mut ZIndex, &BackgroundIndex), Without<Clipped>>,
    mut extension: Query<&mut Style, With<Clipped>>,
    mut commands: Commands,
) {
    for (bank_entity, seed_bank, children) in &seed_bank {
        let remaining = seed_bank.packet_number.saturating_sub(grid_info.natural_packet_count);
        let complete_extensions = remaining / grid_info.extension_packet_count;
        let remaining_packets = remaining % grid_info.extension_packet_count;
        let background_expected = complete_extensions + 1 + if remaining_packets != 0 { 1 } else { 0 };

        let mut background_count = 0;
        for child in children {
            let Ok(child) = background.get_mut(*child) else { continue; };
            let (this, children, mut style, mut z_index, &BackgroundIndex(index)) = child;
            if index as usize >= background_expected {
                commands.entity(this).despawn_recursive();
                continue;
            }
            background_count += 1;
            *z_index = ZIndex::Local(index as i32);
            if index == 0 { continue; }
            style.left = Val::Px(grid_info.background_at(index as usize));
            let complete = index as usize <= complete_extensions;
            let packet_count = if complete {
                grid_info.extension_packet_count
            } else {
                remaining_packets
            };
            style.width = Val::Px(grid_info.extension_width(packet_count));
            let &[clipped] = children.deref() else { unreachable!() };
            let mut clipped = extension.get_mut(clipped).unwrap();
            clipped.left = Val::Px(-grid_info.extension_offset(packet_count));
        }

        let bank_width = Val::Px(grid_info.bank_size.x);
        let bank_height = Val::Px(grid_info.bank_size.y);
        if background_count == 0 {
            let background = commands.spawn((
                BackgroundIndex(0),
                ImageBundle {
                    image: UiImage::new(seed_bank_assets.seed_bank_background.clone()),
                    z_index: ZIndex::Local(0),
                    style: Style {
                        position_type: PositionType::Absolute,
                        left: Val::Px(0.0),
                        top: Val::Px(0.0),
                        width: bank_width,
                        height: bank_height,
                        min_width: bank_width,
                        min_height: bank_height,
                        max_width: bank_width,
                        max_height: bank_height,
                        ..default()
                    },
                    ..default()
                },
            )).id();
            commands.entity(bank_entity).add_child(background);
            background_count += 1;
        }
        for index in background_count..background_expected {
            let complete = index <= complete_extensions;
            let packet_count = if complete {
                grid_info.extension_packet_count
            } else {
                remaining_packets
            };
            let container = commands.spawn((
                BackgroundIndex(index.try_into().unwrap()),
                NodeBundle {
                    z_index: ZIndex::Local(index as i32),
                    style: Style {
                        overflow: Overflow::clip(),
                        position_type: PositionType::Absolute,
                        left: Val::Px(grid_info.background_at(index)),
                        width: Val::Px(grid_info.extension_width(packet_count)),
                        height: Val::Px(grid_info.bank_size.y),
                        ..default()
                    },
                    ..default()
                },
            )).id();
            let image = commands.spawn((
                Clipped,
                ImageBundle {
                    image: UiImage::new(seed_bank_assets.seed_bank_background.clone()),
                    style: Style {
                        left: Val::Px(-grid_info.extension_offset(packet_count)),
                        width: bank_width,
                        height: bank_height,
                        min_width: bank_width,
                        min_height: bank_height,
                        max_width: bank_width,
                        max_height: bank_height,
                        ..default()
                    },
                    ..default()
                },
            )).id();
            commands.entity(bank_entity).add_child(container);
            commands.entity(container).add_child(image);
        }
    }
}