Skip to main content

wowlab_engine_combat/state/
deferred_work.rs

1use std::collections::VecDeque;
2
3use wowlab_types::sim::{AuraKey, EnemyIdx};
4
5use super::{AuraApplicationEvent, LocalAuraIdx, PendingEffectProgram};
6
7#[derive(Clone, Copy, Debug)]
8pub(crate) struct PendingAuraExpireHook {
9    pub(crate) key: AuraKey,
10    pub(crate) aura: LocalAuraIdx,
11    pub(crate) target: Option<EnemyIdx>,
12}
13
14#[derive(Clone, Copy, Debug)]
15pub(crate) enum DeferredWork {
16    Effect(PendingEffectProgram),
17    AuraApplication(AuraApplicationEvent),
18    AuraExpire(PendingAuraExpireHook),
19}
20
21/// FIFO effect and aura callbacks, with callbacks taking priority over aura expiry hooks.
22#[derive(Debug, Default)]
23pub(crate) struct DeferredWorkQueue {
24    callbacks: VecDeque<DeferredWork>,
25    aura_expiries: VecDeque<PendingAuraExpireHook>,
26}
27
28impl DeferredWorkQueue {
29    pub(crate) fn push_effect(&mut self, program: PendingEffectProgram) {
30        self.callbacks.push_back(DeferredWork::Effect(program));
31    }
32
33    pub(crate) fn push_aura_application(&mut self, event: AuraApplicationEvent) {
34        self.callbacks
35            .push_back(DeferredWork::AuraApplication(event));
36    }
37
38    pub(crate) fn push_aura_expire(&mut self, hook: PendingAuraExpireHook) {
39        self.aura_expiries.push_back(hook);
40    }
41
42    pub(crate) fn pop_next(&mut self) -> Option<DeferredWork> {
43        self.callbacks
44            .pop_front()
45            .or_else(|| self.aura_expiries.pop_front().map(DeferredWork::AuraExpire))
46    }
47
48    pub(crate) fn clear(&mut self) {
49        self.callbacks.clear();
50        self.aura_expiries.clear();
51    }
52}
53
54#[cfg(test)]
55mod tests;