2024-11-23 17:54:12 +01:00
|
|
|
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
|
|
|
|
2024-11-22 22:14:13 +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};
|
2024-11-23 17:54:12 +01:00
|
|
|
|
|
|
|
mod animation;
|
|
|
|
|
2024-11-22 22:14:13 +01:00
|
|
|
#[derive(Component)]
|
2024-11-23 19:21:51 +01:00
|
|
|
struct Player {
|
|
|
|
move_cooldown: Timer,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Player {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
move_cooldown: Timer::from_seconds(0.01, TimerMode::Repeating),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-22 22:14:13 +01:00
|
|
|
|
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 19:21:51 +01:00
|
|
|
app.add_systems(Update, (move_player, debug_player_pos))
|
|
|
|
.init_resource::<PlayerSpawnOneshot>();
|
2024-11-22 22:14:13 +01:00
|
|
|
}
|
|
|
|
|
2024-11-23 18:10:34 +01:00
|
|
|
#[derive(Component, Hash, PartialEq, Eq, Default)]
|
2024-11-23 17:54:12 +01:00
|
|
|
enum PlayerAnimations {
|
|
|
|
#[default]
|
|
|
|
Idle,
|
|
|
|
Walk,
|
|
|
|
}
|
|
|
|
|
2024-11-23 19:21:51 +01:00
|
|
|
fn debug_player_pos(query: Query<&Transform, With<Player>>) {
|
|
|
|
let trans = query.single();
|
|
|
|
dbg!(trans);
|
|
|
|
}
|
|
|
|
fn move_player(
|
|
|
|
kb_input: Res<ButtonInput<KeyCode>>,
|
|
|
|
mut query: Query<(
|
|
|
|
&mut Velocity,
|
2024-11-23 20:19:49 +01:00
|
|
|
&Transform,
|
2024-11-23 19:21:51 +01:00
|
|
|
&mut Player,
|
|
|
|
&mut KinematicCharacterController,
|
|
|
|
)>,
|
2024-11-23 20:19:49 +01:00
|
|
|
mut camera_query: Query<&mut Transform, (With<Camera2d>, Without<Player>)>,
|
2024-11-23 19:21:51 +01:00
|
|
|
time: Res<Time>,
|
|
|
|
) {
|
2024-11-23 20:19:49 +01:00
|
|
|
let (mut vel, p_transform, mut player, mut controller) = query.single_mut();
|
|
|
|
let (mut cam_transform) = camera_query.single_mut();
|
2024-11-23 19:21:51 +01:00
|
|
|
if player.move_cooldown.tick(time.delta()).finished() {
|
|
|
|
let mut moved = false;
|
|
|
|
let mut mv = 0;
|
2024-11-23 20:19:49 +01:00
|
|
|
let mut jump = false;
|
2024-11-23 19:21:51 +01:00
|
|
|
|
|
|
|
if kb_input.pressed(KeyCode::KeyA) {
|
|
|
|
moved = true;
|
|
|
|
mv -= 1;
|
|
|
|
}
|
|
|
|
if kb_input.pressed(KeyCode::KeyD) {
|
|
|
|
moved = true;
|
|
|
|
mv += 1;
|
|
|
|
}
|
|
|
|
|
2024-11-23 20:19:49 +01:00
|
|
|
let orig = cam_transform.translation;
|
|
|
|
println!("{orig:?}");
|
|
|
|
cam_transform.translation -= ((orig - p_transform.translation.xy().extend(0.0)) / 30.0);
|
|
|
|
|
2024-11-23 19:21:51 +01:00
|
|
|
if moved {
|
|
|
|
dbg!(mv);
|
|
|
|
controller.translation = Some(Vec2::new(mv as f32 * 6., 0.));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-23 19:00:54 +01:00
|
|
|
|
|
|
|
fn add_player(
|
2024-11-23 01:40:50 +01:00
|
|
|
mut commands: Commands,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
2024-11-23 17:54:12 +01:00
|
|
|
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
|
|
|
) {
|
2024-11-23 17:54:12 +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((
|
2024-11-23 19:21:51 +01:00
|
|
|
Player::default(),
|
2024-11-23 19:00:54 +01:00
|
|
|
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 20:19:49 +01:00
|
|
|
//translation: todo!(),
|
|
|
|
//custom_shape: todo!(),
|
|
|
|
//custom_mass: todo!(),
|
|
|
|
//up: todo!(),
|
|
|
|
//offset: todo!(),
|
|
|
|
//slide: todo!(),
|
|
|
|
//autostep: Some(CharacterAutostep {
|
|
|
|
// max_height: CharacterLength::Relative(0.1),
|
|
|
|
// min_width: CharacterLength::Absolute(1.0),
|
|
|
|
// include_dynamic_bodies: false,
|
|
|
|
//}),
|
|
|
|
//max_slope_climb_angle: todo!(),
|
|
|
|
//min_slope_slide_angle: todo!(),
|
|
|
|
//apply_impulse_to_dynamic_bodies: todo!(),
|
|
|
|
//snap_to_ground: Some(CharacterLength::Absolute(1.0)),
|
|
|
|
//filter_flags: todo!(),
|
|
|
|
//filter_groups: todo!(),
|
|
|
|
//normal_nudge_factor: todo!(),
|
2024-11-23 18:10:34 +01:00
|
|
|
..Default::default()
|
2024-11-23 19:00:54 +01:00
|
|
|
});
|
2024-11-22 22:14:13 +01:00
|
|
|
}
|