DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: 266a4f354ad61f6acba8a12409aff529887b7c55 deck-builder/client/src/plugins/game/supply/mod.rs -rw-r--r-- 2.2 KiB
266a4f35Jonni Liljamo chore(client): fmt, clippy, cleanup 1 year, 5 months 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
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

use super::{
    card::{visual_card_kind, VisualCard, VisualCardBundle},
    GameData,
};

pub struct SupplyPlugin;

impl Plugin for SupplyPlugin {
    fn build(&self, app: &mut App) {
        app.add_event::<SpawnSupplyPilesEvent>()
            .add_event::<PositionSupplyPilesEvent>()
            .add_system(spawn_supply_piles.run_if(on_event::<SpawnSupplyPilesEvent>()))
            .add_system(position_supply_piles.run_if(on_event::<PositionSupplyPilesEvent>()));
    }
}

pub struct SpawnSupplyPilesEvent;

fn spawn_supply_piles(
    mut commands: Commands,
    game_data: Res<GameData>,
    mut psp_ev_w: EventWriter<PositionSupplyPilesEvent>,
) {
    let Some(status) = &game_data.game_status else {
        warn!("game_status was none");
        return;
    };

    for (index, pile) in status.supply_piles.iter().enumerate() {
        commands
            .spawn(VisualCardBundle {
                visual_card: VisualCard {
                    card: pile.card.clone(),
                },
                rigid_body: RigidBody::Fixed,
                ..Default::default()
            })
            .insert(visual_card_kind::Supply(index));
    }

    psp_ev_w.send(PositionSupplyPilesEvent);
}

pub struct PositionSupplyPilesEvent;

fn position_supply_piles(
    mut pile_query: Query<(&VisualCard, &mut Transform), With<visual_card_kind::Supply>>,
) {
    let mut piles: Vec<(&VisualCard, Mut<Transform>)> = pile_query.iter_mut().collect::<Vec<_>>();

    piles.sort_by_key(|(vc, _t)| vc.card.name.clone());
    piles.sort_by_key(|(vc, _t)| vc.card.cost);

    // split piles into top and bottom row
    let mid = piles.len() / 2;
    let (p1, p2) = piles.split_at_mut(mid);

    // keep track of offset between cards
    let mut offset = 0.;

    // top row
    for (_, t) in p1 {
        t.translation.x += offset;
        offset += 1.0;
    }

    // reset offset when changing rows
    offset = 0.;

    // bottom row
    for (_, t) in p2 {
        t.translation.z += 1.5;
        t.translation.x += offset;
        offset += 1.0;
    }
}