41 lines
1.4 KiB
GDScript
41 lines
1.4 KiB
GDScript
extends AbstractFood
|
|
class_name FoodMolecular
|
|
|
|
@onready var collision: Area2D = $Collision
|
|
@onready var sprite: AnimatedSprite2D = $Collision/Sprite
|
|
@onready var player = get_tree().get_first_node_in_group("player") # FIXME: this is not v efficient; global resource? or at least per Stage.
|
|
|
|
func _ready() -> void:
|
|
sprite.play()
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
var dpos = Vector2(GameManager.flow_x, GameManager.flow_y) * delta * flow_carry_speed
|
|
collision.position = GameManager.get_boundaried_position(collision.position + dpos)
|
|
|
|
func _process(delta):
|
|
collision.position = GameManager.get_boundaried_position(collision.position)
|
|
|
|
func _on_body_entered(body: Node2D) -> void:
|
|
eat(body)
|
|
|
|
func eat(consumer: Node2D) -> void:
|
|
print(consumer.name + " ate: ", food_name)
|
|
consumed.emit()
|
|
|
|
apply_effect(consumer)
|
|
|
|
queue_free()
|
|
|
|
func apply_effect(consumer: Node2D) -> void:
|
|
if consumer.has_method("collect_resource"): # TODO: Define a "consumer" group instead?
|
|
consumer.collect_resource(val) # val is from parent (default 1), override if required
|
|
# TODO: *Some cool effect here*
|
|
|
|
|
|
# FIXME: position tracking inaccurate?
|
|
func _on_collision_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
|
|
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
|
if player:
|
|
player.target = self.collision
|