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>,
}
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(Event, Clone, PartialEq)]
pub struct SendPacket(pub Packet);
#[derive(Event, Clone, PartialEq)]
pub struct RecvPacket(pub Packet);