This commit is contained in:
hodasemi 2025-02-28 08:19:35 +01:00
parent 7253251e9d
commit d7a58d7352
8 changed files with 147 additions and 201 deletions

View file

@ -87,6 +87,10 @@ impl World {
self.entity_object_manager.create_entity()
}
pub fn clone_without_components(&mut self, entity: &EntityObject) -> EntityObject {
entity.clone_without_components(self.entity_object_manager.fetch_add_entity_id())
}
pub fn entities(&self) -> impl Iterator<Item = &EntityObject> {
self.entities.values()
}

View file

@ -5,7 +5,11 @@ use crate::prelude::*;
use anyhow::Result;
pub trait AssetLoader {
fn load_entity(&mut self, assets: AssetHandler<'_>, entity_file: &str) -> Result<EntityObject>;
fn load_entity(
&mut self,
assets: &mut AssetHandler<'_>,
entity_file: &str,
) -> Result<EntityObject>;
}
pub struct AssetHandler<'a> {

View file

@ -1,24 +0,0 @@
use std::any::Any;
use crate::prelude::*;
use anyhow::Result;
use utilities::prelude::cgmath::{Vector2, Vector3};
pub trait Map: Any + Send + Sync {
fn name(&self) -> &str;
fn check_walkability(
&self,
position: Vector2<f32>,
direction: Vector2<f32>,
radius: f32,
) -> Result<bool>;
fn get_height(&self, x: f32, y: f32) -> Result<f32>;
fn spawn_positions(&self) -> Result<Vec<Vector3<f32>>>;
fn leave_markers(&self) -> Result<Vec<Entity>>;
fn disable(&self, scene: &mut Scene) -> Result<()>;
}

View file

@ -1,5 +1,4 @@
pub mod components;
pub mod map;
pub mod prelude;
// mod query;

View file

@ -1,5 +1,3 @@
pub use super::map::Map;
pub use super::components::{animation::Animation, audio::Audio, draw::Draw};
pub use super::components::{

View file

@ -126,7 +126,7 @@ impl AssetLoader for EntityManager {
/// Loads an entity file and creates an Entity
fn load_entity(
&mut self,
mut assets: AssetHandler<'_>,
assets: &mut AssetHandler<'_>,
entity_file: &str,
) -> Result<EntityObject> {
// load entity file

View file

@ -2,7 +2,6 @@
use anyhow::Result;
use assetpath::AssetPath;
use engine::prelude::PrimitiveMaterial;
use engine::prelude::*;
use super::{
@ -12,25 +11,19 @@ use super::{
};
use super::async_db::AsyncDBAccess;
use super::map_db::{DBEntityInfo, MapDataBase};
// std
use std::cmp::{max, min};
use std::sync::atomic::{AtomicBool, Ordering::SeqCst};
use std::sync::{Arc, RwLock};
use std::{collections::HashMap, intrinsics::transmute};
use std::collections::HashMap;
use std::ops::IndexMut;
use std::sync::atomic::{AtomicBool, Ordering::SeqCst};
use std::sync::{Arc, RwLock};
// cgmath
use cgmath::Rad;
use cgmath::{InnerSpace, Vector2, Vector3, Zero, vec2};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -58,16 +51,11 @@ impl Map {
// describes the length of a chunk (e.g.: 10 in x and in y direction = 100 tiles per chunk)
const CHUNK_DIMENSION: u32 = 10;
pub fn downcast(upcasted: &Box<dyn engine::prelude::Map>) -> &Box<Self> {
unsafe { transmute(destructure_traitobject::data(upcasted)) }
}
pub fn new(
name: &str,
width: u32,
height: u32,
engine: &Engine,
scene: &mut Scene,
world: &mut World,
maps: &mut HashMap<String, AssetPath>,
tiles: &HashMap<String, AssetPath>,
entity_manager: &mut impl AssetLoader,
@ -76,8 +64,8 @@ impl Map {
let map_path = build_path(&("maps/".to_string() + name));
let sql_connection = MapDataBase::new(map_path.full_path(), name, width, height)?;
let map = Self::load_data_from_db(engine, scene, sql_connection, tiles, entity_manager)?;
scene.set_map(Some(Box::new(map)))?;
let map = Self::load_data_from_db(world, sql_connection, tiles, entity_manager)?;
world.resources.insert(map);
maps.insert(name.to_string(), map_path);
@ -85,15 +73,14 @@ impl Map {
}
pub fn load_raw_scene(
engine: &Engine,
scene: &mut Scene,
world: &mut World,
path: AssetPath,
tiles: &HashMap<String, AssetPath>,
entity_manager: &mut impl AssetLoader,
) -> Result<()> {
let sql_connection = MapDataBase::load(&path.full_path())?;
let map = Self::load_data_from_db(engine, scene, sql_connection, tiles, entity_manager)?;
scene.set_map(Some(Box::new(map)))?;
let map = Self::load_data_from_db(world, sql_connection, tiles, entity_manager)?;
world.resources.insert(map);
Ok(())
}
@ -129,7 +116,7 @@ impl Map {
Self::calculate_chunk_and_primitive_indices(dx, dy);
let chunk_entity = data.chunk_handles[&chunk];
let entity = scene.entity_mut(chunk_entity)?;
let entity = world.entity_mut(chunk_entity)?;
let draw = entity.get_component_mut::<Draw>()?;
let mesh = &mut draw[0];
@ -189,8 +176,8 @@ impl Map {
self.updated.store(true, SeqCst);
}
pub fn check_height_at(&self, x: u32, y: u32, scene: &mut Scene) -> Result<()> {
self.data.write().unwrap().check_height_at(x, y, scene)
pub fn check_height_at(&self, x: u32, y: u32, world: &mut World) -> Result<()> {
self.data.write().unwrap().check_height_at(x, y, world)
}
pub fn npc_spawn_info(&self, position: Vector2<f32>) -> Result<MapNPCSpawnInfo> {
@ -245,13 +232,13 @@ impl Map {
pub fn change_npc_spawn(
&self,
scene_contents: &mut impl SceneEntities,
world: &mut World,
position: Vector2<f32>,
npc_spawn_parameter: NPCSpawnParameter,
) -> Result<()> {
self.data.write().unwrap().change_npc_spawn(
&self.async_db,
scene_contents,
world,
position,
npc_spawn_parameter,
)?;
@ -273,7 +260,7 @@ impl Map {
x: u32,
y: u32,
texture: &Arc<Image>,
scene: &mut impl SceneEntities,
world: &mut World,
) -> Result<()> {
let mut data = self.data.write().unwrap();
@ -283,7 +270,7 @@ impl Map {
let (chunk, primitive_index) = Self::calculate_chunk_and_primitive_indices(x, y);
let chunk_entity = data.chunk_handles[&chunk];
let entity = scene.entity_mut(chunk_entity)?;
let entity = world.entity_mut(chunk_entity)?;
let draw = entity.get_component_mut::<Draw>()?;
let mesh = &mut draw[0];
@ -312,7 +299,7 @@ impl Map {
pub fn set_entity(
&self,
scene: &mut impl SceneEntities,
world: &mut World,
entity_opt: Option<(String, EntityObject)>,
position: Vector2<f32>,
rotation: impl Into<Rad<f32>>,
@ -320,18 +307,18 @@ impl Map {
self.data
.write()
.unwrap()
.set_entity(scene, entity_opt, position, rotation, &self.async_db)
.set_entity(world, entity_opt, position, rotation, &self.async_db)
}
pub fn set_spawn_location(
&self,
scene: &mut impl SceneEntities,
world: &mut World,
entity_opt: Option<(String, EntityObject)>,
position: Vector2<f32>,
rotation: impl Into<Rad<f32>>,
) -> Result<()> {
self.data.write().unwrap().set_spawn_location(
scene,
world,
entity_opt,
position,
rotation,
@ -339,19 +326,19 @@ impl Map {
)
}
pub fn disable_spawns(&self, scene: &mut impl SceneEntities) -> Result<()> {
self.data.write().unwrap().disable_spawns(scene)
pub fn disable_spawns(&self, world: &mut World) -> Result<()> {
self.data.write().unwrap().disable_spawns(world)
}
pub fn set_leave_location(
&self,
scene: &mut impl SceneEntities,
world: &mut World,
entity_opt: Option<(String, EntityObject)>,
position: Vector2<f32>,
rotation: impl Into<Rad<f32>>,
) -> Result<()> {
self.data.write().unwrap().set_leave_location(
scene,
world,
entity_opt,
position,
rotation,
@ -359,19 +346,19 @@ impl Map {
)
}
pub fn toggle_npc_spawns(&self, engine: &Engine, scene: &mut impl SceneEntities) -> Result<()> {
self.data.write().unwrap().toggle_npc_spawns(engine, scene)
pub fn toggle_npc_spawns(&self, world: &mut World) -> Result<()> {
self.data.write().unwrap().toggle_npc_spawns(world)
}
pub fn set_npc_spawn_marker(
&self,
flag_name: &str,
area_name: &str,
engine: &Engine,
world: &mut World,
entity_manager: &mut impl AssetLoader,
) -> Result<()> {
self.data.write().unwrap().npc_spawn_marker = Some(SpawnMarkerEntities::new(
engine,
world,
entity_manager,
flag_name,
area_name,
@ -382,56 +369,38 @@ impl Map {
pub fn set_npc_spawn(
&self,
engine: &Engine,
scene: &mut impl SceneEntities,
world: &mut World,
position: Vector2<f32>,
radius: f32,
min: u32,
max: u32,
) -> Result<()> {
self.data.write().unwrap().set_npc_spawn(
engine,
scene,
position,
&self.async_db,
radius,
min,
max,
)
}
pub fn unset_npc_spawn(
&self,
scene: &mut impl SceneEntities,
position: Vector2<f32>,
) -> Result<()> {
self.data
.write()
.unwrap()
.unset_npc_spawn(scene, position, &self.async_db)
.set_npc_spawn(world, position, &self.async_db, radius, min, max)
}
pub fn set_boss_spawn(
&self,
engine: &Engine,
scene: &mut impl SceneEntities,
position: Vector2<f32>,
) -> Result<()> {
pub fn unset_npc_spawn(&self, world: &mut World, position: Vector2<f32>) -> Result<()> {
self.data
.write()
.unwrap()
.set_boss_spawn(engine, scene, position, &self.async_db)
.unset_npc_spawn(world, position, &self.async_db)
}
pub fn unset_boss_spawn(
&self,
scene: &mut impl SceneEntities,
position: Vector2<f32>,
) -> Result<()> {
pub fn set_boss_spawn(&self, world: &mut World, position: Vector2<f32>) -> Result<()> {
self.data
.write()
.unwrap()
.unset_boss_spawn(scene, position, &self.async_db)
.set_boss_spawn(world, position, &self.async_db)
}
pub fn unset_boss_spawn(&self, world: &mut World, position: Vector2<f32>) -> Result<()> {
self.data
.write()
.unwrap()
.unset_boss_spawn(world, position, &self.async_db)
}
#[inline]
@ -483,8 +452,7 @@ impl Map {
}
fn create_tiles(
engine: &Engine,
scene: &mut Scene,
world: &mut World,
textures: &HashMap<u32, OrderedElement<Arc<Image>>>,
width: u32,
height: u32,
@ -492,6 +460,9 @@ impl Map {
) -> Result<HashMap<(u32, u32), Entity>> {
let tile_handles = Map::create_tile_handles(width, height, &surface);
let context = world.resources.get_unchecked::<Context>();
let scene = world.resources.get_unchecked::<Scene>();
#[cfg(debug_assertions)]
Self::verify_tile_handles(width, height, &tile_handles);
@ -511,7 +482,7 @@ impl Map {
let mut tile_entity = {
let (tiles, bounding_box) = &tile_handles[&(chunk_x, chunk_y)];
let mut asset_mesh = AssetMesh::new(&scene.device(), scene.render_type())?;
let mut asset_mesh = AssetMesh::new(context.device(), scene.render_type())?;
for tile_x in 0..Self::CHUNK_DIMENSION {
for tile_y in 0..Self::CHUNK_DIMENSION {
@ -527,7 +498,7 @@ impl Map {
)
.set_memory_usage(MemoryUsage::CpuOnly)
.set_data(&buffer_data)
.build(engine.device().clone())?;
.build(context.device().clone())?;
asset_mesh.add_primitive(
buffer,
@ -539,7 +510,7 @@ impl Map {
}
}
let mut entity_object = engine.assets().empty_entity();
let mut entity_object = AssetHandler::create(world).empty_entity();
entity_object.insert_component(Draw::new(vec![asset_mesh]));
entity_object.insert_component(bounding_box.clone());
@ -552,7 +523,7 @@ impl Map {
tile_entity.debug_name = Some(format!("Chunk ({}, {})", chunk_x, chunk_y));
}
let entity = scene.add_entity(tile_entity)?;
let entity = world.add_entity(tile_entity)?;
tiles.insert((chunk_x, chunk_y), entity);
}
@ -656,8 +627,7 @@ impl Map {
// DB load and create map handle
impl Map {
fn load_data_from_db(
engine: &Engine,
scene: &mut Scene,
world: &mut World,
data_base: MapDataBase,
tiles: &HashMap<String, AssetPath>,
entity_manager: &mut impl AssetLoader,
@ -671,6 +641,8 @@ impl Map {
let mob_spawn_locations = data_base.read_mob_spawn_locations()?;
let boss_spawn_locations = data_base.read_boss_spawn_locations()?;
let context = world.resources.get_unchecked::<Context>();
// check if the amount of tiles and points is correct
let tile_count = data_base.width() * data_base.height();
let point_count = (data_base.width() + 1) * (data_base.height() + 1);
@ -727,8 +699,8 @@ impl Map {
let image = match tiles.get(&texture.name) {
Some(path) => Image::from_file(path.clone())?
.format(VK_FORMAT_R8G8B8A8_UNORM)
.attach_sampler(Sampler::nearest_sampler().build(engine.device())?)
.build(engine.device(), engine.queue())?,
.attach_sampler(Sampler::nearest_sampler().build(context.device())?)
.build(context.device(), context.queue())?,
None => {
return Err(anyhow::Error::msg(format!(
"Map: Image ({}) could not be found",
@ -785,34 +757,39 @@ impl Map {
map_tiles,
)?;
let asset_manager = engine.assets();
let mut asset_manager = AssetHandler::create(unsafe { remove_life_time_mut(world) });
let entity_positions =
Self::create_entity_alike(asset_manager, entity_manager, entity_pos, &surface, scene)?;
let entity_positions = Self::create_entity_alike(
&mut asset_manager,
entity_manager,
entity_pos,
&surface,
world,
)?;
let spawn_positions = Self::create_entity_alike(
asset_manager,
&mut asset_manager,
entity_manager,
spawn_locations,
&surface,
scene,
world,
)?;
let mut spawns = HashMap::new();
for (coordinate, entity) in spawn_positions.into_iter() {
let e = scene.entity(entity)?;
let e = world.entity(entity)?;
let position = e.get_component::<Location>()?.position();
spawns.insert(coordinate, (entity, position));
}
let leave_positions = Self::create_entity_alike(
asset_manager,
&mut asset_manager,
entity_manager,
leave_locations,
&surface,
scene,
world,
)?;
let npc_spawn_positions = mob_spawn_locations
@ -876,8 +853,7 @@ impl Map {
data: RwLock::new(MapData {
chunk_handles: Self::create_tiles(
engine,
scene,
world,
&textures_info,
data_base.width(),
data_base.height(),
@ -919,11 +895,11 @@ impl Map {
#[inline]
fn create_entity_alike(
asset_manager: AssetHandler<'_>,
asset_manager: &mut AssetHandler<'_>,
entity_manager: &mut impl AssetLoader,
source: Vec<DBEntityInfo>,
surface: &Surface,
scene: &mut Scene,
world: &mut World,
) -> Result<HashMap<Coordinate, Entity>> {
let mut map = HashMap::new();
@ -943,7 +919,7 @@ impl Map {
Location::new_and_setup(&mut entity_object)?.update_raw(position, info.rotation);
let entity = scene.add_entity(entity_object)?;
let entity = world.add_entity(entity_object)?;
map.insert(
Coordinate {
@ -956,14 +932,12 @@ impl Map {
Ok(map)
}
}
impl engine::prelude::Map for Map {
fn name(&self) -> &str {
pub fn name(&self) -> &str {
&self.name
}
fn check_walkability(
pub fn check_walkability(
&self,
position: Vector2<f32>,
direction: Vector2<f32>,
@ -1010,45 +984,45 @@ impl engine::prelude::Map for Map {
Ok(true)
}
fn get_height(&self, x: f32, y: f32) -> Result<f32> {
pub fn get_height(&self, x: f32, y: f32) -> Result<f32> {
Ok(self.data.read().unwrap().get_height(x, y))
}
fn spawn_positions(&self) -> Result<Vec<Vector3<f32>>> {
pub fn spawn_positions(&self) -> Result<Vec<Vector3<f32>>> {
Ok(self.data.read().unwrap().spawn_locations())
}
fn leave_markers(&self) -> Result<Vec<Entity>> {
pub fn leave_markers(&self) -> Result<Vec<Entity>> {
Ok(self.data.read().unwrap().leave_markers())
}
fn disable(&self, scene: &mut Scene) -> Result<()> {
pub fn disable(&self, world: &mut World) -> Result<()> {
let data = self.data.read().unwrap();
// clear tiles
for chunk in data.chunk_handles.values() {
scene.remove_entity(*chunk)?;
world.remove_entity(*chunk)?;
}
// clear entities
for entity in data.entities.values() {
scene.remove_entity(*entity)?;
world.remove_entity(*entity)?;
}
// clear spawns
for (spawn, _) in data.spawn_locations.values() {
scene.remove_entity(*spawn)?;
world.remove_entity(*spawn)?;
}
// clear exits
for leave in data.leave_locations.values() {
scene.remove_entity(*leave)?;
world.remove_entity(*leave)?;
}
// clear npc spawns
for (_, marker) in data.npc_spawn_areas.iter() {
if let Some(marker) = marker {
marker.remove(scene)?;
marker.remove(world)?;
}
}

View file

@ -4,7 +4,7 @@ use engine::prelude::*;
use super::{async_db::AsyncDBAccess, map::MapObjectType, map_db::EntityDBType, surface::Surface};
use anyhow::Result;
use cgmath::{vec3, Rad};
use cgmath::{Rad, vec3};
use cgmath::{Vector2, Vector3};
// std
@ -53,14 +53,14 @@ pub struct SpawnMarkerEntities {
impl SpawnMarkerEntities {
pub fn new(
engine: &Engine,
world: &mut World,
entity_manager: &mut impl AssetLoader,
flag_name: &str,
area_name: &str,
) -> Result<Self> {
let assets = engine.assets();
let mut assets = AssetHandler::create(world);
let mut flag = entity_manager.load_entity(assets, flag_name)?;
let mut flag = entity_manager.load_entity(&mut assets, flag_name)?;
#[cfg(debug_assertions)]
{
@ -69,7 +69,7 @@ impl SpawnMarkerEntities {
Location::new_and_setup(&mut flag)?;
let mut area = entity_manager.load_entity(assets, area_name)?;
let mut area = entity_manager.load_entity(&mut assets, area_name)?;
#[cfg(debug_assertions)]
{
@ -81,8 +81,8 @@ impl SpawnMarkerEntities {
Ok(Self { flag, area })
}
fn clone_entity(entity: &EntityObject, engine: &Engine) -> Result<EntityObject> {
let mut flag = entity.clone_without_components(engine);
fn clone_entity(entity: &EntityObject, world: &mut World) -> Result<EntityObject> {
let mut flag = world.clone_without_components(entity);
flag.clone_component_from::<Draw>(entity)?;
flag.clone_component_from::<Location>(entity)?;
flag.clone_component_from::<BoundingBox>(entity)?;
@ -92,9 +92,9 @@ impl SpawnMarkerEntities {
Ok(flag)
}
fn clone(&self, engine: &Engine) -> Result<Self> {
let flag = Self::clone_entity(&self.flag, engine)?;
let area = Self::clone_entity(&self.area, engine)?;
fn clone(&self, world: &mut World) -> Result<Self> {
let flag = Self::clone_entity(&self.flag, world)?;
let area = Self::clone_entity(&self.area, world)?;
Ok(Self { flag, area })
}
@ -118,9 +118,9 @@ impl SpawnMarkerEntities {
.set_scale(vec3(radius, radius, 1.0));
}
pub fn add(self, scene: &mut impl SceneEntities) -> Result<SpawnMarker> {
let flag = scene.add_entity(self.flag)?;
let area = scene.add_entity(self.area)?;
pub fn add(self, world: &mut World) -> Result<SpawnMarker> {
let flag = world.add_entity(self.flag)?;
let area = world.add_entity(self.area)?;
Ok(SpawnMarker { flag, area })
}
@ -132,8 +132,8 @@ pub struct SpawnMarker {
}
impl SpawnMarker {
fn set_radius(&self, scene_contents: &mut impl SceneEntities, radius: f32) -> Result<()> {
let flag_object = scene_contents.entity_mut(self.area)?;
fn set_radius(&self, world: &mut World, radius: f32) -> Result<()> {
let flag_object = world.entity_mut(self.area)?;
flag_object
.get_component_mut::<Location>()?
@ -193,9 +193,9 @@ impl AlterEntities for HashMap<Coordinate, (Entity, Vector3<f32>)> {
}
impl SpawnMarker {
pub fn remove(&self, scene: &mut impl SceneEntities) -> Result<()> {
scene.remove_entity(self.flag)?;
scene.remove_entity(self.area)?;
pub fn remove(&self, world: &mut World) -> Result<()> {
world.remove_entity(self.flag)?;
world.remove_entity(self.area)?;
Ok(())
}
@ -222,10 +222,10 @@ pub struct MapBossSpawnInfo {
impl MapBossSpawnInfo {
fn setup_flag(
marker: &SpawnMarkerEntities,
engine: &Engine,
world: &mut World,
position: Vector3<f32>,
) -> Result<EntityObject> {
let mut flag = SpawnMarkerEntities::clone_entity(&marker.flag, engine)?;
let mut flag = SpawnMarkerEntities::clone_entity(&marker.flag, world)?;
let location = flag.get_component_mut::<Location>()?;
let scale = location.scale();
@ -472,9 +472,9 @@ impl MapData {
Ok(())
}
pub fn check_height_at(&mut self, x: u32, y: u32, scene: &mut Scene) -> Result<()> {
pub fn check_height_at(&mut self, x: u32, y: u32, world: &mut World) -> Result<()> {
self.execute_on_entity(x, y, |entity| {
let entity_object = scene.entity_mut(entity)?;
let entity_object = world.entity_mut(entity)?;
let location = entity_object.get_component_mut::<Location>()?;
@ -493,7 +493,7 @@ impl MapData {
pub fn set_spawn_location(
&mut self,
scene: &mut impl SceneEntities,
world: &mut World,
entity_opt: Option<(String, EntityObject)>,
position: cgmath::Vector2<f32>,
rotation: impl Into<Rad<f32>>,
@ -508,7 +508,7 @@ impl MapData {
(self.width, self.height),
&self.surface,
EntityDBType::spawn_location(),
scene,
world,
)?;
Ok(())
@ -539,7 +539,7 @@ impl MapData {
pub fn change_npc_spawn(
&mut self,
async_db: &AsyncDBAccess,
scene_contents: &mut impl SceneEntities,
world: &mut World,
position: Vector2<f32>,
npc_spawn_parameter: NPCSpawnParameter,
) -> Result<()> {
@ -580,10 +580,7 @@ impl MapData {
NPCSpawnParameter::Radius(radius) => {
spawn_info.radius = radius;
marker
.as_ref()
.unwrap()
.set_radius(scene_contents, radius)?;
marker.as_ref().unwrap().set_radius(world, radius)?;
async_db.add(move |sql| sql.update_npc_spawn_radius(&coordinate, radius))?;
}
@ -593,12 +590,12 @@ impl MapData {
Ok(())
}
pub fn disable_spawns(&mut self, scene: &mut impl SceneEntities) -> Result<()> {
pub fn disable_spawns(&mut self, world: &mut World) -> Result<()> {
if self.show_spawn_locations {
self.show_spawn_locations = false;
for (spawn_entity, _) in self.spawn_locations.values() {
scene.remove_entity(*spawn_entity)?;
world.remove_entity(*spawn_entity)?;
}
}
@ -621,7 +618,7 @@ impl MapData {
pub fn set_leave_location(
&mut self,
scene: &mut impl SceneEntities,
world: &mut World,
entity_opt: Option<(String, EntityObject)>,
position: cgmath::Vector2<f32>,
rotation: impl Into<Rad<f32>>,
@ -636,15 +633,11 @@ impl MapData {
(self.width, self.height),
&self.surface,
EntityDBType::leave_location(),
scene,
world,
)
}
pub fn toggle_npc_spawns(
&mut self,
engine: &Engine,
scene: &mut impl SceneEntities,
) -> Result<()> {
pub fn toggle_npc_spawns(&mut self, world: &mut World) -> Result<()> {
self.show_npc_spawns = !self.show_npc_spawns;
for (spawn_info, marker) in self.npc_spawn_areas.iter_mut() {
@ -660,13 +653,13 @@ impl MapData {
Some(marker) => Some(marker),
None => match &self.npc_spawn_marker {
Some(global_marker) => {
let mut new_marker = global_marker.clone(engine)?;
let mut new_marker = global_marker.clone(world)?;
// set position
new_marker.set_position(spawn_info.position.extend(height));
new_marker.set_radius(spawn_info.radius);
*marker = Some(new_marker.add(scene)?);
*marker = Some(new_marker.add(world)?);
marker.as_mut()
}
@ -680,7 +673,7 @@ impl MapData {
if self.show_npc_spawns {
// marker.add(scene)?;
} else {
marker.remove(scene)?;
marker.remove(world)?;
}
}
}
@ -700,11 +693,11 @@ impl MapData {
Some(global_marker) => {
let flag = MapBossSpawnInfo::setup_flag(
global_marker,
engine,
world,
spawn_info.position.extend(height),
)?;
*flag_marker = Some(scene.add_entity(flag)?);
*flag_marker = Some(world.add_entity(flag)?);
*flag_marker
}
@ -718,7 +711,7 @@ impl MapData {
if self.show_npc_spawns {
// marker.add(scene)?;
} else {
scene.remove_entity(marker)?;
world.remove_entity(marker)?;
}
}
}
@ -728,8 +721,7 @@ impl MapData {
pub fn set_npc_spawn(
&mut self,
engine: &Engine,
scene: &mut impl SceneEntities,
world: &mut World,
position: cgmath::Vector2<f32>,
async_db: &AsyncDBAccess,
radius: f32,
@ -749,13 +741,13 @@ impl MapData {
let marker = match self.npc_spawn_marker.as_ref() {
Some(managed_entity) => {
let mut marker = managed_entity.clone(engine)?;
let mut marker = managed_entity.clone(world)?;
// set position
marker.set_position(position.extend(self.get_height(position.x, position.y)));
marker.set_radius(radius);
Some(marker.add(scene)?)
Some(marker.add(world)?)
}
None => None,
};
@ -781,7 +773,7 @@ impl MapData {
pub fn unset_npc_spawn(
&mut self,
scene: &mut impl SceneEntities,
world: &mut World,
position: cgmath::Vector2<f32>,
async_db: &AsyncDBAccess,
) -> Result<()> {
@ -797,7 +789,7 @@ impl MapData {
async_db.add(move |sql| sql.remove_npc_spawn((coordinate.x, coordinate.y)))?;
if let Some(marker) = marker {
marker.remove(scene)?;
marker.remove(world)?;
}
}
@ -806,8 +798,7 @@ impl MapData {
pub fn set_boss_spawn(
&mut self,
engine: &Engine,
scene: &mut impl SceneEntities,
world: &mut World,
position: cgmath::Vector2<f32>,
async_db: &AsyncDBAccess,
) -> Result<()> {
@ -826,11 +817,11 @@ impl MapData {
Some(managed_entity) => {
let flag = MapBossSpawnInfo::setup_flag(
managed_entity,
engine,
world,
position.extend(self.get_height(position.x, position.y)),
)?;
Some(scene.add_entity(flag)?)
Some(world.add_entity(flag)?)
}
None => None,
};
@ -851,7 +842,7 @@ impl MapData {
pub fn unset_boss_spawn(
&mut self,
scene: &mut impl SceneEntities,
world: &mut World,
position: cgmath::Vector2<f32>,
async_db: &AsyncDBAccess,
) -> Result<()> {
@ -867,7 +858,7 @@ impl MapData {
async_db.add(move |sql| sql.remove_boss_spawn((coordinate.x, coordinate.y)))?;
if let Some(marker) = marker {
scene.remove_entity(marker)?;
world.remove_entity(marker)?;
}
}
@ -883,7 +874,7 @@ impl MapData {
(width, height): (u32, u32),
surface: &Surface,
table_name: EntityDBType,
scene: &mut impl SceneEntities,
world: &mut World,
) -> Result<()> {
let coordinate = Coordinate {
x: position.x as u32,
@ -927,7 +918,7 @@ impl MapData {
.unwrap()
.update_raw(position_3d, rad_rotation);
let handle = scene.add_entity(entity)?;
let handle = world.add_entity(entity)?;
entities.insert(coordinate, handle, position_3d);
}
@ -938,7 +929,7 @@ impl MapData {
sql.remove_entity(table_name, (coordinate.x, coordinate.y))
})?;
scene.remove_entity(entity)?;
world.remove_entity(entity)?;
return Ok(());
}
@ -950,7 +941,7 @@ impl MapData {
pub fn set_entity(
&mut self,
scene: &mut impl SceneEntities,
world: &mut World,
entity_opt: Option<(String, EntityObject)>,
position: cgmath::Vector2<f32>,
rotation: impl Into<Rad<f32>>,
@ -965,7 +956,7 @@ impl MapData {
(self.width, self.height),
&self.surface,
EntityDBType::entity(),
scene,
world,
)
}
@ -1122,7 +1113,7 @@ impl MapData {
return Err(anyhow::Error::msg(format!(
"Index ({}) in textures_info (MapData) not found",
remove_index
)))
)));
}
};
@ -1132,7 +1123,7 @@ impl MapData {
return Err(anyhow::Error::msg(format!(
"Could not remove key ({}) from textures_index",
info.name
)))
)));
}
};