Godot Version
How to fix when i reset my animation after reaching >99% my star is invisible
I just showed how is working my animation.If anyone needs video i will show it, no problem.
extends Line2D
var drawing = false
var custom_points = []
onready var score_label = get_node("/root/Game/Control/Score") # Score label
onready var center_point = get_node("/root/Game/Control/Star") # Center point of the circle
onready var animation_player = get_node("/root/Game/Control/AnimationPlayer") # Animation player
onready var blackhole = get_node("/root/Game/Control/Blackhole")
onready var explosion = get_node("/root/Game/Control/Explosion")
var min_circle_points = 10 # Minimum number of points to consider a circle
var line_thickness = 10 # Initial line thickness
var start_angle = 0.0 # Start angle for circle drawing
var angle_covered = 0.0 # Total angle covered during drawing
var last_angle = 0.0 # Last angle position
var total_sign = 0.0 # Total movement direction
var error_change_direction = false # Error check state
var is_animation_playing = false # Track if animation is currently playing
func _ready():
self.width = line_thickness # Set the initial line thickness
self.default_color = Color(1, 1, 1) # Set the line color to white
if animation_player:
animation_player.connect("animation_finished", self, "_on_animation_finished")
else:
print("Error: AnimationPlayer node is not assigned")
func _input(event):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
drawing = true
error_change_direction = false # Reset error check
custom_points = [] # Clear previous points
start_angle = get_angle() # Save the start angle
angle_covered = 0.0 # Reset angle coverage
total_sign = 0.0 # Reset sign count
last_angle = start_angle # Initialize last angle
if is_animation_playing:
_reset_animation() # Reset the animation if it is playing
elif event.button_index == BUTTON_LEFT and not event.pressed:
drawing = false
_on_draw_finished()
if event is InputEventMouseMotion and drawing:
custom_points.append(event.position)
update()
_on_draw_in_progress()
func _draw():
if custom_points.size() > 1:
draw_polyline(custom_points, self.default_color, line_thickness) # Always draw the line white
func _on_draw_in_progress():
var perfection_score = calculate_perfection()
score_label.text = "%0.1f" % perfection_score
var current_angle = get_angle()
var angle_diff1 = fmod(current_angle + 360.0, 360.0) - fmod(last_angle + 360.0, 360.0)
var angle_diff2 = fmod(current_angle + 540.0, 360.0) - fmod(last_angle + 540.0, 360.0)
var sign_diff = 0
if abs(angle_diff1) <= abs(angle_diff2):
sign_diff = sign(angle_diff1)
else:
sign_diff = sign(angle_diff2)
total_sign += sign_diff
if abs(total_sign) < custom_points.size() * 0.75 and custom_points.size() > 3:
error_change_direction = true
drawing = false
_on_draw_finished()
var angle_diff = min(abs(angle_diff1), abs(angle_diff2))
angle_covered += sign_diff * angle_diff
last_angle = current_angle
if abs(angle_covered) >= 360.0:
drawing = false
_on_draw_finished()
func _on_draw_finished():
if error_change_direction or abs(angle_covered) < 340.0:
score_label.text = "It's not a circle"
custom_points = []
return
var perfection_score = calculate_perfection()
if perfection_score >= 50:
_play_explosion_animation() # Play explosion animation when score is 50 or more
explosion.visible = true
if is_near_complete_circle():
print("Completed a circle!")
custom_points = []
func is_near_complete_circle():
if custom_points.size() < min_circle_points:
return false
var center = Vector2()
for point in custom_points:
center += point
center /= custom_points.size()
var total_distance = 0.0
for point in custom_points:
total_distance += center.distance_to(point)
var average_radius = total_distance / custom_points.size()
var max_deviation = 0.0
for point in custom_points:
var distance = center.distance_to(point)
max_deviation = max(max_deviation, abs(distance - average_radius))
return max_deviation < 15
func calculate_perfection():
if custom_points.size() < 3:
return 0
var center = Vector2()
for point in custom_points:
center += point
center /= custom_points.size()
var total_distance = 0.0
for point in custom_points:
total_distance += center.distance_to(point)
var average_distance = total_distance / custom_points.size()
var deviation = 0.0
for point in custom_points:
deviation += abs(center.distance_to(point) - average_distance)
var average_deviation = deviation / custom_points.size()
return max(0, min(100, 100 - average_deviation))
func get_angle():
if not center_point:
print("Error: center_point is null")
return 0
var center_position = center_point.global_position
return center_position.angle_to_point(get_global_mouse_position()) * 180.0 / PI
func _play_explosion_animation():
if animation_player:
animation_player.play("Explosion")
is_animation_playing = true # Mark animation as playing
else:
print("Error: AnimationPlayer node is not assigned")
func _reset_animation():
if animation_player:
animation_player.stop() # Stop the current animation
is_animation_playing = false # Mark animation as stopped
blackhole.visible = false # Hide the blackhole
explosion.visible = false # Hide the explosion
center_point.visible = true # Show the center point
print("Animation reset.") # Debug print to verify reset
else:
print("Error: AnimationPlayer node is not assigned")
func _on_animation_finished(anim_name):
print("Animation finished: " + anim_name)
if anim_name == "Explosion":
blackhole.visible = true
explosion.visible = false
center_point.visible = false
animation_player.play("Blackhole")
elif anim_name == "Blackhole":
is_animation_playing = false # Reset animation playing state
blackhole.visible = true # Ensure blackhole is visible after animation ends
center_point.visible = false # Hide the center point
print("Blackhole animation finished