From 9d6f44ae5364a62195760a3296f0ead88ad8d175 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Thu, 28 Nov 2024 08:52:11 -0600 Subject: [PATCH] Ship thruster color changes when firing --- src/config.rs | 2 ++ src/lib.rs | 62 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/config.rs b/src/config.rs index ad918f7..f90b344 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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 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_ROTATION: f32 = 0.1; // +/- rotation speed in... radians per frame diff --git a/src/lib.rs b/src/lib.rs index ba18120..d0b3e9c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ mod config; use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_THRUST, SHIP_ROTATION}; use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; +use config::{SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE}; pub struct AsteroidPlugin; @@ -31,6 +32,12 @@ struct Rotation(f32); #[derive(Component)] 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, Handle); + fn spawn_camera(mut commands: Commands) { commands.spawn(Camera2dBundle::default()); } @@ -40,22 +47,45 @@ fn spawn_player( mut meshes: ResMut>, mut materials: ResMut>, ) { - 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, Position(Vec2::default()), Velocity(Vec2::ZERO), Rotation(0.0), - MaterialMesh2dBundle { - mesh: meshes.add(Triangle2d::new( - Vec2::new( 0.5, 0.0) * 20.0, - Vec2::new(-0.5, 0.25) * 20.0, - Vec2::new(-0.5, -0.25) * 20.0 - )).into(), - material: materials.add(PLAYER_SHIP_COLOR), - transform: Transform::default(), - ..default() - }, + ship_mesh, + ThrusterColors( + thruster_firing_id, + thruster_stopped_id + ) )); + + ship_id.add_child(thruster); } /* @@ -63,15 +93,21 @@ fn spawn_player( */ fn input_ship_thruster( keyboard_input: Res>, - mut query: Query<(&mut Velocity, &Rotation), With>, + mut query: Query<(&mut Velocity, &Rotation, &mut Children, &ThrusterColors), With>, + 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(); 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) { velocity.0 += Vec2::from_angle(rotation.0) * SHIP_THRUST; + commands.entity(*thrusters).insert(colors.0.clone()); + } else { + commands.entity(*thrusters).insert(colors.1.clone()); } }