extends RigidBody2D export var spin_speed = -1 export var move_speed = 60 export var power_magnitude = 10 var start_position var is_dragging = false var waiting_for_shoot = true var ready_to_shoot var ready_to_hold var power var spin_rotation = 0 var has_exited var exit_position func _ready(): start_position = position func toggle_mode(): if waiting_for_shoot: waiting_for_shoot = false shoot() else: # Adds offset of ball's rotation to sprite AFTER the entire object has rotated from physics. # Done to keep appearance of same rotation to add consistency when resetting the ball. $Sprite.rotation += rotation global_variables.has_scored = false waiting_for_shoot = true set_use_custom_integrator(waiting_for_shoot) func shoot(): apply_impulse(Vector2(2, 2), Vector2(power)) # Override the default physics when we want to manually set the position and rotation. func _integrate_forces(state): if waiting_for_shoot: state.transform = Transform2D(0, start_position) state.linear_velocity = Vector2() state.angular_velocity = 0 # Temporary testing controls func _process(delta): if waiting_for_shoot: $Sprite.rotation += spin_speed * delta $GhostSprite.rotation += spin_speed * delta if Input.is_action_pressed("ui_left"): start_position.x -= move_speed * delta if Input.is_action_pressed("ui_right"): start_position.x += move_speed * delta start_position.x = clamp(start_position.x, 0, get_viewport_rect().size.x) func _input(event): if event.is_action_pressed("shoot") and ready_to_hold and waiting_for_shoot: is_dragging = true $GhostSprite.visible = true $Sprite.position = event.position - start_position $GhostSprite.rotation = $Sprite.rotation if !has_exited and is_dragging and event is InputEventMouseMotion: $Sprite.position = event.position - start_position if event.is_action_released("shoot"): if !waiting_for_shoot: toggle_mode() if is_dragging and ready_to_shoot: is_dragging = false $GhostSprite.visible = false $Sprite.position = Vector2() var basketball_position if has_exited: basketball_position = exit_position else: basketball_position = event.position power = (start_position - basketball_position) * power_magnitude toggle_mode() func on_OuterShape_mouse_entered(): ready_to_shoot = true has_exited = false func _on_OuterShape_mouse_exited(): if is_dragging: exit_position = get_global_mouse_position() $Sprite.position = get_local_mouse_position() has_exited = true else: ready_to_shoot = false func _on_InnerShape_mouse_entered(): ready_to_hold = true func _on_InnerShape_mouse_exited(): ready_to_hold = false func position_changer(): var clamp_minimum; var clamp_maximum clamp_minimum = $OuterShape/CollisionShape2D.shape.radius + 0 clamp_maximum = global_variables.hoop_position - 40 if global_variables.score < 5: start_position.x -= 20 start_position.x = clamp(start_position.x, clamp_minimum, clamp_maximum) else: start_position.x = rand_range(clamp_minimum, clamp_maximum) func _on_Hoop_score(): position_changer()