From 74066c01895bfca90e893a1f8f38a20bf47bc7cf Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sat, 2 Apr 2022 15:27:26 -0400 Subject: [PATCH] wip objects --- game/game_logic/src/model/world_object.rs | 80 ++++++++++++++++++++--- game/game_logic/src/scenes/test_fox.rs | 6 +- 2 files changed, 77 insertions(+), 9 deletions(-) diff --git a/game/game_logic/src/model/world_object.rs b/game/game_logic/src/model/world_object.rs index 59fb0094..44a14d15 100644 --- a/game/game_logic/src/model/world_object.rs +++ b/game/game_logic/src/model/world_object.rs @@ -1,15 +1,19 @@ +use std::collections::HashMap; + use nalgebra as na; use serde::Deserialize; +use crate::asset_manager::{load_json_structure, InternalJsonLoadError}; + #[derive(Debug, Clone, Deserialize)] pub struct PossiblyAnimatedTexture { /// Signal if the texture is animated or static - pub animated: bool, + pub animated: Option, /// Relative file path from `dist` to the texture pub file_path: String, } - +/// Defines a collider in object space. #[derive(Debug, Clone, Deserialize)] pub struct ObjectCollider { /// 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, } +/// Definition of an object. Only one of these should exist *per object*, and they will be GPU instanced. #[derive(Debug, Clone, Deserialize)] pub struct WorldObject { /// Object name. Must match the name of the texture 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, - /// Object position. 1,1 being up and to the right - pub position: na::Vector2, - /// Object rotation, positive is clockwise - pub rotation_radians: f32, /// The object's bottom texture pub bottom_texture: PossiblyAnimatedTexture, /// The object's top texture @@ -41,3 +40,68 @@ pub struct WorldObject { /// Temperature pub temperature: Option, } + +/// 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, + /// 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, + /// The object references + pub object_references: Vec, + /// Bottom static textures + pub bottom_static_textures: HashMap, + /// Top static textures + pub top_static_textures: HashMap, + /// Bottom animated textures + pub bottom_animated_textures: HashMap, + /// Top animated textures + pub top_animated_textures: HashMap, +} + +impl WorldObjectPackage { + pub fn load(map_objects_file_path: &str) -> Result { + // Attempt to load the object reference list + let object_references: Vec = 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, + }) + } + + +} diff --git a/game/game_logic/src/scenes/test_fox.rs b/game/game_logic/src/scenes/test_fox.rs index d9d1973e..34625b67 100644 --- a/game/game_logic/src/scenes/test_fox.rs +++ b/game/game_logic/src/scenes/test_fox.rs @@ -7,7 +7,7 @@ use raylib::prelude::*; use crate::{ discord::DiscordChannel, 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)] @@ -15,6 +15,7 @@ pub struct TestFoxScene { fox_animation: AnimatedTexture, world_map: MapRenderer, camera: Camera2D, + objects: WorldObjectPackage } impl TestFoxScene { @@ -25,6 +26,7 @@ impl TestFoxScene { // Load the map let map_renderer = MapRenderer::new("map_gameMap.tmx", raylib_handle, thread).unwrap(); + let objects = WorldObjectPackage::load("map_gameMap.objects.json").unwrap(); // Create a camera let camera = Camera2D { @@ -37,10 +39,12 @@ impl TestFoxScene { zoom: 1.0, }; + Self { fox_animation: fox, world_map: map_renderer, camera, + objects } }