World object loading
This commit is contained in:
parent
74066c0189
commit
2d944dc2f3
@ -1,2 +1,3 @@
|
|||||||
pub mod player;
|
pub mod player;
|
||||||
pub mod world_object;
|
pub mod world_object;
|
||||||
|
pub mod world_object_package;
|
@ -3,7 +3,10 @@ use std::collections::HashMap;
|
|||||||
use nalgebra as na;
|
use nalgebra as na;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::asset_manager::{load_json_structure, InternalJsonLoadError};
|
use crate::{
|
||||||
|
asset_manager::{load_json_structure, InternalJsonLoadError},
|
||||||
|
rendering::utilities::anim_texture::AnimatedTexture,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
pub struct PossiblyAnimatedTexture {
|
pub struct PossiblyAnimatedTexture {
|
||||||
@ -55,53 +58,4 @@ pub struct WorldObjectRef {
|
|||||||
pub rotation_radians: f32,
|
pub rotation_radians: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A simply interface for the madness
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct WorldObjectPackage {
|
|
||||||
/// The object definitions
|
|
||||||
pub object_definitions: HashMap<String, WorldObject>,
|
|
||||||
/// The object references
|
|
||||||
pub object_references: Vec<WorldObjectRef>,
|
|
||||||
/// Bottom static textures
|
|
||||||
pub bottom_static_textures: HashMap<String, AnimatedTexture>,
|
|
||||||
/// Top static textures
|
|
||||||
pub top_static_textures: HashMap<String, AnimatedTexture>,
|
|
||||||
/// Bottom animated textures
|
|
||||||
pub bottom_animated_textures: HashMap<String, AnimatedTexture>,
|
|
||||||
/// Top animated textures
|
|
||||||
pub top_animated_textures: HashMap<String, AnimatedTexture>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WorldObjectPackage {
|
|
||||||
pub fn load(map_objects_file_path: &str) -> Result<Self, InternalJsonLoadError> {
|
|
||||||
// Attempt to load the object reference list
|
|
||||||
let object_references: Vec<WorldObjectRef> = load_json_structure(map_objects_file_path)?;
|
|
||||||
|
|
||||||
// We also need to load the object definitions
|
|
||||||
let mut object_definitions = HashMap::new();
|
|
||||||
for reference in &object_references {
|
|
||||||
// If this is a new object, load it.
|
|
||||||
let object_key = format!("{}:{}", reference.kind, reference.name);
|
|
||||||
if !object_definitions.contains_key(object_key.as_str()) {
|
|
||||||
// Construct the file path from the data we know about the reference
|
|
||||||
let path = format!(
|
|
||||||
"assets/{}/{}/{}.json",
|
|
||||||
reference.kind, reference.name, reference.name
|
|
||||||
);
|
|
||||||
|
|
||||||
// Attempt to load the object definition
|
|
||||||
let object_definition: WorldObject = load_json_structure(&path)?;
|
|
||||||
|
|
||||||
// Store the object definition
|
|
||||||
object_definitions.insert(object_key.to_string(), object_definition);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
object_definitions,
|
|
||||||
object_references,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
111
game/game_logic/src/model/world_object_package.rs
Normal file
111
game/game_logic/src/model/world_object_package.rs
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use raylib::{texture::Texture2D, RaylibHandle, RaylibThread};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
asset_manager::{load_json_structure, load_texture_from_internal_data},
|
||||||
|
rendering::utilities::anim_texture::AnimatedTexture,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::world_object::{WorldObject, WorldObjectRef};
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum WorldObjectPackageLoadError {
|
||||||
|
#[error(transparent)]
|
||||||
|
JsonError(#[from] crate::asset_manager::InternalJsonLoadError),
|
||||||
|
#[error(transparent)]
|
||||||
|
ResourceError(#[from] crate::asset_manager::ResourceLoadError),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A simply interface for the madness
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct WorldObjectPackage {
|
||||||
|
/// The object definitions
|
||||||
|
pub object_definitions: HashMap<String, WorldObject>,
|
||||||
|
/// The object references
|
||||||
|
pub object_references: Vec<WorldObjectRef>,
|
||||||
|
/// Bottom static textures
|
||||||
|
pub bottom_static_textures: HashMap<String, Texture2D>,
|
||||||
|
/// Top static textures
|
||||||
|
pub top_static_textures: HashMap<String, Texture2D>,
|
||||||
|
/// Bottom animated textures
|
||||||
|
pub bottom_animated_textures: HashMap<String, AnimatedTexture>,
|
||||||
|
/// Top animated textures
|
||||||
|
pub top_animated_textures: HashMap<String, AnimatedTexture>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WorldObjectPackage {
|
||||||
|
pub fn load(
|
||||||
|
raylib_handle: &mut RaylibHandle,
|
||||||
|
thread: &RaylibThread,
|
||||||
|
map_objects_file_path: &str,
|
||||||
|
) -> Result<Self, WorldObjectPackageLoadError> {
|
||||||
|
// Attempt to load the object reference list
|
||||||
|
let object_references: Vec<WorldObjectRef> = load_json_structure(map_objects_file_path)?;
|
||||||
|
|
||||||
|
// We also need to load the object definitions
|
||||||
|
let mut object_definitions = HashMap::new();
|
||||||
|
let mut bottom_static_textures = HashMap::new();
|
||||||
|
let mut top_static_textures = HashMap::new();
|
||||||
|
let mut bottom_animated_textures = HashMap::new();
|
||||||
|
let mut top_animated_textures = HashMap::new();
|
||||||
|
for reference in &object_references {
|
||||||
|
// If this is a new object, load it.
|
||||||
|
let object_key = format!("{}:{}", reference.kind, reference.name);
|
||||||
|
if !object_definitions.contains_key(object_key.as_str()) {
|
||||||
|
// Construct the file path from the data we know about the reference
|
||||||
|
let path = format!(
|
||||||
|
"assets/{}/{}/{}.json",
|
||||||
|
reference.kind, reference.name, reference.name
|
||||||
|
);
|
||||||
|
|
||||||
|
// Attempt to load the object definition
|
||||||
|
let object_definition: WorldObject = load_json_structure(&path)?;
|
||||||
|
|
||||||
|
// If this object has a static bottom texture, load it
|
||||||
|
if object_definition.bottom_texture.animated.unwrap_or(false) {
|
||||||
|
panic!("Animated bottom textures are not supported yet")
|
||||||
|
} else {
|
||||||
|
// Load the bottom texture and save it
|
||||||
|
bottom_static_textures.insert(
|
||||||
|
object_key.to_string(),
|
||||||
|
load_texture_from_internal_data(
|
||||||
|
raylib_handle,
|
||||||
|
thread,
|
||||||
|
&object_definition.bottom_texture.file_path,
|
||||||
|
)?,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is a top texture, load it
|
||||||
|
if let Some(top_texture) = &object_definition.top_texture {
|
||||||
|
if top_texture.animated.unwrap_or(false) {
|
||||||
|
panic!("Animated top textures are not supported yet")
|
||||||
|
} else {
|
||||||
|
// Load the top texture and save it
|
||||||
|
top_static_textures.insert(
|
||||||
|
object_key.to_string(),
|
||||||
|
load_texture_from_internal_data(
|
||||||
|
raylib_handle,
|
||||||
|
thread,
|
||||||
|
&top_texture.file_path,
|
||||||
|
)?,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store the object definition
|
||||||
|
object_definitions.insert(object_key.to_string(), object_definition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
object_definitions,
|
||||||
|
object_references,
|
||||||
|
bottom_static_textures,
|
||||||
|
top_static_textures,
|
||||||
|
bottom_animated_textures,
|
||||||
|
top_animated_textures,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -7,7 +7,8 @@ use raylib::prelude::*;
|
|||||||
use crate::{
|
use crate::{
|
||||||
discord::DiscordChannel,
|
discord::DiscordChannel,
|
||||||
global_resource_package::GlobalResources,
|
global_resource_package::GlobalResources,
|
||||||
rendering::utilities::{anim_texture::AnimatedTexture, map_render::MapRenderer}, model::world_object::WorldObjectPackage,
|
model::world_object_package::WorldObjectPackage,
|
||||||
|
rendering::utilities::{anim_texture::AnimatedTexture, map_render::MapRenderer},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -15,7 +16,7 @@ pub struct TestFoxScene {
|
|||||||
fox_animation: AnimatedTexture,
|
fox_animation: AnimatedTexture,
|
||||||
world_map: MapRenderer,
|
world_map: MapRenderer,
|
||||||
camera: Camera2D,
|
camera: Camera2D,
|
||||||
objects: WorldObjectPackage
|
objects: WorldObjectPackage,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestFoxScene {
|
impl TestFoxScene {
|
||||||
@ -26,7 +27,8 @@ impl TestFoxScene {
|
|||||||
|
|
||||||
// Load the map
|
// Load the map
|
||||||
let map_renderer = MapRenderer::new("map_gameMap.tmx", raylib_handle, thread).unwrap();
|
let map_renderer = MapRenderer::new("map_gameMap.tmx", raylib_handle, thread).unwrap();
|
||||||
let objects = WorldObjectPackage::load("map_gameMap.objects.json").unwrap();
|
let objects =
|
||||||
|
WorldObjectPackage::load(raylib_handle, thread, "map_gameMap.objects.json").unwrap();
|
||||||
|
|
||||||
// Create a camera
|
// Create a camera
|
||||||
let camera = Camera2D {
|
let camera = Camera2D {
|
||||||
@ -39,12 +41,11 @@ impl TestFoxScene {
|
|||||||
zoom: 1.0,
|
zoom: 1.0,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
fox_animation: fox,
|
fox_animation: fox,
|
||||||
world_map: map_renderer,
|
world_map: map_renderer,
|
||||||
camera,
|
camera,
|
||||||
objects
|
objects,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user