From 3d26b0915d57a48e2a895f381f7b033f13b1f8c0 Mon Sep 17 00:00:00 2001 From: yuni Date: Tue, 11 Jun 2024 02:46:31 +0200 Subject: [PATCH] add Id2V resource, "wants matchvelocitywith" command --- src/actor.rs | 15 +++++++++++++-- src/cmd.rs | 9 +++++++++ src/data/defs.txt | 14 ++++++++++++++ src/game.rs | 11 +++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index c5e98a3..d6820e1 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -164,6 +164,8 @@ pub struct WantsMaxVelocity(pub f64); #[derive(Component)] pub struct WantsToLookAt(pub String); #[derive(Component)] +pub struct WantsMatchVelocityWith(pub String); +#[derive(Component)] pub struct Identifier(pub String); #[derive(Component)] pub struct OrbitsJupiter; @@ -554,12 +556,21 @@ fn handle_wants_maxvelocity( &Engine, &WantsMaxVelocity, Option<&OrbitsJupiter>, + Option<&WantsMatchVelocityWith>, )>, + id2v: Res, jupiter_pos: Res, ) { let dt = time.delta_seconds(); - for (pos, mut v, engine, maxv, orbits_jupiter) in &mut query { - let target_velocity = if orbits_jupiter.is_some() { + for (pos, mut v, engine, maxv, orbits_jupiter, matchwith) in &mut query { + let target_velocity = if let Some(matchwith) = matchwith { + if let Some(target_v) = id2v.0.get(&matchwith.0) { + *target_v + } else { + warn!("Can't match velocity with nonexisting ID {}", matchwith.0); + continue; + } + } else if orbits_jupiter.is_some() { let relative_pos = pos.0 - jupiter_pos.0; nature::orbital_velocity(relative_pos, nature::JUPITER_MASS) } else { diff --git a/src/cmd.rs b/src/cmd.rs index 17e37e7..77e13b0 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -86,6 +86,7 @@ struct ParserState { wants_maxrotation: Option, wants_maxvelocity: Option, wants_tolookat_id: Option, + wants_matchvelocity_id: Option, collider_is_mesh: bool, collider_is_one_mesh_of_scene: bool, thrust_forward: f32, @@ -143,6 +144,7 @@ impl Default for ParserState { wants_maxrotation: None, wants_maxvelocity: None, wants_tolookat_id: None, + wants_matchvelocity_id: None, collider_is_mesh: false, collider_is_one_mesh_of_scene: false, thrust_forward: default_engine.thrust_forward, @@ -582,6 +584,10 @@ pub fn load_defs(mut ew_spawn: EventWriter) { // NOTE: Will not work if the actor has no engine state.wants_tolookat_id = Some(id.to_string()); } + ["wants", "matchvelocitywith", id] => { + // NOTE: Will not work if the actor has no engine + state.wants_matchvelocity_id = Some(id.to_string()); + } ["armodel", asset_name] => { state.ar_model = Some(asset_name.to_string()); } @@ -815,6 +821,9 @@ fn spawn_entities( if let Some(value) = &state.wants_tolookat_id { actor.insert(actor::WantsToLookAt(value.clone())); } + if let Some(value) = &state.wants_matchvelocity_id { + actor.insert(actor::WantsMatchVelocityWith(value.clone())); + } if let Some(color) = state.light_color { actor.insert(( PointLight { diff --git a/src/data/defs.txt b/src/data/defs.txt index 3fd429e..505db58 100644 --- a/src/data/defs.txt +++ b/src/data/defs.txt @@ -288,6 +288,7 @@ actor -55e3 44e3 0 suitv2 name "Yuni" chatid Yuni rotationx 180 + wants matchvelocitywith thebe actor 5000 0 -3000 moonlet name Moonlet @@ -309,12 +310,14 @@ actor 13200 300 -3000 hollow_asteroid actor 0 0 0 suitv2 template person relativeto cultasteroid + wants matchvelocitywith cultasteroid name "Ash" chatid Ash pronoun they actor -8 8 0 suitv2 template person relativeto cultasteroid + wants matchvelocitywith cultasteroid name "River" chatid River rotationy 54 @@ -443,6 +446,7 @@ actor -3300 10 0 pizzeria relativeto pizzeria armodel clippy_ar wants lookat PLAYERCAMERA + wants matchvelocitywith pizzeria rotationy -126 chatid SubduedClippy @@ -453,6 +457,7 @@ actor -3300 10 0 pizzeria chatid PizzaChef armodel suit_ar_chefhat wants lookat PLAYERCAMERA + wants matchvelocitywith pizzeria rotationy -90 pronoun he @@ -465,6 +470,7 @@ actor 30 -12 -40 suitv2 armodel suit_ar_wings angularmomentum 0.4 0.2 0.1 wants maxrotation 0.5 + wants matchvelocitywith pizzeria rotationy 108 rotationx 180 pronoun it @@ -496,6 +502,7 @@ actor -300 0 40 suitv2 name "梓涵" chatid Drifter alive no + wants maxvelocity none oxygen 0.08 pronoun she @@ -506,12 +513,14 @@ actor 100 -18000 2000 clippy name "StarTrans Clippy™ Serenity Station" armodel clippy_ar wants lookat PLAYERCAMERA + wants matchvelocitywith orbbusstopserenity rotationy -90 chatid ClippyTransSerenity actor 60 0 0 "orb_busstop" name "StarTrans Bus Stop: Serenity Station" relativeto busstopclippy + id orbbusstopserenity scale 5 actor 80 0 0 "orb_busstop" name "StarTrans Bus Stop: Serenity Station" @@ -638,6 +647,7 @@ actor 100 -18000 2000 clippy actor 8 20 0 suitv2 template person relativeto "busstopclippy" + wants matchvelocitywith orbbusstopserenity name "Rudy" chatid NPCinCryoStasis pronoun he @@ -649,12 +659,14 @@ actor -184971e3 149410e3 -134273e3 clippy name "StarTrans Clippy™ Farview Station" armodel clippy_ar wants lookat PLAYERCAMERA + wants matchvelocitywith orbbusstopfarview rotationy -90 chatid ClippyTransFarview actor 60 0 0 "orb_busstop" name "StarTrans Bus Stop: Farview Station" relativeto busstopclippy2 + id orbbusstopfarview scale 5 actor 80 0 0 "orb_busstop" name "StarTrans Bus Stop: Farview Station" @@ -786,6 +798,7 @@ actor 0 -44e3 0 clippy name "StarTrans Clippy™ Metis Prime Station" armodel clippy_ar wants lookat PLAYERCAMERA + wants matchvelocitywith orbbusstopmetis orbitaround jupiter 128000e3 orbit_phase_offset -0.002 rotationy -90 @@ -794,6 +807,7 @@ actor 0 -44e3 0 clippy actor 60 0 0 "orb_busstop" name "StarTrans Bus Stop: Metis Prime Station" relativeto busstopclippy3 + id orbbusstopmetis scale 5 actor 80 0 0 "orb_busstop" name "StarTrans Bus Stop: Metis Prime Station" diff --git a/src/game.rs b/src/game.rs index 8afa8ff..79e4fb5 100644 --- a/src/game.rs +++ b/src/game.rs @@ -26,12 +26,14 @@ impl Plugin for GamePlugin { app.add_systems(PostUpdate, handle_game_event); app.add_systems(PreUpdate, handle_player_death); app.add_systems(PostUpdate, update_id2pos); + app.add_systems(PostUpdate, update_id2v); app.add_systems( Update, handle_achievement_event.run_if(on_event::()), ); app.add_systems(Update, check_achievements); app.insert_resource(Id2Pos(HashMap::new())); + app.insert_resource(Id2V(HashMap::new())); app.insert_resource(JupiterPos(DVec3::ZERO)); app.insert_resource(var::AchievementTracker::default()); app.insert_resource(var::Settings::default()); @@ -51,6 +53,8 @@ pub struct PlayerDiesEvent(pub actor::DamageType); #[derive(Resource)] pub struct Id2Pos(pub HashMap); #[derive(Resource)] +pub struct Id2V(pub HashMap); +#[derive(Resource)] pub struct JupiterPos(pub DVec3); #[derive(Resource)] pub struct AchievementCheckTimer(pub Timer); @@ -368,6 +372,13 @@ fn update_id2pos( } } +fn update_id2v(mut id2v: ResMut, q_id: Query<(&LinearVelocity, &actor::Identifier)>) { + id2v.0.clear(); + for (v, id) in &q_id { + id2v.0.insert(id.0.clone(), v.0); + } +} + fn debug( settings: Res, keyboard_input: Res>,