fish_logic.gd 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. export(String) var fish_name = ""
  10. export(String) var fish_species = "basic_fish"
  11. export(float) var fish_hunger = 0.0
  12. export(float) var fish_ammonia_stress = 0.0
  13. var global = null
  14. func _ready():
  15. set_child_node_properties()
  16. set_process(true)
  17. global = get_node("/root/Global")
  18. fish_hunger = get_fish_species_property("max_hunger")
  19. fish_ammonia_stress = get_fish_species_property("ammonia_tolerance")
  20. func _process(delta):
  21. set_position(next_wander(delta))
  22. func next_wander(delta):
  23. var current_pos = get_position()
  24. if (current_pos.x < MIN_X and trajectory.x < 0 or current_pos.x > MAX_X and trajectory.x > 0):
  25. speed_derivative = -100
  26. if current_pos.y < MIN_Y:
  27. trajectory.y = 1
  28. elif current_pos.y > MAX_Y:
  29. trajectory.y = -1
  30. trajectory = slightly_random(trajectory, delta)
  31. speed_derivative += delta * rand_range(-100.0, 100.0)
  32. speed += delta * speed_derivative
  33. if (speed_derivative < -200.0):
  34. speed_derivative = -50.0
  35. elif (speed_derivative > 200.0):
  36. speed_derivative = 0.0
  37. if (speed < -0.2):
  38. speed = -speed * 2
  39. trajectory = -trajectory
  40. speed_derivative = 50.0
  41. if (speed > 110):
  42. speed = 100.0
  43. speed_derivative = 0.0
  44. if trajectory.x < -0.5:
  45. get_node("sprite").set_flip_h(false)
  46. elif trajectory.x > 0.5:
  47. get_node("sprite").set_flip_h(true)
  48. return get_position() + delta * trajectory * speed
  49. func slightly_random(vec, delta):
  50. var x = (vec.x + delta * rand_range(-2.0, 2.0)) * 1.005
  51. var y = vec.y + delta * rand_range(-2.0, 2.0)
  52. var V = Vector2(x, y)
  53. return V.normalized()
  54. func set_child_node_properties():
  55. get_node("nametag").text = fish_name
  56. get_node("canvas/fish_menu/rename_input").text = fish_name
  57. func update_fish_name(new_name):
  58. fish_name = new_name
  59. set_child_node_properties()
  60. func get_fish_species_property(key):
  61. return global.get_fish_species(fish_species)[key]
  62. func calculate_temperature_health(x):
  63. if (x <= 5.0):
  64. return 1
  65. else:
  66. return 1 - min(1, (x - 5) * (x - 5) / 10)
  67. func calculate_ph_health(x):
  68. if (x <= 0.5):
  69. return 1
  70. else:
  71. return 1 - min(1, (x - 0.5) * (x - 0.5))
  72. func calculate_salinity_health(x):
  73. if (x <= 0.05):
  74. return 1
  75. else:
  76. return 1 - min(1, (x - 0.05) * (x - 0.05) * 10)
  77. func get_general_fish_health():
  78. var hunger_pct = fish_hunger / get_fish_species_property("max_hunger")
  79. var ammonia_pct = fish_ammonia_stress / get_fish_species_property("ammonia_tolerance")
  80. var temperature_diff = abs(get_node("..").aquarium_temperature - get_fish_species_property("preferred_temperature"))
  81. var ph_diff = abs(get_node("..").aquarium_ph - get_fish_species_property("preferred_ph"))
  82. var salinity_diff = abs(get_node("..").aquarium_salinity - get_fish_species_property("preferred_salinity"))
  83. var temperature_health = calculate_temperature_health(temperature_diff)
  84. var ph_health = calculate_ph_health(ph_diff)
  85. var salinity_health = calculate_salinity_health(salinity_diff)
  86. return hunger_pct * ammonia_pct * temperature_health * ph_health * salinity_health