fish_logic.gd 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. extends Node2D
  2. const MIN_X = 0
  3. const MIN_Y = 0
  4. const MAX_X = 800
  5. const MAX_Y = 600
  6. var trajectory = Vector2(0,1)
  7. var speed = 50.0
  8. var speed_derivative = 0.0
  9. func _ready():
  10. set_process(true)
  11. func _process(delta):
  12. set_position(next_wander(delta))
  13. func next_wander(delta):
  14. var current_pos = get_position()
  15. if (current_pos.x < MIN_X and trajectory.x < 0 or current_pos.x > MAX_X and trajectory.x > 0):
  16. speed_derivative = -100
  17. if current_pos.y < MIN_Y:
  18. trajectory.y = 1
  19. elif current_pos.y > MAX_Y:
  20. trajectory.y = -1
  21. trajectory = slightly_random(trajectory, delta)
  22. speed_derivative += delta * rand_range(-100.0, 100.0)
  23. speed += delta * speed_derivative
  24. if (speed_derivative < -200.0):
  25. speed_derivative = -50.0
  26. elif (speed_derivative > 200.0):
  27. speed_derivative = 0.0
  28. if (speed < -0.2):
  29. speed = -speed * 2
  30. trajectory = -trajectory
  31. speed_derivative = 50.0
  32. if (speed > 110):
  33. speed = 100.0
  34. speed_derivative = 0.0
  35. if trajectory.x < -0.5:
  36. get_node("sprite").set_flip_h(false)
  37. elif trajectory.x > 0.5:
  38. get_node("sprite").set_flip_h(true)
  39. return get_position() + delta * trajectory * speed
  40. func slightly_random(vec, delta):
  41. var x = (vec.x + delta * rand_range(-2.0, 2.0)) * 1.005
  42. var y = vec.y + delta * rand_range(-2.0, 2.0)
  43. var V = Vector2(x, y)
  44. return V.normalized()