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.

69 lines
2.0KB

  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. var body_radius = 0
  7. var mouse_position = Vector2.ZERO
  8. var mouse_down = false
  9. var push = false
  10. var traveling = 0;
  11. # Called when the node enters the scene tree for the first time.
  12. func _ready():
  13. set_process(true)
  14. set_physics_process(true)
  15. set_process_input(true)
  16. body_radius = $CollisionShape2D.shape.radius
  17. func _input(event):
  18. if event is InputEventMouseMotion:
  19. mouse_position = event.position
  20. if Input.is_action_just_pressed("ButtonA"):
  21. mouse_down = true
  22. elif Input.is_action_just_released("ButtonA"):
  23. mouse_down = false
  24. push = true
  25. func _physics_process(delta):
  26. if mouse_down:
  27. return # We don't follow the mouse or move the squiq when mouse is down.
  28. var distance = clamp(mouse_position.x - position.x, -max_distance, max_distance)
  29. var dpercent = abs(distance / max_distance)
  30. if push:
  31. push = false
  32. traveling = max_travel_time
  33. var v_direction = (position - mouse_position).normalized()
  34. apply_central_impulse(v_direction * dpercent * push_force)
  35. elif traveling > 0:
  36. traveling -= delta
  37. else:
  38. if abs(distance) > body_radius:
  39. $Particles.process_material.tangential_accel = dpercent * max_tangential_accel;
  40. apply_central_impulse(Vector2(distance * delta, 0))
  41. else:
  42. $Particles.process_material.tangential_accel = 0
  43. func _process(delta):
  44. update()
  45. func _draw():
  46. if mouse_down:
  47. var distance = min(position.distance_to(mouse_position), max_distance)
  48. var v_direction = (position - mouse_position).normalized().rotated(-rotation)
  49. var v_start = v_direction * body_radius
  50. var v_end = v_direction * distance
  51. var v_tipA = v_end - (v_direction.rotated(deg2rad(30)) * min(distance, 16))
  52. var v_tipB = v_end - (v_direction.rotated(-deg2rad(30)) * min(distance, 16))
  53. var c_line = Color(0.25, 1.0, 0.0)
  54. draw_line(v_start, v_end, c_line)
  55. draw_line(v_end, v_tipA, c_line)
  56. draw_line(v_end, v_tipB, c_line)