70 lines
2.1 KiB
GDScript
70 lines
2.1 KiB
GDScript
class_name CellData
|
|
extends Resource
|
|
|
|
const BUILD = preload("res://data/interactions/build.tres")
|
|
const GATHER = preload("res://data/interactions/gather.tres")
|
|
|
|
@export var layer_info: Dictionary = {
|
|
Constants.TilemapLayers.CORRUPTION: false
|
|
}
|
|
var _pos: Vector2i
|
|
var interaction_display: ProgressBar
|
|
|
|
func _to_string() -> String:
|
|
return "{%s %s}" % [_pos, layer_info]
|
|
|
|
func _init(pos: Vector2i) -> void:
|
|
_pos = pos
|
|
|
|
func change_layer(layer: int, data: Variant) -> bool:
|
|
if layer_info.has(layer) and layer_info[layer] == data:
|
|
return false
|
|
layer_info[layer] = data
|
|
return true
|
|
|
|
func change_resource(data: GameResource) -> void:
|
|
layer_info[Constants.TilemapLayers.ENVIRONMENT] = data
|
|
|
|
func get_resource() -> GameResource:
|
|
return layer_info[Constants.TilemapLayers.ENVIRONMENT] as GameResource
|
|
|
|
func has_resource() -> bool:
|
|
return has_layer(Constants.TilemapLayers.ENVIRONMENT)
|
|
|
|
func has_building() -> bool:
|
|
return has_layer(Constants.TilemapLayers.BUILDINGS)
|
|
|
|
func is_corrupted() -> bool:
|
|
return layer_info[Constants.TilemapLayers.CORRUPTION]
|
|
|
|
func has_layer(layer: int) -> bool:
|
|
return layer_info.has(layer)
|
|
|
|
func interact(timer: Timer) -> void:
|
|
var is_interactable := has_resource() or not has_building()
|
|
if is_interactable and not interaction_display:
|
|
timer.timeout.connect(_on_interaction_finished.bind(timer))
|
|
timer.start(3)
|
|
|
|
interaction_display = ProgressBar.new()
|
|
interaction_display.position = Grid.grid_to_world_center(_pos) - Vector2(30, 10)
|
|
interaction_display.size = Vector2(60, 20)
|
|
interaction_display.show_percentage = false
|
|
var tween = timer.get_tree().create_tween()
|
|
tween.tween_property(interaction_display, "value", 100, 3)
|
|
timer.get_parent().add_sibling(interaction_display)
|
|
|
|
func get_interaction_options() -> Array[Interaction]:
|
|
var interactions: Array[Interaction] = []
|
|
if has_resource():
|
|
interactions.append(GATHER)
|
|
if not has_building():
|
|
interactions.append(BUILD)
|
|
return interactions
|
|
|
|
func _on_interaction_finished(timer: Timer) -> void:
|
|
timer.timeout.disconnect(_on_interaction_finished)
|
|
if has_resource():
|
|
interaction_display.queue_free()
|
|
get_resource().gained_resource.emit(get_resource())
|