wip objects
This commit is contained in:
parent
f5c63a5c55
commit
74066c0189
@ -1,15 +1,19 @@
|
|||||||
|
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};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
pub struct PossiblyAnimatedTexture {
|
pub struct PossiblyAnimatedTexture {
|
||||||
/// Signal if the texture is animated or static
|
/// Signal if the texture is animated or static
|
||||||
pub animated: bool,
|
pub animated: Option<bool>,
|
||||||
/// Relative file path from `dist` to the texture
|
/// Relative file path from `dist` to the texture
|
||||||
pub file_path: String,
|
pub file_path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Defines a collider in object space.
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
pub struct ObjectCollider {
|
pub struct ObjectCollider {
|
||||||
/// Position, relative to the object's center (north east is 1,1 south west is -1,-1)
|
/// Position, relative to the object's center (north east is 1,1 south west is -1,-1)
|
||||||
@ -20,16 +24,11 @@ pub struct ObjectCollider {
|
|||||||
pub radius: Option<f32>,
|
pub radius: Option<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Definition of an object. Only one of these should exist *per object*, and they will be GPU instanced.
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
pub struct WorldObject {
|
pub struct WorldObject {
|
||||||
/// Object name. Must match the name of the texture
|
/// Object name. Must match the name of the texture
|
||||||
pub name: String,
|
pub name: String,
|
||||||
/// Object variant name. Must match the name of the texture, or None if there is only one variant
|
|
||||||
pub variant_name: Option<String>,
|
|
||||||
/// Object position. 1,1 being up and to the right
|
|
||||||
pub position: na::Vector2<f32>,
|
|
||||||
/// Object rotation, positive is clockwise
|
|
||||||
pub rotation_radians: f32,
|
|
||||||
/// The object's bottom texture
|
/// The object's bottom texture
|
||||||
pub bottom_texture: PossiblyAnimatedTexture,
|
pub bottom_texture: PossiblyAnimatedTexture,
|
||||||
/// The object's top texture
|
/// The object's top texture
|
||||||
@ -41,3 +40,68 @@ pub struct WorldObject {
|
|||||||
/// Temperature
|
/// Temperature
|
||||||
pub temperature: Option<f32>,
|
pub temperature: Option<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Used to reference an object in the world definition
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
pub struct WorldObjectRef {
|
||||||
|
/// Object type
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
pub kind: String,
|
||||||
|
/// Object name
|
||||||
|
pub name: String,
|
||||||
|
/// Object position. 1,1 being up and to the right
|
||||||
|
pub position: na::Vector2<f32>,
|
||||||
|
/// Object rotation, positive is clockwise
|
||||||
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -7,7 +7,7 @@ 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},
|
rendering::utilities::{anim_texture::AnimatedTexture, map_render::MapRenderer}, model::world_object::WorldObjectPackage,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -15,6 +15,7 @@ pub struct TestFoxScene {
|
|||||||
fox_animation: AnimatedTexture,
|
fox_animation: AnimatedTexture,
|
||||||
world_map: MapRenderer,
|
world_map: MapRenderer,
|
||||||
camera: Camera2D,
|
camera: Camera2D,
|
||||||
|
objects: WorldObjectPackage
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestFoxScene {
|
impl TestFoxScene {
|
||||||
@ -25,6 +26,7 @@ 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();
|
||||||
|
|
||||||
// Create a camera
|
// Create a camera
|
||||||
let camera = Camera2D {
|
let camera = Camera2D {
|
||||||
@ -37,10 +39,12 @@ 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user