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.

189 lines
5.5KB

  1. extends RigidBody2D
  2. const COLLISION_MINOR_SPEED_THRESHOLD = 150
  3. const COLLISION_MAJOR_SPEED_THRESHOLD = 400
  4. const COLLISION_TIMEDT_THRESHOLD = 0.1
  5. export var max_stamina = 1000
  6. export var stamina_regen_per_sec = 125
  7. # The base tangential acceleration that will be applied to the particles to give the
  8. # player the illusion it's trying to move on it's own. This is just effects things visually (ATM)
  9. export var base_tangential_accel = 32 setget _set_base_tangential_accel
  10. # The base force applied when being pushed.
  11. export var base_push_force = 64 setget _set_base_push_force
  12. export var push_stamina_cost = 100
  13. var current_stamina = max_stamina
  14. # The maximum distance from the player in which the mouse will adjust the push force.
  15. # at max_mouse_distance the full base_push_force <+ modifiers> will be applied.
  16. var max_mouse_distance = 128
  17. var neutral_rim = Color(0.9, 0.9, 0.9, 1.0)
  18. var mouse_position = Vector2.ZERO
  19. var mouse_down = false
  20. var push = false
  21. var in_air = true
  22. var air_time = 0
  23. var last_speed = 0
  24. func set_colors(prime, alt):
  25. $Sprite.material.set_shader_param("rim_color", alt);
  26. $Particles.process_material.color = alt
  27. if alt.r > alt.g and alt.r > alt.b:
  28. _set_color_collision_mask(1)
  29. elif alt.g > alt.r and alt.g > alt.b:
  30. _set_color_collision_mask(2)
  31. elif alt.b > alt.r and alt.b > alt.g:
  32. _set_color_collision_mask(3)
  33. else:
  34. # This should reset all of the collision masks to ON!
  35. _set_color_collision_mask(0)
  36. $Mood.adjust_mood_from_color(prime)
  37. func get_colors():
  38. return {
  39. "prime": $Sprite.material.get_shader_param("cell_color"),
  40. "alt": $Sprite.material.get_shader_param("rim_color")
  41. };
  42. func get_rim_color():
  43. return $Sprite.material.get_shader_param("rim_color")
  44. func get_tangential_acceleration():
  45. return base_tangential_accel * $Mood.get_need()
  46. func get_push_force():
  47. var dist = (position - mouse_position).length() - get_body_radius()
  48. if dist <= 0.0:
  49. return 0.0
  50. var push = base_push_force * (dist / (max_mouse_distance))
  51. var agg = $Mood.get_aggression()
  52. var con = $Mood.get_contentment()
  53. if agg > con:
  54. if agg > 0.1 and agg < 0.5:
  55. push *= 1.25
  56. elif agg >= 0.5:
  57. push *= 1.5
  58. elif $Mood.is_aggressive():
  59. push *= 2
  60. elif con > agg:
  61. if con > 0.1 and con < 0.5:
  62. push *= 0.75
  63. elif con >= 0.5:
  64. push *= 0.5
  65. return push
  66. func get_body_radius():
  67. return $CollisionShape2D.shape.radius
  68. func get_mood():
  69. return $Mood
  70. func _set_color_collision_mask(id):
  71. set_collision_mask_bit(10, true)
  72. set_collision_mask_bit(11, true)
  73. set_collision_mask_bit(12, true)
  74. if id >= 1 and id <= 3:
  75. set_collision_mask_bit(9 + id, false)
  76. func _set_base_tangential_accel(v):
  77. base_tangential_accel = max(1.0, v)
  78. func _set_base_push_force(v):
  79. base_push_force = max(1.0, v)
  80. # Called when the node enters the scene tree for the first time.
  81. func _ready():
  82. set_process(true)
  83. set_physics_process(true)
  84. set_process_input(true)
  85. $Sprite.material.set_shader_param("rim_color", neutral_rim);
  86. $Particles.process_material.color = neutral_rim
  87. func _input(event):
  88. if Input.is_action_just_pressed("ButtonA"):
  89. mouse_down = true
  90. elif Input.is_action_just_released("ButtonA"):
  91. mouse_down = false
  92. if current_stamina >= push_stamina_cost:
  93. current_stamina -= push_stamina_cost
  94. push = true
  95. func _physics_process(delta):
  96. mouse_position = get_global_mouse_position()
  97. if mouse_down:
  98. return # We don't follow the mouse or move the squiq when mouse is down.
  99. if push:
  100. push = false
  101. var v_direction = (position - mouse_position).normalized()
  102. apply_central_impulse(v_direction * v_direction.length() * get_push_force())
  103. elif in_air:
  104. air_time += delta
  105. else:
  106. var distance = clamp(mouse_position.x - position.x, -max_mouse_distance, max_mouse_distance)
  107. var dpercent = distance / max_mouse_distance
  108. if !$Mood.is_content() and abs(distance) > get_body_radius():
  109. var v_horizontal = Physics2DServer.area_get_param(get_world_2d().get_space(), Physics2DServer.AREA_PARAM_GRAVITY_VECTOR).rotated(deg2rad(-90))
  110. $Particles.process_material.tangential_accel = dpercent * get_tangential_acceleration();
  111. apply_central_impulse(v_horizontal * distance * $Mood.get_need() * delta)
  112. else:
  113. $Particles.process_material.tangential_accel = 0
  114. $Mood.shift_mood(delta, in_air, last_speed, (position - mouse_position).length())
  115. last_speed = linear_velocity.length()
  116. current_stamina += stamina_regen_per_sec * delta
  117. if current_stamina > max_stamina:
  118. current_stamina = max_stamina
  119. func _process(delta):
  120. update()
  121. func _draw():
  122. if mouse_down:
  123. var distance = min(position.distance_to(mouse_position), max_mouse_distance)
  124. var v_direction = (position - mouse_position).normalized().rotated(-rotation)
  125. var v_start = v_direction * get_body_radius()
  126. var v_end = v_direction * distance
  127. var v_tipA = v_end - (v_direction.rotated(deg2rad(30)) * min(distance, 16))
  128. var v_tipB = v_end - (v_direction.rotated(-deg2rad(30)) * min(distance, 16))
  129. var c_line = Color(0.25, 1.0, 0.0)
  130. draw_line(v_start, v_end, c_line)
  131. draw_line(v_end, v_tipA, c_line)
  132. draw_line(v_end, v_tipB, c_line)
  133. func _on_Player_body_entered(body):
  134. in_air = false
  135. #print(linear_velocity)
  136. if air_time > COLLISION_TIMEDT_THRESHOLD:
  137. #var lvlen = linear_velocity.length()
  138. #print("Last Speed: ", last_speed)
  139. if last_speed >= COLLISION_MINOR_SPEED_THRESHOLD and last_speed < COLLISION_MAJOR_SPEED_THRESHOLD:
  140. $Mood.adjust_mood(0.1, 0.0, -0.1)
  141. $Camera/ScreenShake.start()
  142. elif last_speed >= COLLISION_MAJOR_SPEED_THRESHOLD:
  143. $Mood.adjust_mood(0.25, 0.0, -0.25)
  144. $Camera/ScreenShake.start(0.4, 15, 24)
  145. air_time = 0
  146. func _on_Player_body_exited(body):
  147. in_air = true