21:44
This commit is contained in:
108
Source/scripts/bringman.gd
Normal file
108
Source/scripts/bringman.gd
Normal file
@@ -0,0 +1,108 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
export (NodePath) var city
|
||||
export (NodePath) var market
|
||||
export (NodePath) var company
|
||||
export (Vector2) var spawn
|
||||
|
||||
|
||||
var speed_spawn = 500
|
||||
var speed_phys = 5
|
||||
var dest = 0
|
||||
|
||||
var current_target = null
|
||||
|
||||
var wait_start = 0.5
|
||||
var wait_market = 1.8
|
||||
var wait_city = 1.8
|
||||
var wait_final = 0.2
|
||||
|
||||
var velocity = Vector2()
|
||||
|
||||
var oldNodePosHitAngle = null
|
||||
|
||||
func _ready():
|
||||
get_node("sprite").texture = load("res://sprites/bringmans/bm"+str(randi()%10).pad_zeros(2)+".png")
|
||||
|
||||
|
||||
func state_next():
|
||||
velocity = Vector2.ZERO
|
||||
oldNodePosHitAngle = null
|
||||
dest+=1
|
||||
|
||||
func _process(delta):
|
||||
if dest == 0:
|
||||
move_to_pos(spawn, delta)
|
||||
elif dest == 1:
|
||||
wait_start-=delta
|
||||
if wait_start <= 0: state_next()
|
||||
elif dest == 2:
|
||||
move_to_node(market, delta)
|
||||
elif dest == 3:
|
||||
wait_market-=delta
|
||||
if wait_market <= 0: state_next()
|
||||
elif dest == 4:
|
||||
move_to_node(city, delta)
|
||||
elif dest == 5:
|
||||
wait_city-=delta
|
||||
if wait_city <= 0: state_next()
|
||||
elif dest == 6:
|
||||
move_to_node_pos(company, delta)
|
||||
elif dest == 7:
|
||||
wait_final-=delta
|
||||
if wait_final <= 0: state_next()
|
||||
elif dest == 8:
|
||||
remove_and_skip()
|
||||
|
||||
|
||||
func move_to_node(np, delta):
|
||||
var node = get_node(np)
|
||||
velocity += (node.position - position).normalized() * speed_phys
|
||||
|
||||
current_target = node
|
||||
|
||||
|
||||
func move_to_node_pos(np, delta):
|
||||
var node = get_node(np)
|
||||
velocity += (node.position - position).normalized() * speed_phys
|
||||
|
||||
var angleNow = sign((node.position - position).angle())
|
||||
|
||||
if oldNodePosHitAngle == null:
|
||||
oldNodePosHitAngle = angleNow
|
||||
else:
|
||||
if angleNow != oldNodePosHitAngle:
|
||||
state_next()
|
||||
else:
|
||||
oldNodePosHitAngle = angleNow
|
||||
|
||||
|
||||
func move_to_pos(pos, delta):
|
||||
velocity = Vector2.ZERO
|
||||
current_target = null
|
||||
|
||||
var angle_prev = sign((pos - position).angle())
|
||||
|
||||
position = position.move_toward(pos, delta * speed_spawn)
|
||||
|
||||
var angle_post = sign((pos - position).angle())
|
||||
|
||||
if angle_prev != angle_post:
|
||||
state_next()
|
||||
|
||||
func radius():
|
||||
return get_node("sprite").texture.get_width()*get_node("sprite").transform.get_scale().x/2
|
||||
|
||||
|
||||
func _physics_process(delta):
|
||||
if (velocity.length_squared() == 0): return
|
||||
|
||||
velocity = move_and_slide(velocity, Vector2(0, -1))
|
||||
|
||||
if current_target != null:
|
||||
var slide_count = get_slide_count()
|
||||
if slide_count:
|
||||
var collision = get_slide_collision(slide_count - 1)
|
||||
var collider = collision.collider
|
||||
if collider == current_target:
|
||||
state_next()
|
||||
4
Source/scripts/btnAdd.gd
Normal file
4
Source/scripts/btnAdd.gd
Normal file
@@ -0,0 +1,4 @@
|
||||
extends Sprite
|
||||
|
||||
func radius():
|
||||
return texture.get_width()*transform.get_scale().x/2
|
||||
7
Source/scripts/city.gd
Normal file
7
Source/scripts/city.gd
Normal file
@@ -0,0 +1,7 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
func _ready():
|
||||
get_node("sprite").texture = load("res://sprites/cities/city"+str(randi()%11).pad_zeros(2)+".png")
|
||||
|
||||
func radius():
|
||||
return get_node("sprite").texture.get_width()*get_node("sprite").transform.get_scale().x/2
|
||||
59
Source/scripts/main.gd
Normal file
59
Source/scripts/main.gd
Normal file
@@ -0,0 +1,59 @@
|
||||
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()]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
15
Source/scripts/market.gd
Normal file
15
Source/scripts/market.gd
Normal file
@@ -0,0 +1,15 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
|
||||
# Declare member variables here. Examples:
|
||||
# var a = 2
|
||||
# var b = "text"
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
func radius():
|
||||
return get_node("sprite").texture.get_width()*get_node("sprite").transform.get_scale().x/2
|
||||
Reference in New Issue
Block a user