Skip to content

Commit

Permalink
feat!: Cast only spells that are unlocked, add spell debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
PraxTube committed Dec 18, 2023
1 parent eab95ef commit a1d221a
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 27 deletions.
3 changes: 3 additions & 0 deletions src/enemy/slime/collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ fn fireball_collisions(
continue;
};

if fireball.disabled {
continue;
}
fireball.disabled = true;

let dir = (slime_transform.translation - fireball_transform.translation)
Expand Down
16 changes: 15 additions & 1 deletion src/item/item_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::f32::consts::PI;

use bevy::prelude::*;

use crate::GameAssets;
use crate::{spell::Spell, GameAssets};

use super::{
enemy_sub_spawner::{EnemySubSpawner, SpawnFormation},
Expand Down Expand Up @@ -72,6 +72,20 @@ pub fn item_wall_offset(item: &Item) -> f32 {
}
}

pub fn item_spell(item: &Item) -> Spell {
match item {
Item::NotImplemented => Spell::Flub,
Item::Tutorial => Spell::Fireball,
Item::IgnisPila => Spell::IgnisPila,
Item::InfernoPila => Spell::InfernoPila,
Item::Fulgur => Spell::Fulgur,
Item::ScutumGlaciei => Spell::ScutumGlaciei,
Item::AerTracto => Spell::AerTracto,
Item::AerPello => Spell::AerPello,
Item::FulgurAvis => Spell::FulgurAvis,
}
}

pub fn statue_sub_spawner(statue: &Statue) -> Vec<(f32, EnemySubSpawner)> {
match statue.item {
Item::NotImplemented => Vec::new(),
Expand Down
15 changes: 0 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ use bevy::window::{PresentMode, Window, WindowMode};

use bevy_asset_loader::prelude::*;
use bevy_rapier2d::prelude::*;
use bevy_screen_diagnostics::{
ScreenDiagnosticsPlugin, ScreenEntityDiagnosticsPlugin, ScreenFrameDiagnosticsPlugin,
};
use bevy_trickfilm::Animation2DPlugin;

const BACKGROUND_COLOR: Color = Color::rgb(0.95, 0.90, 0.75);
Expand Down Expand Up @@ -49,18 +46,6 @@ fn main() {
})
.set(ImagePlugin::default_nearest())
.build(),
ScreenDiagnosticsPlugin {
timestep: 1.0,
style: Style {
position_type: PositionType::Absolute,
top: Val::Px(5.0),
left: Val::Px(15.0),
..default()
},
..default()
},
ScreenFrameDiagnosticsPlugin,
ScreenEntityDiagnosticsPlugin,
RapierPhysicsPlugin::<NoUserData>::default(),
RapierDebugRenderPlugin::default(),
Animation2DPlugin,
Expand Down
30 changes: 24 additions & 6 deletions src/spell/spell.rs → src/spell/cast_spell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use std::time::Duration;
use bevy::prelude::*;

use crate::{
item::{item_value::item_spell, ActiveItems},
player::{Player, PlayerState},
ui::text_field::TypingSubmitEvent,
};

use super::{Spell, SpellCasted};
use super::{debug_spell::DebugSpell, Spell, SpellCasted};

fn double_j_escape(
keys: Res<Input<KeyCode>>,
Expand Down Expand Up @@ -38,7 +39,22 @@ fn double_j_escape(
}
}

fn is_spell_active(active_items: &Res<ActiveItems>, spell: &Spell) -> bool {
if spell == &Spell::Debug || spell == &Spell::Flub {
return true;
}

for item in &active_items.0 {
if &item_spell(item) == spell {
return true;
}
}
false
}

fn submit_spell(
active_items: Res<ActiveItems>,
debug_spell: Res<DebugSpell>,
mut q_player: Query<&mut Player>,
mut ev_typing_submit_event: EventReader<TypingSubmitEvent>,
mut ev_spell_casted: EventWriter<SpellCasted>,
Expand All @@ -51,16 +67,18 @@ fn submit_spell(
for ev in ev_typing_submit_event.read() {
player.state = PlayerState::Idling;
if let Ok(spell) = &ev.value.parse::<Spell>() {
ev_spell_casted.send(SpellCasted {
spell: spell.clone(),
});
if debug_spell.active || is_spell_active(&active_items, spell) {
ev_spell_casted.send(SpellCasted {
spell: spell.clone(),
});
}
}
}
}

pub struct SpellPlugin;
pub struct CastSpellPlugin;

impl Plugin for SpellPlugin {
impl Plugin for CastSpellPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, (double_j_escape, submit_spell));
}
Expand Down
28 changes: 28 additions & 0 deletions src/spell/debug_spell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use bevy::prelude::*;

use super::{Spell, SpellCasted};

#[derive(Resource, Default)]
pub struct DebugSpell {
pub active: bool,
}

fn toggle_debug_mod(
mut debug_spell: ResMut<DebugSpell>,
mut ev_spell_casted: EventReader<SpellCasted>,
) {
for ev in ev_spell_casted.read() {
if ev.spell == Spell::Debug {
debug_spell.active = !debug_spell.active;
}
}
}

pub struct DebugSpellPlugin;

impl Plugin for DebugSpellPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<DebugSpell>()
.add_systems(Update, toggle_debug_mod);
}
}
12 changes: 8 additions & 4 deletions src/spell/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
pub mod aer_tracto;
pub mod debug_spell;
pub mod fireball;
pub mod icicle;
pub mod lightning;
pub mod lightning_bird;

mod cast_spell;
mod death;
mod flub;
mod kill_player;
mod phantasma;
mod speed_boost;
mod spell;

use std::error::Error;
use std::fmt::Display;
Expand All @@ -32,14 +33,15 @@ impl Plugin for SpellPlugin {
death::DeathPlugin,
flub::FlubPlugin,
kill_player::KillPlayerPlugin,
spell::SpellPlugin,
cast_spell::CastSpellPlugin,
debug_spell::DebugSpellPlugin,
))
.add_event::<SpellCasted>();
}
}

#[derive(PartialEq, Debug, Clone)]
enum Spell {
pub enum Spell {
Fireball,
IgnisPila,
InfernoPila,
Expand All @@ -53,10 +55,11 @@ enum Spell {
Death,
Flub,
KillPlayer,
Debug,
}

#[derive(Debug)]
struct InvalidSpell;
pub struct InvalidSpell;

impl Display for InvalidSpell {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down Expand Up @@ -88,6 +91,7 @@ impl FromStr for Spell {
"phantasma" => Ok(Spell::Phantasma),
"now you" | "jetzt du" => Ok(Spell::Death),
"kill player" => Ok(Spell::KillPlayer),
"debug" => Ok(Spell::Debug),
_ => Ok(Spell::Flub),
}
}
Expand Down
60 changes: 60 additions & 0 deletions src/utils/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use bevy::prelude::*;
use bevy_screen_diagnostics::{
ScreenDiagnostics, ScreenDiagnosticsPlugin, ScreenEntityDiagnosticsPlugin,
ScreenFrameDiagnosticsPlugin,
};

use crate::{spell::debug_spell::DebugSpell, GameState};

#[derive(Resource, Default)]
struct Diagnostics {
active: bool,
}

fn toggle(screen_diags: &mut ResMut<ScreenDiagnostics>) {
screen_diags.modify("fps").toggle();
screen_diags.modify("ms/frame").toggle();
screen_diags.modify("entities").toggle();
}

fn toggle_off(mut screen_diags: ResMut<ScreenDiagnostics>) {
toggle(&mut screen_diags);
}

fn toggle_diagnostics(
debug_spell: Res<DebugSpell>,
mut diags: ResMut<Diagnostics>,
mut screen_diags: ResMut<ScreenDiagnostics>,
) {
if debug_spell.active != diags.active {
diags.active = debug_spell.active;
toggle(&mut screen_diags);
}
}

pub struct DiagnosticsPlugin;

impl Plugin for DiagnosticsPlugin {
fn build(&self, app: &mut App) {
app.add_plugins((
ScreenDiagnosticsPlugin {
timestep: 1.0,
style: Style {
position_type: PositionType::Absolute,
top: Val::Px(5.0),
left: Val::Px(15.0),
..default()
},
..default()
},
ScreenFrameDiagnosticsPlugin,
ScreenEntityDiagnosticsPlugin,
))
.init_resource::<Diagnostics>()
.add_systems(OnEnter(GameState::Gaming), toggle_off)
.add_systems(
Update,
toggle_diagnostics.run_if(resource_changed::<DebugSpell>()),
);
}
}
8 changes: 7 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
pub mod anim_sprite;

mod diagnostics;

use bevy::prelude::*;
use bevy_rapier2d::plugin::RapierTransformPropagateSet;

pub struct UtilsPlugin;

impl Plugin for UtilsPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(anim_sprite::AnimSpritePlugin).add_systems(
app.add_plugins((
anim_sprite::AnimSpritePlugin,
diagnostics::DiagnosticsPlugin,
))
.add_systems(
PostUpdate,
reset_rotations.before(RapierTransformPropagateSet),
);
Expand Down

0 comments on commit a1d221a

Please sign in to comment.