ld52_olive_harvest/scenery/facilities/Press.gd

197 lines
5.2 KiB
GDScript

extends StaticBody2D
var olives_in_crusher = 0
var crushed_olives_in_reserve = 0
var oil_level_in_bottle = 0
const FRESH_OLIVE_SPRITE = preload("res://items/olive.png")
var fresh_olive_sprites = []
var crushed_olive_sprites = []
const OLIVE_OIL_W = 32
# 4 - 37
const OLIVE_OIL_HEIGHTS = [0, 14, 25, 48]
var bottles = []
func _ready():
bottles = [$BottleOliveoil, $BottleOliveoil2, $BottleOliveoil3, $BottleOliveoil4, $BottleOliveoil5, $BottleOliveoil6]
_reset_bottle()
for bottle in bottles:
bottle.visible = false
# The current animation finished, do something if there's something to be done
func _next_step():
if $Tween.is_active():
return
if self._add_label_and_lid():
return
if self._new_bottle():
return
if not $FillingBottle.visible:
_reset_bottle()
if self._spray_bottle():
return
func _reset_bottle():
_set_oil_height(OLIVE_OIL_HEIGHTS[0])
$FillingBottle.visible = true
$FillingBottle/BottleOliveoilLid.visible = false
$FillingBottle/BottleOliveoilLabel.visible = false
func _add_label_and_lid():
if $FillingBottle/BottleOliveoilLid.visible or oil_level_in_bottle < 3:
return false
$FillingBottle/BottleOliveoilLid.visible = true
$FillingBottle/BottleOliveoilLid.modulate.a = 0
$FillingBottle/BottleOliveoilLabel.visible = true
$FillingBottle/BottleOliveoilLabel.modulate.a = 0
$Tween.interpolate_property($FillingBottle/BottleOliveoilLid, "modulate:a", 0.0, 1.0, 0.5, Tween.TRANS_QUAD, Tween.EASE_IN_OUT)
$Tween.interpolate_property($FillingBottle/BottleOliveoilLabel, "modulate:a", 0.0, 1.0, 0.5, Tween.TRANS_QUAD, Tween.EASE_IN_OUT, 0.5)
$Tween.start()
return true
func _new_bottle():
if oil_level_in_bottle < 3:
return false
var any_free_bottles = false
for bottle in bottles:
if not bottle.visible:
any_free_bottles = true
break
if not any_free_bottles:
return true
oil_level_in_bottle = 0
var positions_in_chain = [
$FillingBottle.position,
$BottleOliveoil.position,
$BottleOliveoil2.position,
$BottleOliveoil3.position,
$BottleOliveoil4.position,
$BottleOliveoil5.position,
$BottleOliveoil6.position
]
var time_to_slide = rand_range(1.5, 2.5)
for pos in positions_in_chain:
print("PIC ", pos.x)
var carrying_on = true
for i in range(6):
var next_src_pos = positions_in_chain[i]
var bottle_to_affect = bottles[i]
if not bottle_to_affect.visible:
if not carrying_on:
break
carrying_on = false
bottle_to_affect.set_deferred("visible", true)
var old_x = bottle_to_affect.position.x
bottle_to_affect.position.x = next_src_pos.x
$Tween.interpolate_property(bottle_to_affect, "position:y", next_src_pos.y, bottle_to_affect.position.y, 0.6, Tween.TRANS_LINEAR, Tween.EASE_IN)
$Tween.interpolate_property(bottle_to_affect, "position:x", next_src_pos.x, old_x, time_to_slide, Tween.TRANS_LINEAR, Tween.EASE_IN, 0.6)
bottle_to_affect.position.y = next_src_pos.y
$FillingBottle.visible = false
$Tween.start()
return true
func _set_oil_height(new_h: float):
new_h = round(new_h)
$FillingBottle/BottleOliveoilOil.region_rect = Rect2(0, 48 - new_h, 32, new_h)
$FillingBottle/BottleOliveoilOil.offset.y = - new_h
func _spray_bottle():
if crushed_olives_in_reserve < 1 or oil_level_in_bottle >= 3:
return false
crushed_olives_in_reserve -= 1
crushed_olive_sprites.pop_back().queue_free()
var old_oil_h = OLIVE_OIL_HEIGHTS[oil_level_in_bottle]
oil_level_in_bottle += 1
var new_oil_h = OLIVE_OIL_HEIGHTS[oil_level_in_bottle]
$Tween.interpolate_method(self, "_set_oil_height", old_oil_h, new_oil_h, 1.0, Tween.TRANS_QUAD, Tween.EASE_IN_OUT)
$Tween.start()
$SpraySfx.pitch_scale = rand_range(0.9, 1.2)
$SpraySfx.play()
return true
func _on_Tween_tween_all_completed():
_next_step()
func _on_CollectionArea_body_entered(body: PhysicsBody2D):
if body.has_method("on_press_area_entered"):
body.on_press_area_entered(self)
func crushing_needed():
return olives_in_crusher > 0
func crush():
if olives_in_crusher < 1:
return
olives_in_crusher -= 1
crushed_olives_in_reserve += 1
var olive_sprite = fresh_olive_sprites.pop_back()
# give it a 'squished' effect
olive_sprite.scale = Vector2(rand_range(1.5, 2.5), rand_range(0.15, 0.45))
# make it a bit more yellowy
olive_sprite.modulate = Color(0.5, 1.0, 1.0)
crushed_olive_sprites.push_back(olive_sprite)
$SquishSfx.pitch_scale = rand_range(0.9, 1.2)
$SquishSfx.play()
_next_step()
func add_fresh_olives(num: int):
olives_in_crusher += num
var dist = rand_range(10, 52)
var offs = rand_range(0, 2 * PI)
for i in range(num):
var angle = PI * i * (2.0 / num) + offs
var new_olive_sprite = Sprite.new()
$PressB.add_child(new_olive_sprite)
new_olive_sprite.texture = FRESH_OLIVE_SPRITE
new_olive_sprite.position = Vector2(0, -52) + Vector2(dist, 0).rotated(angle)
fresh_olive_sprites.push_back(new_olive_sprite)
func interact_bottle_area(player: Node2D):
if not player.has_free_item_slot():
return false
var l = len(bottles)
for i in range(l - 1, -1, -1):
if bottles[i].visible:
bottles[i].visible = false
var new_olive_oil = preload("res://items/ItemOliveOil.tscn").instance()
new_olive_oil.interact(player)
_next_step()
break
return true
func _on_CollectionArea_body_exited(body):
if body.has_method("on_press_area_exited"):
body.on_press_area_exited(self)