54 lines
1.3 KiB
GDScript
54 lines
1.3 KiB
GDScript
class_name FarmState
|
|
extends RefCounted
|
|
|
|
signal land_changed(position: int)
|
|
signal changed
|
|
|
|
var lands_by_position: Dictionary = {}
|
|
var changed_positions: Array[int] = []
|
|
|
|
|
|
func setup_from_player_data(player_data: Dictionary, land_count: int) -> void:
|
|
lands_by_position.clear()
|
|
changed_positions.clear()
|
|
var land_list = player_data.get("landList", [])
|
|
if not land_list is Array:
|
|
changed.emit()
|
|
return
|
|
|
|
for item in land_list:
|
|
if not item is Dictionary:
|
|
continue
|
|
var position := int(item.get("position", -1))
|
|
if position >= 0 and position < land_count:
|
|
lands_by_position[position] = item.duplicate(true)
|
|
changed.emit()
|
|
|
|
|
|
func get_land(position: int) -> Dictionary:
|
|
return lands_by_position.get(position, {}).duplicate(true)
|
|
|
|
|
|
func set_land(land_data: Dictionary) -> void:
|
|
if land_data.is_empty():
|
|
return
|
|
var position := int(land_data.get("position", -1))
|
|
if position < 0:
|
|
return
|
|
lands_by_position[position] = land_data.duplicate(true)
|
|
if not changed_positions.has(position):
|
|
changed_positions.append(position)
|
|
land_changed.emit(position)
|
|
changed.emit()
|
|
|
|
|
|
func first_missing_position(land_count: int) -> int:
|
|
for position in range(land_count):
|
|
if not lands_by_position.has(position):
|
|
return position
|
|
return -1
|
|
|
|
|
|
func snapshot() -> Dictionary:
|
|
return lands_by_position.duplicate(true)
|