58 lines
2.5 KiB
GDScript
58 lines
2.5 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 and shooting poison pellets
|
|
onready var mouth_inhale = $Positions/MouthInhale.position # Used for eating poison pellets
|
|
onready var tail = $Positions/Tail.position # Used for shooting poison pellets
|
|
|
|
|
|
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()
|
|
_fire_pellet()
|
|
|
|
|
|
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:
|
|
pellets = 1 # This is just hax so we don't have to eat pellets first to fire them
|
|
if pellets > 0: # But usually we require fish to eat pellets before he shoots them
|
|
var poison_scene = load("res://Hazards/Poison/Poison.tscn")
|
|
var poison = poison_scene.instance()
|
|
poison.friendly = true # This will prevent the fish from auto-eating it the second it spawns
|
|
get_parent().add_child(poison) # We add this projectile to the /tank/ so they don't move with the fish
|
|
poison.position = get_global_position() # 'position' returns the local position, but we need where it's at in relation to the tank
|
|
#poison.direction = position.angle_to_point(mouth)
|
|
poison.applied_force = 50 * ($Positions/Mouth.get_global_position() - $Positions/Tail.get_global_position())
|
|
pellets -= 1
|
|
|
|
|
|
func kill(cause: String) -> void:
|
|
queue_free()
|