Ship thruster color changes when firing
This commit is contained in:
@@ -7,6 +7,8 @@ use bevy::color::Color;
|
|||||||
|
|
||||||
pub(crate) const BACKGROUND_COLOR: Color = Color::srgb(0.3, 0.3, 0.3);
|
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 PLAYER_SHIP_COLOR: Color = Color::srgb(1.0, 1.0, 1.0);
|
||||||
|
pub(crate) const SHIP_THRUSTER_COLOR_ACTIVE: Color = Color::srgb(1.0, 0.2, 0.2);
|
||||||
|
pub(crate) const SHIP_THRUSTER_COLOR_INACTIVE: Color = Color::srgb(0.5, 0.5, 0.5);
|
||||||
|
|
||||||
pub(crate) const SHIP_THRUST: f32 = 1.0;
|
pub(crate) const SHIP_THRUST: f32 = 1.0;
|
||||||
pub(crate) const SHIP_ROTATION: f32 = 0.1; // +/- rotation speed in... radians per frame
|
pub(crate) const SHIP_ROTATION: f32 = 0.1; // +/- rotation speed in... radians per frame
|
||||||
|
|||||||
62
src/lib.rs
62
src/lib.rs
@@ -3,6 +3,7 @@ mod config;
|
|||||||
use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_THRUST, SHIP_ROTATION};
|
use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_THRUST, SHIP_ROTATION};
|
||||||
|
|
||||||
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
|
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
|
||||||
|
use config::{SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE};
|
||||||
|
|
||||||
pub struct AsteroidPlugin;
|
pub struct AsteroidPlugin;
|
||||||
|
|
||||||
@@ -31,6 +32,12 @@ struct Rotation(f32);
|
|||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct Ship;
|
struct Ship;
|
||||||
|
|
||||||
|
// Data component to store color properties attached to an entity
|
||||||
|
// This was easier (and imo better) than holding global consts with
|
||||||
|
// UUID assets.
|
||||||
|
#[derive(Component)]
|
||||||
|
struct ThrusterColors(Handle<ColorMaterial>, Handle<ColorMaterial>);
|
||||||
|
|
||||||
fn spawn_camera(mut commands: Commands) {
|
fn spawn_camera(mut commands: Commands) {
|
||||||
commands.spawn(Camera2dBundle::default());
|
commands.spawn(Camera2dBundle::default());
|
||||||
}
|
}
|
||||||
@@ -40,22 +47,45 @@ fn spawn_player(
|
|||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||||
) {
|
) {
|
||||||
commands.spawn((
|
let triangle = Triangle2d::new(
|
||||||
|
Vec2::new( 0.5, 0.0),
|
||||||
|
Vec2::new(-0.5, 0.45),
|
||||||
|
Vec2::new(-0.5, -0.45)
|
||||||
|
);
|
||||||
|
let thruster_firing_id = materials.add(SHIP_THRUSTER_COLOR_ACTIVE);
|
||||||
|
let thruster_stopped_id = materials.add(SHIP_THRUSTER_COLOR_INACTIVE);
|
||||||
|
|
||||||
|
let ship_mesh = MaterialMesh2dBundle {
|
||||||
|
mesh: meshes.add(triangle).into(),
|
||||||
|
material: materials.add(PLAYER_SHIP_COLOR),
|
||||||
|
transform: Transform::default().with_scale(Vec3::new(20.0, 20.0, 20.0)),
|
||||||
|
..default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let thruster_mesh = MaterialMesh2dBundle {
|
||||||
|
mesh: meshes.add(triangle).into(),
|
||||||
|
material: materials.add(PLAYER_SHIP_COLOR),
|
||||||
|
transform: Transform::default()
|
||||||
|
.with_scale(Vec3::splat(0.5))
|
||||||
|
.with_translation(Vec3::new(-0.5, 0.0, 0.0)),
|
||||||
|
..default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let thruster = commands.spawn(thruster_mesh).id();
|
||||||
|
|
||||||
|
let mut ship_id = commands.spawn((
|
||||||
Ship,
|
Ship,
|
||||||
Position(Vec2::default()),
|
Position(Vec2::default()),
|
||||||
Velocity(Vec2::ZERO),
|
Velocity(Vec2::ZERO),
|
||||||
Rotation(0.0),
|
Rotation(0.0),
|
||||||
MaterialMesh2dBundle {
|
ship_mesh,
|
||||||
mesh: meshes.add(Triangle2d::new(
|
ThrusterColors(
|
||||||
Vec2::new( 0.5, 0.0) * 20.0,
|
thruster_firing_id,
|
||||||
Vec2::new(-0.5, 0.25) * 20.0,
|
thruster_stopped_id
|
||||||
Vec2::new(-0.5, -0.25) * 20.0
|
)
|
||||||
)).into(),
|
|
||||||
material: materials.add(PLAYER_SHIP_COLOR),
|
|
||||||
transform: Transform::default(),
|
|
||||||
..default()
|
|
||||||
},
|
|
||||||
));
|
));
|
||||||
|
|
||||||
|
ship_id.add_child(thruster);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -63,15 +93,21 @@ fn spawn_player(
|
|||||||
*/
|
*/
|
||||||
fn input_ship_thruster(
|
fn input_ship_thruster(
|
||||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||||
mut query: Query<(&mut Velocity, &Rotation), With<Ship>>,
|
mut query: Query<(&mut Velocity, &Rotation, &mut Children, &ThrusterColors), With<Ship>>,
|
||||||
|
mut commands: Commands,
|
||||||
) {
|
) {
|
||||||
let Ok((mut velocity, rotation)) = query.get_single_mut() else {
|
let Ok((mut velocity, rotation, children, colors)) = query.get_single_mut() else {
|
||||||
let count = query.iter().count();
|
let count = query.iter().count();
|
||||||
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let thrusters = children.first().expect("Couldn't find first child, which should be the thruster");
|
||||||
|
|
||||||
if keyboard_input.pressed(KeyCode::KeyW) {
|
if keyboard_input.pressed(KeyCode::KeyW) {
|
||||||
velocity.0 += Vec2::from_angle(rotation.0) * SHIP_THRUST;
|
velocity.0 += Vec2::from_angle(rotation.0) * SHIP_THRUST;
|
||||||
|
commands.entity(*thrusters).insert(colors.0.clone());
|
||||||
|
} else {
|
||||||
|
commands.entity(*thrusters).insert(colors.1.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user