~starkingdoms/starkingdoms

ref: 87db7538d05166cb8cdb701886b38b32de9bc708 starkingdoms/crates/client/src/components.rs -rw-r--r-- 2.5 KiB
87db7538 — core cargo fmt 8 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
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(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,
}