-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rebuild world object structure to allow for dynamic compositions of d…
…ifferent behaviors
- Loading branch information
1 parent
88d71e3
commit 1e0e5de
Showing
23 changed files
with
247 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
[gd_resource type="Resource" script_class="StableWorldObjectTemplate" load_steps=6 format=3 uid="uid://hr5ivg0tjaki"] | ||
[gd_resource type="Resource" script_class="WorldObjectTemplate" load_steps=6 format=3 uid="uid://hidttrpsvqv6"] | ||
|
||
[ext_resource type="Script" path="res://world_object/templates/stable.gd" id="1_n62g3"] | ||
[ext_resource type="Script" path="res://world_object/template.gd" id="1_u320r"] | ||
[ext_resource type="Script" path="res://world_object/tiles/static.gd" id="2_luko2"] | ||
[ext_resource type="Texture2D" uid="uid://ojnsm3dp50rt" path="res://assets/objects/static/dirt_block.png" id="3_emlvk"] | ||
|
||
[sub_resource type="WorldObjectCommonData" id="WorldObjectCommonData_a61jb"] | ||
[sub_resource type="WorldObjectCommonData" id="WorldObjectCommonData_olorh"] | ||
id = 1 | ||
display_behavior = 1 | ||
|
||
[sub_resource type="WorldObjectTileBase" id="WorldObjectTileBase_0jst5"] | ||
[sub_resource type="WorldObjectTileBase" id="WorldObjectTileBase_pe8ve"] | ||
tile_type = 1 | ||
script = ExtResource("2_luko2") | ||
texture = ExtResource("3_emlvk") | ||
y_offset = 0 | ||
|
||
[resource] | ||
script = ExtResource("1_n62g3") | ||
common_data = SubResource("WorldObjectCommonData_a61jb") | ||
tiles = Array[Resource("res://world_object/tile.gd")]([SubResource("WorldObjectTileBase_0jst5")]) | ||
script = ExtResource("1_u320r") | ||
common_data = SubResource("WorldObjectCommonData_olorh") | ||
tiles = Array[Resource("res://world_object/tile.gd")]([SubResource("WorldObjectTileBase_pe8ve")]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
[gd_resource type="Resource" script_class="StableWorldObjectTemplate" load_steps=6 format=3 uid="uid://cnyktohymxniv"] | ||
[gd_resource type="Resource" script_class="WorldObjectTemplate" load_steps=6 format=3 uid="uid://bpgd30g763e3k"] | ||
|
||
[ext_resource type="Script" path="res://world_object/templates/stable.gd" id="1_ly3ag"] | ||
[ext_resource type="Script" path="res://world_object/tiles/static.gd" id="2_w80pq"] | ||
[ext_resource type="Texture2D" uid="uid://8f6qgxacftvm" path="res://assets/objects/static/dirt_top.png" id="3_owb08"] | ||
[ext_resource type="Script" path="res://world_object/template.gd" id="1_mbuf5"] | ||
[ext_resource type="Script" path="res://world_object/tiles/static.gd" id="2_sa6gk"] | ||
[ext_resource type="Texture2D" uid="uid://8f6qgxacftvm" path="res://assets/objects/static/dirt_top.png" id="3_d614x"] | ||
|
||
[sub_resource type="WorldObjectCommonData" id="WorldObjectCommonData_g47l8"] | ||
[sub_resource type="WorldObjectCommonData" id="WorldObjectCommonData_r7rob"] | ||
id = 2 | ||
display_behavior = 1 | ||
|
||
[sub_resource type="WorldObjectTileBase" id="WorldObjectTileBase_4n2f6"] | ||
[sub_resource type="WorldObjectTileBase" id="WorldObjectTileBase_l6r8l"] | ||
tile_type = 1 | ||
script = ExtResource("2_w80pq") | ||
texture = ExtResource("3_owb08") | ||
script = ExtResource("2_sa6gk") | ||
texture = ExtResource("3_d614x") | ||
y_offset = 0 | ||
|
||
[resource] | ||
script = ExtResource("1_ly3ag") | ||
common_data = SubResource("WorldObjectCommonData_g47l8") | ||
tiles = Array[Resource("res://world_object/tile.gd")]([SubResource("WorldObjectTileBase_4n2f6")]) | ||
script = ExtResource("1_mbuf5") | ||
common_data = SubResource("WorldObjectCommonData_r7rob") | ||
tiles = Array[Resource("res://world_object/tile.gd")]([SubResource("WorldObjectTileBase_l6r8l")]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,29 @@ | ||
[gd_resource type="Resource" script_class="HousingWorldObjectTemplate" load_steps=7 format=3 uid="uid://nb6v3btv6dtg"] | ||
[gd_resource type="Resource" script_class="WorldObjectTemplate" load_steps=8 format=3 uid="uid://dhrgvruess3i7"] | ||
|
||
[ext_resource type="Script" path="res://world_object/templates/housing.gd" id="1_t0n0v"] | ||
[ext_resource type="Script" path="res://world_object/tiles/static.gd" id="2_pgydf"] | ||
[ext_resource type="Texture2D" uid="uid://cn0let38x5y65" path="res://assets/objects/hut/hut.png" id="3_wt7f1"] | ||
[ext_resource type="Script" path="res://world_object/template.gd" id="1_4m4qr"] | ||
[ext_resource type="Script" path="res://world_object/tiles/static.gd" id="2_nnrdn"] | ||
[ext_resource type="Texture2D" uid="uid://cn0let38x5y65" path="res://assets/objects/hut/hut.png" id="3_aj7wx"] | ||
|
||
[sub_resource type="WorldObjectCommonData" id="WorldObjectCommonData_boc4h"] | ||
[sub_resource type="WorldObjectCommonData" id="WorldObjectCommonData_dvlu8"] | ||
id = 3 | ||
display_behavior = 2 | ||
|
||
[sub_resource type="WorldObjectHousingData" id="WorldObjectHousingData_puc52"] | ||
capacity = 3 | ||
[sub_resource type="WorldObjectHousingData" id="WorldObjectHousingData_8xemu"] | ||
|
||
[sub_resource type="WorldObjectTileBase" id="WorldObjectTileBase_1svj2"] | ||
tile_type = 1 | ||
script = ExtResource("2_pgydf") | ||
texture = ExtResource("3_wt7f1") | ||
[sub_resource type="WorldObjectTileBase" id="WorldObjectTileBase_4dsgp"] | ||
tile_type = 2 | ||
script = ExtResource("2_nnrdn") | ||
texture = ExtResource("3_aj7wx") | ||
y_offset = 0 | ||
|
||
[sub_resource type="WorldObjectTileBase" id="WorldObjectTileBase_lk7vj"] | ||
tile_type = 3 | ||
script = ExtResource("2_nnrdn") | ||
texture = ExtResource("3_aj7wx") | ||
y_offset = 1 | ||
|
||
[resource] | ||
script = ExtResource("1_t0n0v") | ||
housing_data = SubResource("WorldObjectHousingData_puc52") | ||
common_data = SubResource("WorldObjectCommonData_boc4h") | ||
tiles = Array[Resource("res://world_object/tile.gd")]([SubResource("WorldObjectTileBase_1svj2")]) | ||
script = ExtResource("1_4m4qr") | ||
common_data = SubResource("WorldObjectCommonData_dvlu8") | ||
housing_data = SubResource("WorldObjectHousingData_8xemu") | ||
tiles = Array[Resource("res://world_object/tile.gd")]([SubResource("WorldObjectTileBase_4dsgp"), SubResource("WorldObjectTileBase_lk7vj")]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use godot::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::enums::{ | ||
display_behavior::DisplayBehavior, tile_type::TileType, world_object_id::WorldObjectId, | ||
}; | ||
|
||
use super::data::common::WorldObjectCommonData; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Default)] | ||
pub struct BaseWorldObject { | ||
pub id: WorldObjectId, | ||
#[serde(rename = "dp_bhvr")] | ||
pub display_behavior: DisplayBehavior, | ||
} | ||
|
||
impl BaseWorldObject { | ||
pub fn apply_common_data(&mut self, common_data: &Gd<WorldObjectCommonData>) { | ||
let data = common_data.bind(); | ||
self.id = data.id; | ||
self.display_behavior = data.display_behavior; | ||
} | ||
|
||
pub fn get_current_tile_type(&self) -> TileType { | ||
match self.display_behavior { | ||
DisplayBehavior::Persistent => TileType::Persistent, | ||
_ => TileType::None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,28 @@ | ||
use godot::prelude::*; | ||
use godot::obj::Gd; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_repr::{Deserialize_repr, Serialize_repr}; | ||
|
||
use crate::{ | ||
entities::display_tile::DisplayTile, | ||
enums::{tile_type::TileType, world_object_id::WorldObjectId}, | ||
}; | ||
use super::{behaviors::housing::HousingBehavior, data::housing::WorldObjectHousingData}; | ||
|
||
use super::{ | ||
behaviors::{housing::HousingBehavior, stable::StableBehavior}, | ||
data::common::WorldObjectCommonData, | ||
traits::housing::HousingBehaviorTrait, | ||
}; | ||
|
||
/// A trait that has to be implemented by all behaviors. | ||
pub trait WorldObjectBehavior { | ||
fn get_id(&self) -> WorldObjectId; | ||
fn set_id(&mut self, id: WorldObjectId); | ||
fn get_current_tile_type(&self) -> TileType; | ||
|
||
fn get_display_tile(&self, location: Vector2i) -> DisplayTile { | ||
DisplayTile { | ||
object_id: self.get_id(), | ||
tile_type: self.get_current_tile_type(), | ||
location, | ||
} | ||
} | ||
|
||
fn get_display_tile_gd(&self, location: Vector2i) -> Gd<DisplayTile> { | ||
Gd::from_object(self.get_display_tile(location)) | ||
} | ||
|
||
fn apply_common_data(&mut self, common_data: &Gd<WorldObjectCommonData>) { | ||
self.set_id(common_data.bind().id); | ||
} | ||
#[derive( | ||
Debug, Serialize_repr, Deserialize_repr, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, | ||
)] | ||
#[repr(u32)] | ||
pub enum BehaviorType { | ||
Housing, | ||
} | ||
|
||
/// All behaviors share an enum so their instances can be stored together in a hashmap | ||
/// besides their different properties and behaviors. | ||
#[derive(Debug, Serialize, Deserialize, Clone)] | ||
pub enum WorldObjectBehaviorType { | ||
Stable(StableBehavior), | ||
pub enum Behavior { | ||
Housing(HousingBehavior), | ||
} | ||
|
||
impl WorldObjectBehavior for WorldObjectBehaviorType { | ||
fn get_id(&self) -> WorldObjectId { | ||
self.as_behavior().get_id() | ||
} | ||
|
||
fn set_id(&mut self, id: WorldObjectId) { | ||
self.as_behavior_mut().set_id(id); | ||
} | ||
|
||
fn get_display_tile_gd(&self, location: Vector2i) -> Gd<DisplayTile> { | ||
self.as_behavior().get_display_tile_gd(location) | ||
} | ||
|
||
fn get_current_tile_type(&self) -> TileType { | ||
self.as_behavior().get_current_tile_type() | ||
} | ||
} | ||
|
||
impl WorldObjectBehaviorType { | ||
pub fn as_behavior(&self) -> &dyn WorldObjectBehavior { | ||
match self { | ||
Self::Stable(state) => state, | ||
Self::Housing(state) => state, | ||
} | ||
} | ||
|
||
pub fn as_behavior_mut(&mut self) -> &mut dyn WorldObjectBehavior { | ||
match self { | ||
Self::Stable(state) => state, | ||
Self::Housing(state) => state, | ||
} | ||
} | ||
|
||
pub fn as_housing(&self) -> Option<&dyn HousingBehaviorTrait> { | ||
match self { | ||
Self::Housing(state) => Some(state), | ||
_ => None, | ||
} | ||
impl Behavior { | ||
pub fn new_housing(housing_data: &Gd<WorldObjectHousingData>) -> Behavior { | ||
let data = housing_data.bind(); | ||
let behavior = HousingBehavior { | ||
capacity: data.capacity, | ||
}; | ||
Self::Housing(behavior) | ||
} | ||
} |
36 changes: 1 addition & 35 deletions
36
rust/main_logic/src/entities/world_object/behaviors/housing.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,6 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{ | ||
entities::world_object::{ | ||
behavior::WorldObjectBehavior, traits::housing::HousingBehaviorTrait, | ||
}, | ||
enums::{job_type::JobType, tile_type::TileType, world_object_id::WorldObjectId}, | ||
}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Default, Clone)] | ||
pub struct HousingBehavior { | ||
id: WorldObjectId, | ||
capacity: u32, | ||
job_type: JobType, | ||
job_production: u32, | ||
} | ||
|
||
impl WorldObjectBehavior for HousingBehavior { | ||
fn get_id(&self) -> WorldObjectId { | ||
self.id | ||
} | ||
|
||
fn set_id(&mut self, id: WorldObjectId) { | ||
self.id = id | ||
} | ||
|
||
fn get_current_tile_type(&self) -> TileType { | ||
TileType::Persistent | ||
} | ||
} | ||
|
||
impl HousingBehaviorTrait for HousingBehavior { | ||
fn get_housing_capacity(&self) -> u32 { | ||
self.capacity | ||
} | ||
|
||
fn set_housing_capacity(&mut self, capacity: u32) { | ||
self.capacity = capacity | ||
} | ||
pub capacity: u32, | ||
} |
25 changes: 0 additions & 25 deletions
25
rust/main_logic/src/entities/world_object/behaviors/stable.rs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.