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.

191 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_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 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 get_current_energy():
  71. return current_energy
  72. func _set_color_collision_mask(id):
  73. set_collision_mask_bit(10, true)
  74. set_collision_mask_bit(11, true)
  75. set_collision_mask_bit(12, true)
  76. if id >= 1 and id <= 3:
  77. set_collision_mask_bit(9 + id, false)
  78. func _set_base_tangential_accel(v):
  79. base_tangential_accel = max(1.0, v)
  80. func _set_base_push_force(v):
  81. base_push_force = max(1.0, v)
  82. # Called when the node enters the scene tree for the first time.
  83. func _ready():
  84. set_process(true)
  85. set_physics_process(true)
  86. set_process_input(true)
  87. $Sprite.material.set_shader_param("rim_color", neutral_rim);
  88. $Particles.process_material.color = neutral_rim
  89. func _input(event):
  90. if Input.is_action_just_pressed("ButtonA"):
  91. mouse_down = true
  92. elif Input.is_action_just_released("ButtonA"):
  93. mouse_down = false
  94. if current_energy >= push_energy_cost:
  95. current_energy -= push_energy_cost
  96. push = true
  97. func _physics_process(delta):
  98. mouse_position = get_global_mouse_position()
  99. if mouse_down:
  100. return # We don't follow the mouse or move the squiq when mouse is down.
  101. if push:
  102. push = false
  103. var v_direction = (position - mouse_position).normalized()
  104. apply_central_impulse(v_direction * v_direction.length() * get_push_force())
  105. elif in_air:
  106. air_time += delta
  107. else:
  108. var distance = clamp(mouse_position.x - position.x, -max_mouse_distance, max_mouse_distance)
  109. var dpercent = distance / max_mouse_distance
  110. if !$Mood.is_content() and abs(distance) > get_body_radius():
  111. var v_horizontal = Physics2DServer.area_get_param(get_world_2d().get_space(), Physics2DServer.AREA_PARAM_GRAVITY_VECTOR).rotated(deg2rad(-90))
  112. $Particles.process_material.tangential_accel = dpercent * get_tangential_acceleration();
  113. apply_central_impulse(v_horizontal * distance * $Mood.get_need() * delta)
  114. else:
  115. $Particles.process_material.tangential_accel = 0
  116. $Mood.shift_mood(delta, in_air, last_speed, (position - mouse_position).length())
  117. last_speed = linear_velocity.length()
  118. current_energy += energy_regen_per_sec * delta
  119. if current_energy > max_energy:
  120. current_energy = max_energy
  121. func _process(delta):
  122. update()
  123. func _draw():
  124. if mouse_down:
  125. var distance = min(position.distance_to(mouse_position), max_mouse_distance)
  126. var v_direction = (position - mouse_position).normalized().rotated(-rotation)
  127. var v_start = v_direction * get_body_radius()
  128. var v_end = v_direction * distance
  129. var v_tipA = v_end - (v_direction.rotated(deg2rad(30)) * min(distance, 16))
  130. var v_tipB = v_end - (v_direction.rotated(-deg2rad(30)) * min(distance, 16))
  131. var c_line = Color(0.25, 1.0, 0.0)
  132. draw_line(v_start, v_end, c_line)
  133. draw_line(v_end, v_tipA, c_line)
  134. draw_line(v_end, v_tipB, c_line)
  135. func _on_Player_body_entered(body):
  136. in_air = false
  137. #print(linear_velocity)
  138. if air_time > COLLISION_TIMEDT_THRESHOLD:
  139. #var lvlen = linear_velocity.length()
  140. #print("Last Speed: ", last_speed)
  141. if last_speed >= COLLISION_MINOR_SPEED_THRESHOLD and last_speed < COLLISION_MAJOR_SPEED_THRESHOLD:
  142. $Mood.adjust_mood(0.1, 0.0, -0.1)
  143. $Camera/ScreenShake.start()
  144. elif last_speed >= COLLISION_MAJOR_SPEED_THRESHOLD:
  145. $Mood.adjust_mood(0.25, 0.0, -0.25)
  146. $Camera/ScreenShake.start(0.4, 15, 24)
  147. air_time = 0
  148. func _on_Player_body_exited(body):
  149. in_air = true