Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions engine/pill_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ lazy_static = "1.4.0"
dyn-clone = "1.0.4"
egui = { version = "0.32.1", optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
bincode = "1.0"
once_cell = "1.21.3"
bitvec = "1.0.1"
rand = "0.9.2"
web-time = "1.1"
rapier3d = { version = "0.32.0", optional = true }
uuid = { version = "1.23.4", features = ["v4"] }

# Native-only dependencies (not available on WASM)
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
1 change: 1 addition & 0 deletions engine/pill_engine/src/ecs/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub(crate) mod physics_world_component;
pub(crate) mod rigid_body_component;
pub(crate) mod time_component;
pub(crate) mod transform_component;
pub(crate) mod uuid_component;

// --- Use ---

Expand Down
30 changes: 30 additions & 0 deletions engine/pill_engine/src/ecs/components/uuid_component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use pill_core::PillTypeMapKey;
use uuid::Uuid;

use crate::{Component, ComponentStorage};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct UuidComponent {
pub uuid: u128,
}

impl Default for UuidComponent {
fn default() -> Self {
Self::new()
}
}

impl UuidComponent {
pub fn new() -> Self {
Self {
uuid: Uuid::new_v4().as_u128(),
}
}
}

impl PillTypeMapKey for UuidComponent {
type Storage = ComponentStorage<UuidComponent>;
}

impl Component for UuidComponent {}
2 changes: 2 additions & 0 deletions engine/pill_engine/src/ecs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub use components::network_manager_component::{
#[cfg(not(target_arch = "wasm32"))]
pub use components::network_state_component::{NetworkEntityState, NetworkStateComponent};

pub use components::uuid_component::UuidComponent;

// - Systems

pub use systems::{SystemFunction, SystemManager, UpdatePhase};
Expand Down
69 changes: 63 additions & 6 deletions engine/pill_engine/src/engine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::app_config::EngineProcessInfo;
use crate::serdeserialization::{JsonBackend, Serdeser, SerdeserBackend};
use crate::{app_config::EngineConfig, config::*, ecs::*, graphics::*, resources::*};

use pill_core::{
Expand All @@ -7,7 +8,7 @@
};

use pill_core::{ErrorContext, Result};
use std::{any::TypeId, collections::VecDeque};
use std::{any::TypeId, collections::VecDeque, path::Path};
use winit::{dpi::PhysicalPosition, event::KeyEvent};

// -------------------------------------------------------------------------------
Expand Down Expand Up @@ -38,6 +39,8 @@
pub(crate) window_size: winit::dpi::PhysicalSize<u32>,
pub(crate) game_resources_directory_path: std::path::PathBuf,
pub(crate) frame_delta_time: f32, // In milliseconds
pub(crate) serdeser: Serdeser,
pub(crate) serdeser_backend: Box<dyn SerdeserBackend>,
}

// ---- INTERNAL -----------------------------------------------------------------
Expand Down Expand Up @@ -71,6 +74,8 @@
window_size: winit::dpi::PhysicalSize::<u32>::default(),
game_resources_directory_path,
frame_delta_time: 0.0,
serdeser: Serdeser::new(),
serdeser_backend: Box::new(JsonBackend),
}
}

Expand All @@ -94,7 +99,9 @@
render_queue: Vec::<RenderQueueItem>::with_capacity(max_entity_count),
window_size: winit::dpi::PhysicalSize::<u32>::default(),
game_resources_directory_path: std::path::PathBuf::new(),
frame_delta_time: 0.0.into(),
frame_delta_time: 0.0,
serdeser: Serdeser::new(),
serdeser_backend: Box::new(JsonBackend),
}
}

Expand Down Expand Up @@ -391,6 +398,11 @@
self.create_default_resources()
.context("Failed to create default resources")?;

// TODO: register resources in serializer?
// register desired components in serializer's registry
self.serdeser.register_component::<TransformComponent>()?;
self.serdeser.register_component::<UuidComponent>()?;

// Start game
self.start_game()?;

Expand Down Expand Up @@ -569,6 +581,35 @@
pub fn get_input_queue(&self) -> &VecDeque<InputEvent> {
&self.input_queue
}

// TODO: temporary functions to trigger scene loading/saving
pub fn save_scene(&self) -> Result<()> {
let entities = self.iterate_two_components::<UuidComponent, TransformComponent>()?;
// TODO: for now not dancing around this - later we get into trouble as we pass entities
// here that contain mut ref to the engine
let mut backend = JsonBackend;
let path = Path::new("test.scene");
self.serdeser.serialize(entities, &mut backend, path)?;
println!("SCENE SAVED!");

Ok(())
}

pub fn load_scene(&mut self, scene_handle: SceneHandle) -> Result<()> {

Check failure on line 598 in engine/pill_engine/src/engine.rs

View workflow job for this annotation

GitHub Actions / build_examples (examples/net_minimal/server)

unused variable: `scene_handle`
// TODO: not loading the scene yet, just reading out what we have parsed from the file
// TODO: for now not dancing around this - later we get into trouble as we pass entities
// here that contain mut ref to the engine
let mut backend = JsonBackend;
let path = Path::new("test.scene");
let decoded = self.serdeser.deserialize(&mut backend, path)?;
println!("Num of decoded entities: {}", decoded.len());
for entity in decoded {
println!("Entity with uuid: {}", entity.uuid);
// TODO: spawn necessary objects
}
println!("SCENE LOADED !");
Ok(())
}
}

// --- API ------------------------------------------------------------------
Expand Down Expand Up @@ -679,12 +720,21 @@
.name_style()
);

self.scene_manager
let entity_handle = self
.scene_manager
.create_entity(scene_handle)
.context(format!(
"Creating {} failed",
"Entity".general_object_style()
))
));

if let Ok(handle) = entity_handle {
// add UUID to all created entities
self.add_component_to_entity(scene_handle, handle, UuidComponent::new())
.unwrap();
}

entity_handle
}

// Removes entity specified with entity handle from scene specified with scene handle
Expand Down Expand Up @@ -1020,10 +1070,17 @@
// Creates scene
pub fn create_scene(&mut self, name: &str) -> Result<SceneHandle> {
info!(LogContext::ECS => "Creating scene: {}", name);
self.scene_manager.create_scene(name).context(format!(
let scene_handle = self.scene_manager.create_scene(name).context(format!(
"Creating new {} failed",
"Scene".general_object_style()
))
));

// always register the uuid component with every scene
if let Ok(scene) = scene_handle {
self.register_component::<UuidComponent>(scene)?;
};

scene_handle
}

/// Returns handle to the scene specified by its name
Expand Down
1 change: 1 addition & 0 deletions engine/pill_engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod ecs;
mod engine;
mod graphics;
mod resources;
mod serdeserialization;

// --- Macros ---

Expand Down
7 changes: 7 additions & 0 deletions engine/pill_engine/src/serdeserialization/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![cfg_attr(debug_assertions, allow(dead_code, unused_imports, unused_variables))]

mod serdeser;
mod serdeser_backend;

pub use serdeser::Serdeser;
pub use serdeser_backend::{JsonBackend, SerdeserBackend};
Loading
Loading