Added init and player movement
This commit is contained in:
parent
6c7ec48be9
commit
067e14609b
93
src/main.rs
93
src/main.rs
@ -1,28 +1,83 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
// Window width and height
|
||||
const WW: f32 = 1200.0;
|
||||
const WH: f32 = 700.0;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(
|
||||
DefaultPlugins
|
||||
.set(ImagePlugin::default_nearest())
|
||||
.set(WindowPlugin {
|
||||
primary_window: Some(Window {
|
||||
resizable: true,
|
||||
focused: true,
|
||||
resolution: (WW, WH).into(),
|
||||
..Default::default()
|
||||
}),
|
||||
..Default::default()
|
||||
}),
|
||||
)
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_systems(Startup, setup)
|
||||
.add_systems(Update, player_movement)
|
||||
.run();
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands) {
|
||||
commands.spawn(Camera2d::default());
|
||||
#[derive(Component)]
|
||||
struct Player {
|
||||
is_main: bool,
|
||||
color: Color,
|
||||
is_stunned: bool,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct Bullet {
|
||||
direction: Vec2,
|
||||
}
|
||||
|
||||
#[derive(Component, Debug, Copy, Clone, PartialEq, Eq)]
|
||||
enum Side {
|
||||
Red,
|
||||
Blue,
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands) {
|
||||
commands.spawn(Camera2d);
|
||||
|
||||
// Red player spawn
|
||||
commands.spawn((
|
||||
Sprite::from_color(Color::srgb(100., 100., 100.), Vec2::splat(50.)),
|
||||
Transform::from_xyz(-300., 0., 1.),
|
||||
Player {
|
||||
is_main: true,
|
||||
color: Color::srgb(100.0, 100.0, 100.0),
|
||||
is_stunned: false,
|
||||
},
|
||||
Side::Red,
|
||||
));
|
||||
|
||||
// Blue player spawn
|
||||
commands.spawn((
|
||||
Sprite::from_color(Color::srgb(0., 0., 0.), Vec2::splat(50.)),
|
||||
Transform::from_xyz(300., 0., 1.),
|
||||
Player {
|
||||
is_main: false,
|
||||
color: Color::srgb(0.0, 0.0, 0.0),
|
||||
is_stunned: false,
|
||||
},
|
||||
Side::Blue,
|
||||
));
|
||||
}
|
||||
|
||||
fn player_movement(
|
||||
time: Res<Time>,
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Query<(&mut Transform, &Player)>,
|
||||
//side_query: Query<(&Transform, &Side)>,
|
||||
) {
|
||||
for (mut transform, player) in query.iter_mut() {
|
||||
if player.is_main {
|
||||
if keyboard_input.pressed(KeyCode::ArrowLeft) && can_move_to() {
|
||||
transform.translation += Vec3::new(-1., 0., 0.) * time.delta_secs() * 200.;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::ArrowRight) && can_move_to() {
|
||||
transform.translation += Vec3::new(1., 0., 0.) * time.delta_secs() * 200.;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::ArrowDown) && can_move_to() {
|
||||
transform.translation += Vec3::new(0., -1., 0.) * time.delta_secs() * 200.;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::ArrowUp) && can_move_to() {
|
||||
transform.translation += Vec3::new(0., 1., 0.) * time.delta_secs() * 200.;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn can_move_to() -> bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user