bevy-expedition-demo/src/layout/asset/structs/inner.rs
2ndbeam 08751ff12f feat: lighting system
- Added lamp bundle
- Moved door collider to its children
- Updated level structure
2026-03-31 13:37:54 +03:00

240 lines
6.5 KiB
Rust

use super::*;
pub(super) fn default_floors() -> u8 { 2 }
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Clone, Copy, Reflect)]
#[reflect(Debug, Default, Deserialize, Serialize, PartialEq, Clone)]
#[serde(default)]
pub(super) struct Pos {
pub x: f32,
pub y: f32,
}
impl From<Pos> for Vec2 {
fn from(Pos { x, y }: Pos) -> Self {
Self { x, y }
}
}
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq, Clone, Copy, Reflect)]
#[reflect(Debug, Default, Deserialize, Serialize, PartialEq, Clone)]
#[serde(default)]
pub(super) struct UPos {
pub x: u32,
pub y: u32,
}
impl From<UPos> for UVec2 {
fn from(UPos { x, y }: UPos) -> Self {
Self { x, y }
}
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone, Copy, Reflect)]
#[reflect(Debug, Default, Deserialize, Serialize, PartialEq, Clone)]
#[serde(default)]
pub(super) struct USize {
pub w: u32,
pub h: u32,
}
impl Default for USize {
fn default() -> Self {
Self { w: 1, h: 1 }
}
}
impl From<USize> for UVec2 {
fn from(USize { w, h }: USize) -> Self {
Self { x: w, y: h }
}
}
#[derive(Debug, Deserialize, Serialize, Default, PartialEq, Eq, Clone, Copy, Reflect)]
#[reflect(Debug, Default, Deserialize, Serialize, PartialEq, Clone)]
#[serde(default)]
pub(super) struct USizeRect {
#[serde(flatten)]
pub pos: UPos,
#[serde(flatten)]
pub size: USize,
}
impl From<USizeRect> for URect {
fn from(USizeRect { pos, size: USize { w, h } }: USizeRect) -> Self {
URect::from_corners(pos.into(), uvec2(pos.x + w - 1, pos.y + h - 1))
}
}
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq, Clone, Copy, Reflect)]
#[reflect(Debug, Default, Deserialize, Serialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase")]
pub(super) enum Facing {
#[default]
Left,
Right,
}
impl From<Facing> for bool {
fn from(value: Facing) -> Self {
match value {
Facing::Left => true,
Facing::Right => false,
}
}
}
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Clone, Copy, Reflect)]
#[reflect(Debug, Default, Deserialize, Serialize, PartialEq, Clone)]
pub(super) struct DoorDataInner {
#[serde(flatten)]
pub pos: Pos,
#[serde(default)]
pub facing: Option<Facing>,
#[serde(default)]
pub lock: Option<Facing>,
}
impl From<DoorDataInner> for DoorData {
fn from(DoorDataInner { pos, facing, lock }: DoorDataInner) -> Self {
let lock =
if let Some(lock) = lock { Some(lock.into()) }
else { None };
Self {
pos: pos.into(),
facing_left: facing.unwrap_or_default().into(),
lock,
}
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Copy, Reflect)]
#[reflect(Debug, Default, Deserialize, Serialize, PartialEq, Clone)]
pub(super) struct StairsInnerData {
#[serde(flatten)]
pub pos: Pos,
#[serde(default = "default_floors")]
pub floors: u8,
}
impl From<StairsInnerData> for StairsData {
fn from(StairsInnerData { pos, floors }: StairsInnerData) -> Self {
Self {
pos: pos.into(),
floors,
}
}
}
impl Default for StairsInnerData {
fn default() -> Self {
Self {
pos: Pos::default(),
floors: default_floors(),
}
}
}
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq, Clone, Reflect)]
#[reflect(Debug, Default, Deserialize, Serialize, PartialEq, Clone)]
pub(super) struct ItemDataInner {
pub id: String,
#[serde(default, flatten)]
pub pos: UPos,
#[serde(default)]
pub rotated: bool,
}
impl From<ItemDataInner> for ItemData {
fn from(ItemDataInner { id, pos, rotated }: ItemDataInner) -> Self {
Self {
id,
pos: pos.into(),
rotated,
}
}
}
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Clone, Reflect)]
#[reflect(Debug, Default, Deserialize, Serialize, PartialEq, Clone)]
pub(super) struct ContainerDataInner {
#[serde(flatten)]
pub pos: Pos,
#[serde(flatten, default)]
pub size: Option<USize>,
#[serde(default)]
pub items: Option<Vec<ItemData>>,
}
impl From<ContainerDataInner> for ContainerData {
fn from(ContainerDataInner { pos, size, items }: ContainerDataInner) -> Self {
Self {
pos: pos.into(),
size: size.unwrap_or_default().into(),
items: items.unwrap_or_default(),
}
}
}
pub(super) fn default_intensity() -> f32 { 2. }
pub(super) fn default_radius() -> f32 { 4. }
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Clone, Reflect)]
#[reflect(Debug, Default, Deserialize, Serialize, PartialEq, Clone)]
pub(super) struct LampDataInner {
#[serde(flatten)]
pub pos: Pos,
#[serde(default = "default_intensity")]
pub intensity: f32,
#[serde(default = "default_radius")]
pub radius: f32,
}
impl From<LampDataInner> for LampData {
fn from(LampDataInner { pos, intensity, radius }: LampDataInner) -> Self {
Self {
pos: pos.into(),
intensity,
radius,
}
}
}
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Clone, Reflect)]
#[reflect(Debug, Default, Deserialize, Serialize, PartialEq, Clone)]
pub(super) struct InteractiveInner {
pub player: Pos,
#[serde(default)]
pub doors: Option<Vec<DoorData>>,
#[serde(default)]
pub stairs: Option<Vec<StairsData>>,
#[serde(default)]
pub containers: Option<Vec<ContainerData>>,
#[serde(default)]
pub lamps: Option<Vec<LampData>>,
}
impl From<InteractiveInner> for Interactive {
fn from(InteractiveInner { player, doors, stairs, containers, lamps }: InteractiveInner) -> Self {
Self {
player: player.into(),
doors: doors.unwrap_or_default(),
stairs: stairs.unwrap_or_default(),
containers: containers.unwrap_or_default(),
lamps: lamps.unwrap_or_default(),
}
}
}
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq, Clone, Reflect)]
#[reflect(Debug, Default, Deserialize, Serialize, PartialEq, Clone)]
#[serde(transparent)]
pub(super) struct TilesInner(HashMap<String, Vec<USizeRect>>);
impl From<TilesInner> for Tiles {
fn from(TilesInner(tiles): TilesInner) -> Self {
// This is probably the funniest one-liner I've ever written
Tiles { tiles: tiles.into_iter().map(|(k, v)| (k, v.into_iter().map(|v| v.into()).collect())).collect() }
}
}