add honeydew tweening on gathering

This commit is contained in:
2024-10-06 23:20:55 +10:00
parent ea3ac3b688
commit 9e98556f8c
2 changed files with 48 additions and 12 deletions

View File

@@ -1,10 +1,15 @@
extends Interactable extends Interactable
class_name Honeydew class_name Honeydew
signal moved
const HEIGHT_OFFSET: float = 0.1 const HEIGHT_OFFSET: float = 0.1
const DROP_SPREAD: float = 0.1 const MOVE_SPEED: float = 8
var carried: bool = false var carried: bool = false
var move_to: Vector3
var move_from: Vector3
var moving_timer: float = 0
@onready var collision_shape: CollisionShape3D = $CollisionShape3D @onready var collision_shape: CollisionShape3D = $CollisionShape3D
@@ -15,11 +20,29 @@ func _ready() -> void:
global_position.y = HEIGHT_OFFSET global_position.y = HEIGHT_OFFSET
func _process(delta: float) -> void:
super._process(delta)
if moving_timer <= 0:
if move_to != Vector3.ZERO:
move_to = Vector3.ZERO
moved.emit()
return
moving_timer -= delta * MOVE_SPEED
global_position = lerp(
move_from,
move_to,
(1 - moving_timer),
)
func set_carried(on: bool) -> void: func set_carried(on: bool) -> void:
carried = on carried = on
can_interact = not carried can_interact = not carried
collision_shape.disabled = carried collision_shape.disabled = carried
if (not carried):
global_position.x += randf_range(-DROP_SPREAD, DROP_SPREAD)
global_position.y = HEIGHT_OFFSET func start_moving(to: Vector3) -> Honeydew:
global_position.z += randf_range(-DROP_SPREAD, DROP_SPREAD) moving_timer = 1
move_from = global_position
move_to = to
return self

View File

@@ -7,6 +7,7 @@ signal stop_gathering
const DEFAULT_MAX_CARRYING = 3 const DEFAULT_MAX_CARRYING = 3
const DEFAULT_DROP_INTERVAL = 0.25 const DEFAULT_DROP_INTERVAL = 0.25
const DEFAULT_PICKUP_INTERVAL = 0.5 const DEFAULT_PICKUP_INTERVAL = 0.5
const DROP_SPREAD: float = 0.1
enum GatherState { enum GatherState {
PICKING_UP, PICKING_UP,
@@ -33,18 +34,14 @@ func _ready() -> void:
func _process(_delta: float) -> void: func _process(_delta: float) -> void:
for i in range(carrying_items.size()): for i in range(carrying_items.size()):
var item := carrying_items[i] var item := carrying_items[i]
item.global_position = ( item.global_position = get_nth_pile_pos(i)
global_position
+ (Vector3.UP * 0.5)
+ (Vector3.UP * 0.1 * i)
)
if target != null: if target != null:
DebugDraw.circle(target.global_position) DebugDraw.circle(target.global_position)
func initialize( func initialize(
from: Anthill, from: Anthill,
max_carry: int = DEFAULT_MAX_CARRYING, max_carry: int = DEFAULT_MAX_CARRYING,
drop_interv: float = DEFAULT_DROP_INTERVAL, drop_interv: float = DEFAULT_DROP_INTERVAL,
pickup_interv: float = DEFAULT_PICKUP_INTERVAL, pickup_interv: float = DEFAULT_PICKUP_INTERVAL,
@@ -92,11 +89,21 @@ func stop_all_gathering() -> void:
state = GatherState.STOP state = GatherState.STOP
target = null target = null
func get_nth_pile_pos(n: int) -> Vector3:
return (
global_position
+ (Vector3.UP * 0.5)
+ (Vector3.UP * 0.1 * n)
)
func _pick_up() -> void: func _pick_up() -> void:
if not target.carried: if not target.carried:
carrying_items.append(target) carrying_items.append(target)
target.set_carried(true) target.set_carried(true)
await target.start_moving(
get_nth_pile_pos(carrying_items.size() - 1)
).moved
await get_tree().create_timer(pickup_interval).timeout await get_tree().create_timer(pickup_interval).timeout
if carrying_items.size() >= max_carrying: if carrying_items.size() >= max_carrying:
@@ -125,6 +132,7 @@ func _deposit() -> void:
return return
var item := carrying_items.pop_back() as Honeydew var item := carrying_items.pop_back() as Honeydew
await item.start_moving(anthill.global_position).moved
ItemsManager.erase_honeydew(item) ItemsManager.erase_honeydew(item)
_erase_honeydew(item) _erase_honeydew(item)
item.queue_free() item.queue_free()
@@ -149,7 +157,12 @@ func _deposit() -> void:
func _drop_everything() -> void: func _drop_everything() -> void:
while carrying_items.size() > 0: while carrying_items.size() > 0:
var item := carrying_items.pop_back() as Honeydew var item := carrying_items.pop_back() as Honeydew
item.set_carried(false) var new_pos := Vector3(
randf_range(-DROP_SPREAD, DROP_SPREAD),
Honeydew.HEIGHT_OFFSET,
randf_range(-DROP_SPREAD, DROP_SPREAD),
)
await item.start_moving(global_position + new_pos).moved
await get_tree().create_timer(drop_interval).timeout await get_tree().create_timer(drop_interval).timeout