use crate::prelude::*;
use core::fmt::Debug;
#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "bevy", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bevy", reflect(Debug, PartialEq, Default))]
#[cfg_attr(
all(feature = "bevy", feature = "serde"),
reflect(Serialize, Deserialize)
)]
pub(crate) struct State {
pub(crate) program_counter: usize,
pub(crate) current_options: Vec<DialogueOption>,
pub(crate) stack: Vec<InternalValue>,
}
impl State {
pub(crate) fn push(&mut self, value: impl Into<InternalValue>) {
self.stack.push(value.into())
}
pub(crate) fn pop<T>(&mut self) -> T
where
T: TryFrom<InternalValue>,
<T as TryFrom<InternalValue>>::Error: Debug,
{
self.pop_value()
.try_into()
.unwrap_or_else(|e| panic!("Failed to convert popped value: {e:?}",))
}
pub(crate) fn pop_value(&mut self) -> InternalValue {
self.stack
.pop()
.unwrap_or_else(|| panic!("Tried to pop value, but the stack was empty."))
}
pub(crate) fn peek<T>(&self) -> T
where
T: TryFrom<InternalValue>,
<T as TryFrom<InternalValue>>::Error: Debug,
{
self.peek_value()
.clone()
.try_into()
.unwrap_or_else(|e| panic!("Failed to convert popped value: {e:?}",))
}
pub(crate) fn peek_value(&self) -> &InternalValue {
self.stack
.last()
.unwrap_or_else(|| panic!("Tried to peek value, but the stack was empty."))
}
}