diff --git a/generic/constructibles/conveyor.tres b/generic/constructibles/conveyor.tres index cf8b491..04d8ee9 100644 --- a/generic/constructibles/conveyor.tres +++ b/generic/constructibles/conveyor.tres @@ -1,7 +1,7 @@ -[gd_resource type="Resource" script_class="Constructible" load_steps=5 format=3 uid="uid://dj524jjal1f0n"] +[gd_resource type="Resource" script_class="Prototype" load_steps=5 format=3 uid="uid://dj524jjal1f0n"] -[ext_resource type="Script" uid="uid://c80sp6f77l5ha" path="res://scripts/constructible.gd" id="1_53h7j"] -[ext_resource type="Texture2D" uid="uid://cbtre16ilxte0" path="res://sprites/atlasses/Popekko.png" id="1_x27pp"] +[ext_resource type="Script" uid="uid://c80sp6f77l5ha" path="res://scripts/prototype.gd" id="1_53h7j"] +[ext_resource type="Texture2D" uid="uid://dinyjq8853usn" path="res://sprites/atlasses/Popekko.png" id="1_x27pp"] [ext_resource type="PackedScene" uid="uid://b0h8dd82b3ox5" path="res://scenes/conveyor.tscn" id="2_hinjf"] [sub_resource type="AtlasTexture" id="AtlasTexture_uvy8r"] diff --git a/generic/constructibles/dbg_assembler.tres b/generic/constructibles/dbg_assembler.tres new file mode 100644 index 0000000..857a443 --- /dev/null +++ b/generic/constructibles/dbg_assembler.tres @@ -0,0 +1,15 @@ +[gd_resource type="Resource" script_class="Constructible" load_steps=5 format=3 uid="uid://yn1iesx30nfu"] + +[ext_resource type="Script" uid="uid://c80sp6f77l5ha" path="res://scripts/prototype.gd" id="1_mqcr0"] +[ext_resource type="Texture2D" uid="uid://dinyjq8853usn" path="res://sprites/atlasses/Popekko.png" id="1_sh8t1"] +[ext_resource type="PackedScene" uid="uid://dfatkxv6n55dw" path="res://scenes/debug_assembler.tscn" id="2_kf3x0"] + +[sub_resource type="AtlasTexture" id="AtlasTexture_kf3x0"] +atlas = ExtResource("1_sh8t1") +region = Rect2(32, 0, 32, 32) + +[resource] +script = ExtResource("1_mqcr0") +scene = ExtResource("2_kf3x0") +preview = SubResource("AtlasTexture_kf3x0") +metadata/_custom_type_script = "uid://c80sp6f77l5ha" diff --git a/project.godot b/project.godot index b6648e2..352b36b 100644 --- a/project.godot +++ b/project.godot @@ -18,8 +18,8 @@ config/icon="res://icon.svg" [autoload] GuiEventBus="*res://scripts/gui/gui_event_bus.gd" -RuntimePlayerData="*res://scripts/runtime_player_data.gd" -Registry="*res://scripts/registry.gd" +RuntimePlayerData="*res://scripts/autoloads/runtime_player_data.gd" +Registry="*res://scripts/autoloads/registry.gd" [display] diff --git a/scenes/2d_world.tscn b/scenes/2d_world.tscn index 5e93434..4e0d946 100644 --- a/scenes/2d_world.tscn +++ b/scenes/2d_world.tscn @@ -1,10 +1,11 @@ -[gd_scene load_steps=9 format=3 uid="uid://u7thalwj5742"] +[gd_scene load_steps=10 format=3 uid="uid://u7thalwj5742"] [ext_resource type="PackedScene" uid="uid://b7du4vul4bmpq" path="res://scenes/player_platform.tscn" id="1_4ia00"] [ext_resource type="Script" uid="uid://db37xmo8ygwhg" path="res://scripts/gui/construction_button.gd" id="2_27lod"] [ext_resource type="Resource" uid="uid://dj524jjal1f0n" path="res://generic/constructibles/conveyor.tres" id="3_2crwe"] -[ext_resource type="Script" uid="uid://b4nkk0ndqcto" path="res://scripts/construction_placer.gd" id="4_i7yte"] +[ext_resource type="Script" uid="uid://b4nkk0ndqcto" path="res://scripts/placement_manager.gd" id="4_i7yte"] [ext_resource type="PackedScene" uid="uid://bac552xgua68e" path="res://scenes/other_platform.tscn" id="5_mnxrb"] +[ext_resource type="Resource" uid="uid://yn1iesx30nfu" path="res://generic/constructibles/dbg_assembler.tres" id="5_ymyc2"] [sub_resource type="Animation" id="Animation_bq5r8"] length = 0.001 @@ -81,6 +82,24 @@ expand_icon = true script = ExtResource("2_27lod") constructible = ExtResource("3_2crwe") +[node name="AssemblerButton" type="Button" parent="GUI/Panel"] +layout_mode = 1 +anchors_preset = -1 +anchor_left = 0.008 +anchor_top = 0.086 +anchor_right = 0.1 +anchor_bottom = 0.88600004 +offset_left = 50.904 +offset_top = -0.01999998 +offset_right = 50.8 +offset_bottom = -0.020004272 +size_flags_horizontal = 3 +theme_type_variation = &"CRT" +icon_alignment = 1 +expand_icon = true +script = ExtResource("2_27lod") +constructible = ExtResource("5_ymyc2") + [node name="Placer" type="Node2D" parent="."] script = ExtResource("4_i7yte") diff --git a/scenes/conveyor.tscn b/scenes/conveyor.tscn index dbf0273..4f9ddf3 100644 --- a/scenes/conveyor.tscn +++ b/scenes/conveyor.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=4 format=3 uid="uid://b0h8dd82b3ox5"] -[ext_resource type="Texture2D" uid="uid://cbtre16ilxte0" path="res://sprites/atlasses/Popekko.png" id="1_kqxj7"] -[ext_resource type="Script" uid="uid://bbd7o2st8kmgl" path="res://scripts/construction.gd" id="1_y326v"] +[ext_resource type="Texture2D" uid="uid://dinyjq8853usn" path="res://sprites/atlasses/Popekko.png" id="1_kqxj7"] +[ext_resource type="Script" uid="uid://bbd7o2st8kmgl" path="res://scripts/structure.gd" id="1_y326v"] [sub_resource type="AtlasTexture" id="AtlasTexture_uvy8r"] atlas = ExtResource("1_kqxj7") diff --git a/scenes/debug_assembler.tscn b/scenes/debug_assembler.tscn new file mode 100644 index 0000000..1b8d172 --- /dev/null +++ b/scenes/debug_assembler.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=4 format=3 uid="uid://dfatkxv6n55dw"] + +[ext_resource type="Script" uid="uid://bbd7o2st8kmgl" path="res://scripts/structure.gd" id="1_k5y3y"] +[ext_resource type="Texture2D" uid="uid://dinyjq8853usn" path="res://sprites/atlasses/Popekko.png" id="2_4befw"] + +[sub_resource type="AtlasTexture" id="AtlasTexture_xh4eg"] +atlas = ExtResource("2_4befw") +region = Rect2(32, 0, 32, 32) + +[node name="DebugAssembler" type="Node2D"] +script = ExtResource("1_k5y3y") +dimensions = Rect2i(0, 0, 2, 2) + +[node name="Sprite2D" type="Sprite2D" parent="."] +texture = SubResource("AtlasTexture_xh4eg") +offset = Vector2(8, 8) diff --git a/scenes/other_platform.tscn b/scenes/other_platform.tscn index 125ccab..47d0b84 100644 --- a/scenes/other_platform.tscn +++ b/scenes/other_platform.tscn @@ -1,13 +1,20 @@ -[gd_scene load_steps=3 format=4 uid="uid://bac552xgua68e"] +[gd_scene load_steps=4 format=4 uid="uid://bac552xgua68e"] [ext_resource type="TileSet" uid="uid://bv75bu0af04vs" path="res://generic/platform.tres" id="1_fs1oy"] -[ext_resource type="Script" uid="uid://lw1ya3iu14uf" path="res://scripts/build_zone.gd" id="2_bwire"] +[ext_resource type="Script" uid="uid://lw1ya3iu14uf" path="res://scripts/placement_zone.gd" id="2_bwire"] +[ext_resource type="Script" uid="uid://x5edy155eg0s" path="res://scripts/grid_controller.gd" id="3_qaudu"] [node name="OtherPlatform" type="TileMapLayer"] tile_map_data = PackedByteArray("AAAAAAAAAAAAAAAAAAAAAAEAAAAAAAEAAAAAAAIAAAAAAAEAAAAAAAMAAAAAAAEAAAAAAAQAAAAAAAEAAAABAAAAAAAAAAEAAAABAAEAAAAAAAEAAAABAAIAAAAAAAEAAAABAAMAAAAAAAAAAAABAAQAAAAAAAEAAAACAAAAAAAAAAAAAAACAAEAAAAAAAEAAAACAAIAAAAAAAEAAAACAAMAAAAAAAAAAAACAAQAAAAAAAAAAAD/////AAACAAEAAAD//wAAAAAEAAIAAAD//wEAAAAEAAIAAAD//wIAAAAEAAIAAAD//wMAAAAEAAIAAAD//wQAAAAEAAIAAAAAAP//AAABAAIAAAABAP//AAABAAIAAAACAP//AAABAAIAAAADAP//AAADAAEAAAD//wUAAAAEAAEAAAADAAUAAAAFAAEAAAAAAAUAAAAGAAEAAAABAAUAAAAGAAEAAAACAAUAAAAGAAEAAAADAAAAAAAFAAIAAAADAAEAAAAFAAIAAAADAAIAAAAFAAIAAAADAAMAAAAFAAIAAAADAAQAAAAFAAIAAAA=") tile_set = ExtResource("1_fs1oy") -[node name="BuildZone" type="Marker2D" parent="."] +[node name="BuildZone" type="Marker2D" parent="." node_paths=PackedStringArray("grid_controller")] script = ExtResource("2_bwire") building_rect = Rect2(0, 0, 48, 80) +grid_controller = NodePath("../EntityHolder") metadata/_custom_type_script = "uid://lw1ya3iu14uf" + +[node name="EntityHolder" type="Node2D" parent="." node_paths=PackedStringArray("building_zone")] +script = ExtResource("3_qaudu") +building_zone = NodePath("../BuildZone") +metadata/_custom_type_script = "uid://x5edy155eg0s" diff --git a/scenes/player_platform.tscn b/scenes/player_platform.tscn index c23ecc3..ca73456 100644 --- a/scenes/player_platform.tscn +++ b/scenes/player_platform.tscn @@ -1,8 +1,8 @@ [gd_scene load_steps=4 format=4 uid="uid://b7du4vul4bmpq"] [ext_resource type="TileSet" uid="uid://bv75bu0af04vs" path="res://generic/platform.tres" id="1_khkq2"] -[ext_resource type="Script" uid="uid://lw1ya3iu14uf" path="res://scripts/build_zone.gd" id="2_wpmwa"] -[ext_resource type="Script" uid="uid://x5edy155eg0s" path="res://scripts/entity_holder.gd" id="3_6hjoe"] +[ext_resource type="Script" uid="uid://lw1ya3iu14uf" path="res://scripts/placement_zone.gd" id="2_wpmwa"] +[ext_resource type="Script" uid="uid://x5edy155eg0s" path="res://scripts/grid_controller.gd" id="3_6hjoe"] [node name="PlayerPlatform" type="Node2D"] @@ -10,9 +10,10 @@ tile_map_data = PackedByteArray("AAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAACAAAAAAAAAAEAAAADAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAFAAAAAAAAAAAAAAAGAAAAAAAAAAEAAAAHAAAAAAAAAAAAAAAIAAAAAAAAAAEAAAAJAAAAAAAAAAEAAAAKAAAAAAAAAAEAAAALAAAAAAAAAAAAAAAMAAAAAAAAAAEAAAANAAAAAAAAAAEAAAAOAAAAAAAAAAEAAAAPAAAAAAAAAAAAAAAQAAAAAAAAAAEAAAAQAAEAAAAAAAAAAAAPAAEAAAAAAAAAAAAOAAEAAAAAAAEAAAANAAEAAAAAAAAAAAAMAAEAAAAAAAAAAAALAAEAAAAAAAEAAAAKAAEA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AAAEAP//AAAAAAIAAAAFAP//AAAAAAIAAAAGAP//AAAAAAIAAAAHAP//AAAAAAIAAAAIAP//AAAAAAIAAAAJAP//AAAAAAIAAAAKAP//AAAAAAIAAAALAP//AAAAAAIAAAAMAP//AAAAAAIAAAANAP//AAAAAAIAAAAOAP//AAAAAAIAAAAPAP//AAAAAAIAAAAQAP//AAAAAAIAAAD/////AAACAAIAAAARAP//AAADAAIAAAD//wgAAAADAAAAAAARAAgAAAAEAAAAAAAQAAgAAAACAAAAAAAPAAgAAAACAAAAAAAOAAgAAAACAAAAAAANAAgAAAACAAAAAAAMAAgAAAACAAAAAAALAAgAAAACAAAAAAAKAAgAAAACAAAAAAAJAAgAAAACAAAAAAAIAAgAAAACAAAAAAAHAAgAAAACAAAAAAAGAAgAAAACAAAAAAAFAAgAAAACAAAAAAAEAAgAAAACAAAAAAADAAgAAAACAAAAAAACAAgAAAACAAAAAAABAAgAAAACAAAAAAAAAAgAAAACAAAAAAA=") tile_set = ExtResource("1_khkq2") -[node name="BuildRect" type="Marker2D" parent="."] +[node name="BuildRect" type="Marker2D" parent="." node_paths=PackedStringArray("grid_controller")] script = ExtResource("2_wpmwa") building_rect = Rect2(0, 0, 272, 128) +grid_controller = NodePath("../EntityHolder") [node name="EntityHolder" type="Node2D" parent="." node_paths=PackedStringArray("building_zone")] script = ExtResource("3_6hjoe") diff --git a/scripts/autoloads/registry.gd b/scripts/autoloads/registry.gd new file mode 100644 index 0000000..76dbf46 --- /dev/null +++ b/scripts/autoloads/registry.gd @@ -0,0 +1,2 @@ +## Global class for runtime +extends Node diff --git a/scripts/registry.gd.uid b/scripts/autoloads/registry.gd.uid similarity index 100% rename from scripts/registry.gd.uid rename to scripts/autoloads/registry.gd.uid diff --git a/scripts/registry.gd b/scripts/autoloads/runtime_player_data.gd similarity index 53% rename from scripts/registry.gd rename to scripts/autoloads/runtime_player_data.gd index e408bfa..4f199b3 100644 --- a/scripts/registry.gd +++ b/scripts/autoloads/runtime_player_data.gd @@ -1,9 +1,12 @@ +## Global class for player in-game data + extends Node func _ready() -> void: get_tree().scene_changed.connect(cleanup_runtime_cache) +## Method to cleanup all data that will be nulled func cleanup_runtime_cache(): build_zones.clear() -var build_zones : Array[BuildZone] +var build_zones : Array[PlacementZone] diff --git a/scripts/runtime_player_data.gd.uid b/scripts/autoloads/runtime_player_data.gd.uid similarity index 100% rename from scripts/runtime_player_data.gd.uid rename to scripts/autoloads/runtime_player_data.gd.uid diff --git a/scripts/constructible.gd b/scripts/constructible.gd deleted file mode 100644 index 5fbd326..0000000 --- a/scripts/constructible.gd +++ /dev/null @@ -1,6 +0,0 @@ -extends Resource - -class_name Constructible - -@export var scene : PackedScene -@export var preview : Texture2D diff --git a/scripts/construction.gd b/scripts/construction.gd deleted file mode 100644 index 7d86f32..0000000 --- a/scripts/construction.gd +++ /dev/null @@ -1,6 +0,0 @@ -extends Node2D - -class_name Construction - -func get_relative(dv : Vector2) -> Construction: - return get_parent().get_at(global_position+dv) diff --git a/scripts/entity_holder.gd b/scripts/entity_holder.gd deleted file mode 100644 index 6b6b129..0000000 --- a/scripts/entity_holder.gd +++ /dev/null @@ -1,22 +0,0 @@ -extends Node2D - -class_name EntityHolder - -const GRID_SIZE : Vector2 = Vector2(16,16) - -@export var building_zone : BuildZone -var constructions : Array[Construction] - -func _ready() -> void: - constructions.resize(building_zone.get_capacity()) - -func add_construction(construction : Construction) -> bool: - if constructions[building_zone.indexify_global_point(construction.global_position)]: - return false - construction.reparent(self) - construction.global_position = building_zone.get_placement_position(construction.global_position) - constructions[building_zone.indexify_global_point(construction.global_position)] = construction - return true - -func get_at(point : Vector2): - return constructions[building_zone.indexify_global_point(point)] diff --git a/scripts/globals.gd b/scripts/globals.gd new file mode 100644 index 0000000..d7ff107 --- /dev/null +++ b/scripts/globals.gd @@ -0,0 +1,6 @@ +@abstract +extends Object + +class_name Globals + +const GRID_SIZE : Vector2 = Vector2(16,16) diff --git a/scripts/globals.gd.uid b/scripts/globals.gd.uid new file mode 100644 index 0000000..4bb6907 --- /dev/null +++ b/scripts/globals.gd.uid @@ -0,0 +1 @@ +uid://bnmjkc8vfd7mo diff --git a/scripts/grid_controller.gd b/scripts/grid_controller.gd new file mode 100644 index 0000000..15cb8b4 --- /dev/null +++ b/scripts/grid_controller.gd @@ -0,0 +1,42 @@ +extends Node2D + +# TODO: Make able to hold multiple placement zones, supporting uniform forms + +## Class that contains all structures of platform + +class_name GridController + + +## Building zone controller uses for checks and capacity +@export var building_zone : PlacementZone + +## Internal structure storage +var structures : Array[Structure] + +func _ready() -> void: + structures.resize(building_zone.get_capacity()) + +## Tries to add structure to its internal storage [br] +## Returns false if has colliding structures +func add_structure(structure : Structure) -> bool: + var structure_dp = structure.get_dimension_points() + for point in structure_dp: + if structures[building_zone.indexify_global_point(structure.global_position + point)]: + return false + structure.reparent(self) + structure.global_position = building_zone.get_placement_position(structure.global_position) + + for point in structure_dp: + structures[building_zone.indexify_global_point(structure.global_position + point)] = structure + return true + +## Returns structure at point. [br] +## Returns null if no structure is at point +func get_at(point : Vector2) -> Structure: + var index = building_zone.indexify_global_point(point) + if index == -1: + return null + return structures[index] + +func is_point_occupied(point : Vector2) -> bool: + return get_at(point) != null diff --git a/scripts/entity_holder.gd.uid b/scripts/grid_controller.gd.uid similarity index 100% rename from scripts/entity_holder.gd.uid rename to scripts/grid_controller.gd.uid diff --git a/scripts/gui/construction_button.gd b/scripts/gui/construction_button.gd index b56e8c8..a38d23e 100644 --- a/scripts/gui/construction_button.gd +++ b/scripts/gui/construction_button.gd @@ -1,6 +1,6 @@ extends Button -@export var constructible : Constructible +@export var constructible : Prototype func _ready() -> void: icon = constructible.preview diff --git a/scripts/gui/gui_event_bus.gd b/scripts/gui/gui_event_bus.gd index a5ab2b0..57b7d3b 100644 --- a/scripts/gui/gui_event_bus.gd +++ b/scripts/gui/gui_event_bus.gd @@ -2,5 +2,5 @@ extends Node @warning_ignore_start("unused_signal") -signal construction_selected(constructible : Constructible) -signal construction_placed(constructible : Constructible) +signal construction_selected(constructible : Prototype) +signal construction_placed(constructible : Prototype) diff --git a/scripts/construction_placer.gd b/scripts/placement_manager.gd similarity index 57% rename from scripts/construction_placer.gd rename to scripts/placement_manager.gd index bbbdb82..d58e676 100644 --- a/scripts/construction_placer.gd +++ b/scripts/placement_manager.gd @@ -1,6 +1,7 @@ extends Node2D -var held_construction : Construction +## Currently held structure +var held_construction : Structure func _ready() -> void: GuiEventBus.construction_selected.connect(on_construction_selected) @@ -13,13 +14,13 @@ func _input(event: InputEvent) -> void: if zone == null: held_construction.queue_free() else: - if zone.get_parent().get_node("EntityHolder").add_construction(held_construction): + if held_construction.try_place(zone): held_construction = null if event.is_action_pressed("plc_cancel"): held_construction.queue_free() -func on_construction_selected(constructible : Constructible): +func on_construction_selected(constructible : Prototype): if held_construction: held_construction.queue_free() held_construction = constructible.scene.instantiate() @@ -27,16 +28,17 @@ func on_construction_selected(constructible : Constructible): func _process(_delta: float) -> void: - if held_construction != null: - var mouse_pos = get_global_mouse_position() - var zone = try_get_zone(mouse_pos) - if zone: - global_position = zone.get_placement_position(mouse_pos) - else: - global_position = mouse_pos + if held_construction == null: + return + var mouse_pos = get_global_mouse_position() + var zone = try_get_zone(mouse_pos) + if zone and held_construction.can_be_placed(zone): + global_position = zone.get_placement_position(mouse_pos) + else: + global_position = mouse_pos -func try_get_zone(point : Vector2) -> BuildZone: - for zone in Registry.build_zones: +func try_get_zone(point : Vector2) -> PlacementZone: + for zone in RuntimePlayerData.build_zones: if zone.is_global_point_in_zone(point): return zone return null diff --git a/scripts/construction_placer.gd.uid b/scripts/placement_manager.gd.uid similarity index 100% rename from scripts/construction_placer.gd.uid rename to scripts/placement_manager.gd.uid diff --git a/scripts/build_zone.gd b/scripts/placement_zone.gd similarity index 72% rename from scripts/build_zone.gd rename to scripts/placement_zone.gd index 23a16f8..84d1efa 100644 --- a/scripts/build_zone.gd +++ b/scripts/placement_zone.gd @@ -4,9 +4,7 @@ ## Class that helps to manage construction sites extends Marker2D -class_name BuildZone - -const GRID_SIZE : Vector2 = Vector2(16,16) +class_name PlacementZone ## Rect that used for bounds check and conversions @export var building_rect : Rect2: @@ -17,9 +15,11 @@ const GRID_SIZE : Vector2 = Vector2(16,16) get: return building_rect +@export var grid_controller : GridController + func _ready() -> void: if not Engine.is_editor_hint(): - Registry.build_zones.append(self) + RuntimePlayerData.build_zones.append(self) func _draw() -> void: if Engine.is_editor_hint() and EditorInterface.get_inspector().get_edited_object() == self: @@ -37,7 +37,7 @@ func is_global_point_in_zone(point: Vector2) -> bool: func indexify_point(point : Vector2) -> int: if is_point_in_zone(point) == false: return -1 - return int(point.x) / int(GRID_SIZE.x) + int(building_rect.size.x/GRID_SIZE.x)*(int(point.y) / int(GRID_SIZE.y)) + return int(point.x) / int(Globals.GRID_SIZE.x) + int(building_rect.size.x/Globals.GRID_SIZE.x)*(int(point.y) / int(Globals.GRID_SIZE.y)) ## Returns index of point (global coordinates) to be used in array func indexify_global_point(point : Vector2) -> int: @@ -45,7 +45,7 @@ func indexify_global_point(point : Vector2) -> int: ## Inverses indexification of point, returning snapped position func inverse_index(index: int) -> Vector2: - return to_global(Vector2(index%int(building_rect.size.x/GRID_SIZE.x)*GRID_SIZE.x,index / int(building_rect.size.x/GRID_SIZE.x) * GRID_SIZE.y)) + return to_global(Vector2(index%int(building_rect.size.x/Globals.GRID_SIZE.x)*Globals.GRID_SIZE.x,index / int(building_rect.size.x/Globals.GRID_SIZE.x) * Globals.GRID_SIZE.y)) ## Returns snapped position of point (global coordinates). ## Equivalent of [code] @@ -54,8 +54,8 @@ func inverse_index(index: int) -> Vector2: func get_placement_position(point : Vector2) -> Vector2: if is_global_point_in_zone(point) == false: return Vector2.ZERO - return to_global((to_local(point) / GRID_SIZE).floor()*GRID_SIZE + GRID_SIZE/2.0) + return to_global((to_local(point) / Globals.GRID_SIZE).floor()*Globals.GRID_SIZE + Globals.GRID_SIZE/2.0) ## Returns capacity of building zone to be used in the array func get_capacity() -> int: - return int(building_rect.size.x/GRID_SIZE.x)*int(building_rect.size.y/GRID_SIZE.y) + return int(building_rect.size.x/Globals.GRID_SIZE.x)*int(building_rect.size.y/Globals.GRID_SIZE.y) diff --git a/scripts/build_zone.gd.uid b/scripts/placement_zone.gd.uid similarity index 100% rename from scripts/build_zone.gd.uid rename to scripts/placement_zone.gd.uid diff --git a/scripts/prototype.gd b/scripts/prototype.gd new file mode 100644 index 0000000..83f60a9 --- /dev/null +++ b/scripts/prototype.gd @@ -0,0 +1,11 @@ +extends Resource + +## Player instantiatable resource + +class_name Prototype + +## Scene to instantiate +@export var scene : PackedScene + +## UI Preview +@export var preview : Texture2D diff --git a/scripts/constructible.gd.uid b/scripts/prototype.gd.uid similarity index 100% rename from scripts/constructible.gd.uid rename to scripts/prototype.gd.uid diff --git a/scripts/runtime_player_data.gd b/scripts/runtime_player_data.gd deleted file mode 100644 index 61510e1..0000000 --- a/scripts/runtime_player_data.gd +++ /dev/null @@ -1 +0,0 @@ -extends Node diff --git a/scripts/structure.gd b/scripts/structure.gd new file mode 100644 index 0000000..16792e4 --- /dev/null +++ b/scripts/structure.gd @@ -0,0 +1,55 @@ +@tool +extends Node2D + +## Game object that interact with other structures in its grid space +class_name Structure + +## Dimensions of structure in grid tiles +@export var dimensions : Rect2i = Rect2i(0,0,1,1): + set(value): + dimensions = value + if Engine.is_editor_hint(): + queue_redraw() + get: + return dimensions + +## Debug draw of points +func _draw() -> void: + if Engine.is_editor_hint(): + for x in range(dimensions.size.x): + for y in range(dimensions.size.y): + draw_circle((dimensions.position+Vector2i(x,y)) * Vector2i(Globals.GRID_SIZE),2,Color.AQUA) + +## Get structure at tile coordinates relative to this structure [br] +## dv : Vector2 - get position in tiles [br] +func get_relative(dv : Vector2) -> Structure: + return get_parent().get_at(global_position+dv*Globals.GRID_SIZE) + +## Check structure is in zone and does not collide with any other structures [br] +## Returns true if structure is in zone and does not collide with any other structure [br] +## Returns false if structure is not in zone or does not collide with any other structure +func can_be_placed(zone : PlacementZone) -> bool: + for dp in get_dimension_points(): + var point = global_position + dp + if zone.is_global_point_in_zone(point) == false: + return false + if zone.grid_controller.is_point_occupied(point): + return false + return true + +## Tries to place structure in zone's grid controller +## Returns false if structure is not in zone or collides with any structure +func try_place(zone : PlacementZone) -> bool: + if can_be_placed(zone) == false: + return false + + return zone.grid_controller.add_structure(self) + +## Returns array of integer points in dimensions +func get_dimension_points() -> Array[Vector2]: + var result : Array[Vector2] = [] + result.resize(dimensions.size.x*dimensions.size.y) + for x in range(dimensions.size.x): + for y in range(dimensions.size.y): + result[x + y * dimensions.size.x] = (Vector2(x,y)*Globals.GRID_SIZE + Vector2(dimensions.position)) + return result diff --git a/scripts/construction.gd.uid b/scripts/structure.gd.uid similarity index 100% rename from scripts/construction.gd.uid rename to scripts/structure.gd.uid diff --git a/sprites/atlasses/Popekko.png b/sprites/atlasses/Popekko.png index 48fd6af..8ff06cb 100644 Binary files a/sprites/atlasses/Popekko.png and b/sprites/atlasses/Popekko.png differ diff --git a/sprites/atlasses/Popekko.png.import b/sprites/atlasses/Popekko.png.import index e1cba67..f2b9199 100644 --- a/sprites/atlasses/Popekko.png.import +++ b/sprites/atlasses/Popekko.png.import @@ -2,7 +2,7 @@ importer="texture" type="CompressedTexture2D" -uid="uid://cbtre16ilxte0" +uid="uid://dinyjq8853usn" path="res://.godot/imported/Popekko.png-5c34a5f2a8c85fcf59453310fa6553d9.ctex" metadata={ "vram_texture": false