add basic (untested) animation thingythingthing

This commit is contained in:
Schrottkatze 2024-11-23 17:54:12 +01:00
parent 620bb273be
commit 1f5b16090b
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
2 changed files with 89 additions and 22 deletions

View file

@ -1,10 +1,15 @@
use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; use std::{hash::Hash, time::Duration};
use bevy::{prelude::*, sprite::MaterialMesh2dBundle, utils::HashMap};
use bevy_rapier2d::prelude::*; use bevy_rapier2d::prelude::*;
use crate::{AppState, METER}; use crate::{AppState, METER};
use super::set::IngameSet; use super::set::IngameSet;
mod animation;
#[derive(Component)] #[derive(Component)]
struct Player {} struct Player {}
@ -12,32 +17,51 @@ pub(super) fn player_plugin(app: &mut App) {
app.add_systems(OnEnter(AppState::InGame), add_player.in_set(IngameSet)); app.add_systems(OnEnter(AppState::InGame), add_player.in_set(IngameSet));
} }
#[derive(Hash, PartialEq, Eq, Default)]
enum PlayerAnimations {
#[default]
Idle,
Walk,
}
pub fn add_player( pub fn add_player(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
) { ) {
// TODO replace let tex_idle = asset_server.load("idle.png");
commands.spawn(SpriteBundle { let layout_idle = TextureAtlasLayout::from_grid(UVec2::splat(512), 2, 1, None, None);
texture: asset_server.load("assets/Blockgrau.png"), let layout_idle_handle = texture_atlas_layouts.add(layout_idle);
..Default::default()
});
commands let tex_walk = asset_server.load("walk.png");
.spawn(( let layout_walk = TextureAtlasLayout::from_grid(UVec2::splat(512), 4, 1, None, None);
Player {}, let layout_walk_handle = texture_atlas_layouts.add(layout_walk);
MaterialMesh2dBundle {
mesh: meshes.add(Circle::new(20.)).into(), let anims = animation::AnimationManager::default()
material: materials.add(asset_server.load("test.png")), .insert(
transform: Transform::from_xyz(10., 10., 0.), PlayerAnimations::Idle,
..default() animation::Animation::new(
}, TextureAtlas {
)) layout: layout_idle_handle,
//.insert(( index: 0,
// RigidBody::Dynamic, },
// Collider::cuboid(0.4 * METER, 0.9 * METER), 2,
// Velocity::default(), 4,
//)) tex_idle,
.insert(ActiveEvents::COLLISION_EVENTS); ),
)
.insert(
PlayerAnimations::Walk,
animation::Animation::new(
TextureAtlas {
layout: layout_walk_handle,
index: 0,
},
2,
4,
tex_walk,
),
);
} }

View file

@ -0,0 +1,43 @@
use bevy::prelude::*;
use std::time::Duration;
use bevy::utils::HashMap;
use std::hash::Hash;
#[derive(Default)]
pub(crate) struct AnimationManager<Anim: Hash + Eq> {
animations: HashMap<Anim, Animation>,
}
impl<Anim: Hash + Eq> AnimationManager<Anim> {
pub fn insert(mut self, which: Anim, anim: Animation) -> Self {
self.animations.insert(which, anim);
self
}
}
pub(crate) struct Animation {
atlas: TextureAtlas,
max_index: usize,
fps: u8,
frame_timer: Timer,
tex: Handle<Image>,
}
impl Animation {
pub fn new(atlas: TextureAtlas, length: usize, fps: u8, tex_handle: Handle<Image>) -> Self {
Self {
atlas,
max_index: length - 1,
fps,
frame_timer: Self::timer_from_fps(fps),
tex: tex_handle,
}
}
// stolen from https://github.com/bevyengine/bevy/blob/5c759a1be800209f537bea31d32b8ba7e966b0c1/examples/2d/sprite_animation.rs#L53-L55
pub(crate) fn timer_from_fps(fps: u8) -> Timer {
Timer::new(Duration::from_secs_f32(1.0 / (fps as f32)), TimerMode::Once)
}
}