Newer
Older
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
use bevy::asset::Handle;
use bevy::prelude::{Bundle, Component};
use bevy::reflect::TypeUuid;
use crate::directionality::Directionality;
#[derive(Clone, PartialOrd, PartialEq, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AnimationFrames {
pub frames: Vec<usize>,
pub frame_secs: f32,
}
impl AnimationFrames {
pub fn len_secs(&self) -> f32 {
self.frames.len() as f32 * self.frame_secs
}
}
#[derive(Clone, Debug, TypeUuid, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[uuid = "a2823f96-0f63-434e-9030-d8f762898a18"]
pub struct AnimationSet(pub HashMap<String, AnimationFrames>);
impl Deref for AnimationSet {
type Target = HashMap<String, AnimationFrames>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for AnimationSet {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[derive(Copy, Clone, Debug, Component, PartialEq, Eq, Ord, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SyncAnimationsToParent;
#[derive(Copy, Clone, Debug, Component, PartialEq, Eq, Ord, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HasAnimations;
#[derive(Copy, Clone, Debug, Component, PartialEq, Eq, Ord, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HasSimpleAnimations;
#[derive(Copy, Clone, Debug, Component, PartialEq, Eq, Ord, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HasDirectionalityAnimation;
#[derive(Copy, Clone, Debug, Component, PartialEq, Eq, Ord, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AnimationPaused;
#[derive(Copy, Clone, Debug, Component, PartialEq, Eq, Ord, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AnimationUserData(pub u128);
#[derive(Clone, Debug, Component, PartialEq, Eq, Ord, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AnimationMode {
#[default]
Loop,
Once,
OnceThenPlay(String),
}
#[derive(Clone, Debug, Component, PartialEq, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AnimationStatus {
pub active_name: String,
pub active_step: usize,
pub frame_time: f32,
}
impl AnimationStatus {
pub fn assert_animation<T: ToString>(&mut self, name: T) {
self.active_name = name.to_string();
}
pub fn start_animation<T: ToString>(&mut self, name: T) {
self.active_name = name.to_string();
self.active_step = 0;
}
pub fn start_or_continue<T: ToString>(&mut self, name: T) {
let name = name.to_string();
if self.active_name != name {
self.active_name = name;
self.active_step = 0;
}
}
}
#[derive(Clone, Debug, Bundle, PartialEq, PartialOrd, Default)]
pub struct DirectionalSpriteAnimationBundle {
pub animation_handle: Handle<AnimationSet>,
pub mode: AnimationMode,
pub status: AnimationStatus,
pub direction: Directionality,
pub marker: HasDirectionalityAnimation,
}
impl DirectionalSpriteAnimationBundle {
pub fn new(initial_anim: String, handle: Handle<AnimationSet>) -> Self {
Self {
animation_handle: handle,
status: AnimationStatus {
active_name: initial_anim,
active_step: 0,
frame_time: 0.0,
},
mode: AnimationMode::Loop,
direction: Directionality::default(),
marker: HasDirectionalityAnimation,
}
}
pub fn with_initial_facing(
initial_anim: String,
handle: Handle<AnimationSet>,
direction: Directionality,
) -> Self {
Self {
animation_handle: handle,
status: AnimationStatus {
active_name: initial_anim,
active_step: 0,
frame_time: 0.0,
},
mode: AnimationMode::Loop,
marker: HasDirectionalityAnimation,
direction,
}
}
}
#[derive(Clone, Debug, Bundle, PartialEq, PartialOrd, Default)]
pub struct SpriteAnimationBundle {
pub animation_handle: Handle<AnimationSet>,
pub mode: AnimationMode,
pub status: AnimationStatus,
pub marker: HasAnimations,
}
impl SpriteAnimationBundle {
pub fn new(initial_anim: String, handle: Handle<AnimationSet>) -> Self {
Self {
animation_handle: handle,
status: AnimationStatus {
active_name: initial_anim,
active_step: 0,
frame_time: 0.0,
},
mode: AnimationMode::Loop,
marker: HasAnimations,
}
}
}
#[derive(Clone, Debug, Component, PartialEq, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SimpleLoopedAnimation {
pub frames: Vec<usize>,
pub frame_secs: f32,
}
#[derive(Copy, Clone, Debug, Component, PartialEq, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SimpleLoopedAnimationStatus {
pub active_step: usize,
pub frame_time: f32,
}
#[derive(Clone, Debug, Bundle, PartialEq, PartialOrd, Default)]
pub struct SimpleAnimationBundle {
pub anim: SimpleLoopedAnimation,
pub status: SimpleLoopedAnimationStatus,
pub marker: HasSimpleAnimations,
}
impl SimpleAnimationBundle {
pub fn new(frames: Vec<usize>, frame_secs: f32) -> Self {
SimpleAnimationBundle {
anim: SimpleLoopedAnimation { frames, frame_secs },
status: SimpleLoopedAnimationStatus {
active_step: 0,
frame_time: 0.0,
},
marker: HasSimpleAnimations,
}
}
}
#[derive(Clone, Debug, Component, PartialEq, Eq, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AnimationOverride {
pub name: String,
pub user_data: u128,
}
#[derive(Clone, Debug, Bundle, PartialEq, PartialOrd, Default)]
pub struct ChildAnimationBundle {
pub animation_handle: Handle<AnimationSet>,
pub status: AnimationStatus,
pub marker: SyncAnimationsToParent,
}
impl ChildAnimationBundle {
pub fn new(handle: Handle<AnimationSet>) -> Self {
Self {
animation_handle: handle,
..Default::default()
}
}
}