DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: b8bd27dd7fcbade514bc75433e7a0ed068be3f65 deck-builder/client/src/plugins/game/ui/state_button.rs -rw-r--r-- 5.2 KiB
b8bd27ddJonni Liljamo feat(client): finish new refresh, impl game lock 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
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
/*
 * 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 crate::{AppState, plugins::{GameData, GameActionCreateCallEvent}, Global, game_status::PlayerState, api::game::{Action, Command}, seed_gen};

pub struct StateButtonPlugin;

impl Plugin for StateButtonPlugin {
    fn build(&self, app: &mut App) {
        app.add_system(setup_state_button.in_schedule(OnEnter(AppState::InGame)))
            .add_system(update_state_button);
    }
}

const NORMAL_BUTTON: Color = Color::rgb(0.15, 0.15, 0.15);
const HOVERED_BUTTON: Color = Color::rgb(0.25, 0.25, 0.25);
const PRESSED_BUTTON: Color = Color::rgb(0.35, 0.75, 0.35);

#[derive(Component)]
struct StateButton;

#[derive(Component)]
struct StateButtonText;

fn setup_state_button(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
) {
    commands.spawn((
        NodeBundle {
            style: Style {
                position_type: PositionType::Absolute,
                position: UiRect {
                    bottom: Val::Px(20.),
                    right: Val::Px(20.),
                    ..Default::default()
                },
                ..Default::default()
            },
            ..Default::default()
        },
    ))
    .with_children(|parent| {
        parent
            .spawn((ButtonBundle {
                style: Style {
                    size: Size::new(Val::Px(150.), Val::Px(65.)),
                    // center child text
                    justify_content: JustifyContent::Center,
                    align_items: AlignItems::Center,
                    ..Default::default()
                },
                background_color: NORMAL_BUTTON.into(),
                ..Default::default()
            }, StateButton
            ))
            .with_children(|parent| {
                parent.spawn((
                    TextBundle::from_section(
                        "Pass",
                        TextStyle {
                            font: asset_server.load("fonts/FiraMono-Bold.ttf"),
                            font_size: 40.,
                            color: Color::WHITE,
                        },
                    ),
                    StateButtonText,
                ));
            });

    });
}

fn update_state_button(
    mut interaction_query: Query<
        (&Interaction, &mut BackgroundColor, &mut Visibility, &Children),
        (Changed<Interaction>, With<StateButton>)
    >,
    mut text_query: Query<&mut Text>,
    mut game_data: ResMut<GameData>,
    global: Res<Global>,
    mut gac_ev_w: EventWriter<GameActionCreateCallEvent>,
) {
    for (interaction, mut color, mut visibility, children) in &mut interaction_query {
        // NOTE: horrible clones because of borrow funnies
        let Some(game) = game_data.clone().game else {
            return;
        };
        let Some(status) = game_data.clone().game_status else {
            return;
        };
        let Some(user) = &global.user else {
            return;
        };
        let player = status.players.get(&user.id).unwrap();

        let mut text = text_query.get_mut(children[0]).unwrap();

        match player.state {
            PlayerState::Idle => {
                *visibility = Visibility::Hidden;
                return;
            }
            PlayerState::PlayPhase => {
                text.sections[0].value = "Skip Play".to_string();
            }
            PlayerState::BuyPhase => {
                text.sections[0].value = "End Turn".to_string();
            }
        }
        *visibility = Visibility::Inherited;

        if game_data.locked {
            // input locked rn, so don't show any signs of interactivity
            return;
        }

        match interaction {
            Interaction::Clicked => {
                *color = PRESSED_BUTTON.into();

                game_data.locked = true;
                match player.state {
                    PlayerState::PlayPhase => {
                        gac_ev_w.send(GameActionCreateCallEvent {
                            action: Action::new(
                                &game.id,
                                &user.id,
                                &user.id,
                                &Command::ChangePlayerState {
                                    state: PlayerState::BuyPhase,
                                },
                                seed_gen!(),
                            ),
                        });
                    }
                    PlayerState::BuyPhase => {
                        gac_ev_w.send(GameActionCreateCallEvent {
                            action: Action::new(
                                &game.id,
                                &user.id,
                                &user.id,
                                &Command::EndTurn { },
                                seed_gen!(),
                            ),
                        });
                    }
                    _ => {}
                }
            }
            Interaction::Hovered => {
                *color = HOVERED_BUTTON.into();
            }
            Interaction::None => {
                *color = NORMAL_BUTTON.into();
            }
        }
    }
}