extends RigidBody2D export var spin_speed = -1 export var move_speed = 60 export var power_magnitude = 10 export var drag_radius = 30 var start_position: Vector2 var is_dragging = false var waiting_for_shoot = true var mouse_over_ball = false func _ready(): start_position = position # This function toggles between dragging the ball for a shot and allowing the physics engine to move # the ball, setting any necessary properties along the way. func toggle_mode(): if waiting_for_shoot: waiting_for_shoot = false var shoot_power = -$Sprite.position * power_magnitude apply_impulse(Vector2(2, 2), shoot_power) is_dragging = false $GhostSprite.visible = false $Sprite.position = Vector2(0, 0) 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) # 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 func _process(delta): if waiting_for_shoot: $Sprite.rotation += spin_speed * delta $GhostSprite.rotation += spin_speed * delta func _input(event): if event.is_action_pressed("shoot") and mouse_over_ball and waiting_for_shoot: is_dragging = true $Sprite.position = event.position - start_position $GhostSprite.visible = true $GhostSprite.rotation = $Sprite.rotation if is_dragging and event is InputEventMouseMotion: $Sprite.position = event.position - start_position # If the mouse pointer is beyond a certain radius, don't move the sprite any further from # the start position while still keeping it at the same angle as the mouse pointer. var hypot = sqrt(pow($Sprite.position.x, 2) + pow($Sprite.position.y, 2)) if hypot > drag_radius: var angle = atan2($Sprite.position.y, $Sprite.position.x) $Sprite.position = Vector2(cos(angle), sin(angle)) * drag_radius if event.is_action_released("shoot"): if !waiting_for_shoot or is_dragging: toggle_mode() func _on_InnerShape_mouse_entered(): mouse_over_ball = true func _on_InnerShape_mouse_exited(): mouse_over_ball = false func position_changer(): var clamp_minimum = drag_radius + 0 var clamp_maximum = $"../Hoop".position.x - 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()