Commit 646c232c authored by Gerard Gascón's avatar Gerard Gascón
Browse files

feat: disallow spawn of repeated dragons

parent f379dff5
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ patch_margin_left = 74
patch_margin_right = 74

[node name="Label" type="Label" parent="CanvasLayer/TowerNameLabel"]
layout_mode = 0
offset_left = 2.0
offset_top = 2.0
offset_right = 250.0
+51 −4
Original line number Diff line number Diff line
@@ -62,15 +62,62 @@ func _instantiate_random_dragon():
	if spot == null:
		return
	
	#TODO: Add also a way to instantiate dragons in your library with a greater probability
	var r = rng.randi_range(0, 99)
	if r < 30:
		_instantiate_dragon_from_internet(spot)
	else:
		_instantiate_dragon_from_library(spot)


func _instantiate_dragon_from_library(spot: Node2D) -> void:
	var dragons_available: Array[DragonProperties] = []
	for dragon in _library.dragons:
		if not _is_dragon_instantiated(dragon):
			dragons_available.push_back(dragon)
	
	if dragons_available.size() == 0:
		_instantiate_dragon_from_internet(spot)
		return
	
	var r = rng.randi_range(0, dragons_available.size() - 1)
	var dragon_entity: DragonEntity = _generate_entity(spot, dragons_available[r])
	_filled_spots[spot] = dragon_entity.properties.id


func _is_dragon_instantiated(dragon: DragonProperties) -> bool:
	for entity: Dragon in _dragon_entities.values():
		if _are_properties_equal(entity.properties, dragon):
			return true
	for entity: DragonEntity in _instantiated_dragons.values():
		if _are_properties_equal(entity.properties, dragon):
			return true
	return false


func _are_properties_equal(p1: DragonProperties, p2: DragonProperties) -> bool:
	if p1.name != p2.name:
		return false
	if p1.origin != p2.origin:
		return false
	if p1.color != p2.color:
		return false
	if p1.hat != p2.hat:
		return false
	if p1.shirt != p2.shirt:
		return false
	if p1.decor != p2.decor:
		return false
	return true


func _instantiate_dragon_from_internet(spot: Node2D) -> void:
	for i in range(5):
		dragon_sharing.receive(_save_load.get_tower_name())
		var dragon: DragonProperties = await dragon_sharing.on_dragon_received
		if dragon == null:
			return
		for d in _library.dragons:
			# TODO: Search for dragon and if is in library or in instantiated dragons continue next iteration
			pass
		if _is_dragon_instantiated(dragon):
			continue
		var dragon_entity: DragonEntity = _generate_entity(spot, dragon)
		_filled_spots[spot] = dragon_entity.properties.id
		return