Skip to content

Commit

Permalink
Wolf animation
Browse files Browse the repository at this point in the history
  • Loading branch information
rewin123 committed Dec 11, 2023
1 parent 4a45872 commit 18a044b
Show file tree
Hide file tree
Showing 85 changed files with 217 additions and 12 deletions.
Binary file added assets/fox/tile000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile008.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile009.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile011.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile012.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile013.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile014.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile015.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile016.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile017.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile018.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fox/tile019.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sheep/tile000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sheep/tile001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sheep/tile002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sheep/tile003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sheep/tile004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sheep/tile005.png
Binary file added assets/sheep/tile006.png
Binary file added assets/sheep/tile007.png
Binary file added assets/sheep/tile008.png
Binary file added assets/sheep/tile009.png
Binary file added assets/sheep/tile010.png
Binary file added assets/sheep/tile011.png
Binary file added assets/sheep/tile012.png
Binary file added assets/sheep/tile013.png
Binary file added assets/sheep/tile014.png
Binary file added assets/sheep/tile015.png
Binary file added assets/sheep/tile016.png
Binary file added assets/sheep/tile017.png
Binary file added assets/sheep/tile018.png
Binary file added assets/sheep/tile019.png
Binary file added assets/sheep/tile020.png
Binary file added assets/sheep/tile021.png
Binary file added assets/sheep/tile022.png
Binary file added assets/sheep/tile023.png
Binary file added assets/sheep/tile024.png
Binary file added assets/sheep/tile025.png
Binary file added assets/sheep/tile026.png
Binary file added assets/sheep/tile027.png
Binary file added assets/sheep/tile028.png
Binary file added assets/sheep/tile029.png
Binary file added assets/sheep/tile030.png
Binary file added assets/sheep/tile031.png
Binary file added assets/sheep/tile032.png
Binary file added assets/sheep/tile033.png
Binary file added assets/sheep/tile034.png
Binary file added assets/sheep/tile035.png
Binary file added assets/sheep/tile036.png
Binary file added assets/sheep/tile037.png
Binary file added assets/sheep/tile038.png
Binary file added assets/sheep/tile039.png
Binary file added assets/wolf/tile000.png
Binary file added assets/wolf/tile001.png
Binary file added assets/wolf/tile002.png
Binary file added assets/wolf/tile003.png
Binary file added assets/wolf/tile004.png
Binary file added assets/wolf/tile005.png
Binary file added assets/wolf/tile006.png
Binary file added assets/wolf/tile007.png
Binary file added assets/wolf/tile008.png
Binary file added assets/wolf/tile009.png
Binary file added assets/wolf/tile010.png
Binary file added assets/wolf/tile011.png
Binary file added assets/wolf/tile012.png
Binary file added assets/wolf/tile013.png
Binary file added assets/wolf/tile014.png
Binary file added assets/wolf/tile015.png
Binary file added assets/wolf/tile016.png
Binary file added assets/wolf/tile017.png
Binary file added assets/wolf/tile018.png
Binary file added assets/wolf/tile019.png
99 changes: 99 additions & 0 deletions src/auto_anim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use bevy::prelude::*;

use crate::GameSet;


pub trait AnimSet {
fn get_folder_path() -> String;
fn get_index_range(&self) -> AnimRange;
fn get_tile_count() -> usize;

fn get_tile_name(idx : usize) -> String {
//write in format tileXXX.png with exactly 3 digits
format!("tile{:03}.png", idx)
}

fn get_tile_path(idx : usize) -> String {
format!("{}/{}", Self::get_folder_path(), Self::get_tile_name(idx))
}
}

#[derive(Default)]
pub struct AutoAnimPlugin<T : AnimSet> {
_phantom : std::marker::PhantomData<T>
}

impl<T : AnimSet + Send + Sync + 'static> Plugin for AutoAnimPlugin<T> {
fn build(&self, app: &mut App) {
app
.add_systems(Startup, init_storage::<T>)
.add_systems(Update, auto_anim::<T>.in_set(GameSet::Playing));
}
}

fn auto_anim<T : AnimSet + Send + Sync + 'static>(
mut commands : Commands,
mut auto_anim_query : Query<(Entity, &mut AutoAnim<T>)>,
time : Res<Time>,
mut materials : ResMut<MaterialStorage<T>>
) {
for (entity, mut auto_anim) in auto_anim_query.iter_mut() {
if auto_anim.timer.tick(time.delta()).just_finished() {
auto_anim.current_frame = (auto_anim.current_frame + 1) % (auto_anim.set.get_index_range().end - auto_anim.set.get_index_range().start);
commands.entity(entity).insert(materials.materials[
auto_anim.set.get_index_range().start + auto_anim.current_frame
].clone());
}
}
}

fn init_storage<T : AnimSet + Send + Sync + 'static>(
mut commands : Commands,
asset_server : Res<AssetServer>,
mut materials : ResMut<Assets<StandardMaterial>>
) {
let mut ms = Vec::new();
for i in 0..T::get_tile_count() {
ms.push(materials.add(StandardMaterial {
base_color_texture: Some(asset_server.load(T::get_tile_path(i))),
alpha_mode: AlphaMode::Blend,
reflectance: 0.1,
..default()
}));
}
commands.insert_resource::<MaterialStorage<T>>(MaterialStorage {
materials : ms,
_phantom : std::marker::PhantomData
});
}

#[derive(Resource)]
pub struct MaterialStorage<T : AnimSet + Send + Sync + 'static> {
pub materials : Vec<Handle<StandardMaterial>>,
_phantom : std::marker::PhantomData<T>
}

#[derive(Component)]
pub struct AutoAnim<T : AnimSet> {
pub set : T,
pub current_frame : usize,
pub timer : Timer
}


#[derive(Clone)]
pub struct AnimRange {
pub start: usize,
pub end: usize,
}

impl AnimRange {
pub fn new(start : usize, end : usize) -> Self {
Self {
start,
end
}
}

}

8 changes: 8 additions & 0 deletions src/global_task/wolf_attack.rs
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
use bevy::prelude::*;

pub struct WolfAttackPlugin;

impl Plugin for WolfAttackPlugin {
fn build(&self, app: &mut App) {

}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod test_level;
pub mod torch;
pub mod wolf;
pub mod ambient;
pub mod auto_anim;

use std::f32::consts::PI;

Expand Down
41 changes: 37 additions & 4 deletions src/sheep.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{f32::consts::PI, time::Duration};
use std::{f32::consts::PI, time::Duration, fmt::format};

use bevy::prelude::*;
use rand::{rngs::ThreadRng, Rng};
Expand All @@ -11,14 +11,15 @@ use crate::{
safe_area::SafeArea,
sprite_material::create_plane_mesh,
test_level::LevelSize,
GameSet, GameStuff,
GameSet, GameStuff, auto_anim::{AnimRange, AnimSet, AutoAnimPlugin, AutoAnim},
};

use bevy_spatial::{
kdtree::KDTree3, AutomaticUpdate, SpatialAccess, SpatialStructure, TransformMode,
};

const SHEEP_PATH: &str = "test/sheep.png";
const SHEEP_FOLDER_PATH: &str = "sheep/";

pub const SHEEP_SPEED: f32 = DOG_SPEED * 0.5;
const SHEEP_ACCELERATION: f32 = SHEEP_SPEED * 3.0;
Expand Down Expand Up @@ -75,7 +76,34 @@ impl Plugin for SheepPlugin {
.with_transform(TransformMode::Transform)
.with_spatial_ds(SpatialStructure::KDTree3),
)
.add_systems(Update, collect_field);
.add_systems(Update, collect_field)
.add_plugins(AutoAnimPlugin::<SheepAnim>::default());
}
}

#[derive(Default)]
pub enum SheepAnim {
#[default]
Idle,
Walk,
Feed
}

impl AnimSet for SheepAnim {
fn get_folder_path() -> String {
format!("sheep")
}

fn get_index_range(&self) -> AnimRange {
match self {
SheepAnim::Idle => AnimRange::new(28, 39),
SheepAnim::Walk => AnimRange::new(23, 28),
SheepAnim::Feed => AnimRange::new(13, 16),
}
}

fn get_tile_count() -> usize {
40
}
}

Expand Down Expand Up @@ -450,7 +478,7 @@ pub fn setup(
material: sheep_material.clone(),
transform: Transform::from_xyz(pos.x, pos.y, pos.z)
.with_rotation(get_sprite_rotation())
.with_scale(Vec3::new(13.0 / 10.0, 1.0, 1.0)),
.with_scale(Vec3::new(1.0, 1.0, 1.0) * 1.7),
..default()
},
Sheep::default(),
Expand All @@ -463,6 +491,11 @@ pub fn setup(
},
SheepTargetVel::default(),
GameStuff,
AutoAnim {
set: SheepAnim::Idle,
timer: Timer::from_seconds(0.1 + rng.gen_range(-0.01..=0.01), TimerMode::Repeating),
current_frame: 0
}
));
exact_sheep_count += 1;
}
Expand Down
80 changes: 72 additions & 8 deletions src/wolf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::{prelude::*, audio::Volume};
use bevy_egui::egui::Options;
use rand::Rng;

use crate::{
common_storage::CommonStorage,
Expand All @@ -8,7 +9,7 @@ use crate::{
player::{Bark, DOG_SPEED},
safe_area::{OutOfSafeArea, SafeArea},
test_level::LevelSize,
GameStuff,
GameStuff, auto_anim::{AnimSet, AnimRange, AutoAnimPlugin, AutoAnim},
};

const WOLF_SPEED: f32 = DOG_SPEED * 1.3;
Expand All @@ -31,7 +32,31 @@ impl Plugin for WolfPlugin {
apply_deferred,
)
.chain(),
);
).add_plugins(AutoAnimPlugin::<WolfAnim>::default());
}
}

#[derive(Default)]
pub enum WolfAnim {
Eat,
#[default]
Run
}

impl AnimSet for WolfAnim {
fn get_folder_path() -> String {
"wolf".to_string()
}

fn get_index_range(&self) -> crate::auto_anim::AnimRange {
match self {
WolfAnim::Eat => AnimRange::new(0, 5),
WolfAnim::Run => AnimRange::new(6, 11),
}
}

fn get_tile_count() -> usize {
20
}
}

Expand All @@ -41,6 +66,7 @@ pub struct Wolf;
#[derive(Component)]
pub struct TryToCatchSheep {
pub target: Entity,
pub ignore_safe : bool
}

#[derive(Component)]
Expand Down Expand Up @@ -94,11 +120,13 @@ fn wolf_spawner(
transform: Transform::from_translation(
sheep_transform.translation.normalize() * level_size.0 * 2.0,
)
.with_rotation(get_sprite_rotation()),
.with_rotation(get_sprite_rotation())
.with_scale(Vec3::new(1.0, 1.0, 1.0) * 2.0),
..default()
},
TryToCatchSheep {
target: sheep_entity,
ignore_safe : false
},
Velocity::default(),
WalkController {
Expand All @@ -107,6 +135,11 @@ fn wolf_spawner(
target_velocity: Vec3::ZERO,
},
GameStuff,
AutoAnim {
set: WolfAnim::Run,
current_frame: 0,
timer: Timer::from_seconds(0.1 + rand::thread_rng().gen_range(-0.01..=0.01), TimerMode::Repeating),
}
));

commands.entity(sheep_entity).insert(UnderHunting);
Expand All @@ -126,7 +159,12 @@ fn catch_system(
commands
.entity(wolf)
.insert(Eating { time: 2.0 })
.remove::<TryToCatchSheep>();
.remove::<TryToCatchSheep>()
.insert(AutoAnim {
set: WolfAnim::Eat,
current_frame: 0,
timer: Timer::from_seconds(0.1, TimerMode::Repeating),
});

commands
.entity(try_to_catch_sheep.target)
Expand Down Expand Up @@ -160,7 +198,12 @@ fn eating_system(
for (wolf, _wolf_transform, mut eating, mut walk_controller) in wolfs.iter_mut() {
eating.time -= time.delta_seconds();
if eating.time <= 0.0 {
commands.entity(wolf).remove::<Eating>().insert(GoOut);
commands.entity(wolf).remove::<Eating>().insert(GoOut)
.insert(AutoAnim {
set: WolfAnim::Run,
current_frame: 0,
timer: Timer::from_seconds(0.1, TimerMode::Repeating),
});
} else {
walk_controller.target_velocity = Vec3::ZERO;
}
Expand All @@ -186,7 +229,12 @@ fn go_out_system(
y: wolf_transform.translation.z,
})
}) {
commands.entity(wolf).insert(GoOut);
commands.entity(wolf).insert(GoOut).remove::<Eating>()
.insert(AutoAnim {
set: WolfAnim::Run,
current_frame: 0,
timer: Timer::from_seconds(0.1, TimerMode::Repeating),
});
}
}
}
Expand All @@ -205,6 +253,12 @@ fn run_out_system(
safearea: Query<&SafeArea>,
) {
for (wolf, wolf_transform, mut walk_controller, catch) in wolfs.iter_mut() {
if let Some(catch) = catch {
if catch.ignore_safe {
continue;
}
}

let in_safe_area = safearea.iter().filter(|area| {
area.in_area(Vec2 {
x: wolf_transform.translation.x,
Expand All @@ -218,7 +272,12 @@ fn run_out_system(
.entity(wolf)
.insert(GoOut)
.remove::<TryToCatchSheep>()
.remove::<Eating>();
.remove::<Eating>()
.insert(AutoAnim {
set: WolfAnim::Run,
current_frame: 0,
timer: Timer::from_seconds(0.1, TimerMode::Repeating),
});

if let Some(catch) = catch {
commands.entity(catch.target).remove::<UnderHunting>();
Expand All @@ -242,7 +301,12 @@ fn bark(
.entity(wolf)
.insert(GoOut)
.remove::<Eating>()
.remove::<TryToCatchSheep>();
.remove::<TryToCatchSheep>()
.insert(AutoAnim {
set: WolfAnim::Run,
current_frame: 0,
timer: Timer::from_seconds(0.1, TimerMode::Repeating),
});

if let Some(catch) = catch {
commands.entity(catch.target).remove::<UnderHunting>();
Expand Down

0 comments on commit 18a044b

Please sign in to comment.