Comets.gd/scripts/comet.gd

69 lines
1.8 KiB
GDScript

extends Area2D
signal exploded (new_size: SIZE, current_position: Vector2)
enum SIZE {SMALL, MEDIUM, LARGE}
@export var current_size : SIZE = SIZE.LARGE
var size_data = {
SIZE.SMALL : {"Scale": 0.5, "Speed-Multi": 1.5},
SIZE.MEDIUM : {"Scale": 1.0, "Speed-Multi": 1.0},
SIZE.LARGE : {"Scale": 2.5, "Speed-Multi": 0.5}
}
var texture_options = [
"res://assets/img/comet.png",
"res://assets/img/comet2.png",
"res://assets/img/comet3.png",
"res://assets/img/comet4.png"
]
var movement_speed : int
var movement_speed_min : int = 50
var movement_speed_max : int = 250
var movement_direciton := Vector2.UP
var rotation_speed : int
var rotaion_direction : int
func _ready() -> void:
$Sprite2D.texture = load(texture_options.pick_random())
movement_speed = randi_range(movement_speed_min, movement_speed_max)
movement_speed *= size_data[current_size]["Speed-Multi"]
rotaion_direction = randi_range(0, 1)
if rotaion_direction < 1:
rotaion_direction = -1
rotation_speed = randi_range(50, 150)
rotate(randi_range(0, 360))
self.scale *= size_data[current_size]["Scale"]
func _physics_process(delta: float) -> void:
$CollisionPolygon2D.rotate(deg_to_rad(rotaion_direction * rotation_speed * delta))
$Sprite2D.rotate(deg_to_rad(rotaion_direction * rotation_speed * delta))
position += movement_direciton.rotated(rotation) * movement_speed * delta
# Screen Wrap
var viewport_size := get_viewport_rect().size
position.x = wrap(position.x, 0, viewport_size.x)
position.y = wrap(position.y, 0,viewport_size.y)
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("ship"):
body.crash()
func _on_area_entered(area: Area2D) -> void:
if area.is_in_group("bullet"):
area.queue_free()
$AnimationPlayer.play("explode")
func explode() -> void:
exploded.emit(current_size - 1, global_position)
queue_free()