A game created for the Godot Wild Jam #21
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.7KB

  1. extends Node2D
  2. const ANIM_RATE_DEFAULT = 3.5
  3. const ANIM_RATE_AGGRESSIVE = 6.0
  4. const ANIM_RATE_CONTENT = 1.0
  5. const EXCITEMENT_MIN_SPEED = 150
  6. const EXCITEMENT_MAX_SPEED = 350
  7. # Mood = r: <aggression>, g: <neediness>, b: <contentment>
  8. # <aggression> Affects how strong a players push is and how strong the player's collision with the world will be.
  9. # <neediness> Affects how quickly and strongly the player will follow the mouse
  10. # <contentment> Affects how slowly mood color is shifted back to neutral and widens comfort distance
  11. var _mood = Color.black
  12. onready var _spr = get_parent().get_node("Sprite")
  13. onready var _body_radius = get_parent().get_node("CollisionShape2D").shape.radius
  14. func reset_mood():
  15. _mood = Color.black
  16. _spr.material.set_shader_param("cell_color", _mood);
  17. func adjust_mood(r, g, b):
  18. _mood.r = clamp(_mood.r + r, 0.0, 1.0)
  19. _mood.g = clamp(_mood.g + g, 0.0, 1.0)
  20. _mood.b = clamp(_mood.b + b, 0.0, 1.0)
  21. _spr.material.set_shader_param("cell_color", _mood);
  22. if is_aggressive():
  23. _spr.material.set_shader_param("cell_energy", ANIM_RATE_AGGRESSIVE)
  24. elif is_content():
  25. _spr.material.set_shader_param("cell_energy", ANIM_RATE_CONTENT)
  26. else:
  27. _spr.material.set_shader_param("cell_energy", ANIM_RATE_DEFAULT)
  28. func adjust_mood_from_color(c):
  29. if c.r != c.g or c.r != c.b: # Only adjust mood if not all values are the same.
  30. # Even if not all the same, there must be a single dominant color for mood to be adjusted.
  31. if c.r > c.g and c.r > c.b:
  32. adjust_mood(0.25, 0.0, 0.0)
  33. if c.g > c.r and c.g > c.b:
  34. adjust_mood(0.0, 0.25, 0.0)
  35. if c.b > c.r and c.b > c.g:
  36. adjust_mood(0.0, 0.0, 0.25)
  37. func is_aggressive():
  38. return _mood.r > _mood.g and _mood.r > _mood.b
  39. func is_needie():
  40. return _mood.g > _mood.r and _mood.g > _mood.b
  41. func is_content():
  42. return _mood.b > _mood.r and _mood.b > _mood.g
  43. func is_neutral():
  44. return not is_aggressive() and not is_needie() and not is_content()
  45. func get_aggression():
  46. return _mood.r
  47. func get_need():
  48. return _mood.g
  49. func get_contentment():
  50. return _mood.b
  51. func get_comfort_distance():
  52. return get_parent().get_node("CollisionShape2D").shape.radius * 2.1
  53. func get_mood_color():
  54. return _mood
  55. func _agg_shift(in_air, speed):
  56. var v = -0.1 # By default, aggression is always lowering
  57. if in_air and speed >= EXCITEMENT_MIN_SPEED and speed <= EXCITEMENT_MAX_SPEED:
  58. v = 0.1 # If in the air, going at an "exciting" speed, aggression rises!
  59. elif is_aggressive():
  60. v = -0.05 # Otherwise, if we're dominantly aggressive, slow aggression cooldown.
  61. if speed > 1.0 and _mood.r * 0.5 > _mood.b and _mood.r * 0.5 > _mood.g:
  62. v = 0.0 # But, if we're moving and our aggression is more than twice the other emotions, don't cooldown at all.
  63. return v
  64. func _need_shift(in_air, distance):
  65. var v = -0.1
  66. if in_air:
  67. # Decrease neediness faster while in air, as "player" is giving attension (in the air) or
  68. # otherwise excited!!
  69. v = -0.25
  70. else:
  71. var cdist = get_comfort_distance()
  72. if distance > cdist:
  73. v = 0.1
  74. if distance > cdist * 8.0:
  75. v = 0.25
  76. return v
  77. func _con_shift(in_air, speed, distance):
  78. var v = -0.1
  79. if in_air and speed < EXCITEMENT_MIN_SPEED:
  80. v = 0.1
  81. if is_aggressive(): # Assuming aggression hasn't taken over...
  82. v = 0.05 # Levitating in the air is nice!
  83. else:
  84. var cdist = get_comfort_distance()
  85. if distance <= cdist:
  86. v = 0.1 # It's nice when the player is nearby
  87. if is_needie():
  88. v = 0.05 # My neediness is slowing my contentment.
  89. elif is_aggressive():
  90. v = -0.25 # If aggressive, reduce contentment a lot.
  91. return v
  92. func shift_mood(delta, in_air, speed, distance):
  93. adjust_mood(
  94. _agg_shift(in_air, speed) * delta,
  95. _need_shift(in_air, distance) * delta,
  96. _con_shift(in_air, speed, distance) * delta
  97. )