bring-inc-prototype/Source/scripts/main.gd
2022-01-27 21:44:45 +01:00

60 lines
1.3 KiB
GDScript

extends Node2D
var bringmanScene = preload("res://scenes/bringman.tscn")
onready var btnAdd = get_node("btnAdd")
onready var city_parent = get_node("city_parent")
onready var market_parent = get_node("market_parent")
onready var bringman_parent = get_node("bringman_parent")
onready var button_diam = btnAdd.texture.get_width()*btnAdd.transform.get_scale().x
func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT && event.pressed:
if (event.position - btnAdd.position).length() < button_diam/2:
spawn()
func spawn():
var market = random_market()
var city = random_city()
var bm = bringmanScene.instance()
var rot = rand_range(-TAU/5, +TAU/5)
var vec_start = Vector2(0, 1).rotated(rot)
bm.position = btnAdd.position + (vec_start * -(button_diam/2 - bm.radius()))
bm.spawn = btnAdd.position + (vec_start * -(button_diam/2 + bm.radius()))
bm.market = market.get_path()
bm.city = city.get_path()
bm.company = btnAdd.get_path()
print("spawn", bm.position)
bringman_parent.add_child(bm)
func random_market():
var r = []
for child in market_parent.get_children():
r.append(child)
return r[randi() % r.size()]
func random_city():
var r = []
for child in city_parent.get_children():
r.append(child)
return r[randi() % r.size()]