Restrict spawning near the hero unit
This commit is contained in:
parent
3cb59c172e
commit
a7866fb0ef
|
@ -11,6 +11,7 @@ export(PackedScene) var spawned_unit setget set_spawned_unit
|
|||
var dragged = false
|
||||
var base_position
|
||||
var selected_anchor
|
||||
var hero
|
||||
|
||||
func _ready():
|
||||
set_portrait(portrait)
|
||||
|
@ -40,14 +41,22 @@ func set_spawned_unit(val):
|
|||
$points.visible = true
|
||||
else:
|
||||
$points.visible = false
|
||||
|
||||
func set_hero(val: Node):
|
||||
hero = val
|
||||
|
||||
func can_be_used_on_anchor(anchor):
|
||||
if anchor is Anchor:
|
||||
if spawned_unit and not anchor.is_empty():
|
||||
return false
|
||||
return true
|
||||
if spawned_unit:
|
||||
return anchor.is_empty() and hero and anchor.is_connected_to(hero.get_parent())
|
||||
else:
|
||||
return true
|
||||
else:
|
||||
return false
|
||||
|
||||
func set_hand_location(loc: Vector2):
|
||||
rect_position = loc
|
||||
base_position = loc
|
||||
|
||||
func update_anchors():
|
||||
var position = rect_global_position + rect_size / 2 if dragged else null
|
||||
|
|
|
@ -19,9 +19,10 @@ static func update_anchors(tree: SceneTree, position, acceptability) -> Anchor:
|
|||
|
||||
return selected_anchor
|
||||
|
||||
static func spawn_unit(name: String, anchor: Anchor):
|
||||
static func spawn_unit(name: String, anchor: Anchor) -> Node:
|
||||
""" Spawn a unit on an empty anchor
|
||||
"""
|
||||
var scene = load("res://units/" + name + ".tscn")
|
||||
var node = scene.instance()
|
||||
anchor.set_content(node)
|
||||
return node
|
||||
|
|
|
@ -7,12 +7,15 @@ export(PackedScene) var deck_defend
|
|||
|
||||
onready var deck_attack_cards = deck_attack.instance()
|
||||
onready var deck_defend_cards = deck_defend.instance()
|
||||
var hero_attack
|
||||
var hero_defend
|
||||
|
||||
func _ready():
|
||||
create_hero_units()
|
||||
hero_attack = create_hero_unit("tomahawk", "attack_start")
|
||||
hero_defend = create_hero_unit("rhino", "defend_start")
|
||||
|
||||
fill_hand($hand_attack, deck_attack_cards)
|
||||
fill_hand($hand_defend, deck_defend_cards)
|
||||
fill_hand($hand_attack, deck_attack_cards, hero_attack)
|
||||
fill_hand($hand_defend, deck_defend_cards, hero_defend)
|
||||
|
||||
func find_free_anchor(anchor_type: String):
|
||||
for anchor in get_tree().get_nodes_in_group("anchors"):
|
||||
|
@ -23,17 +26,16 @@ func find_free_anchor(anchor_type: String):
|
|||
func create_hero_unit(name: String, anchor_type: String):
|
||||
var anchor = find_free_anchor(anchor_type)
|
||||
if anchor:
|
||||
BattleHelper.spawn_unit("heroes/" + name, anchor)
|
||||
return BattleHelper.spawn_unit("heroes/" + name, anchor)
|
||||
else:
|
||||
return null
|
||||
|
||||
func create_hero_units():
|
||||
create_hero_unit("tomahawk", "attack_start")
|
||||
create_hero_unit("rhino", "defend_start")
|
||||
|
||||
func fill_hand(hand: Node, deck: Node, limit=4):
|
||||
func fill_hand(hand: Node, deck: Node, hero: Node, limit=4):
|
||||
while hand.get_child_count() < 4:
|
||||
var count = deck.get_child_count()
|
||||
var index = floor(rand_range(0, count))
|
||||
var card = deck.get_child(index).duplicate()
|
||||
card.set_hero(hero)
|
||||
hand.add_child(card)
|
||||
|
||||
reorganize_hand(hand)
|
||||
|
@ -41,5 +43,5 @@ func fill_hand(hand: Node, deck: Node, limit=4):
|
|||
func reorganize_hand(hand: Node):
|
||||
var i = 0
|
||||
for card in hand.get_children():
|
||||
card.rect_position = Vector2(i * 210, 0)
|
||||
card.set_hand_location(Vector2(i * 210, 0))
|
||||
i += 1
|
||||
|
|
Loading…
Reference in New Issue