bring-inc-prototype/Source/scripts/main.gd

66 lines
1.4 KiB
GDScript3
Raw Normal View History

2022-01-27 21:44:45 +01:00
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")
2022-01-28 00:28:17 +01:00
onready var money_frame = get_node("moneyframe")
2022-01-27 21:44:45 +01:00
onready var button_diam = btnAdd.texture.get_width()*btnAdd.transform.get_scale().x
2022-01-28 00:28:17 +01:00
func _ready():
randomize()
2022-01-27 21:44:45 +01:00
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)
2022-01-28 00:28:17 +01:00
var diag = sqrt(2*pow(button_diam/2, 2))
2022-01-27 21:44:45 +01:00
bm.position = btnAdd.position + (vec_start * -(button_diam/2 - bm.radius()))
2022-01-28 00:28:17 +01:00
bm.spawn = btnAdd.position + (vec_start * -(diag + bm.radius()))
2022-01-27 21:44:45 +01:00
bm.market = market.get_path()
bm.city = city.get_path()
bm.company = btnAdd.get_path()
2022-01-28 00:28:17 +01:00
bm.moneyframe = money_frame.get_path()
2022-01-27 21:44:45 +01:00
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()]