starcheese/asteroid.gd

44 lines
1.5 KiB
GDScript3
Raw Normal View History

2024-10-01 16:43:34 +02:00
extends StaticBody2D
@export var max_health: float = 100.0 # Maximum health for the asteroid
var current_health: float
var is_destroyed: bool = false # Track if the asteroid has already been destroyed
func _ready():
current_health = max_health
update_health_label() # Update the health display when the game starts
# Function to handle taking damage
func take_damage(damage: float):
if is_destroyed: # Prevent taking damage if already destroyed
return
current_health -= damage # Subtract the damage from current health
current_health = max(current_health, 0) # Ensure health doesn't drop below 0
update_health_label() # Update the label to reflect new health value
if current_health <= 0.0:
break_apart()
# Function to handle when the asteroid is destroyed
func break_apart():
is_destroyed = true # Mark the asteroid as destroyed
queue_free() # Remove the asteroid from the scene
print("Asteroid mined!")
# Load the Cheese scene
var cheese_scene = load("res://Cheese.tscn")
# Instance the cheese
var cheese_instance = cheese_scene.instantiate()
# Set the position where the cheese should spawn (at the asteroid's position)
cheese_instance.global_position = global_position + Vector2(randf() * 50 - 25, randf() * 50 - 25)
# Add the cheese instance to the scene
get_parent().add_child(cheese_instance)
# Function to update the health label display
func update_health_label():
$AsteroidHealthLabel.text = str(round(current_health)) + " / " + str(max_health)