Skip to content

Commit

Permalink
Sheep anim, corpses
Browse files Browse the repository at this point in the history
  • Loading branch information
rewin123 committed Dec 11, 2023
1 parent 18a044b commit 724de84
Show file tree
Hide file tree
Showing 63 changed files with 200 additions and 23 deletions.
Binary file added assets/dog/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/dog/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/dog/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/dog/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/dog/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/dog/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/dog/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/dog/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/dog/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/dog/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/dog/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/dog/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/dog/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/dog/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/dog/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/dog/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 modified 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 modified 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 modified 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 modified 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 modified 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 modified assets/sheep/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 modified assets/sheep/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 modified assets/sheep/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 modified assets/sheep/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 modified assets/sheep/tile009.png
Binary file modified assets/sheep/tile010.png
Binary file modified assets/sheep/tile011.png
Binary file modified assets/sheep/tile012.png
Binary file modified assets/sheep/tile013.png
Binary file modified assets/sheep/tile014.png
Binary file modified assets/sheep/tile015.png
Binary file modified assets/sheep/tile016.png
Binary file modified assets/sheep/tile017.png
Binary file modified assets/sheep/tile018.png
Binary file modified assets/sheep/tile019.png
Binary file modified assets/sheep/tile020.png
Binary file modified assets/sheep/tile021.png
Binary file modified assets/sheep/tile022.png
Binary file modified assets/sheep/tile023.png
Binary file modified assets/sheep/tile024.png
Binary file modified assets/sheep/tile025.png
Binary file modified assets/sheep/tile026.png
Binary file modified assets/sheep/tile027.png
Binary file modified assets/sheep/tile028.png
Binary file modified assets/sheep/tile029.png
Binary file modified assets/sheep/tile030.png
Binary file modified assets/sheep/tile031.png
Binary file modified assets/sheep/tile032.png
Binary file modified assets/sheep/tile033.png
Binary file modified assets/sheep/tile034.png
Binary file modified assets/sheep/tile035.png
Binary file modified assets/sheep/tile036.png
Binary file modified assets/sheep/tile037.png
Binary file modified assets/sheep/tile038.png
Binary file modified assets/sheep/tile039.png
6 changes: 4 additions & 2 deletions src/auto_anim.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::prelude::*;
use bevy::{prelude::*, render::render_resource::Face};

use crate::GameSet;

Expand Down Expand Up @@ -39,7 +39,7 @@ fn auto_anim<T : AnimSet + Send + Sync + 'static>(
) {
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);
auto_anim.current_frame = (auto_anim.current_frame + 1) % (auto_anim.set.get_index_range().end + 1 - 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());
Expand All @@ -58,6 +58,8 @@ fn init_storage<T : AnimSet + Send + Sync + 'static>(
base_color_texture: Some(asset_server.load(T::get_tile_path(i))),
alpha_mode: AlphaMode::Blend,
reflectance: 0.1,
double_sided: true,
cull_mode: None,
..default()
}));
}
Expand Down
82 changes: 82 additions & 0 deletions src/corpse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use bevy::prelude::*;
use rand::Rng;

use crate::{GameSet, GameStuff};

pub struct CorpsePlugin;

impl Plugin for CorpsePlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, (corpse_system, spawn_corpse_system).in_set(GameSet::Playing))
.add_event::<SpawnCorpse>()
.add_systems(Startup, setup_corpse_storage);
}
}

#[derive(Component)]
pub struct Corpse {
pub time : f32
}

fn corpse_system(
mut commands : Commands,
time : Res<Time>,
mut query : Query<(Entity, &mut Corpse)>
) {
for (entity, mut corpse) in query.iter_mut() {
corpse.time -= time.delta_seconds();
if corpse.time <= 0.0 {
commands.entity(entity).despawn_recursive();
}
}
}

#[derive(Resource)]
pub struct CorpseStoage {
pub material: Handle<StandardMaterial>,
pub mesh: Handle<Mesh>,
}

fn setup_corpse_storage(
mut commands: Commands,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
mut meshs : ResMut<Assets<Mesh>>
) {
commands.insert_resource(CorpseStoage {
material: materials.add(StandardMaterial {
base_color_texture: Some(asset_server.load("sheep/tile001.png")),
alpha_mode: AlphaMode::Blend,
reflectance: 0.1,
..default()
}),
mesh: meshs.add(shape::Plane::default().into())
});
}

#[derive(Event)]
pub struct SpawnCorpse {
pub position : Vec3
}

fn spawn_corpse_system(
mut commands : Commands,
mut event : EventReader<SpawnCorpse>,
storage : Res<CorpseStoage>
) {
for event in event.iter() {
commands.spawn((
PbrBundle {
mesh: storage.mesh.clone(),
material: storage.material.clone(),
transform: Transform::from_translation(event.position + Vec3::Y * rand::thread_rng().gen_range(0.01..=0.02)).with_scale(Vec3::new(2.0, 2.0, 2.0)),
..default()
},
Corpse {
time: 20.0
},
GameStuff
));
}
event.clear();
}
2 changes: 1 addition & 1 deletion src/global_task/change_safe_area_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn start_change_safe_area(
let target_pos = Vec2::new(start_pos.x + pos, start_pos.y + pos);

let mut change = ChangeSafeArea {
target_scale: 0.5,
target_scale: 0.7,
start_scale: 1.0,

target_pos: target_pos,
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod torch;
pub mod wolf;
pub mod ambient;
pub mod auto_anim;
pub mod corpse;

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

Expand Down Expand Up @@ -104,9 +105,10 @@ impl Plugin for GamePlugin {
global_task::GlobalTaskPlugin,
));

app.add_plugins(
ambient::AmbientPlugin
);
app.add_plugins((
ambient::AmbientPlugin,
corpse::CorpsePlugin
));

//For long term updates
app.insert_resource(Time::<Fixed>::from_seconds(1.0));
Expand Down
94 changes: 81 additions & 13 deletions src/player.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::f32::consts::PI;

use bevy::{
input::mouse::MouseWheel,
pbr::{CascadeShadowConfig, CascadeShadowConfigBuilder},
Expand All @@ -9,7 +11,7 @@ use crate::{
get_sprite_rotation,
physics::Velocity,
sprite_material::{create_plane_mesh, SpriteExtension, SpriteMaterial},
GameStuff, GameSet,
GameStuff, GameSet, auto_anim::{AnimSet, AnimRange, AutoAnimPlugin, AutoAnim},
};

const DOG_PATH: &str = "test/dog.png";
Expand Down Expand Up @@ -48,7 +50,9 @@ impl Plugin for PlayerPlugin {
player_movemnt_by_mouse.run_if(in_state(MovementStyle::Mouse)),
)
.add_systems(Update, (change_movement_style, bark).in_set(GameSet::Playing))
.add_systems(Update, (set_cam_distance, camera_movement, stamina_increse).in_set(GameSet::Playing));
.add_systems(Update, (set_cam_distance, camera_movement, stamina_increse).in_set(GameSet::Playing))
.add_plugins(AutoAnimPlugin::<PlayerAnim>::default())
.add_systems(Update, set_anim_state.in_set(GameSet::Playing));
}
}

Expand Down Expand Up @@ -85,6 +89,36 @@ fn stamina_increse(
}
}

#[derive(Default)]
pub enum PlayerAnim {
BigBark,
Bark,
WalkAndBark,
Walk,
#[default]
Idle
}

impl AnimSet for PlayerAnim {
fn get_folder_path() -> String {
"dog".to_string()
}

fn get_index_range(&self) -> crate::auto_anim::AnimRange {
match self {
PlayerAnim::BigBark => AnimRange::new(0, 2),
PlayerAnim::Bark => AnimRange::new(3, 5),
PlayerAnim::WalkAndBark => AnimRange::new(6,7),
PlayerAnim::Walk => AnimRange::new(9, 11),
PlayerAnim::Idle => AnimRange::new(12, 15),
}
}

fn get_tile_count() -> usize {
16
}
}

#[derive(Component)]
pub struct Player;

Expand Down Expand Up @@ -122,31 +156,26 @@ fn spawn_player_by_event(
mut event_reader: EventReader<SpawnPlayer>,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
_materials: ResMut<Assets<StandardMaterial>>,
mut sprite_material: ResMut<Assets<SpriteMaterial>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
for event in event_reader.read() {
let plane = meshes.add(create_plane_mesh());
let material = sprite_material.add(SpriteMaterial {
base: StandardMaterial {
let material = materials.add(
StandardMaterial {
base_color_texture: Some(asset_server.load(DOG_PATH)),
alpha_mode: AlphaMode::Opaque,
..default()
},
extension: SpriteExtension {
base_teture: Some(asset_server.load(DOG_PATH)),
},
});
});

info!("Spawn player at {:?}", event.position);

commands.spawn((
MaterialMeshBundle {
PbrBundle {
mesh: plane.clone(),
material: material.clone(),
transform: Transform::from_translation(event.position)
.with_rotation(get_sprite_rotation())
.with_scale(Vec3::new(12.0 / 8.0, 1.0, 1.0)),
.with_scale(Vec3::new(1.0, 1.0, 1.0) * 2.0),
..default()
},
Player,
Expand All @@ -157,6 +186,11 @@ fn spawn_player_by_event(
value: 1.0,
blocked: false
},
AutoAnim {
set: PlayerAnim::Idle,
timer: Timer::from_seconds(0.1, TimerMode::Repeating),
current_frame: 0,
}
)).with_children(|parent| {
parent.spawn((
DogBarkSource,
Expand Down Expand Up @@ -456,3 +490,37 @@ fn set_cam_distance(

commands.entity(e).insert(CameraDistance(dist));
}

fn set_anim_state(
mut player : Query<(&mut AutoAnim<PlayerAnim>, &Velocity, &mut Transform)>,
input: Res<Input<KeyCode>>
) {
let Ok((mut player, vel, mut t)) = player.get_single_mut() else {
return;
};

let moving = input.any_pressed([KeyCode::W, KeyCode::S, KeyCode::A, KeyCode::D]);
let barking = input.pressed(KeyCode::Space);
let big_bark = input.pressed(KeyCode::ControlLeft);

if big_bark {
player.set = PlayerAnim::BigBark;
} else {
if barking && moving {
player.set = PlayerAnim::WalkAndBark;
} else if barking {
player.set = PlayerAnim::Bark;
} else if moving {
player.set = PlayerAnim::Walk;
} else {
player.set = PlayerAnim::Idle;
}
}

if vel.0.x > 0.1 {
t.rotation = get_sprite_rotation();
t.rotate_local_z(PI);
} else if vel.0.x < -0.1 {
t.rotation = get_sprite_rotation();
}
}
22 changes: 20 additions & 2 deletions src/sheep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ impl Plugin for SheepPlugin {
.with_spatial_ds(SpatialStructure::KDTree3),
)
.add_systems(Update, collect_field)
.add_plugins(AutoAnimPlugin::<SheepAnim>::default());
.add_plugins(AutoAnimPlugin::<SheepAnim>::default())

.add_systems(Update, set_anim_state.in_set(GameSet::Playing));
}
}

Expand Down Expand Up @@ -478,7 +480,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(1.0, 1.0, 1.0) * 1.7),
.with_scale(Vec3::new(1.0, 1.0, 1.0) * 2.0),
..default()
},
Sheep::default(),
Expand Down Expand Up @@ -595,3 +597,19 @@ fn collect_field(
}
}
}

fn set_anim_state(
mut sheep : Query<(&mut AutoAnim<SheepAnim>, Option<&GoTo>, Option<&IdleFeeding>, Option<&IsScared>), With<Sheep>>
) {
for (mut anim, go_to, idle, scared) in sheep.iter_mut() {
if go_to.is_some() {
anim.set = SheepAnim::Walk;
} else if idle.is_some() {
anim.set = SheepAnim::Feed;
} else if scared.is_some() {
anim.set = SheepAnim::Walk;
} else {
anim.set = SheepAnim::Idle;
}
}
}
9 changes: 7 additions & 2 deletions src/wolf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
player::{Bark, DOG_SPEED},
safe_area::{OutOfSafeArea, SafeArea},
test_level::LevelSize,
GameStuff, auto_anim::{AnimSet, AnimRange, AutoAnimPlugin, AutoAnim},
GameStuff, auto_anim::{AnimSet, AnimRange, AutoAnimPlugin, AutoAnim}, corpse::SpawnCorpse,
};

const WOLF_SPEED: f32 = DOG_SPEED * 1.3;
Expand Down Expand Up @@ -121,7 +121,7 @@ fn wolf_spawner(
sheep_transform.translation.normalize() * level_size.0 * 2.0,
)
.with_rotation(get_sprite_rotation())
.with_scale(Vec3::new(1.0, 1.0, 1.0) * 2.0),
.with_scale(Vec3::new(1.0, 1.0, 1.0) * 3.0),
..default()
},
TryToCatchSheep {
Expand All @@ -146,11 +146,14 @@ fn wolf_spawner(
}
}



fn catch_system(
mut commands: Commands,
sheep: Query<&Transform>,
mut wolfs: Query<(Entity, &Transform, &mut WalkController, &TryToCatchSheep)>,
asset_server : Res<AssetServer>,
mut spawn_corpse : EventWriter<SpawnCorpse>
) {
for (wolf, wolf_transform, mut walk_controller, try_to_catch_sheep) in wolfs.iter_mut() {
let wolf_translation = wolf_transform.translation;
Expand All @@ -170,6 +173,8 @@ fn catch_system(
.entity(try_to_catch_sheep.target)
.despawn_recursive();

spawn_corpse.send(SpawnCorpse { position: sheep.translation });

commands.spawn(AudioBundle {
source: asset_server.load("audio/kill_sound.ogg"),
settings: PlaybackSettings {
Expand Down

0 comments on commit 724de84

Please sign in to comment.