extends KinematicBody2D # external settings export var swim_speed: float = 30.0 export var swim_up_down_variance = 0.75 # variables var velocity = Vector2() var dropping := true var turningright := false var turningleft := false var nearest_food : Node var hungry := true var canseefood := false func _ready() -> void: randomize() # generate random seed to randomize each fish # SETTINGS var swim_speed_variance = 0.25 # CONNECT SIGNAL var edgeL = get_tree().get_root().find_node("TankEdgeL", true, false) edgeL.connect("edgehit", self, "handle_edgehit") var edgeR = get_tree().get_root().find_node("TankEdgeR", true, false) edgeR.connect("edgehit", self, "handle_edgehit") # ACTUAL CODE swim_speed = swim_speed * (1 + rand_range(-swim_speed_variance, swim_speed_variance)) # give the fish a random speed in range $DropTimer.wait_time = rand_range(0.5,2) # drop the fish to a random depth in range $AnimatedSprite.play() if randf() >= 0.5: $AnimatedSprite.animation = "left" else: $AnimatedSprite.animation = "right" func handle_edgehit(action: String): if action == "turn right": turningright = true elif action == "turn left": turningleft = true func _process(delta: float) -> void: #print(velocity) if dropping: velocity = Vector2(0,175) if get_tree().get_nodes_in_group("Food").size() > 0: canseefood = true nearest_food = find_nearest_food() # $Line2D.set_point_position(1, nearest_food.get_global_position() - get_global_position()) else: canseefood = false if hungry and canseefood: velocity = position.direction_to(nearest_food.get_global_position()) * 100 # idk why i need to x100 this but i do # THIS ALWAYS CONTROLS WHERE THE FISH IS MOVING move_and_slide(velocity) # func _on_Vision_body_entered(body: Node) -> void: # if body.is_in_group("Food"): # print("Yummy! I see some food!") # var hungry: bool # var angle_toward_food: float # if hungry: # angle_toward_food = get_angle(me, nearest_food) # natural_movement_angle = random(-15, 15) # natural_movement = angle_toward_food + natural_movement_angle # # random_speed = random(0, 5) + 3 # # move_and_slide(vector2(random_speed, natural_movement)) func _on_SwimTimer_timeout() -> void: if not canseefood: var direction = randf() var swimdirection = 0 if direction >= .75 or turningright: swimdirection = 1 $AnimatedSprite.animation = "right" turningright = false if direction <= .25 or turningleft: swimdirection = -1 $AnimatedSprite.animation = "left" turningleft = false velocity = Vector2(1, rand_range(-swim_up_down_variance, swim_up_down_variance)) velocity = velocity * swim_speed * swimdirection # rotate correctly for both directions & sprite animations $AnimatedSprite.rotation = Vector2(0,0).angle_to_point(velocity) + deg2rad(90 * (swimdirection + 1) * swimdirection) func _on_DropTimer_timeout(): dropping = false # identify and record the nearest food func find_nearest_food() -> Node: var all_food = get_tree().get_nodes_in_group("Food") if get_tree().get_nodes_in_group("Food").size() > 0: nearest_food = all_food[0] # assume the first food is closest # look through spawn nodes to see if any are closer for food in all_food: if food.global_position.distance_to(global_position) < nearest_food.global_position.distance_to(global_position): nearest_food = food return nearest_food else: return null func _on_FishHitbox_body_entered(body: Node) -> void: if body.is_in_group("Food") and hungry: body.queue_free() hungry = false $HungerTimer.start() func _on_HungerTimer_timeout(): if hungry: queue_free() else: hungry = true