78 lines
1.9 KiB
GDScript
78 lines
1.9 KiB
GDScript
extends RigidBody2D
|
|
|
|
var SPEED : float
|
|
var direction : Vector2
|
|
var type:= 1
|
|
var is_in := false
|
|
var delay_dup := 0.0
|
|
|
|
@export var particles := load("res://scene/destroy.tscn")
|
|
|
|
@export var asteroidx1 : = load("res://scene/asteroidx1.tscn")
|
|
@export var asteroidx2 : = load("res://scene/asteroidx2.tscn")
|
|
@export var asteroidx3 : = load("res://scene/asteroidx3.tscn")
|
|
|
|
|
|
func _ready() -> void:
|
|
add_to_group("asteroid")
|
|
|
|
rotation = randf_range(0.0, 360.0)
|
|
|
|
func _process(delta: float) -> void:
|
|
|
|
if delay_dup < 1:
|
|
delay_dup += delta
|
|
linear_velocity = direction * SPEED
|
|
|
|
func split():
|
|
var packed_use: PackedScene
|
|
|
|
if type == 1:
|
|
var part = particles.instantiate()
|
|
part.global_position = global_position
|
|
get_parent().call_deferred("add_child", part)
|
|
queue_free()
|
|
return
|
|
elif type == 2:
|
|
packed_use = asteroidx1
|
|
elif type == 3:
|
|
packed_use = asteroidx2
|
|
elif type == 4:
|
|
packed_use = asteroidx3
|
|
|
|
var first = packed_use.instantiate()
|
|
var second = packed_use.instantiate()
|
|
var nor = direction.rotated(PI/2).normalized()
|
|
first.direction = direction
|
|
second.direction = direction
|
|
first.global_position = position + (2**type * nor)
|
|
first.type = type-1
|
|
first.SPEED = SPEED
|
|
second.SPEED = SPEED
|
|
second.global_position = position - (2**type * nor)
|
|
second.type = type-1
|
|
var parent = get_parent()
|
|
var part = particles.instantiate()
|
|
part.global_position = global_position
|
|
parent.call_deferred("add_child", first)
|
|
parent.call_deferred("add_child", second)
|
|
parent.call_deferred("add_child", part)
|
|
queue_free()
|
|
|
|
func _on_body_entered(body: Node) -> void:
|
|
if body.is_in_group("asteroid"):
|
|
get_parent()._add_collision(body, self)
|
|
elif body.is_in_group("shoot"):
|
|
body.destroy = true
|
|
body.queue_free()
|
|
GameController.emit_signal("point_gain", type)
|
|
split()
|
|
elif body is Ship and !body.is_inv():
|
|
body.is_currently_hit = true
|
|
#GameController.emit_signal("player_hit")
|
|
|
|
|
|
func _on_body_exited(body: Node) -> void:
|
|
if body is Ship:
|
|
body.is_currently_hit = false
|