Placement and platforms
This commit is contained in:
parent
a2164dca72
commit
d37c4ec858
31 changed files with 351 additions and 11 deletions
|
@ -17,6 +17,10 @@ const GRID_SIZE : Vector2 = Vector2(16,16)
|
|||
get:
|
||||
return building_rect
|
||||
|
||||
func _ready() -> void:
|
||||
if not Engine.is_editor_hint():
|
||||
Registry.build_zones.append(self)
|
||||
|
||||
func _draw() -> void:
|
||||
if Engine.is_editor_hint() and EditorInterface.get_inspector().get_edited_object() == self:
|
||||
draw_rect(building_rect,Color.CYAN,false,1)
|
||||
|
|
6
scripts/constructible.gd
Normal file
6
scripts/constructible.gd
Normal file
|
@ -0,0 +1,6 @@
|
|||
extends Resource
|
||||
|
||||
class_name Constructible
|
||||
|
||||
@export var scene : PackedScene
|
||||
@export var preview : Texture2D
|
1
scripts/constructible.gd.uid
Normal file
1
scripts/constructible.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://c80sp6f77l5ha
|
6
scripts/construction.gd
Normal file
6
scripts/construction.gd
Normal file
|
@ -0,0 +1,6 @@
|
|||
extends Node2D
|
||||
|
||||
class_name Construction
|
||||
|
||||
func get_relative(dv : Vector2) -> Construction:
|
||||
return get_parent().get_at(global_position+dv)
|
1
scripts/construction.gd.uid
Normal file
1
scripts/construction.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://bbd7o2st8kmgl
|
42
scripts/construction_placer.gd
Normal file
42
scripts/construction_placer.gd
Normal file
|
@ -0,0 +1,42 @@
|
|||
extends Node2D
|
||||
|
||||
var held_construction : Construction
|
||||
|
||||
func _ready() -> void:
|
||||
GuiEventBus.construction_selected.connect(on_construction_selected)
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if held_construction == null:
|
||||
return
|
||||
if event.is_action_pressed("plc_place"):
|
||||
var zone = try_get_zone(held_construction.global_position)
|
||||
if zone == null:
|
||||
held_construction.queue_free()
|
||||
else:
|
||||
if zone.get_parent().get_node("EntityHolder").add_construction(held_construction):
|
||||
held_construction = null
|
||||
|
||||
if event.is_action_pressed("plc_cancel"):
|
||||
held_construction.queue_free()
|
||||
|
||||
func on_construction_selected(constructible : Constructible):
|
||||
if held_construction:
|
||||
held_construction.queue_free()
|
||||
held_construction = constructible.scene.instantiate()
|
||||
add_child(held_construction)
|
||||
|
||||
|
||||
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
|
||||
|
||||
func try_get_zone(point : Vector2) -> BuildZone:
|
||||
for zone in Registry.build_zones:
|
||||
if zone.is_global_point_in_zone(point):
|
||||
return zone
|
||||
return null
|
1
scripts/construction_placer.gd.uid
Normal file
1
scripts/construction_placer.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://b4nkk0ndqcto
|
22
scripts/entity_holder.gd
Normal file
22
scripts/entity_holder.gd
Normal file
|
@ -0,0 +1,22 @@
|
|||
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)]
|
1
scripts/entity_holder.gd.uid
Normal file
1
scripts/entity_holder.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://x5edy155eg0s
|
9
scripts/gui/construction_button.gd
Normal file
9
scripts/gui/construction_button.gd
Normal file
|
@ -0,0 +1,9 @@
|
|||
extends Button
|
||||
|
||||
@export var constructible : Constructible
|
||||
|
||||
func _ready() -> void:
|
||||
icon = constructible.preview
|
||||
|
||||
func _pressed() -> void:
|
||||
GuiEventBus.construction_selected.emit(constructible)
|
1
scripts/gui/construction_button.gd.uid
Normal file
1
scripts/gui/construction_button.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://db37xmo8ygwhg
|
6
scripts/gui/gui_event_bus.gd
Normal file
6
scripts/gui/gui_event_bus.gd
Normal file
|
@ -0,0 +1,6 @@
|
|||
extends Node
|
||||
|
||||
@warning_ignore_start("unused_signal")
|
||||
|
||||
signal construction_selected(constructible : Constructible)
|
||||
signal construction_placed(constructible : Constructible)
|
1
scripts/gui/gui_event_bus.gd.uid
Normal file
1
scripts/gui/gui_event_bus.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://x5eghpenp777
|
|
@ -1 +0,0 @@
|
|||
uid://b004eslxx7ylv
|
9
scripts/registry.gd
Normal file
9
scripts/registry.gd
Normal file
|
@ -0,0 +1,9 @@
|
|||
extends Node
|
||||
|
||||
func _ready() -> void:
|
||||
get_tree().scene_changed.connect(cleanup_runtime_cache)
|
||||
|
||||
func cleanup_runtime_cache():
|
||||
build_zones.clear()
|
||||
|
||||
var build_zones : Array[BuildZone]
|
1
scripts/registry.gd.uid
Normal file
1
scripts/registry.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://coqgbea1uchn4
|
1
scripts/runtime_player_data.gd.uid
Normal file
1
scripts/runtime_player_data.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://bmowwtsy0f8mf
|
Loading…
Add table
Add a link
Reference in a new issue