first commit
This commit is contained in:
152
Actors/Fish.gd
Normal file
152
Actors/Fish.gd
Normal file
@@ -0,0 +1,152 @@
|
||||
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
|
80
Actors/Fish.tscn
Normal file
80
Actors/Fish.tscn
Normal file
@@ -0,0 +1,80 @@
|
||||
[gd_scene load_steps=7 format=2]
|
||||
|
||||
[ext_resource path="res://Actors/Fish.gd" type="Script" id=1]
|
||||
[ext_resource path="res://fish.png" type="Texture" id=2]
|
||||
[ext_resource path="res://fish2.png" type="Texture" id=3]
|
||||
|
||||
[sub_resource type="CapsuleShape2D" id=1]
|
||||
radius = 25.4989
|
||||
height = 59.0012
|
||||
|
||||
[sub_resource type="RectangleShape2D" id=2]
|
||||
extents = Vector2( 253, 195 )
|
||||
|
||||
[sub_resource type="SpriteFrames" id=3]
|
||||
animations = [ {
|
||||
"frames": [ ExtResource( 3 ) ],
|
||||
"loop": true,
|
||||
"name": "left",
|
||||
"speed": 5.0
|
||||
}, {
|
||||
"frames": [ ExtResource( 2 ) ],
|
||||
"loop": true,
|
||||
"name": "right",
|
||||
"speed": 5.0
|
||||
} ]
|
||||
|
||||
[node name="Fish" type="KinematicBody2D" groups=["Fish"]]
|
||||
position = Vector2( 56.4995, 307.501 )
|
||||
script = ExtResource( 1 )
|
||||
__meta__ = {
|
||||
"_edit_group_": true
|
||||
}
|
||||
swim_speed = 90.0
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
position = Vector2( -1, 0 )
|
||||
rotation = 1.5708
|
||||
shape = SubResource( 1 )
|
||||
|
||||
[node name="Vision" type="Area2D" parent="."]
|
||||
|
||||
[node name="VisionRadius" type="CollisionShape2D" parent="Vision"]
|
||||
shape = SubResource( 2 )
|
||||
|
||||
[node name="SwimTimer" type="Timer" parent="."]
|
||||
autostart = true
|
||||
|
||||
[node name="DropTimer" type="Timer" parent="."]
|
||||
wait_time = 2.0
|
||||
one_shot = true
|
||||
autostart = true
|
||||
|
||||
[node name="HungerTimer" type="Timer" parent="."]
|
||||
wait_time = 10.0
|
||||
autostart = true
|
||||
|
||||
[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
|
||||
frames = SubResource( 3 )
|
||||
animation = "left"
|
||||
|
||||
[node name="FishHitbox" type="Area2D" parent="."]
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="FishHitbox"]
|
||||
position = Vector2( -1, 0 )
|
||||
rotation = 1.5708
|
||||
shape = SubResource( 1 )
|
||||
|
||||
[node name="Line2D" type="Line2D" parent="."]
|
||||
points = PoolVector2Array( 0, 0, 0, 0 )
|
||||
|
||||
[node name="Timer2" type="Timer" parent="."]
|
||||
wait_time = 60.0
|
||||
one_shot = true
|
||||
autostart = true
|
||||
|
||||
[connection signal="body_entered" from="Vision" to="." method="_on_Vision_body_entered"]
|
||||
[connection signal="timeout" from="SwimTimer" to="." method="_on_SwimTimer_timeout"]
|
||||
[connection signal="timeout" from="DropTimer" to="." method="_on_DropTimer_timeout"]
|
||||
[connection signal="timeout" from="HungerTimer" to="." method="_on_HungerTimer_timeout"]
|
||||
[connection signal="body_entered" from="FishHitbox" to="." method="_on_FishHitbox_body_entered"]
|
Reference in New Issue
Block a user