Dummy ship, single input, and motion integrator
The ship, such as it is, exists and moves when the player presses "W". Position is updated according to velocity, and the mesh transform is updated to match the position.
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "asteroids"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.14.2"
|
||||
12
src/config.rs
Normal file
12
src/config.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
Global constants used all over the program. Rather than leaving them scattered
|
||||
where ever they happen to be needed, I'm concentrating them here.
|
||||
*/
|
||||
|
||||
use bevy::color::Color;
|
||||
|
||||
pub(crate) const BACKGROUND_COLOR: Color = Color::srgb(0.3, 0.3, 0.3);
|
||||
pub(crate) const PLAYER_SHIP_COLOR: Color = Color::srgb(1.0, 1.0, 1.0);
|
||||
|
||||
pub(crate) const SHIP_THRUST_LIMIT: f32 = 10.0;
|
||||
pub(crate) const SHIP_ROTATION_LIMIT: f32 = 5.0; // +/- rotation speed in... uunniittss
|
||||
94
src/lib.rs
Normal file
94
src/lib.rs
Normal file
@@ -0,0 +1,94 @@
|
||||
mod config;
|
||||
|
||||
use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR};
|
||||
|
||||
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
|
||||
|
||||
pub struct AsteroidPlugin;
|
||||
|
||||
impl Plugin for AsteroidPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, (spawn_camera, spawn_player))
|
||||
.insert_resource(ClearColor(BACKGROUND_COLOR))
|
||||
.add_systems(FixedUpdate, input_ship_thruster)
|
||||
// .add_systems(FixedUpdate, input_ship_rotation);
|
||||
.add_systems(FixedPostUpdate, (integrate_velocity, update_positions));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct Position(bevy::math::Vec2);
|
||||
|
||||
#[derive(Component)]
|
||||
struct Velocity(bevy::math::Vec2);
|
||||
|
||||
#[derive(Component)]
|
||||
struct Rotation(bevy::prelude::Quat);
|
||||
|
||||
#[derive(Component)]
|
||||
struct Ship;
|
||||
|
||||
fn spawn_camera(mut commands: Commands) {
|
||||
commands.spawn(Camera2dBundle::default());
|
||||
}
|
||||
|
||||
fn spawn_player(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||
) {
|
||||
commands.spawn((
|
||||
Ship,
|
||||
Position(Vec2::default()),
|
||||
Velocity(Vec2::ZERO),
|
||||
MaterialMesh2dBundle {
|
||||
mesh: meshes.add(Circle::new(10.0)).into(),
|
||||
material: materials.add(PLAYER_SHIP_COLOR),
|
||||
transform: Transform::default(),
|
||||
..default()
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
/*
|
||||
Checks if "W" is pressed and increases velocity accordingly.
|
||||
*/
|
||||
fn input_ship_thruster(
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Query<&mut Velocity, With<Ship>>,
|
||||
) {
|
||||
let Ok(mut velocity) = query.get_single_mut() else {
|
||||
let count = query.iter().count();
|
||||
panic!("There should be exactly one player ship! Instead, there seem to be {count}.");
|
||||
};
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyW) {
|
||||
velocity.0 += Vec2::new(10.0, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Checks if "A" or "D" is pressed and rotates the ship accordingly
|
||||
*/
|
||||
fn input_ship_rotation(
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Query<&mut Rotation, With<Ship>>,
|
||||
) {
|
||||
todo!();
|
||||
}
|
||||
|
||||
/*
|
||||
Add velocity to position
|
||||
*/
|
||||
fn integrate_velocity(mut query: Query<(&mut Position, &Velocity)>, time: Res<Time>) {
|
||||
for (mut position, velocity) in &mut query {
|
||||
position.0 += velocity.0 * time.delta_seconds();
|
||||
}
|
||||
}
|
||||
|
||||
fn update_positions(mut query: Query<(&mut Transform, &Position)>) {
|
||||
for (mut transform, position) in &mut query {
|
||||
transform.translation.x = position.0.x;
|
||||
transform.translation.y = position.0.y;
|
||||
}
|
||||
}
|
||||
10
src/main.rs
Normal file
10
src/main.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use asteroids::AsteroidPlugin;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_plugins(AsteroidPlugin)
|
||||
.run();
|
||||
}
|
||||
Reference in New Issue
Block a user