DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: 7981ec061606d1d1fa65617ebc1173a7bc79c9eb deck-builder/client/src/plugins/game/supply/mod.rs -rw-r--r-- 4.8 KiB
7981ec06Jonni Liljamo feat(client): revamp log system 1 year, 7 months ago
                                                                                
de6cc159 skye
de6cc159 skye
0bac8c54 skye
de6cc159 skye
de6cc159 skye
de6cc159 skye
de6cc159 skye
f4d2db42 skye
f4d2db42 skye
f4d2db42 skye
0762ff89 skye
f4d2db42 skye
0762ff89 skye
de6cc159 skye
de6cc159 skye
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
/*
 * 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_mod_picking::prelude::*;
use bevy_rapier3d::prelude::*;

use crate::{
    api::game::{Action, Command},
    game_status::PlayerState,
    plugins::GameActionCreateCallEvent,
    Global,
};

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

pub struct SupplyPlugin;

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

pub struct SpawnSupplyPilesEvent;

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

    // despawn possible existing supply piles
    for entity in pile_query.iter() {
        commands.entity(entity).despawn_recursive();
    }

    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))
            .insert(OnPointer::<Over>::target_component_mut::<Transform>(
                |_over, transform| {
                    transform.translation.y += 0.1;
                },
            ))
            .insert(OnPointer::<Out>::target_component_mut::<Transform>(
                |_over, transform| {
                    transform.translation.y -= 0.1;
                },
            ));
    }

    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;
    }
}

fn handle_clicked_supply_pile(
    mut commands: Commands,
    card_query: Query<
        (Entity, &VisualCard, &visual_card_kind::Supply),
        (With<visual_card_kind::Supply>, With<ClickedCard>),
    >,
    mut gac_ev_w: EventWriter<GameActionCreateCallEvent>,
    mut game_data: ResMut<GameData>,
    global: Res<Global>,
) {
    if game_data.locked {
        return;
    }

    let Ok((entity, card, card_kind)) = card_query.get_single() else {
        return;
    };

    commands.entity(entity).remove::<ClickedCard>();

    let player = game_data
        .game_status
        .as_ref()
        .unwrap()
        .players
        .get(&global.user.as_ref().unwrap().id)
        .unwrap();

    #[allow(clippy::if_same_then_else)]
    if player.state != PlayerState::BuyPhase {
        // we ain't buying rn
        return;
    } else if player.buys == 0 {
        // not enough buys
        return;
    } else if player.currency < card.card.cost {
        // not enough currency
        return;
    } else if game_data
        .game_status
        .as_ref()
        .unwrap()
        .supply_piles
        .get(card_kind.0)
        .unwrap()
        .amount
        == 0
    {
        // no cards in supply
        return;
    }

    game_data.locked = true;
    gac_ev_w.send(GameActionCreateCallEvent {
        action: Action::new(
            &game_data.game.as_ref().unwrap().id,
            &global.user.as_ref().unwrap().id,
            &global.user.as_ref().unwrap().id,
            &Command::TakeFromPile {
                index: card_kind.0,
                for_cost: card.card.cost,
            },
            None,
        ),
    });
}