-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bevy_eventlistener
: Intent
s are EntityEvent
s, behaviors are `Li…
…stener`s
- Loading branch information
Showing
32 changed files
with
265 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
use super::{attack::AttackBehavior, movement::MovementBehavior, wait::WaitBehavior}; | ||
|
||
pub type ActorBehavior = (MovementBehavior, AttackBehavior, WaitBehavior); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use bevy::prelude::*; | ||
use bevy_eventlistener::prelude::*; | ||
use bevy_prng::WyRand; | ||
use bevy_rand::resource::GlobalEntropy; | ||
use rand::seq::IteratorRandom; | ||
|
||
use crate::{ | ||
behavior, | ||
components::{name::Name, position::Position}, | ||
events::{intents::attack::AttackIntent, took_turn::TookTurn}, | ||
}; | ||
|
||
fn attack( | ||
q_actor: Query<(&Name, &Position)>, | ||
attack: Listener<AttackIntent>, | ||
q_target: Query<(&Name, &Position)>, // TODO: replace by collision system caching positions | ||
mut ev_took_turn: EventWriter<TookTurn>, | ||
mut rng: ResMut<GlobalEntropy<WyRand>>, | ||
) { | ||
let attacker = attack.listener(); | ||
let attack_direction = attack.vector; | ||
let Ok((attacker_name, attacker_position)) = q_actor.get(attacker) else { | ||
return; | ||
}; | ||
let attack_location = (attacker_position.xy.as_ivec2() + attack_direction).as_uvec2(); | ||
|
||
if let Some((target_name, _)) = q_target | ||
.iter() | ||
.filter(|(_, position)| position.xy == attack_location) | ||
.choose(&mut *rng) | ||
{ | ||
println!("{} attacks {}", attacker_name, target_name); | ||
} | ||
|
||
ev_took_turn.send(attacker.into()); | ||
} | ||
|
||
behavior!(attack); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use impl_trait_for_tuples::impl_for_tuples; | ||
|
||
pub mod actor; | ||
pub mod attack; | ||
pub mod movement; | ||
pub mod wait; | ||
|
||
pub trait Behavior { | ||
fn behavior() -> Self; | ||
} | ||
|
||
#[impl_for_tuples(1, 16)] | ||
impl Behavior for BehaviorIdentifier { | ||
fn behavior() -> Self { | ||
for_tuples!( ( #( (BehaviorIdentifier::behavior()) ),* ) ) | ||
} | ||
} | ||
|
||
// If I was an adult, I'd implement this as a derive macro | ||
#[macro_export] | ||
macro_rules! behavior { | ||
($n:ident) => { | ||
paste::paste! { | ||
pub type [< $n:camel Behavior >] = On< [<$n:camel Intent >] >; | ||
|
||
impl $crate::behaviors::Behavior for [< $n:camel Behavior >] { | ||
fn behavior() -> Self { | ||
On::< [< $n:camel Intent >] >::run( [< $n >] ) | ||
} | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use bevy::prelude::*; | ||
use bevy_eventlistener::prelude::*; | ||
|
||
use crate::{ | ||
behavior, | ||
components::{energy::Active, position::Position}, | ||
events::{intents::movement::MovementIntent, took_turn::TookTurn}, | ||
plugins::map::resources::Map, | ||
}; | ||
|
||
fn movement( | ||
mut query: Query<&mut Position, With<Active>>, | ||
map: Res<Map>, | ||
intent: Listener<MovementIntent>, | ||
mut ev_took_turn: EventWriter<TookTurn>, | ||
) { | ||
let entity = intent.listener(); | ||
let Ok(mut position) = query.get_mut(entity) else { | ||
return; | ||
}; | ||
let new_position = map.iclamp(&(position.xy.as_ivec2() + intent.vector)); | ||
if map.is_walkable(&new_position) { | ||
position.xy = new_position; | ||
} | ||
ev_took_turn.send(entity.into()); | ||
} | ||
|
||
behavior!(movement); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use bevy::prelude::*; | ||
use bevy_eventlistener::prelude::*; | ||
|
||
use crate::{ | ||
behavior, | ||
events::{intents::wait::WaitIntent, took_turn::TookTurn}, | ||
}; | ||
|
||
fn wait(wait: Listener<WaitIntent>, mut ev_took_turn: EventWriter<TookTurn>) { | ||
ev_took_turn.send(wait.listener().into()); | ||
} | ||
|
||
behavior!(wait); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,38 @@ | ||
use bevy::{ecs::bundle::Bundle, render::color::Color}; | ||
use bevy_ascii_terminal::TileFormatter; | ||
|
||
use crate::components::{ | ||
ai::Ai, | ||
energy::Energy, | ||
name::Name, | ||
position::Position, | ||
renderable::{RenderLayer, Renderable}, | ||
use crate::{ | ||
behaviors::{actor::ActorBehavior, Behavior}, | ||
components::{ | ||
ai::Ai, | ||
energy::Energy, | ||
name::Name, | ||
position::Position, | ||
renderable::{RenderLayer, Renderable}, | ||
}, | ||
}; | ||
|
||
type Behaviors = ActorBehavior; | ||
|
||
#[derive(Bundle)] | ||
pub struct DogBundle { | ||
pub renderable: Renderable, | ||
pub position: Position, | ||
pub energy: Energy, | ||
pub ai: Ai, | ||
pub name: Name, | ||
pub behaviors: Behaviors, | ||
} | ||
|
||
impl Default for DogBundle { | ||
fn default() -> Self { | ||
DogBundle { | ||
renderable: Renderable::new('d'.fg(Color::WHITE), RenderLayer::Monsters), | ||
position: Position::default(), | ||
energy: Energy::new(1), | ||
ai: Ai::Dog, | ||
name: Name::new("Fluffy"), | ||
behaviors: Behaviors::behavior(), | ||
position: Position::default(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
pub mod ai; | ||
pub mod energy; | ||
pub mod intents; | ||
pub mod name; | ||
pub mod player; | ||
pub mod position; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use bevy::prelude::*; | ||
use bevy_eventlistener::prelude::*; | ||
|
||
#[derive(Clone, Event, EntityEvent)] | ||
pub struct AttackIntent { | ||
// This reads weird, watch out: it's the target of the event, but the attacker in the attack. | ||
#[target] | ||
pub actor: Entity, | ||
pub vector: IVec2, | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use bevy::prelude::*; | ||
use bevy_eventlistener::prelude::*; | ||
|
||
#[derive(Clone, Event, EntityEvent)] | ||
pub struct MovementIntent { | ||
#[target] | ||
pub actor: Entity, | ||
pub vector: IVec2, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use bevy::prelude::*; | ||
use bevy_eventlistener::prelude::*; | ||
|
||
#[derive(Clone, Event, EntityEvent)] | ||
pub struct WaitIntent { | ||
#[target] | ||
pub actor: Entity, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
use bevy::prelude::*; | ||
use bevy_eventlistener::prelude::*; | ||
|
||
use crate::events::intents::{attack::AttackIntent, wait::WaitIntent}; | ||
|
||
use self::intents::movement::MovementIntent; | ||
|
||
pub mod intents; | ||
pub mod took_turn; | ||
|
||
// short for "event listener plugins" | ||
macro_rules! elp { | ||
($app:ident, $($events:ident),+ $(,)?) => { | ||
$($app.add_plugins(EventListenerPlugin::<$events>::default());)+ | ||
}; | ||
} | ||
|
||
pub struct EventsPlugin; | ||
|
||
impl Plugin for EventsPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_event::<took_turn::TookTurn>(); | ||
elp!(app, MovementIntent, AttackIntent, WaitIntent); | ||
} | ||
} |
Oops, something went wrong.