2021-06-12 00:17:29 +02:00
|
|
|
extends KinematicBody2D
|
|
|
|
|
2021-06-12 13:37:56 +02:00
|
|
|
export var ACCELERATION = 60
|
|
|
|
export var MAX_SPEED = 150
|
|
|
|
export var FRICTION = 50
|
2021-06-12 00:17:29 +02:00
|
|
|
|
2021-06-12 02:04:22 +02:00
|
|
|
var velocity = Vector2.ZERO
|
2021-06-12 13:41:12 +02:00
|
|
|
var last_in_line
|
2021-06-12 19:12:34 +02:00
|
|
|
var destinationColor = Color.transparent
|
2021-06-12 23:08:45 +02:00
|
|
|
var guestName = 'Car'
|
2021-06-12 00:17:29 +02:00
|
|
|
|
2021-06-12 23:08:45 +02:00
|
|
|
signal scored(value)
|
2021-06-12 13:41:12 +02:00
|
|
|
var guests = []
|
2021-06-12 00:17:29 +02:00
|
|
|
|
|
|
|
func _ready():
|
2021-06-12 19:59:12 +02:00
|
|
|
last_in_line = self
|
|
|
|
guests.append(last_in_line)
|
2021-06-12 00:17:29 +02:00
|
|
|
pass # Replace with function body.
|
|
|
|
|
2021-06-12 13:41:12 +02:00
|
|
|
func add_Guest_to_Line(parent,guest):
|
|
|
|
guests.append(guest)
|
2021-06-12 23:08:45 +02:00
|
|
|
print('Picked up Guest %s with color %s'%[guest.guestName,guest.destinationColor])
|
2021-06-12 22:07:47 +02:00
|
|
|
var parentAnchor = parent.get_node("Anchor")
|
|
|
|
parentAnchor.add_child(get_a_springjoint(parent,guest))
|
|
|
|
var pua = guest.get_node("PickUpArea")
|
|
|
|
pua.monitorable = false
|
|
|
|
#springJoint.rotation = -rotation
|
|
|
|
return guest
|
|
|
|
|
|
|
|
func get_a_springjoint(parent,child):
|
2021-06-12 19:12:34 +02:00
|
|
|
var springJoint = DampedSpringJoint2D.new()
|
|
|
|
#springJoint.rotation+=get_angle_to(guest.global_position)
|
2021-06-12 23:08:45 +02:00
|
|
|
springJoint.length = 5
|
|
|
|
springJoint.rest_length = 5
|
2021-06-12 19:12:34 +02:00
|
|
|
springJoint.stiffness = 64
|
|
|
|
springJoint.damping = 1.0
|
2021-06-12 23:08:45 +02:00
|
|
|
springJoint.disable_collision =true
|
2021-06-12 22:07:47 +02:00
|
|
|
|
2021-06-12 19:59:12 +02:00
|
|
|
springJoint.node_a =parent.get_path()
|
2021-06-12 22:07:47 +02:00
|
|
|
springJoint.node_b =child.get_path()
|
|
|
|
springJoint.add_child(child)
|
|
|
|
return springJoint
|
2021-06-12 19:12:34 +02:00
|
|
|
|
2021-06-12 23:08:45 +02:00
|
|
|
func get_score_from_guest(guest):
|
2021-06-12 23:28:39 +02:00
|
|
|
var now = OS.get_system_time_msecs()
|
|
|
|
var diff = now - guest.pickup_time
|
|
|
|
var score = diff / 1000
|
|
|
|
score = 50 - score
|
|
|
|
return score
|
2021-06-12 23:08:45 +02:00
|
|
|
|
2021-06-12 19:12:34 +02:00
|
|
|
func remove_Guests_from_Line(color):
|
|
|
|
|
2021-06-12 19:59:12 +02:00
|
|
|
var colormatches = []
|
2021-06-12 19:12:34 +02:00
|
|
|
|
|
|
|
for g in range(guests.size()):
|
2021-06-12 19:59:12 +02:00
|
|
|
if guests[g]!= null:
|
|
|
|
if guests[g].destinationColor == color:
|
|
|
|
colormatches.append(guests[g])
|
2021-06-12 19:12:34 +02:00
|
|
|
|
2021-06-12 19:59:12 +02:00
|
|
|
for i in colormatches:
|
2021-06-12 23:08:45 +02:00
|
|
|
var scoreValue = get_score_from_guest(i)
|
|
|
|
emit_signal("scored",scoreValue)
|
2021-06-12 19:59:12 +02:00
|
|
|
var pos = guests.find(i)
|
|
|
|
i.queue_free()
|
|
|
|
guests.remove(pos)
|
2021-06-12 19:12:34 +02:00
|
|
|
return guests.back()
|
2021-06-12 13:41:12 +02:00
|
|
|
|
|
|
|
func _on_PickupCheckArea_area_entered(area):
|
|
|
|
|
|
|
|
if area.get_parent().is_in_group("DropOffPoint"):
|
|
|
|
print("It's a DOP")
|
2021-06-12 19:12:34 +02:00
|
|
|
var dop = area.get_parent()
|
|
|
|
var color = dop.destinationColor
|
|
|
|
print(color)
|
2021-06-12 23:08:45 +02:00
|
|
|
last_in_line = remove_Guests_from_Line(color)
|
2021-06-12 13:41:12 +02:00
|
|
|
#drop all guests after first guest.color == DOP.color, also vanish all guests.color == DOP.color
|
|
|
|
pass
|
|
|
|
if area.get_parent().is_in_group("Guest"):
|
2021-06-12 19:12:34 +02:00
|
|
|
if guests.has(area.get_parent()):
|
2021-06-12 22:07:47 +02:00
|
|
|
print("Area has parent %s" % area.get_parent())
|
|
|
|
print("Guests we have:")
|
|
|
|
print(guests)
|
2021-06-12 19:12:34 +02:00
|
|
|
print("We already have you in line")
|
|
|
|
else:
|
2021-06-12 22:07:47 +02:00
|
|
|
print("Area has parent %s" % area.get_parent())
|
2021-06-12 19:12:34 +02:00
|
|
|
print("It's a Guest")
|
|
|
|
last_in_line = add_Guest_to_Line(last_in_line,area.get_parent())
|
|
|
|
print(last_in_line)
|
2021-06-12 13:41:12 +02:00
|
|
|
|
|
|
|
print(guests)
|
|
|
|
|
|
|
|
|
2021-06-12 00:17:29 +02:00
|
|
|
|
2021-06-12 02:04:22 +02:00
|
|
|
func _physics_process(delta):
|
|
|
|
var direction = Vector2.UP.rotated(rotation).normalized() #Playerrotation nehmen ist sicherer
|
2021-06-12 13:37:56 +02:00
|
|
|
var forward_backward = Input.get_action_strength("ui_up") - Input.get_action_strength("ui_down")
|
2021-06-12 02:04:22 +02:00
|
|
|
|
2021-06-12 13:37:56 +02:00
|
|
|
|
|
|
|
if forward_backward != 0:
|
|
|
|
velocity = velocity.move_toward(direction * MAX_SPEED * forward_backward, ACCELERATION * delta)
|
2021-06-12 02:04:22 +02:00
|
|
|
else:
|
|
|
|
velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
|
|
|
|
|
2021-06-12 02:25:59 +02:00
|
|
|
var steer_dir = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
|
|
|
|
|
2021-06-12 13:37:56 +02:00
|
|
|
if steer_dir != 0 && velocity.length() > 0:
|
|
|
|
var direction_new = direction.rotated(PI/1.5 * steer_dir * delta)
|
2021-06-12 19:12:34 +02:00
|
|
|
#print(velocity.length())
|
2021-06-12 02:25:59 +02:00
|
|
|
#print(velocity.angle_to(velocity.rotated(direction_new.angle())))
|
|
|
|
velocity = velocity.rotated(direction.angle_to(direction_new))
|
|
|
|
rotate(direction.angle_to(direction_new))
|
|
|
|
|
2021-06-12 02:04:22 +02:00
|
|
|
|
2021-06-12 13:37:56 +02:00
|
|
|
velocity = move_and_slide(velocity)
|