/*
* 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 crate::{
api::game::{Game, GameState},
game_status::GameStatus,
AppState, Global,
};
use self::{hand::SpawnHandEvent, supply::SpawnSupplyPilesEvent, ui::log::UpdateLogEvent};
use super::GameDetailsCallEvent;
mod card;
mod hand;
mod supply;
mod ui;
pub struct GamePlugin;
impl Plugin for GamePlugin {
fn build(&self, app: &mut App) {
app.register_type::<GameData>()
.register_type::<Game>()
.register_type::<GameState>()
.init_resource::<GameData>()
.add_plugin(ui::GameUIPlugin)
.add_plugin(card::CardPlugin)
.add_plugin(supply::SupplyPlugin)
.add_plugin(hand::HandPlugin)
.add_event::<RefreshGameEvent>()
.add_event::<FinishedRefreshGameEvent>()
.add_system(game_setup.in_schedule(OnEnter(AppState::InGame)))
.add_system(handle_refresh_game_event.run_if(on_event::<RefreshGameEvent>()))
.add_system(
handle_finished_refresh_game_event.run_if(on_event::<FinishedRefreshGameEvent>()),
);
}
}
#[derive(Resource, Clone, Reflect)]
#[reflect(Resource)]
pub struct GameData {
pub game: Option<Game>,
pub game_status: Option<GameStatus>,
pub locked: bool,
}
impl Default for GameData {
fn default() -> Self {
Self {
game: None,
game_status: None,
locked: true,
}
}
}
fn game_setup(mut commands: Commands, mut rg_ev_w: EventWriter<RefreshGameEvent>) {
rg_ev_w.send(RefreshGameEvent);
// create the playing surface
commands
.spawn(Collider::cuboid(20., 0.5, 20.))
.insert(TransformBundle::from(Transform::from_xyz(0., -0.5, 0.)));
}
pub struct RefreshGameEvent;
fn handle_refresh_game_event(
mut details_ev_w: EventWriter<GameDetailsCallEvent>,
mut game_data: ResMut<GameData>,
global: Res<Global>,
) {
game_data.locked = true;
game_data.game = None;
game_data.game_status = None;
details_ev_w.send(GameDetailsCallEvent {
game_id: global.cur_game_id.clone(),
});
}
pub struct FinishedRefreshGameEvent;
fn handle_finished_refresh_game_event(
mut ssp_ev_w: EventWriter<SpawnSupplyPilesEvent>,
mut sh_ev_w: EventWriter<SpawnHandEvent>,
mut ul_ev_w: EventWriter<UpdateLogEvent>,
) {
ssp_ev_w.send(SpawnSupplyPilesEvent);
sh_ev_w.send(SpawnHandEvent);
ul_ev_w.send(UpdateLogEvent);
}