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.

101 lines
2.9KB

  1. extends RigidBody2D
  2. export var max_distance = 64
  3. export var max_tangential_accel = 32
  4. export var max_travel_time = 2.0
  5. export var push_force = 128
  6. export var collision_minor_speed_threshold = 8
  7. export var collision_major_speed_threshold = 32
  8. export var collision_timedt_threshold = 0.1
  9. var body_radius = 0
  10. var mouse_position = Vector2.ZERO
  11. var mouse_down = false
  12. var push = false
  13. var traveling = 0;
  14. var in_air = true
  15. var air_time = 0
  16. var last_speed = 0
  17. # Called when the node enters the scene tree for the first time.
  18. func _ready():
  19. set_process(true)
  20. set_physics_process(true)
  21. set_process_input(true)
  22. body_radius = $CollisionShape2D.shape.radius
  23. $Particles.process_material.color = $Sprite.material.get_shader_param("rim_color");
  24. func _input(event):
  25. if event is InputEventMouseMotion:
  26. mouse_position = get_global_mouse_position()
  27. if Input.is_action_just_pressed("ButtonA"):
  28. mouse_down = true
  29. elif Input.is_action_just_released("ButtonA"):
  30. mouse_down = false
  31. push = true
  32. func _physics_process(delta):
  33. if mouse_down:
  34. return # We don't follow the mouse or move the squiq when mouse is down.
  35. if push:
  36. push = false
  37. traveling = max_travel_time
  38. var v_direction = (position - mouse_position).normalized()
  39. apply_central_impulse(v_direction * abs(v_direction.length()) * push_force)
  40. elif in_air:
  41. air_time += delta
  42. else:
  43. var distance = clamp(mouse_position.x - position.x, -max_distance, max_distance)
  44. var dpercent = distance / max_distance
  45. if abs(distance) > body_radius:
  46. var v_horizontal = Physics2DServer.area_get_param(get_world_2d().get_space(), Physics2DServer.AREA_PARAM_GRAVITY_VECTOR).rotated(deg2rad(-90))
  47. $Particles.process_material.tangential_accel = dpercent * max_tangential_accel;
  48. apply_central_impulse(v_horizontal * distance * delta)
  49. else:
  50. $Particles.process_material.tangential_accel = 0
  51. last_speed = linear_velocity.length()
  52. #print(last_speed)
  53. func _process(delta):
  54. update()
  55. func _draw():
  56. if mouse_down:
  57. var distance = min(position.distance_to(mouse_position), max_distance)
  58. var v_direction = (position - mouse_position).normalized().rotated(-rotation)
  59. var v_start = v_direction * body_radius
  60. var v_end = v_direction * distance
  61. var v_tipA = v_end - (v_direction.rotated(deg2rad(30)) * min(distance, 16))
  62. var v_tipB = v_end - (v_direction.rotated(-deg2rad(30)) * min(distance, 16))
  63. var c_line = Color(0.25, 1.0, 0.0)
  64. draw_line(v_start, v_end, c_line)
  65. draw_line(v_end, v_tipA, c_line)
  66. draw_line(v_end, v_tipB, c_line)
  67. func _on_Player_body_entered(body):
  68. in_air = false
  69. #print(linear_velocity)
  70. if air_time > collision_timedt_threshold:
  71. var lvlen = linear_velocity.length()
  72. if lvlen >= collision_minor_speed_threshold and lvlen < collision_major_speed_threshold:
  73. print("ooOF")
  74. elif lvlen >= collision_major_speed_threshold:
  75. print("Oooooch!")
  76. air_time = 0
  77. func _on_Player_body_exited(body):
  78. in_air = true
  79. print("I'm flying!")