use bevy::{
ecs::schedule::States,
prelude::*,
};
use crate::{
interactives::{
Action,
Interactive,
},
state::WorldState,
textdisplay::Message,
Ignores,
};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, States, Reflect)]
pub enum NoState {
#[default]
State,
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Component, Clone)]
pub struct Simple<State> {
actions: Vec<Action<State>>,
}
impl<State> Simple<State> {
pub fn push(&mut self, action: Action<State>) -> &mut Self {
self.actions.push(action);
self
}
pub fn extend(&mut self, actions: Vec<Action<State>>) -> &mut Self {
self.actions.extend(actions);
self
}
#[must_use]
pub fn animation(mut self, name: &str) -> Self {
self.push(Action::Animation(name.to_owned()));
self
}
#[must_use]
pub fn audio(mut self, name: &str) -> Self {
self.push(Action::Audio(name.to_owned()));
self
}
#[must_use]
pub fn jump(mut self, name: &str) -> Self {
self.push(Action::Jump(name.to_owned()));
self
}
}
impl<State> From<Vec<Action<State>>> for Simple<State> {
fn from(actions: Vec<Action<State>>) -> Self {
Self { actions }
}
}
impl<State> From<Action<State>> for Simple<State> {
fn from(action: Action<State>) -> Self {
<Vec<Action<State>>>::from(action).into()
}
}
impl<State> Interactive for Simple<State>
where
State: States,
{
type State = State;
fn interact(&mut self, _: &mut ResMut<WorldState>) -> Vec<Action<Self::State>> {
self.actions.clone()
}
}
pub struct Description;
impl Description {
pub fn build(text: &str) -> Simple<NoState> {
Action::Message(Message::new(text)).into()
}
}
pub struct MoveTo;
impl MoveTo {
pub fn build(spot: &str) -> Simple<NoState> {
Action::Move(spot.to_owned()).into()
}
}
pub struct Portal;
impl Portal {
pub fn build<State>(state: State) -> Simple<State> {
Action::Transition(state).into()
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Component, Default, Reflect)]
#[reflect(Component)]
pub struct Prop;
impl Interactive for Prop {
type State = NoState;
fn interact(&mut self, _state: &mut ResMut<WorldState>) -> Vec<Action<Self::State>> {
vec![]
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Component, Default, Reflect)]
#[reflect(Component)]
pub struct Trigger;
impl Trigger {
pub fn build(name: &str) -> (Ignores, Self) {
(Ignores::single(name), Self)
}
}
impl Interactive for Trigger {
type State = NoState;
fn interact(&mut self, _state: &mut ResMut<WorldState>) -> Vec<Action<Self::State>> {
vec![]
}
}