haw-gj13-game/src/game/player.rs

105 lines
2.7 KiB
Rust
Raw Normal View History

use std::{hash::Hash, time::Duration};
2024-11-23 18:10:34 +01:00
use animation::AnimBundle;
2024-11-23 18:28:37 +01:00
use bevy::{ecs::system::SystemId, prelude::*, sprite::MaterialMesh2dBundle, utils::HashMap};
2024-11-22 22:26:20 +01:00
use bevy_rapier2d::prelude::*;
2024-11-22 22:55:45 +01:00
use crate::{AppState, METER};
2024-11-22 22:26:20 +01:00
2024-11-23 18:42:52 +01:00
use super::{scene::PlayerCoords, set::IngameSet};
mod animation;
#[derive(Component)]
2024-11-23 18:10:34 +01:00
struct Player;
2024-11-23 18:28:37 +01:00
#[derive(Resource)]
2024-11-23 18:33:27 +01:00
pub struct PlayerSpawnOneshot(pub SystemId);
2024-11-23 18:28:37 +01:00
impl FromWorld for PlayerSpawnOneshot {
fn from_world(world: &mut World) -> Self {
Self(world.register_system(add_player))
}
}
2024-11-23 18:29:50 +01:00
pub(super) fn player_plugin(app: &mut App) {
2024-11-23 18:42:52 +01:00
app.init_resource::<PlayerSpawnOneshot>();
}
2024-11-23 18:10:34 +01:00
#[derive(Component, Hash, PartialEq, Eq, Default)]
enum PlayerAnimations {
#[default]
Idle,
Walk,
}
2024-11-23 19:00:54 +01:00
// fn move_player(
// kb_input: Res<ButtonInput<KeyCode>>,
// mut query:
// )
fn add_player(
2024-11-23 01:40:50 +01:00
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
2024-11-23 16:24:16 +01:00
asset_server: Res<AssetServer>,
2024-11-23 18:42:52 +01:00
player_coords: Res<PlayerCoords>,
2024-11-23 01:40:50 +01:00
) {
let tex_idle = asset_server.load("idle.png");
let layout_idle = TextureAtlasLayout::from_grid(UVec2::splat(512), 2, 1, None, None);
let layout_idle_handle = texture_atlas_layouts.add(layout_idle);
let tex_walk = asset_server.load("walk.png");
let layout_walk = TextureAtlasLayout::from_grid(UVec2::splat(512), 4, 1, None, None);
let layout_walk_handle = texture_atlas_layouts.add(layout_walk);
let anims = animation::AnimationManager::default()
.insert(
PlayerAnimations::Idle,
animation::Animation::new(
TextureAtlas {
layout: layout_idle_handle,
index: 0,
},
2,
4,
tex_idle,
),
)
.insert(
PlayerAnimations::Walk,
animation::Animation::new(
TextureAtlas {
layout: layout_walk_handle,
index: 0,
},
2,
4,
tex_walk,
),
);
2024-11-23 18:10:34 +01:00
2024-11-23 19:00:54 +01:00
commands
.spawn((
Player,
AnimBundle {
tag: PlayerAnimations::Idle,
mgr: anims,
},
SpriteBundle {
transform: (*player_coords).into(),
..Default::default()
},
))
.insert((
RigidBody::Dynamic,
player_coords.get_collider(),
Velocity::default(),
))
.insert(KinematicCharacterController {
2024-11-23 18:10:34 +01:00
..Default::default()
2024-11-23 19:00:54 +01:00
});
}