ld52_olive_harvest/vehicles/Postie.gd

117 lines
3.2 KiB
GDScript

extends Node2D
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var marker: Node2D = null
# Called when the node enters the scene tree for the first time.
func _ready():
set_ramp_enabled(false)
set_backgate_enabled(true)
marker = get_parent().get_parent().find_node("MarkerRoadPoint")
if marker == null:
print("No marker!")
return
self.position = Vector2(-600, marker.position.y)
_drive_anim()
var state = null
func _drive_anim():
$Tween.interpolate_property(self, "position", self.position, marker.position, 5.0, Tween.TRANS_CUBIC, Tween.EASE_OUT)
state = "driving_in"
$Tween.start()
$DriveInSfx.pitch_scale = rand_range(0.9, 1.2)
$DriveInSfx.play()
func set_ramp_enabled(enabled: bool):
$N/BackRamp.visible = enabled
$N/BackRamp/StaticBody2D/BackRampShape.disabled = not enabled
func set_backgate_enabled(enabled: bool):
$N/BackGate.visible = enabled
$N/BackGate/StaticBody2D/BackGateShape.disabled = not enabled
func _on_driven_in():
set_backgate_enabled(false)
set_ramp_enabled(true)
$BackgateOpenSfx.pitch_scale = rand_range(0.8, 1.2)
$BackgateOpenSfx.play()
$Tween.interpolate_callback(self, 5.0, "_drive_off")
$Tween.start()
func _drive_off():
set_ramp_enabled(false)
set_backgate_enabled(true)
# Attach bodies that are in the back, if they are sellable.
var saleable_bodies = $SaleArea.get_overlapping_bodies()
for body in saleable_bodies:
#print("SALEABLE? ", body)
if body.is_in_group("saleable") and body.sale_value != null:
var glob_pos = body.global_position
body.get_parent().remove_child(body)
$N/Back/ToSell.add_child(body)
body.global_position = glob_pos
# Disable collisions
$N.collision_layer = 0
$BackgateClosedSfx.pitch_scale = rand_range(0.8, 1.2)
$BackgateClosedSfx.play()
state = "driving_off"
$Tween.interpolate_property(self, "position:x", position.x, 2000, 5.0, Tween.TRANS_QUAD, Tween.EASE_IN, 3.5)
$DriveOffSfx.pitch_scale = rand_range(0.8, 1.2)
$Tween.interpolate_callback($DriveOffSfx, 1.0, "play")
$Tween.start()
func _on_driven_off():
# Find out what is in the back of the van
# Give the player monies for that.
# N.B. anything that was purchased by the player is resellable for quarter of its original price.
var coin_made = 0
for body in $N/Back/ToSell.get_children():
if body.is_in_group("saleable") and body.sale_value != null:
coin_made += body.sale_value
else:
print("trying to sell ", body, " but no value!")
print("coin made: ", coin_made)
func _on_Tween_tween_all_completed():
if state == "driving_in":
self._on_driven_in()
if state == "driving_off":
self._on_driven_off()
self.queue_free()
state = null
var sale_value_so_far = 0
func _update_sale_value():
# the 4 spaces give a gap for the coin symbol
$SaleValueLabel.text = "Total Sale Value:\n " + str(sale_value_so_far)
func _on_SaleArea_body_entered(body):
#print("SA enter ", body)
if body.is_in_group("saleable") and body.sale_value != null:
sale_value_so_far += body.sale_value
self._update_sale_value()
func _on_SaleArea_body_exited(body):
#print("SA exit ", body)
if body.is_in_group("saleable") and body.sale_value != null:
sale_value_so_far -= body.sale_value
self._update_sale_value()