Basic movement
This commit is contained in:
parent
e87fbca7da
commit
36d5709a66
8 changed files with 110 additions and 1 deletions
BIN
assets/asteroid.png
Normal file
BIN
assets/asteroid.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 187 B |
BIN
assets/enemy.png
Normal file
BIN
assets/enemy.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 181 B |
BIN
assets/player.png
Normal file
BIN
assets/player.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 147 B |
BIN
assets/projectile.png
Normal file
BIN
assets/projectile.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 109 B |
BIN
assets/tileset.png
Normal file
BIN
assets/tileset.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 471 B |
64
src/damagable/mod.rs
Normal file
64
src/damagable/mod.rs
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::damagable::player::{Player, player_movement_system};
|
||||||
|
|
||||||
|
pub mod player;
|
||||||
|
|
||||||
|
pub struct DamagablePlugin;
|
||||||
|
|
||||||
|
impl Plugin for DamagablePlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.insert_resource(Time::<Fixed>::from_hz(60.0))
|
||||||
|
.add_systems(Startup, setup)
|
||||||
|
.add_systems(
|
||||||
|
FixedUpdate,
|
||||||
|
(player_movement_system, movement_system).chain(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Damagable {
|
||||||
|
hp: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Movable {
|
||||||
|
linear_speed: f32,
|
||||||
|
rotation_speed: f32,
|
||||||
|
max_linear_speed: f32,
|
||||||
|
max_rotation_speed: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
|
let player_sprite: Handle<Image> = asset_server.load("player.png");
|
||||||
|
|
||||||
|
commands.spawn(Camera2d);
|
||||||
|
|
||||||
|
//Create player
|
||||||
|
commands.spawn((
|
||||||
|
Player { acceleration: 100. },
|
||||||
|
Movable {
|
||||||
|
linear_speed: 0.,
|
||||||
|
rotation_speed: 0.,
|
||||||
|
max_linear_speed: 500.,
|
||||||
|
max_rotation_speed: f32::to_radians(360.),
|
||||||
|
},
|
||||||
|
Damagable { hp: 10 },
|
||||||
|
Sprite::from_image(player_sprite),
|
||||||
|
Transform::from_xyz(20., 20., 0.),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn movement_system(time: Res<Time>, query: Query<(&Movable, &mut Transform)>) {
|
||||||
|
for (movable, mut transform) in query {
|
||||||
|
transform.rotate_z(movable.rotation_speed * time.delta_secs());
|
||||||
|
let movement_direction = transform.rotation * Vec3::X;
|
||||||
|
let movement_distance = movable.linear_speed * time.delta_secs();
|
||||||
|
let translation_delta = movement_direction * movement_distance;
|
||||||
|
|
||||||
|
transform.translation += translation_delta;
|
||||||
|
|
||||||
|
//TODO: Loop on bounds
|
||||||
|
}
|
||||||
|
}
|
||||||
36
src/damagable/player.rs
Normal file
36
src/damagable/player.rs
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
use crate::damagable::Movable;
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Player {
|
||||||
|
pub(super) acceleration: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn player_movement_system(
|
||||||
|
time: Res<Time>,
|
||||||
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||||
|
query: Single<(&mut Movable, &Player)>,
|
||||||
|
) {
|
||||||
|
let (mut movable, player) = query.into_inner();
|
||||||
|
let mut movement_factor = 0.0;
|
||||||
|
let mut rotation_factor = 0.0;
|
||||||
|
|
||||||
|
if keyboard_input.pressed(KeyCode::ArrowLeft) {
|
||||||
|
rotation_factor += 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if keyboard_input.pressed(KeyCode::ArrowRight) {
|
||||||
|
rotation_factor -= 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if keyboard_input.pressed(KeyCode::ArrowUp) {
|
||||||
|
movement_factor += 1.0;
|
||||||
|
}
|
||||||
|
if keyboard_input.pressed(KeyCode::ArrowDown) {
|
||||||
|
movement_factor -= 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
movable.linear_speed += (movement_factor * player.acceleration * time.delta_secs())
|
||||||
|
.clamp(-movable.max_linear_speed, movable.max_linear_speed);
|
||||||
|
movable.rotation_speed = rotation_factor * movable.max_rotation_speed;
|
||||||
|
}
|
||||||
11
src/main.rs
11
src/main.rs
|
|
@ -1,3 +1,12 @@
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::damagable::DamagablePlugin;
|
||||||
|
|
||||||
|
mod damagable;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
App::new()
|
||||||
|
.add_plugins(DefaultPlugins)
|
||||||
|
.add_plugins(DamagablePlugin)
|
||||||
|
.run();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue