week3 prep includes bullet pool, finite state machine, sprites and animation
This commit is contained in:
1 parent
60bde97dba
commit
28d6c23f44
12 files changed
+428
-9
No files matched your search
@@ -2,14 +2,19 @@ class_name SceneManager extends Node2D
|
||||
|
||||
@onready var triggers: Node2D = $"../Triggers"
|
||||
@onready var game: GameController = $".."
|
||||
@onready var bullet: Bullet = $"../Bullet"
|
||||
@onready var player: Player = $"../Player"
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
|
||||
var bulletArray:Array[Bullet] = []
|
||||
var totalAllowedBullets:int = 7
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
buildLevel()
|
||||
game.teleportSignal.connect(teleport)
|
||||
bullet.bulletDamageSignal.connect(game._on_bullet_hit)
|
||||
|
||||
game.teleportSignal.connect(teleport)
|
||||
game.destroySignal.connect(destroy)
|
||||
player.shootSignal.connect(makeBullet)
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
@@ -29,3 +34,38 @@ func teleport(body, _bodyposition=null)->void:
|
||||
var random_y = randf_range(0, viewport_size.y)
|
||||
var newPosition = Vector2(random_x, random_y)
|
||||
body.teleport_to(newPosition)
|
||||
|
||||
func bulletFactory()->Bullet:
|
||||
var myBullet:Bullet
|
||||
#how many bullets have been produced?
|
||||
if bulletArray.size() <= totalAllowedBullets:
|
||||
#make a new bullet
|
||||
myBullet = bullet.instantiate()
|
||||
add_sibling(myBullet)
|
||||
if not myBullet.bulletDamageSignal.is_connected(game._on_bullet_hit):
|
||||
myBullet.bulletDamageSignal.connect(game._on_bullet_hit)
|
||||
else:
|
||||
myBullet = bulletArray.pop_back()
|
||||
|
||||
bulletArray.push_front(myBullet)
|
||||
return myBullet
|
||||
func makeBullet(spawnPosition, speed)->void:
|
||||
print("make bullet")
|
||||
var myBullet:Bullet = bulletFactory()
|
||||
myBullet.transform = spawnPosition
|
||||
myBullet.setSpeed(speed)
|
||||
myBullet.set_process(true)
|
||||
|
||||
func stash(bullet:Bullet)->void:
|
||||
#hide it off-screen, remove from game loop
|
||||
var stashPosition:Vector2 = Vector2(-100,-100)
|
||||
bullet.position = stashPosition
|
||||
bullet.setSpeed(0)
|
||||
bullet.set_process(false)
|
||||
|
||||
func destroy(body)->void:
|
||||
if body is Crate:
|
||||
body.queue_free()
|
||||
if body is Bullet:
|
||||
#stash the bullet - save memory!
|
||||
stash(body)
|
||||
Reference in new issue
Block a user