init
This commit is contained in:
60
asteroid.gd
Normal file
60
asteroid.gd
Normal file
@@ -0,0 +1,60 @@
|
||||
extends RigidBody2D
|
||||
|
||||
var SPEED : float
|
||||
var direction : Vector2
|
||||
var type:= 1
|
||||
|
||||
@export var particles := load("res://destroy.tscn")
|
||||
|
||||
@export var asteroidx1 : = load("res://asteroidx1.tscn")
|
||||
@export var asteroidx2 : = load("res://asteroidx2.tscn")
|
||||
@export var asteroidx3 : = load("res://asteroidx3.tscn")
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
add_to_group("asteroid")
|
||||
SPEED = randf() / 50 +50
|
||||
rotation = randf_range(0.0, 360.0)
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
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
|
||||
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().emit_signal("add_collision", body, self)
|
||||
if body.is_in_group("shoot"):
|
||||
body.queue_free()
|
||||
split()
|
||||
Reference in New Issue
Block a user