Comets.gd/scripts/comet.gd

69 lines
1.8 KiB
GDScript3
Raw Normal View History

2024-05-12 00:47:14 -04:00
extends Area2D
2024-05-12 22:40:13 -04:00
signal exploded (new_size: SIZE, current_position: Vector2)
2024-05-12 18:54:18 -04:00
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"
]
2024-05-12 18:54:18 -04:00
var movement_speed : int
2024-05-13 17:34:38 -04:00
var movement_speed_min : int = 50
var movement_speed_max : int = 250
2024-05-12 18:54:18 -04:00
var movement_direciton := Vector2.UP
2024-05-12 00:47:14 -04:00
var rotation_speed : int
var rotaion_direction : int
func _ready() -> void:
$Sprite2D.texture = load(texture_options.pick_random())
2024-05-12 18:54:18 -04:00
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"]
2024-05-12 00:47:14 -04:00
func _physics_process(delta: float) -> void:
2024-05-12 18:54:18 -04:00
$CollisionPolygon2D.rotate(deg_to_rad(rotaion_direction * rotation_speed * delta))
$Sprite2D.rotate(deg_to_rad(rotaion_direction * rotation_speed * delta))
2024-05-12 00:47:14 -04:00
2024-05-12 18:54:18 -04:00
position += movement_direciton.rotated(rotation) * movement_speed * delta
2024-05-12 01:03:18 -04:00
# 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)
2024-05-12 01:03:18 -04:00
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("ship"):
body.crash()
2024-05-12 18:54:18 -04:00
2024-05-12 22:40:13 -04:00
func _on_area_entered(area: Area2D) -> void:
if area.is_in_group("bullet"):
area.queue_free()
$AnimationPlayer.play("explode")
2024-05-12 18:54:18 -04:00
func explode() -> void:
2024-05-12 22:40:13 -04:00
exploded.emit(current_size - 1, global_position)
2024-05-12 18:54:18 -04:00
queue_free()