generated from 2ndbeam/bevy-template
feat: Basic player implementation
This commit is contained in:
parent
62199a1817
commit
e4b1475c48
9 changed files with 830 additions and 54 deletions
76
src/player/mod.rs
Normal file
76
src/player/mod.rs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
//! Player module
|
||||
|
||||
use bevy::{camera::ScalingMode, prelude::*};
|
||||
use bevy_rapier2d::prelude::*;
|
||||
use leafwing_input_manager::prelude::*;
|
||||
|
||||
use crate::{
|
||||
meters,
|
||||
input::{
|
||||
DefaultInputMap,
|
||||
PlayerInput,
|
||||
}
|
||||
};
|
||||
|
||||
pub mod systems;
|
||||
|
||||
/// Player component
|
||||
#[derive(Component, Clone, Copy, PartialEq, Debug, Reflect)]
|
||||
#[reflect(Component, Clone, PartialEq, Debug, Default)]
|
||||
#[require(Transform, InputMap<PlayerInput>)]
|
||||
pub struct Player {
|
||||
/// Movement speed in pixels/s
|
||||
pub speed: f32,
|
||||
}
|
||||
|
||||
impl Default for Player {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
speed: meters(1.),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Player {
|
||||
/// Returns player bundle with everything needed
|
||||
pub fn bundle(asset_server: &Res<AssetServer>, position: Vec2) -> impl Bundle {
|
||||
let image = asset_server.load("sprites/player/placeholder.png");
|
||||
(
|
||||
// Basic
|
||||
Name::new("Player"),
|
||||
Player::default(),
|
||||
|
||||
// Visible
|
||||
Sprite::from_image(image),
|
||||
Transform::from_xyz(position.x, position.y, 1.),
|
||||
|
||||
// Input
|
||||
PlayerInput::input_map(),
|
||||
|
||||
// Collision
|
||||
RigidBody::KinematicPositionBased,
|
||||
KinematicCharacterController::default(),
|
||||
ActiveCollisionTypes::default() | ActiveCollisionTypes::KINEMATIC_STATIC,
|
||||
Collider::cuboid(meters(0.3), meters(0.9)),
|
||||
ActiveEvents::COLLISION_EVENTS,
|
||||
Sleeping::disabled(),
|
||||
|
||||
Children::spawn((
|
||||
Spawn((
|
||||
Name::new("Player camera"),
|
||||
Camera2d,
|
||||
Camera {
|
||||
clear_color: ClearColorConfig::Custom(Color::hsl(0.02, 0.67, 0.65)),
|
||||
..default()
|
||||
},
|
||||
Projection::Orthographic(OrthographicProjection {
|
||||
scaling_mode: ScalingMode::FixedVertical {
|
||||
viewport_height: meters(24.),
|
||||
},
|
||||
..OrthographicProjection::default_2d()
|
||||
}),
|
||||
)),
|
||||
)),
|
||||
)
|
||||
}
|
||||
}
|
||||
25
src/player/systems.rs
Normal file
25
src/player/systems.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
//! Player systems
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Do something based on [PlayerInput]
|
||||
pub fn handle_input(
|
||||
time: Res<Time>,
|
||||
|
||||
player_query: Query<(
|
||||
&Player,
|
||||
&ActionState<PlayerInput>,
|
||||
&mut KinematicCharacterController,
|
||||
&mut Sprite,
|
||||
)>,
|
||||
) {
|
||||
for (player, action_state, mut controller, mut sprite) in player_query {
|
||||
let direction = action_state.clamped_value(&PlayerInput::Move);
|
||||
|
||||
controller.translation = Some(vec2(direction * player.speed * time.delta_secs(), 0.));
|
||||
|
||||
if direction != 0. {
|
||||
sprite.flip_x = direction < 0.;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue