From 4d45eca806d0f61200ab175cb7bd900f0dfe4c66 Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Sat, 6 Jan 2024 02:19:26 -0600 Subject: [PATCH] putting the thruster in landing thruster --- server/src/main.rs | 149 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 140 insertions(+), 9 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index 799ef3154cbfd1f4e9cd0432155b99363d3085a4..d9a5a2eab58a19b2cf5e7480051049df201c4477 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,3 +1,4 @@ +use std::f32::consts::PI; // StarKingdoms.IO, a browser game about drifting through space // Copyright (C) 2023 ghostly_zsh, TerraMaster85, core // @@ -23,6 +24,7 @@ use bevy::{ecs::event::ManualEventReader, prelude::*}; use bevy_rapier2d::prelude::*; use bevy_twite::{twite::frame::MessageType, ServerEvent, TwiteServerConfig, TwiteServerPlugin}; use component::*; +use component::Input; use packet::*; use rand::Rng; @@ -43,7 +45,9 @@ const MARS_MASS: f32 = EARTH_MASS / 8.; const GRAVITY: f32 = 0.02; const PART_HALF_SIZE: f32 = 25.0; -const THRUSTER_FORCE: f32 = 0.08; + +const HEARTY_THRUSTER_FORCE: f32 = 0.08; +const LANDING_THRUSTER_FORCE: f32 = 5.; // maybe make this only cargo modules later const FREE_MODULE_CAP: usize = 30; @@ -121,7 +125,7 @@ fn spawn_planets(mut commands: Commands) { .insert(Sensor); }) .insert(RigidBody::Fixed); - let mars_pos = Transform::from_xyz(-3000.0 / SCALE, 0.0, 0.0); + let mars_pos = Transform::from_xyz(3000.0 / SCALE, 700.0 / SCALE, 0.0); commands .spawn(PlanetBundle { planet_type: PlanetType::Mars, @@ -1270,11 +1274,12 @@ fn on_position_change( } fn player_input_update( - mut player_and_body_query: Query<(Entity, &mut Player, &mut ExternalForce, &Transform)>, + mut player_and_body_query: Query<(Entity, &mut Player, &Attach, &mut ExternalForce, &Transform)>, + mut attached_query: Query<(&Attach, &PartType, &mut ExternalForce, &Transform), Without> ) { - for (_, player, mut forces, transform) in &mut player_and_body_query { - forces.torque = 0.0; - forces.force = Vec2::ZERO; + for (_, player, attach, mut forces, transform) in &mut player_and_body_query { + //forces.torque = 0.0; + //forces.force = Vec2::ZERO; if !(player.input.up || player.input.down || player.input.right || player.input.left) { continue; } @@ -1326,7 +1331,7 @@ fn player_input_update( let thruster_pos_uncast = vec2(x_offset / SCALE, y_offset / SCALE); let thruster_pos_cast = rot2d(thruster_pos_uncast, rot) + transform.translation.xy(); - let thruster_force = force_multiplier * THRUSTER_FORCE; + let thruster_force = force_multiplier * HEARTY_THRUSTER_FORCE; let thruster_vec = vec2( -thruster_force / SCALE * rot.sin(), thruster_force / SCALE * rot.cos(), @@ -1340,15 +1345,141 @@ fn player_input_update( forces.torque += thruster_force.torque; } } + search_thrusters(player.input, attach.clone(), *transform, &mut attached_query); + } +} +fn search_thrusters( + input: Input, + attach: Attach, + p_transform: Transform, + attached_query: &mut Query<(&Attach, &PartType, &mut ExternalForce, &Transform), Without> +) { + let p_angle = p_transform.rotation.to_euler(EulerRot::ZYX).0; + for child in attach.children { + if let Some(child) = child { + let (attach, part_type, mut force, transform) = attached_query.get_mut(child).unwrap(); + let angle = transform.rotation.to_euler(EulerRot::ZYX).0; + let relative_angle = (p_angle - angle).abs(); + let relative_pos = transform.translation - p_transform.translation; + let relative_pos = Vec2::new( + relative_pos.x.mul_add((-p_angle).cos(), -relative_pos.y * (-p_angle).sin()), + relative_pos.x.mul_add((-p_angle).sin(), relative_pos.y * (-p_angle).cos()), + ); + + let mut force_mult = 0.; + if *part_type == PartType::LandingThruster { + force_mult = LANDING_THRUSTER_FORCE; + } + if input.up { + if 3.*PI/4. < relative_angle && relative_angle < 5.*PI/4. { + let thruster_force = ExternalForce::at_point( + Vec2::new(-force_mult / SCALE * angle.sin(), + force_mult / SCALE * angle.cos()), + transform.translation.xy(), + transform.translation.xy(), + ); + force.force += thruster_force.force; + force.torque += thruster_force.torque; + } + } + if input.down { + if (0. < relative_angle && relative_angle < PI/4.)||(7.*PI/4. < relative_angle && relative_angle < 2.*PI) { + let thruster_force = ExternalForce::at_point( + Vec2::new(-force_mult / SCALE * angle.sin(), + force_mult / SCALE * angle.cos()), + transform.translation.xy(), + transform.translation.xy(), + ); + force.force += thruster_force.force; + force.torque += thruster_force.torque; + } + } + if input.left { + if 3.*PI/4. < relative_angle && relative_angle < 5.*PI/4. { + if relative_pos.x > 24. / SCALE { + let thruster_force = ExternalForce::at_point( + Vec2::new(-force_mult / SCALE * angle.sin(), + force_mult / SCALE * angle.cos()), + transform.translation.xy(), + transform.translation.xy(), + ); + force.force += thruster_force.force; + force.torque += thruster_force.torque; + } + } + if (0. < relative_angle && relative_angle < PI/4.)||(7.*PI/4. < relative_angle && relative_angle < 2.*PI) { + if relative_pos.x < -24. / SCALE { + let thruster_force = ExternalForce::at_point( + Vec2::new(-force_mult / SCALE * angle.sin(), + force_mult / SCALE * angle.cos()), + transform.translation.xy(), + transform.translation.xy(), + ); + force.force += thruster_force.force; + force.torque += thruster_force.torque; + } + } + if PI/4. < relative_angle && relative_angle < 3.*PI/4. { + if relative_pos.y < -24. / SCALE { + let thruster_force = ExternalForce::at_point( + Vec2::new(-force_mult / SCALE * angle.sin(), + force_mult / SCALE * angle.cos()), + transform.translation.xy(), + transform.translation.xy(), + ); + force.force += thruster_force.force; + force.torque += thruster_force.torque; + } + if -24. / SCALE < relative_pos.y && relative_pos.y < 24. / SCALE { + let thruster_force = ExternalForce::at_point( + Vec2::new(-force_mult / SCALE * angle.sin(), + force_mult / SCALE * angle.cos()), + transform.translation.xy(), + transform.translation.xy(), + ); + force.force += thruster_force.force; + force.torque += thruster_force.torque; + } + } + if 5.*PI/4. < relative_angle && relative_angle < 7.*PI/4. { + if relative_pos.y > 24. / SCALE { + let thruster_force = ExternalForce::at_point( + Vec2::new(-force_mult / SCALE * angle.sin(), + force_mult / SCALE * angle.cos()), + transform.translation.xy(), + transform.translation.xy(), + ); + force.force += thruster_force.force; + force.torque += thruster_force.torque; + } + if -24. / SCALE < relative_pos.y && relative_pos.y < 24. / SCALE { + let thruster_force = ExternalForce::at_point( + Vec2::new(-force_mult / SCALE * angle.sin(), + force_mult / SCALE * angle.cos()), + transform.translation.xy(), + transform.translation.xy(), + ); + force.force += thruster_force.force; + force.torque += thruster_force.torque; + } + } + } + + if *part_type != PartType::LandingThruster { + search_thrusters(input, attach.clone(), p_transform, attached_query); + } + } } } fn gravity_update( - mut part_query: Query<(&Transform, &ReadMassProperties, &mut ExternalImpulse), With>, + mut part_query: Query<(&Transform, &ReadMassProperties, &mut ExternalForce, &mut ExternalImpulse), With>, planet_query: Query<(&Transform, &ReadMassProperties), With>, ) { - for (part_transform, part_mp, mut impulses) in &mut part_query { + for (part_transform, part_mp, mut forces, mut impulses) in &mut part_query { impulses.impulse = Vec2::ZERO; + forces.force = Vec2::ZERO; + forces.torque = 0.; let part_mp = part_mp.get(); let part_mass = part_mp.mass; let part_translate = part_transform.translation;