53 lines
1.9 KiB
GDScript
53 lines
1.9 KiB
GDScript
extends KinematicBody2D
|
|
export var swim_length = 150
|
|
export var swim_cooldown = 1
|
|
|
|
var destination # The position the fish will move to _on_SwimTimer_timeout()
|
|
|
|
var poisoned = false # Used in Hazards/Poison/Poison.gd to determine if fish is dying of poison
|
|
var poison_mutation = true # True when fish can eat and shoot poison pellets
|
|
var pellets = 0 # Tracks the number of poison pellets eaten but not shot yet
|
|
onready var mouth = $Positions/Mouth.position # Used for eating poison pellets
|
|
onready var mouth_inhale = $Positions/MouthInhale.position
|
|
onready var tail = $Positions/Booty
|
|
|
|
|
|
func _ready():
|
|
$SwimTimer.wait_time = swim_cooldown
|
|
|
|
func _on_SwimTimer_timeout() -> void:
|
|
# Swim toward mouse
|
|
destination = Vector2(move_toward(position.x, get_global_mouse_position().x, swim_length), move_toward(position.y, get_global_mouse_position().y, swim_length))
|
|
$Tween.interpolate_property(self, "position", position, destination, 1, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
|
|
$Tween.start()
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
if destination:
|
|
# Smoothly rotate Fish to face mouse
|
|
var v = get_global_mouse_position() - global_position
|
|
var angle = v.angle() + deg2rad(270)
|
|
if rad2deg(angle) < 180 or rad2deg(angle) > 360:
|
|
$Sprite.flip_v = true
|
|
else:
|
|
$Sprite.flip_v = false
|
|
var rotation_speed = PI * 0.75 # PI = 180 degrees
|
|
var angle_delta = rotation_speed * delta
|
|
angle = lerp_angle(global_rotation, angle + 1.57, 1.0)
|
|
angle = clamp(angle, global_rotation - angle_delta, global_rotation + angle_delta)
|
|
global_rotation = angle
|
|
|
|
|
|
func _fire_pellet() -> void:
|
|
if pellets > 0:
|
|
var poison_scene = load("res://Hazards/Poison/Poison.tscn")
|
|
var poison = poison_scene.instance()
|
|
add_child(poison)
|
|
poison.position = position
|
|
#poison.direction = position.angle_to_point(mouth)
|
|
poison.applied_force = Vector2(mouth.get_global_position(), tail.get_global_position())
|
|
|
|
|
|
func kill(cause: String) -> void:
|
|
queue_free()
|