1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Core trait definitions for the Mechanic system.
//!
//! This module defines the fundamental abstractions that all mechanics must implement.
//! The design is based on Policy-Based Design patterns, allowing for compile-time
//! composition of behaviors with zero runtime overhead.
// Re-export execution hints for convenience
pub use ;
/// A trait for emitting events from a mechanic.
///
/// This abstraction allows mechanics to remain engine-agnostic while still
/// communicating state changes to the outside world.
///
/// # Examples
///
/// ```
/// use issun_core::mechanics::EventEmitter;
///
/// struct MyEventEmitter {
/// events: Vec<String>,
/// }
///
/// impl EventEmitter<String> for MyEventEmitter {
/// fn emit(&mut self, event: String) {
/// self.events.push(event);
/// }
/// }
/// ```
/// The core trait that all mechanics must implement.
///
/// A mechanic represents a self-contained piece of game logic that operates on:
/// - `Config`: Static configuration (e.g., base infection rate)
/// - `State`: Mutable state per entity (e.g., infection severity)
/// - `Input`: Per-frame input data (e.g., density, resistance)
/// - `Event`: Events emitted when state changes occur
/// - `Execution`: Execution characteristics hint (e.g., parallel safety, ordering)
///
/// # Type Parameters
///
/// All associated types are determined by the specific mechanic implementation.
/// This allows the mechanic to be completely decoupled from any engine.
///
/// # Design Philosophy
///
/// This trait is designed to support **Policy-Based Design**:
/// - Mechanics are generic over policy traits (e.g., `ContagionMechanic<S, P>`)
/// - All logic is resolved at compile time (static dispatch)
/// - Zero runtime overhead compared to hand-written code
///
/// # Examples
///
/// ```ignore
/// use std::marker::PhantomData;
/// use issun_core::mechanics::{Mechanic, EventEmitter};
///
/// // Define a policy trait
/// trait SpreadPolicy {
/// fn calculate_rate(base_rate: f32, density: f32) -> f32;
/// }
///
/// // A mechanic that is generic over the policy
/// struct MyMechanic<S: SpreadPolicy> {
/// _marker: PhantomData<S>,
/// }
///
/// impl<S: SpreadPolicy> Mechanic for MyMechanic<S> {
/// type Config = MyConfig;
/// type State = MyState;
/// type Input = MyInput;
/// type Event = MyEvent;
///
/// fn step(
/// config: &Self::Config,
/// state: &mut Self::State,
/// input: Self::Input,
/// emitter: &mut impl EventEmitter<Self::Event>,
/// ) {
/// // Pure logic that delegates to policy S
/// let rate = S::calculate_rate(config.base_rate, input.density);
/// // ...
/// }
/// }
/// ```