Skip to content

Commit

Permalink
rebuild world object structure to allow for dynamic compositions of d…
Browse files Browse the repository at this point in the history
…ifferent behaviors
  • Loading branch information
Zitronenjoghurt committed Aug 12, 2024
1 parent 88d71e3 commit 1e0e5de
Show file tree
Hide file tree
Showing 23 changed files with 247 additions and 261 deletions.
15 changes: 8 additions & 7 deletions godot/resources/world_object_templates/dirt_block.tres
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")])
23 changes: 12 additions & 11 deletions godot/resources/world_object_templates/dirt_top.tres
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")])
36 changes: 21 additions & 15 deletions godot/resources/world_object_templates/hut.tres
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")])
5 changes: 5 additions & 0 deletions godot/scripts/autoload/template_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ func load_templates() -> TileSet:

# Store templates and generate tiles
var tileset: TileSet = TileSet.new()
var placeholder_source: TileSetAtlasSource = TileSetAtlasSource.new()
placeholder_source.texture = placeholder_tile_texture
placeholder_source.create_tile(Vector2i(0, 0))
tileset.add_source(placeholder_source)

for path: String in templates_dir.get_files():
var template: WorldObjectTemplate = load(Paths.WORLD_OBJECT_TEMPLATES + path)
if not template is WorldObjectTemplate:
Expand Down
6 changes: 3 additions & 3 deletions godot/world/presets/default.tres
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
[ext_resource type="Script" path="res://world/preset.gd" id="1_ymchk"]
[ext_resource type="Script" path="res://world/shapes/h_line.gd" id="2_orcmc"]
[ext_resource type="Script" path="res://world/shapes/rect.gd" id="3_sxffl"]
[ext_resource type="Resource" uid="uid://cnyktohymxniv" path="res://resources/world_object_templates/dirt_top.tres" id="3_v04w2"]
[ext_resource type="Resource" uid="uid://bpgd30g763e3k" path="res://resources/world_object_templates/dirt_top.tres" id="3_v04w2"]
[ext_resource type="Script" path="res://world/shapes/v_line.gd" id="4_mi7w4"]
[ext_resource type="Resource" uid="uid://hr5ivg0tjaki" path="res://resources/world_object_templates/dirt_block.tres" id="5_fcs2r"]
[ext_resource type="Resource" uid="uid://nb6v3btv6dtg" path="res://resources/world_object_templates/hut.tres" id="7_o4p2r"]
[ext_resource type="Resource" uid="uid://hidttrpsvqv6" path="res://resources/world_object_templates/dirt_block.tres" id="5_fcs2r"]
[ext_resource type="Resource" uid="uid://dhrgvruess3i7" path="res://resources/world_object_templates/hut.tres" id="7_o4p2r"]

[sub_resource type="Resource" id="Resource_hv8ck"]
script = ExtResource("2_orcmc")
Expand Down
9 changes: 7 additions & 2 deletions godot/world_object/template.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ class_name WorldObjectTemplate
extends Resource

@export var common_data: WorldObjectCommonData
@export var housing_data: WorldObjectHousingData
@export var tiles: Array[WorldObjectTile]

func apply_template(_game_state: GameState, _location: Vector2i) -> void:
return
func apply_template(game_state: GameState, location: Vector2i) -> void:
var housing: WorldObjectHousingData = WorldObjectHousingData.new_disabled()
if housing_data is WorldObjectHousingData:
housing = housing_data
housing.apply()
BehaviorFactory.push(game_state, location, common_data, housing)

func get_id() -> int:
return common_data.id
Expand Down
7 changes: 0 additions & 7 deletions godot/world_object/templates/housing.gd

This file was deleted.

5 changes: 0 additions & 5 deletions godot/world_object/templates/stable.gd

This file was deleted.

30 changes: 30 additions & 0 deletions rust/main_logic/src/entities/world_object/base.rs
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,
}
}
}
91 changes: 17 additions & 74 deletions rust/main_logic/src/entities/world_object/behavior.rs
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 rust/main_logic/src/entities/world_object/behaviors/housing.rs
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 rust/main_logic/src/entities/world_object/behaviors/stable.rs

This file was deleted.

Loading

0 comments on commit 1e0e5de

Please sign in to comment.