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.

186 lines
5.4KB

  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_tangential_acceleration():
  43. return base_tangential_accel * $Mood.get_need()
  44. func get_push_force():
  45. var dist = (position - mouse_position).length() - get_body_radius()
  46. if dist <= 0.0:
  47. return 0.0
  48. var push = base_push_force * (dist / (max_mouse_distance))
  49. var agg = $Mood.get_aggression()
  50. var con = $Mood.get_contentment()
  51. if agg > con:
  52. if agg > 0.1 and agg < 0.5:
  53. push *= 1.25
  54. elif agg >= 0.5:
  55. push *= 1.5
  56. elif $Mood.is_aggressive():
  57. push *= 2
  58. elif con > agg:
  59. if con > 0.1 and con < 0.5:
  60. push *= 0.75
  61. elif con >= 0.5:
  62. push *= 0.5
  63. return push
  64. func get_body_radius():
  65. return $CollisionShape2D.shape.radius
  66. func get_mood():
  67. return $Mood
  68. func _set_color_collision_mask(id):
  69. set_collision_mask_bit(10, true)
  70. set_collision_mask_bit(11, true)
  71. set_collision_mask_bit(12, true)
  72. if id >= 1 and id <= 3:
  73. set_collision_mask_bit(9 + id, false)
  74. func _set_base_tangential_accel(v):
  75. base_tangential_accel = max(1.0, v)
  76. func _set_base_push_force(v):
  77. base_push_force = max(1.0, v)
  78. # Called when the node enters the scene tree for the first time.
  79. func _ready():
  80. set_process(true)
  81. set_physics_process(true)
  82. set_process_input(true)
  83. $Sprite.material.set_shader_param("rim_color", neutral_rim);
  84. $Particles.process_material.color = neutral_rim
  85. func _input(event):
  86. if Input.is_action_just_pressed("ButtonA"):
  87. mouse_down = true
  88. elif Input.is_action_just_released("ButtonA"):
  89. mouse_down = false
  90. if current_stamina >= push_stamina_cost:
  91. current_stamina -= push_stamina_cost
  92. push = true
  93. func _physics_process(delta):
  94. mouse_position = get_global_mouse_position()
  95. if mouse_down:
  96. return # We don't follow the mouse or move the squiq when mouse is down.
  97. if push:
  98. push = false
  99. var v_direction = (position - mouse_position).normalized()
  100. apply_central_impulse(v_direction * v_direction.length() * get_push_force())
  101. elif in_air:
  102. air_time += delta
  103. else:
  104. var distance = clamp(mouse_position.x - position.x, -max_mouse_distance, max_mouse_distance)
  105. var dpercent = distance / max_mouse_distance
  106. if !$Mood.is_content() and abs(distance) > get_body_radius():
  107. var v_horizontal = Physics2DServer.area_get_param(get_world_2d().get_space(), Physics2DServer.AREA_PARAM_GRAVITY_VECTOR).rotated(deg2rad(-90))
  108. $Particles.process_material.tangential_accel = dpercent * get_tangential_acceleration();
  109. apply_central_impulse(v_horizontal * distance * $Mood.get_need() * delta)
  110. else:
  111. $Particles.process_material.tangential_accel = 0
  112. $Mood.shift_mood(delta, in_air, last_speed, (position - mouse_position).length())
  113. last_speed = linear_velocity.length()
  114. current_stamina += stamina_regen_per_sec * delta
  115. if current_stamina > max_stamina:
  116. current_stamina = max_stamina
  117. func _process(delta):
  118. update()
  119. func _draw():
  120. if mouse_down:
  121. var distance = min(position.distance_to(mouse_position), max_mouse_distance)
  122. var v_direction = (position - mouse_position).normalized().rotated(-rotation)
  123. var v_start = v_direction * get_body_radius()
  124. var v_end = v_direction * distance
  125. var v_tipA = v_end - (v_direction.rotated(deg2rad(30)) * min(distance, 16))
  126. var v_tipB = v_end - (v_direction.rotated(-deg2rad(30)) * min(distance, 16))
  127. var c_line = Color(0.25, 1.0, 0.0)
  128. draw_line(v_start, v_end, c_line)
  129. draw_line(v_end, v_tipA, c_line)
  130. draw_line(v_end, v_tipB, c_line)
  131. func _on_Player_body_entered(body):
  132. in_air = false
  133. #print(linear_velocity)
  134. if air_time > COLLISION_TIMEDT_THRESHOLD:
  135. #var lvlen = linear_velocity.length()
  136. #print("Last Speed: ", last_speed)
  137. if last_speed >= COLLISION_MINOR_SPEED_THRESHOLD and last_speed < COLLISION_MAJOR_SPEED_THRESHOLD:
  138. $Mood.adjust_mood(0.1, 0.0, -0.1)
  139. $Camera/ScreenShake.start()
  140. elif last_speed >= COLLISION_MAJOR_SPEED_THRESHOLD:
  141. $Mood.adjust_mood(0.25, 0.0, -0.25)
  142. $Camera/ScreenShake.start(0.4, 15, 24)
  143. air_time = 0
  144. func _on_Player_body_exited(body):
  145. in_air = true