Skip to content

Commit

Permalink
Update enemy and player animations
Browse files Browse the repository at this point in the history
  • Loading branch information
PurityLake committed Dec 5, 2023
1 parent 872d38c commit f6da5f3
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 42 deletions.
40 changes: 37 additions & 3 deletions assets/sprites/list.animinfo.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,53 @@
{
"enemy_anim_names": [
"walk",
"die"
],
"player_anim_names": [
"walk",
"hurt",
"die"
],
"enemies": [
{
"name": "demon",
"walk": {
"width": 12,
"width": 16,
"height": 16,
"padding_x": 12,
"padding_x": 0,
"padding_y": 0
},
"die": {
"width": 16,
"height": 16,
"padding_x": 7,
"padding_x": 0,
"padding_y": 0
}
}
],
"players": [
{
"race": "human",
"m": {
"walk": {
"width": 16,
"height": 16,
"padding_x": 0,
"padding_y": 0
},
"hurt": {
"width": 16,
"height": 16,
"padding_x": 0,
"padding_y": 0
},
"die": {
"width": 16,
"height": 16,
"padding_x": 0,
"padding_y": 0
}
}
}
]
}
106 changes: 69 additions & 37 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,32 @@ pub struct AnimationData {
}

#[derive(Asset, TypePath, Debug, Deserialize, Default)]
pub struct AnimationEntry {
pub struct EnemyAnimationEntry {
pub name: String,
pub walk: AnimationData,
pub die: AnimationData,
}

#[derive(Asset, TypePath, Debug, Deserialize, Default)]
pub struct GenderAnimationData {
pub walk: AnimationData,
pub hurt: AnimationData,
pub die: AnimationData,
}

#[derive(Asset, TypePath, Debug, Deserialize, Default)]
pub struct PlayerAnimationEntry {
pub race: String,
pub m: GenderAnimationData,
// pub f: GenderAnimationData,
}

#[derive(Asset, TypePath, Debug, Deserialize, Default)]
pub struct AnimationListAsset {
pub enemies: Vec<AnimationEntry>,
pub enemy_anim_names: Vec<String>,
pub player_anim_names: Vec<String>,
pub enemies: Vec<EnemyAnimationEntry>,
pub players: Vec<PlayerAnimationEntry>,
}

#[derive(Resource, Default)]
Expand All @@ -37,6 +54,7 @@ pub struct AnimationList {
pub enum AnimState {
#[default]
Walking,
Hurting,
Dying,
Flashing,
Dead,
Expand All @@ -47,16 +65,28 @@ impl AnimState {
match self {
AnimState::Walking => true,
AnimState::Dying => true,
AnimState::Hurting => true,
AnimState::Flashing => false,
AnimState::Dead => false,
}
}
}

impl ToString for AnimState {
fn to_string(&self) -> String {
match self {
AnimState::Walking => "walk".to_string(),
AnimState::Dying => "dying".to_string(),
AnimState::Hurting => "hurt".to_string(),
AnimState::Flashing => "flash".to_string(),
AnimState::Dead => "dead".to_string(),
}
}
}

#[derive(Component, Clone, Default)]
pub struct AnimationComponent {
pub walk_handle: Handle<TextureAtlas>,
pub die_handle: Handle<TextureAtlas>,
pub image_handles: HashMap<String, Handle<TextureAtlas>>,
pub first: usize,
pub last: usize,
pub name: String,
Expand All @@ -70,8 +100,7 @@ pub struct AnimationComponent {

impl AnimationComponent {
fn new(
walk: Handle<TextureAtlas>,
die: Handle<TextureAtlas>,
image_handles: HashMap<String, Handle<TextureAtlas>>,
name: String,
timer: Timer,
dying_timer: Timer,
Expand All @@ -80,8 +109,7 @@ impl AnimationComponent {
flash_count: usize,
) -> Self {
Self {
walk_handle: walk,
die_handle: die,
image_handles,
first: 0,
last: 3,
name: name.clone(),
Expand All @@ -93,6 +121,19 @@ impl AnimationComponent {
state: AnimState::default(),
}
}

pub fn get_handle(&self) -> Option<Handle<TextureAtlas>> {
if self.state.should_anim() {
Some(
self.image_handles
.get(&self.state.to_string())
.unwrap()
.clone(),
)
} else {
None
}
}
}

#[derive(Resource, Default)]
Expand Down Expand Up @@ -136,39 +177,30 @@ fn load_animations(
if list.loaded || anim_list.is_none() {
return;
}
let anim_list = anim_list.unwrap();
let mut anim_map: HashMap<String, AnimationComponent> = HashMap::new();
for enemy in anim_list.unwrap().enemies.iter() {
let walk_texture_handle: Handle<Image> =
asset_server.load(format!("sprites/{0}_walk.png", enemy.name));
let die_texture_handle: Handle<Image> =
asset_server.load(format!("sprites/{0}_die.png", enemy.name));
let walk_texture_atlas = TextureAtlas::from_grid(
walk_texture_handle,
Vec2::new(enemy.walk.width as f32, enemy.walk.height as f32),
4,
1,
Some(Vec2::new(
enemy.walk.padding_x as f32,
enemy.walk.padding_y as f32,
)),
None,
);
let die_texture_atlas = TextureAtlas::from_grid(
die_texture_handle,
Vec2::new(enemy.die.width as f32, enemy.die.height as f32),
4,
1,
Some(Vec2::new(
enemy.die.padding_x as f32,
enemy.die.padding_y as f32,
)),
None,
);
for enemy in anim_list.enemies.iter() {
let mut image_handles: HashMap<String, Handle<TextureAtlas>> = HashMap::new();
for name in anim_list.enemy_anim_names.iter() {
let texture_handle: Handle<Image> =
asset_server.load(format!("sprites/{0}_{1}.png", enemy.name, name));
let texture_atlas = TextureAtlas::from_grid(
texture_handle,
Vec2::new(enemy.walk.width as f32, enemy.walk.height as f32),
4,
1,
Some(Vec2::new(
enemy.walk.padding_x as f32,
enemy.walk.padding_y as f32,
)),
None,
);
image_handles.insert(name.clone(), texture_atlases.add(texture_atlas));
}
anim_map.insert(
enemy.name.clone(),
AnimationComponent::new(
texture_atlases.add(walk_texture_atlas),
texture_atlases.add(die_texture_atlas),
image_handles,
enemy.name.clone(),
Timer::new(Duration::from_secs_f32(0.1), TimerMode::Repeating),
Timer::new(Duration::from_secs_f32(0.5), TimerMode::Once),
Expand Down
4 changes: 2 additions & 2 deletions src/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn spawn_enemy(
let anim = enemy_anims.enemies.get("demon").unwrap();
commands.spawn((
SpriteSheetBundle {
texture_atlas: anim.walk_handle.clone(),
texture_atlas: anim.get_handle().unwrap(),
transform: Transform::from_translation(Vec3::new(
500.,
rng.gen_range(-250.0..250.0),
Expand Down Expand Up @@ -97,7 +97,7 @@ fn move_enemies(
if transform.translation.x < 0. {
anim.state = AnimState::Dying;
atlas.index = 0;
*handle = anim.die_handle.clone();
*handle = anim.get_handle().unwrap();
}
}
}
Expand Down

0 comments on commit f6da5f3

Please sign in to comment.