~starkingdoms/starkingdoms

ref: 028986b26fc30a70f28bd165f8a07870854df75e starkingdoms/crates/unified/src/server/player.rs -rw-r--r-- 5.4 KiB
028986b2 — core chore: fix gravity 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::f32::consts::PI;

use crate::config::planet::Planet;
use crate::ecs::{DragRequestEvent, Part, PartBundle, Particles, Player, PlayerThrust, ThrustEvent};
use crate::server::world_config::WorldConfigResource;
use crate::server::{ConnectedGameEntity, ConnectedNetworkEntity};
use bevy::prelude::*;
use bevy_rapier2d::prelude::{
    AdditionalMassProperties, Collider, ExternalForce, ExternalImpulse, MassProperties,
};
use bevy_replicon::prelude::{FromClient, Replicated};
use crate::server::part::{SpawnPart};

pub fn player_management_plugin(app: &mut App) {
    app
        .add_systems(Update, (handle_new_players, player_thrust, dragging));
}

fn dragging(mut events: EventReader<FromClient<DragRequestEvent>>, mut parts: Query<&mut Transform, With<Part>>) {
    for event in events.read() {
        let mut part = parts.get_mut(event.event.0).unwrap();
        part.translation.x = event.event.1.x;
        part.translation.y = event.event.1.y;
    }
}

fn handle_new_players(
    mut commands: Commands,
    q_new_clients: Query<Entity, Added<ConnectedGameEntity>>,
    world_config: Res<WorldConfigResource>,
    planets: Query<(&Transform, &Planet)>,
    asset_server: Res<AssetServer>,
    gt: Query<&GlobalTransform>,
    child_of: Query<&ChildOf>
) {
    let Some(wc) = &world_config.config else {
        return;
    };
    for joined_player in &q_new_clients {
        trace!(?joined_player, "detected joined player!");
        // find earth
        let (spawn_planet_pos, spawn_planet) = planets
            .iter()
            .find(|p| p.1.name == wc.hearty.spawn_at)
            .unwrap_or_else(|| panic!("spawn planet {} is missing? (check that the planet is named exactly '{}')", wc.hearty.spawn_at, wc.hearty.spawn_at));
        let angle = rand::random::<f32>() * std::f32::consts::TAU;
        let offset = spawn_planet.radius + 150.0;
        let mut new_transform =
            Transform::from_xyz(angle.cos() * offset, angle.sin() * offset, 0.0);
        new_transform.rotate_z(angle);
        new_transform.translation += spawn_planet_pos.translation;

        info!(?new_transform, ?joined_player, "set player's position!");

        commands.entity(joined_player)
            .insert(new_transform);

        commands.entity(joined_player)
            .insert(SpawnPart("config/parts/hearty.part.toml".to_string()))
            .insert(PlayerThrust::default())
            .insert(Player {
                client: joined_player
            });
    }
}

fn player_thrust(
    mut players: Query<(&Transform, &mut ExternalForce, &mut PlayerThrust)>,
    clients: Query<&ConnectedNetworkEntity>,
    mut thrust_event: EventReader<FromClient<ThrustEvent>>,
    world_config: Res<WorldConfigResource>,
) {
    for FromClient {
        client_entity,
        event,
    } in thrust_event.read()
    {
        let ConnectedNetworkEntity { game_entity } = clients.get(*client_entity).unwrap();

        let Ok((_, _, mut thrust)) = players.get_mut(*game_entity) else {
            continue;
        };
        match *event {
            ThrustEvent::Up(on) => thrust.up = on,
            ThrustEvent::Down(on) => thrust.down = on,
            ThrustEvent::Left(on) => thrust.left = on,
            ThrustEvent::Right(on) => thrust.right = on,
        }
    }
    for (transform, mut force, thrust) in &mut players {
        let Some(world_config) = &world_config.config else {
            return;
        };

        let forward = (transform.rotation * Vec3::Y).xy();
        let mut external_force = ExternalForce::default();
        let mut thrusters = [0.0; 4]; // counterclockwise wrt +y axis
        if thrust.up {
            thrusters[1] = 1.0;
            thrusters[2] = 1.0;
        }
        if thrust.down {
            thrusters[0] = 1.0;
            thrusters[3] = 1.0;
        }
        if thrust.left {
            thrusters[0] = 1.0;
            thrusters[2] = 1.0;
        }
        if thrust.right {
            thrusters[1] = 1.0;
            thrusters[3] = 1.0;
        }
        let half_size = Vec2::new(
            world_config.part.default_width / 2.0,
            world_config.part.default_height / 2.0,
        )
        .length();
        external_force += ExternalForce::at_point(
            -forward * thrusters[0] * world_config.hearty.thrust,
            transform.translation.xy()
                + half_size
                    * Vec2::new((1.0 * PI / 4.0).cos(), (1.0 * PI / 4.0).sin()).rotate(forward),
            transform.translation.xy(),
        );
        external_force += ExternalForce::at_point(
            forward * thrusters[1] * world_config.hearty.thrust,
            transform.translation.xy()
                + half_size
                    * Vec2::new((3.0 * PI / 4.0).cos(), (3.0 * PI / 4.0).sin()).rotate(forward),
            transform.translation.xy(),
        );
        external_force += ExternalForce::at_point(
            forward * thrusters[2] * world_config.hearty.thrust,
            transform.translation.xy()
                + half_size
                    * Vec2::new((5.0 * PI / 4.0).cos(), (5.0 * PI / 4.0).sin()).rotate(forward),
            transform.translation.xy(),
        );
        external_force += ExternalForce::at_point(
            -forward * thrusters[3] * world_config.hearty.thrust,
            transform.translation.xy()
                + half_size
                    * Vec2::new((7.0 * PI / 4.0).cos(), (7.0 * PI / 4.0).sin()).rotate(forward),
            transform.translation.xy(),
        );
        *force += external_force;
    }
}