2026-06-23 01:04:00 +00:00
|
|
|
extends Node2D
|
|
|
|
|
@onready var game: Node2D = $".."
|
|
|
|
|
@onready var crates: Node2D = $"../Crates"
|
2026-06-29 20:57:59 +00:00
|
|
|
@onready var player: CharacterBody2D = $"../Player"
|
2026-06-23 01:04:00 +00:00
|
|
|
|
2026-06-29 20:57:59 +00:00
|
|
|
var bullet = preload("res://scenes/bullet.tscn")
|
|
|
|
|
var bulletArray:Array[Bullet] = []
|
|
|
|
|
var totalAllowedBullets:int = 7
|
2026-06-23 01:04:00 +00:00
|
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
|
func _ready() -> void:
|
2026-06-29 20:57:59 +00:00
|
|
|
#wire up view
|
|
|
|
|
player.forcePushSignal.connect(push)
|
|
|
|
|
player.shootSignal.connect(makeBullet)
|
|
|
|
|
#wire up gamecontroller
|
|
|
|
|
game.destroySignal.connect(destroy)
|
|
|
|
|
game.levelChangeSignal.connect(changeScene)
|
|
|
|
|
game.fanUpSignal.connect(push)
|
2026-06-29 21:38:15 +00:00
|
|
|
game.teleportSignal.connect(teleport)
|
2026-06-23 01:04:00 +00:00
|
|
|
buildLevel()
|
|
|
|
|
|
|
|
|
|
func buildLevel()->void:
|
2026-06-29 20:57:59 +00:00
|
|
|
updateCrates()
|
|
|
|
|
|
|
|
|
|
func updateCrates()->void:
|
|
|
|
|
var _crateTotal:int = 0
|
2026-06-23 01:04:00 +00:00
|
|
|
if crates:
|
|
|
|
|
for obj in crates.get_children():
|
|
|
|
|
if obj is Crate:
|
2026-06-29 20:57:59 +00:00
|
|
|
if not obj.tree_exited.is_connected(updateCrates):
|
|
|
|
|
obj.tree_exited.connect(updateCrates)
|
|
|
|
|
_crateTotal +=1
|
|
|
|
|
game.crateTotal(_crateTotal)
|
2026-06-23 01:04:00 +00:00
|
|
|
|
2026-06-29 20:57:59 +00:00
|
|
|
#applies impulse to rigidbodies
|
|
|
|
|
func push(body, direction)->void:
|
|
|
|
|
body.apply_central_impulse(direction)
|
|
|
|
|
|
2026-06-29 21:38:15 +00:00
|
|
|
func teleport(body, _bodyposition=null)->void:
|
|
|
|
|
print("teleport a body")
|
|
|
|
|
if body is Crate:
|
|
|
|
|
var viewport_size = get_viewport().get_visible_rect().size
|
|
|
|
|
var random_x = randf_range(0, viewport_size.x)
|
|
|
|
|
var random_y = randf_range(0, viewport_size.y)
|
|
|
|
|
var newPosition = Vector2(random_x,random_y)
|
|
|
|
|
body.teleport_to(newPosition)
|
|
|
|
|
|
2026-06-29 20:57:59 +00:00
|
|
|
func destroy(body)->void:
|
|
|
|
|
body.queue_free()
|
|
|
|
|
|
|
|
|
|
func changeScene(level)->void:
|
|
|
|
|
get_tree().call_deferred("change_scene_to_file",level)
|
|
|
|
|
|
|
|
|
|
func bulletFactory()->Bullet :
|
|
|
|
|
var myBullet:Bullet
|
|
|
|
|
#how many bullets have been made?
|
|
|
|
|
if bulletArray.size() <= totalAllowedBullets:
|
|
|
|
|
myBullet = bullet.instantiate()
|
|
|
|
|
if not myBullet.bulletDamageSignal.is_connected(game.bulletDamage):
|
|
|
|
|
myBullet.bulletDamageSignal.connect(game.bulletDamage)
|
|
|
|
|
add_sibling(myBullet)
|
|
|
|
|
else:
|
|
|
|
|
myBullet = bulletArray.pop_back()
|
|
|
|
|
|
|
|
|
|
bulletArray.push_front(myBullet)
|
|
|
|
|
return myBullet
|
|
|
|
|
|
|
|
|
|
func makeBullet(targetPosition:Transform2D, speed:int)->void:
|
|
|
|
|
var myBullet:Bullet = bulletFactory()
|
|
|
|
|
myBullet.transform = targetPosition
|
|
|
|
|
myBullet.setSpeed(speed)
|
|
|
|
|
myBullet.set_process(true)
|