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.

199 lines
5.8KB

  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_energy = 1000
  6. export var energy_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_energy_cost = 100
  13. var current_energy = max_energy
  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 reset():
  25. $Sprite.material.set_shader_param("rim_color", neutral_rim);
  26. $Particles.process_material.color = neutral_rim
  27. $Mood.reset_mood()
  28. current_energy = max_energy
  29. func set_colors(prime, alt):
  30. $Sprite.material.set_shader_param("rim_color", alt);
  31. $Particles.process_material.color = alt
  32. if alt.r > alt.g and alt.r > alt.b:
  33. _set_color_collision_mask(1)
  34. elif alt.g > alt.r and alt.g > alt.b:
  35. _set_color_collision_mask(2)
  36. elif alt.b > alt.r and alt.b > alt.g:
  37. _set_color_collision_mask(3)
  38. else:
  39. # This should reset all of the collision masks to ON!
  40. _set_color_collision_mask(0)
  41. $Mood.adjust_mood_from_color(prime)
  42. func get_colors():
  43. return {
  44. "prime": $Sprite.material.get_shader_param("cell_color"),
  45. "alt": $Sprite.material.get_shader_param("rim_color")
  46. };
  47. func get_rim_color():
  48. return $Sprite.material.get_shader_param("rim_color")
  49. func get_tangential_acceleration():
  50. return base_tangential_accel * $Mood.get_need()
  51. func get_push_force():
  52. var dist = (position - mouse_position).length() - get_body_radius()
  53. if dist <= 0.0:
  54. return 0.0
  55. var push = base_push_force * (dist / (max_mouse_distance))
  56. var agg = $Mood.get_aggression()
  57. var con = $Mood.get_contentment()
  58. if agg > con:
  59. if agg > 0.1 and agg < 0.5:
  60. push *= 1.25
  61. elif agg >= 0.5:
  62. push *= 1.5
  63. elif $Mood.is_aggressive():
  64. push *= 2
  65. elif con > agg:
  66. if con > 0.1 and con < 0.5:
  67. push *= 0.75
  68. elif con >= 0.5:
  69. push *= 0.5
  70. return push
  71. func get_body_radius():
  72. return $CollisionShape2D.shape.radius
  73. func get_mood():
  74. return $Mood
  75. func get_current_energy():
  76. return current_energy
  77. func _set_color_collision_mask(id):
  78. set_collision_mask_bit(10, true)
  79. set_collision_mask_bit(11, true)
  80. set_collision_mask_bit(12, true)
  81. if id >= 1 and id <= 3:
  82. set_collision_mask_bit(9 + id, false)
  83. func _set_base_tangential_accel(v):
  84. base_tangential_accel = max(1.0, v)
  85. func _set_base_push_force(v):
  86. base_push_force = max(1.0, v)
  87. # Called when the node enters the scene tree for the first time.
  88. func _ready():
  89. set_process(true)
  90. set_physics_process(true)
  91. set_process_input(true)
  92. $Sprite.material.set_shader_param("rim_color", neutral_rim);
  93. $Particles.process_material.color = neutral_rim
  94. func _input(event):
  95. if Input.is_action_just_pressed("ButtonA"):
  96. mouse_down = true
  97. elif Input.is_action_just_released("ButtonA"):
  98. mouse_down = false
  99. if current_energy >= push_energy_cost:
  100. current_energy -= push_energy_cost
  101. push = true
  102. func _physics_process(delta):
  103. mouse_position = get_global_mouse_position()
  104. if mouse_down:
  105. return # We don't follow the mouse or move the squiq when mouse is down.
  106. if push:
  107. push = false
  108. var v_direction = (position - mouse_position).normalized()
  109. apply_central_impulse(v_direction * v_direction.length() * get_push_force())
  110. elif in_air:
  111. air_time += delta
  112. else:
  113. var distance = clamp(mouse_position.x - position.x, -max_mouse_distance, max_mouse_distance)
  114. var dpercent = distance / max_mouse_distance
  115. if !$Mood.is_content() and abs(distance) > get_body_radius():
  116. var v_horizontal = Physics2DServer.area_get_param(get_world_2d().get_space(), Physics2DServer.AREA_PARAM_GRAVITY_VECTOR).rotated(deg2rad(-90))
  117. $Particles.process_material.tangential_accel = dpercent * get_tangential_acceleration();
  118. apply_central_impulse(v_horizontal * distance * $Mood.get_need() * delta)
  119. else:
  120. $Particles.process_material.tangential_accel = 0
  121. $Mood.shift_mood(delta, in_air, last_speed, (position - mouse_position).length())
  122. last_speed = linear_velocity.length()
  123. current_energy += energy_regen_per_sec * delta
  124. if current_energy > max_energy:
  125. current_energy = max_energy
  126. func _process(delta):
  127. update()
  128. func _draw():
  129. if mouse_down:
  130. var distance = min(position.distance_to(mouse_position), max_mouse_distance)
  131. var v_direction = (position - mouse_position).normalized().rotated(-rotation)
  132. var v_start = v_direction * get_body_radius()
  133. var v_end = v_direction * distance
  134. var v_tipA = v_end - (v_direction.rotated(deg2rad(30)) * min(distance, 16))
  135. var v_tipB = v_end - (v_direction.rotated(-deg2rad(30)) * min(distance, 16))
  136. var c_line = Color(0.25, 1.0, 0.0)
  137. draw_line(v_start, v_end, c_line)
  138. draw_line(v_end, v_tipA, c_line)
  139. draw_line(v_end, v_tipB, c_line)
  140. func _on_Player_body_entered(body):
  141. in_air = false
  142. #print(linear_velocity)
  143. if air_time > COLLISION_TIMEDT_THRESHOLD:
  144. #var lvlen = linear_velocity.length()
  145. #print("Last Speed: ", last_speed)
  146. if last_speed >= COLLISION_MINOR_SPEED_THRESHOLD and last_speed < COLLISION_MAJOR_SPEED_THRESHOLD:
  147. $Mood.adjust_mood(0.1, 0.0, -0.1)
  148. $Audio_SmallHit.play()
  149. $Camera/ScreenShake.start()
  150. elif last_speed >= COLLISION_MAJOR_SPEED_THRESHOLD:
  151. $Mood.adjust_mood(0.25, 0.0, -0.25)
  152. $Audio_BigHit.play()
  153. $Camera/ScreenShake.start(0.4, 15, 24)
  154. air_time = 0
  155. func _on_Player_body_exited(body):
  156. in_air = true