~starkingdoms/starkingdoms

ref: e2fa1806cfb5c2d40276cd5c0133372df1c2708f starkingdoms/crates/client/src/components.rs -rw-r--r-- 3.3 KiB
e2fa1806 — core fix: missing clamps 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
use bevy_ecs::schedule::ScheduleLabel;
use bevy_ecs::{bundle::Bundle, component::Component, event::Event, system::Resource};
use nalgebra::{Matrix3, Matrix4, Rotation2, Scale2, Scale3, Translation2, Translation3};
use starkingdoms_common::packet::Packet;

#[derive(Bundle)]
pub struct PlayerBundle {
    pub transform: Transform,
    pub texture: Texture,
    pub player: Player,
    pub part: Part,
}

#[derive(Bundle)]
pub struct PartBundle {
    pub transform: Transform,
    pub texture: Texture,
    pub server_id: ServerId,
    pub part: Part,
}

#[derive(Bundle)]
pub struct OtherPlayerBundle {
    pub part: PartBundle,
    pub username: PlayerUsername,
}

#[derive(Bundle)]
pub struct PlanetBundle {
    pub transform: Transform,
    pub texture: Texture,
    pub server_id: ServerId,
    pub planet: Planet,
}

#[derive(ScheduleLabel, Hash, Clone, Debug, Eq, PartialEq)]
pub enum Schedule {
    Startup,
    Update,
}

#[derive(Component, Debug)]
pub struct PlayerUsername {
    pub username: String,
}

#[derive(Component, Debug)]
pub struct Texture {
    pub name: String,
}

#[derive(Component, Debug)]
pub struct Transform {
    pub translation: Translation3<f32>,
    pub rotation: Rotation2<f32>,
    pub scale: Scale3<f32>,
}
#[derive(Resource, Debug)]
pub struct PlayerResources {
    pub fuel_amount: u32,
    pub fuel_max: u32,
}

impl Transform {
    pub fn to_matrix(&self) -> Matrix4<f32> {
        self.translation.to_homogeneous()
            * self.rotation.to_homogeneous().to_homogeneous()
            * self.scale.to_homogeneous()
    }
}

#[derive(Bundle, Debug)]
pub struct SpriteBundle {
    pub transform: Transform,
    pub texture: Texture,
}

#[derive(Resource, Debug)]
pub struct Camera {
    pub x: f32,
    pub y: f32,
    pub zoom: f32,
    pub width: u32,  // screen width (these are for aspect ratio)
    pub height: u32, // screen height
}
impl Camera {
    pub fn to_matrix(&self) -> Matrix4<f32> {
        let x_scale = self.zoom / self.width as f32 * 2.0;
        let y_scale = self.zoom / self.height as f32 * 2.0;
        Matrix4::from_vec(vec![
            x_scale,
            0.0,
            0.0,
            0.0,
            0.0,
            y_scale,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
            0.0,
            self.x * x_scale,
            self.y * y_scale,
            0.0,
            1.0,
        ])
    }
    pub fn to_cursor_matrix(&self) -> Matrix3<f32> {
        let x = -(self.width as f32 / 2.0);
        let y = -(self.height as f32 / 2.0);
        Translation2::new(-self.x, -self.y).to_homogeneous()
            * Scale2::new(1.0 / self.zoom, 1.0 / self.zoom).to_homogeneous()
            * Translation2::new(x, y).to_homogeneous()
    }
}

#[derive(Component, Debug, Clone, Copy)]
pub struct Player;
#[derive(Component, Debug, Clone, Copy)]
pub struct Planet;
#[derive(Component, Debug, Clone, Copy)]
pub struct Part(pub bool); // Part(attached)
#[derive(Component, Debug, Clone, Copy)]
pub struct ServerId(pub u32);

#[derive(Component, Debug, Clone, Copy)]
pub struct Menu;

#[derive(Event, Clone, PartialEq)]
pub struct SendPacket(pub Packet);
#[derive(Event, Clone, PartialEq)]
pub struct RecvPacket(pub Packet);

#[derive(Resource, Clone)]
pub struct Chat {
    pub messages: Vec<String>,
    pub textbox: String,
}