Skip to content

Commit

Permalink
refactor in game state
Browse files Browse the repository at this point in the history
  • Loading branch information
aratama committed Nov 13, 2024
1 parent 4dcaa7e commit 9536c76
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 92 deletions.
6 changes: 3 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script integrity=sha384-DXfxbHnmRjXl3uhZSP4S1IWEe3DfX/sHDa2A7iHxmUB6Ag+87PaP8wV2AtDOwHfm src=./restart-audio-context-8127143435824044.js type=module></script><link href=assets/favicon.png rel=icon><link href=./index-56627a79e0752a1c.css integrity=sha384-m/ivHNojsD/VAvwMMce+fhrN8VxOZuZt5hms1JExuO36KLCDKKRGDtjLXSlnQq0B rel=stylesheet><script nonce="cdrYqFfZM5LJPsE+pNozpQ==" type=module>import init, * as bindings from './magiacircle-a368992ba380d954.js';
const wasm = await init('./magiacircle-a368992ba380d954_bg.wasm');
<script integrity=sha384-DXfxbHnmRjXl3uhZSP4S1IWEe3DfX/sHDa2A7iHxmUB6Ag+87PaP8wV2AtDOwHfm src=./restart-audio-context-8127143435824044.js type=module></script><link href=assets/favicon.png rel=icon><link href=./index-56627a79e0752a1c.css integrity=sha384-m/ivHNojsD/VAvwMMce+fhrN8VxOZuZt5hms1JExuO36KLCDKKRGDtjLXSlnQq0B rel=stylesheet><script nonce="zMubwU0HnC2WXlxEHA6T1w==" type=module>import init, * as bindings from './magiacircle-48ff9c506442186e.js';
const wasm = await init('./magiacircle-48ff9c506442186e_bg.wasm');


window.wasmBindings = bindings;


dispatchEvent(new CustomEvent("TrunkApplicationStarted", {detail: {wasm}}));</script><title>magiacircle 0.1</title><link crossorigin href=./magiacircle-a368992ba380d954.js integrity=sha384-0QWvyp93b1bmZK8BK09aPZtsyymtJ3P1ukwFU3uCRHIN4T5oDNYdCaqewOxrchen rel=modulepreload><link as=fetch crossorigin href=./magiacircle-a368992ba380d954_bg.wasm integrity=sha384-rFS7nsneGEs0PFrA7NLiDMEe5ubfAtUpwDYPveQuiwRLlyyqBJskaM/MErrlucpR rel=preload type=application/wasm></head><body></body></html>
dispatchEvent(new CustomEvent("TrunkApplicationStarted", {detail: {wasm}}));</script><title>magiacircle 0.1</title><link crossorigin href=./magiacircle-48ff9c506442186e.js integrity=sha384-qQrpiV3nt/pean3QohKiN7h5Yow/eNMNtRgpP00XAzLwISDAZLsy5smOY6g9cFEo rel=modulepreload><link as=fetch crossorigin href=./magiacircle-48ff9c506442186e_bg.wasm integrity=sha384-TvmNHYaxBrFq3VPm1Zte/IIoD0mxQMKWh4omZfQVeRBmgmsLnVrAuml5cDwduraB rel=preload type=application/wasm></head><body></body></html>
1 change: 1 addition & 0 deletions docs/magiacircle-48ff9c506442186e.js

Large diffs are not rendered by default.

Binary file not shown.
1 change: 0 additions & 1 deletion docs/magiacircle-a368992ba380d954.js

This file was deleted.

34 changes: 18 additions & 16 deletions src/controller/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::entity::gold::{spawn_gold, Gold};
use crate::input::{get_direction, get_fire_trigger, MyGamepad};
use crate::inventory_item::InventoryItem;
use crate::states::{GameMenuState, GameState};
use crate::ui::wand_editor::WandEditorRoot;
use bevy::core::FrameCount;
use bevy::input::mouse::MouseWheel;
use bevy::prelude::*;
Expand Down Expand Up @@ -37,14 +36,15 @@ fn move_player(
gamepads: Option<Res<MyGamepad>>,
axes: Res<Axis<GamepadAxis>>,
menu: Res<State<GameMenuState>>,
wand_editor_visible_query: Query<&Visibility, With<WandEditorRoot>>,
) {
if let Ok(mut actor) = player_query.get_single_mut() {
let wand_editor_visible = wand_editor_visible_query.single();
if *menu == GameMenuState::Closed && *wand_editor_visible == Visibility::Hidden {
actor.move_direction = get_direction(keys, axes, &gamepads);
} else {
actor.move_direction = Vec2::ZERO;
match *menu.get() {
GameMenuState::Closed => {
actor.move_direction = get_direction(keys, axes, &gamepads);
}
_ => {
actor.move_direction = Vec2::ZERO;
}
}
}
}
Expand All @@ -67,17 +67,19 @@ fn trigger_bullet(
my_gamepad: Option<Res<MyGamepad>>,
gamepad_buttons: Res<ButtonInput<GamepadButton>>,
menu: Res<State<GameMenuState>>,
wand_editor_visible_query: Query<&Visibility, With<WandEditorRoot>>,
) {
let wand_editor_visible = wand_editor_visible_query.single();
if let Ok(mut player) = player_query.get_single_mut() {
if *menu == GameMenuState::Closed
&& *wand_editor_visible == Visibility::Hidden
&& get_fire_trigger(buttons, gamepad_buttons, &my_gamepad)
{
player.fire_state = ActorFireState::Fire;
} else {
player.fire_state = ActorFireState::Idle;
match *menu.get() {
GameMenuState::Closed => {
if get_fire_trigger(buttons, gamepad_buttons, &my_gamepad) {
player.fire_state = ActorFireState::Fire;
} else {
player.fire_state = ActorFireState::Idle;
}
}
_ => {
player.fire_state = ActorFireState::Idle;
}
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/entity/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use bevy_simple_websocket::{ClientMessage, ReadyState, WebSocketState};
use std::f32::consts::PI;
use uuid::Uuid;

use super::bullet::SpawnBulletProps;

/// ライフを持ち、弾丸のダメージの対象となるエンティティを表します
#[derive(Component)]
pub struct Actor {
Expand Down
2 changes: 1 addition & 1 deletion src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ use crate::page::warp::WarpPagePlugin;
use crate::states::*;
use crate::ui::bar::StatusBarPlugin;
use crate::ui::floating::InventoryItemFloatingPlugin;
use crate::ui::game_menu::GameMenuPlugin;
use crate::ui::hover_color::HoverColorPlugin;
use crate::ui::inventory::InventoryPlugin;
use crate::ui::on_press::OnPressPlugin;
use crate::ui::pause_menu::GameMenuPlugin;
use crate::ui::player_list::PlayerListPlugin;
use crate::ui::spell_information::SpellInformationPlugin;
use crate::ui::wand_editor::WandEditorPlugin;
Expand Down
10 changes: 4 additions & 6 deletions src/hud/pointer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::ui::wand_editor::WandEditorRoot;
use crate::{controller::player::Player, entity::actor::Actor};

use crate::states::GameMenuState;
use crate::{asset::GameAssets, constant::TILE_SIZE, input::MyGamepad, states::GameState};
use crate::{controller::player::Player, entity::actor::Actor};
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_aseprite_ultra::prelude::AsepriteSliceUiBundle;

Expand Down Expand Up @@ -58,7 +57,7 @@ fn update_pointer_by_mouse(
mut player_query: Query<(&mut Actor, &GlobalTransform), With<Player>>,
q_window: Query<&Window, With<PrimaryWindow>>,
camera_query: Query<(&Camera, &GlobalTransform), (With<Camera2d>, Without<Player>)>,
wand_editor_visible_query: Query<&Visibility, With<WandEditorRoot>>,
state: Res<State<GameMenuState>>,
) {
if let Ok((mut player, player_transform)) = player_query.get_single_mut() {
if let Ok(window) = q_window.get_single() {
Expand All @@ -67,8 +66,7 @@ fn update_pointer_by_mouse(
if let Some(mouse_in_world) =
camera.viewport_to_world(camera_global_transform, cursor_in_screen)
{
let wand_editor_visible = wand_editor_visible_query.single();
if *wand_editor_visible == Visibility::Hidden {
if *state.get() == GameMenuState::Closed {
player.pointer = mouse_in_world.origin.truncate()
- player_transform.translation().truncate();
}
Expand Down
6 changes: 4 additions & 2 deletions src/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ pub enum GameMenuState {
#[default]
Closed,

GameMenuOpen,
PauseMenuOpen,

GameMenuClosing,
PauseMenuClosing,

WandEditOpen,
}
2 changes: 1 addition & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub mod bar;
pub mod button;
pub mod floating;
pub mod game_menu;
pub mod hover_color;
pub mod inventory;
pub mod menu_button;
pub mod on_press;
pub mod pause_menu;
pub mod player_list;
pub mod range;
pub mod spell_information;
Expand Down
67 changes: 34 additions & 33 deletions src/ui/game_menu.rs → src/ui/pause_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use bevy::prelude::*;
use bevy_rapier2d::plugin::{PhysicsSet, RapierConfiguration};
use bevy_simple_websocket::ClientMessage;

use super::inventory;
use super::wand_editor::WandEditorRoot;

#[derive(Resource)]
struct ButtonShots {
close: SystemId,
Expand All @@ -27,7 +24,7 @@ struct ButtonShots {
}

#[derive(Component)]
struct GameMenuRoot;
struct PauseMenuRoot;

impl FromWorld for ButtonShots {
fn from_world(world: &mut World) -> Self {
Expand All @@ -44,7 +41,7 @@ impl FromWorld for ButtonShots {
}

fn resume(mut state: ResMut<NextState<GameMenuState>>, mut writer: EventWriter<GameCommand>) {
state.set(GameMenuState::GameMenuClosing);
state.set(GameMenuState::PauseMenuClosing);
writer.send(GameCommand::SEKettei(None));
}

Expand Down Expand Up @@ -92,9 +89,9 @@ fn setup_game_menu(
) {
commands
.spawn((
GameMenuRoot,
PauseMenuRoot,
StateScoped(GameState::InGame),
Name::new("game menu"),
Name::new("Pause Menu"),
NodeBundle {
style: Style {
position_type: PositionType::Absolute,
Expand All @@ -109,7 +106,7 @@ fn setup_game_menu(
row_gap: Val::Px(10.0),
..Default::default()
},
background_color: Color::hsla(0.0, 0.0, 0.1, 0.9).into(),
background_color: Color::hsla(0.0, 0.0, 0.05, 1.0).into(),
z_index: ZIndex::Global(GAME_MENU_Z_INDEX),
visibility: Visibility::Hidden,
..default()
Expand All @@ -130,7 +127,7 @@ fn setup_game_menu(
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Game Menu".to_string(),
"Paused".to_string(),
TextStyle {
font_size: 40.0,
font: assets.dotgothic.clone(),
Expand Down Expand Up @@ -178,13 +175,13 @@ fn setup_game_menu(
fn update_game_menu(
state: Res<State<GameMenuState>>,
mut next: ResMut<NextState<GameMenuState>>,
mut query: Query<&mut Visibility, With<GameMenuRoot>>,
mut query: Query<&mut Visibility, With<PauseMenuRoot>>,
gamepad_buttons: Res<ButtonInput<GamepadButton>>,
my_gamepad: Option<Res<MyGamepad>>,
) {
let mut visibility = query.single_mut();
*visibility = match state.get() {
GameMenuState::GameMenuOpen => Visibility::Visible,
GameMenuState::PauseMenuOpen => Visibility::Visible,
_ => Visibility::Hidden,
};

Expand All @@ -194,8 +191,8 @@ fn update_game_menu(
button_type: GamepadButtonType::Start,
}) {
next.set(match state.get() {
GameMenuState::Closed => GameMenuState::GameMenuOpen,
_ => GameMenuState::GameMenuClosing,
GameMenuState::Closed => GameMenuState::PauseMenuOpen,
_ => GameMenuState::PauseMenuClosing,
});
}
}
Expand All @@ -205,29 +202,32 @@ fn handle_escape_key(
state: Res<State<GameMenuState>>,
mut next: ResMut<NextState<GameMenuState>>,
keys: Res<ButtonInput<KeyCode>>,
mut wand_edit_query: Query<&mut Visibility, With<WandEditorRoot>>,
mut rapier_state: ResMut<RapierConfiguration>,
) {
if keys.just_pressed(KeyCode::Escape) {
let mut wand_edit = wand_edit_query.single_mut();
match *state.get() {
GameMenuState::Closed => {
next.set(GameMenuState::PauseMenuOpen);
}
_ => {
next.set(GameMenuState::Closed);
}
}
}
}

match *wand_edit {
Visibility::Hidden => {
next.set(match state.get() {
GameMenuState::Closed => {
rapier_state.physics_pipeline_active = false;
rapier_state.query_pipeline_active = false;
GameMenuState::GameMenuOpen
}
_ => {
rapier_state.physics_pipeline_active = true;
rapier_state.query_pipeline_active = true;
GameMenuState::GameMenuClosing
}
});
fn switch_physics_activation(
state: Res<State<GameMenuState>>,
mut rapier_state: ResMut<RapierConfiguration>,
) {
if state.is_changed() {
match *state.get() {
GameMenuState::Closed => {
rapier_state.physics_pipeline_active = true;
rapier_state.query_pipeline_active = true;
}
_ => {
*wand_edit = Visibility::Hidden;
rapier_state.physics_pipeline_active = false;
rapier_state.query_pipeline_active = false;
}
}
}
Expand Down Expand Up @@ -263,10 +263,10 @@ fn closing_to_closed(
mut res: ResMut<ButtonShots>,
) {
match state.get() {
GameMenuState::GameMenuOpen => {
GameMenuState::PauseMenuOpen => {
res.wait = 20;
}
GameMenuState::GameMenuClosing => {
GameMenuState::PauseMenuClosing => {
res.wait = (res.wait - 1).max(0);
if res.wait <= 0 {
next.set(GameMenuState::Closed);
Expand All @@ -288,6 +288,7 @@ impl Plugin for GameMenuPlugin {
update_se_volume_label,
update_bgm_volume_label,
handle_escape_key,
switch_physics_activation,
)
.run_if(in_state(GameState::InGame)),
);
Expand Down
2 changes: 1 addition & 1 deletion src/ui/spell_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy_aseprite_ultra::prelude::{AsepriteSlice, AsepriteSliceUiBundle};
use crate::{
asset::GameAssets,
spell::SpellType,
spell_props::{get_spell_appendix, spell_to_props, SpellCast},
spell_props::{get_spell_appendix, spell_to_props},
states::GameState,
};

Expand Down
36 changes: 25 additions & 11 deletions src/ui/wand_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy::prelude::*;
use super::{inventory::spawn_inventory, spell_information::spawn_spell_information};

#[derive(Component)]
pub struct WandEditorRoot;
struct WandEditorRoot;

pub fn spawn_wand_editor(commands: &mut Commands, assets: &Res<GameAssets>) {
commands
Expand Down Expand Up @@ -39,29 +39,43 @@ pub fn spawn_wand_editor(commands: &mut Commands, assets: &Res<GameAssets>) {
});
}

fn toggle_wand_editor_visible(
fn handle_e_key(
keys: Res<ButtonInput<KeyCode>>,
mut query: Query<&mut Visibility, With<WandEditorRoot>>,
state: Res<State<GameMenuState>>,
mut next: ResMut<NextState<GameMenuState>>,
) {
if *state == GameMenuState::Closed && keys.just_pressed(KeyCode::KeyE) {
for mut visibility in query.iter_mut() {
*visibility = match *visibility {
Visibility::Visible => Visibility::Hidden,
Visibility::Hidden => Visibility::Visible,
Visibility::Inherited => Visibility::Visible,
};
if keys.just_pressed(KeyCode::KeyE) {
match state.get() {
GameMenuState::Closed => {
next.set(GameMenuState::WandEditOpen);
}
GameMenuState::WandEditOpen => {
next.set(GameMenuState::Closed);
}
_ => {}
}
}
}

fn apply_wand_editor_visible(
mut query: Query<&mut Visibility, With<WandEditorRoot>>,
state: Res<State<GameMenuState>>,
) {
for mut visibility in query.iter_mut() {
*visibility = match state.get() {
GameMenuState::WandEditOpen => Visibility::Visible,
_ => Visibility::Hidden,
};
}
}

pub struct WandEditorPlugin;

impl Plugin for WandEditorPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
toggle_wand_editor_visible.run_if(in_state(GameState::InGame)),
(handle_e_key, apply_wand_editor_visible).run_if(in_state(GameState::InGame)),
);
}
}
Loading

0 comments on commit 9536c76

Please sign in to comment.