Skip to content

Commit

Permalink
Refactor to use packedscene instead of node type
Browse files Browse the repository at this point in the history
This refactors the projectile effect system to use packed scenes
instead of the base class node type ProjectileEffect.

This is due to the requirement for these scenes to be instantiated for
each projectile, not a shared resource. Without this change, effects
that modify the scene tree (such as HUGE LASER) do not work.
  • Loading branch information
Checkroth committed Aug 18, 2024
1 parent c91e481 commit 066cad0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
9 changes: 6 additions & 3 deletions projectiles/double_laser.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ class_name EffectDoubleLaser
extends ProjectileEffect

var Projectile : PackedScene = preload("res://units/projectile.tscn")

@export var laser_offset = Vector2(0, -50)
const is_double_laser = true

func check_not_double_laser(effect: ProjectileEffect) -> bool:
func check_not_double_laser(effect: PackedScene) -> bool:
# TODO:: Filter on type? This implementation is Bad
if "is_double_laser" in effect:
return not effect.is_double_laser
return true

func modify_creation(owner: Node, projectile_effects: Array[ProjectileEffect], transform: Transform2D):
# TODO:: Hacky? but const on the node does not work for packed scenes
return effect.resource_path != "res://projectiles/double_laser.tscn"

func modify_creation(owner: Node, projectile_effects: Array, transform: Transform2D):
var p = Projectile.instantiate()
# TODO:: Stacking double-lasers? would mean we only want to filter 1 out of this list.
var non_recursive_effects = projectile_effects.filter(check_not_double_laser)
Expand Down
14 changes: 11 additions & 3 deletions units/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const HORIZONTAL_BUFFER = 75
@export var Projectile : PackedScene
@onready var projectile_spawner = $ProjectileSpawner
var projectile_effects: Array[ProjectileEffect] = []
var DoubleLaserEffect : PackedScene = preload("res://projectiles/double_laser.tscn")
var HugeLaserEffect : PackedScene = preload("res://projectiles/huge_laser.tscn")

# Called when the node enters the scene tree for the first time.
func _ready():
Expand All @@ -24,14 +26,20 @@ func _add_test_effects():
# utility functions will get called regardless of its presense in the scene
# tree, as long as thy're in the top-level projectile_effects Array.
# add_child(double_laser.instantiate())
projectile_effects.append(EffectDoubleLaser.new())
projectile_effects.append(HugeLaser.new())
#projectile_effects.append(EffectDoubleLaser.new())
#projectile_effects.append(HugeLaser.new())
projectile_effects.append(DoubleLaserEffect)
projectile_effects.append(HugeLaserEffect)


func fire():
print('fired!')
var p = Projectile.instantiate()
p.spawn(owner, projectile_effects, transform)
# Effects must be instantiated for each projectile.
# Finding a way to manage "which effects we need to instantiate" and
# "Which effects are actually on this projectile" is key
var effects = [DoubleLaserEffect, HugeLaserEffect]
p.spawn(owner, effects, transform)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
Expand Down
17 changes: 10 additions & 7 deletions units/projectile.gd
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
extends Area2D

const SPEED = 750
var projectile_effects: Array[ProjectileEffect] = []
var projectile_effects: Array = []


func spawn(owner: Node, spawn_with_projectile_effects: Array[ProjectileEffect], parent_transform: Transform2D):
func spawn(owner: Node, spawn_with_projectile_effects: Array, parent_transform: Transform2D):
# Execute any spawn modification effects for the projectile
print("spawned!")
projectile_effects = spawn_with_projectile_effects
#projectile_effects = spawn_with_projectile_effects
var local_transform = parent_transform
for effect in projectile_effects:
add_child(effect)
effect.set_owner(self)
effect.modify_creation(owner, projectile_effects, local_transform)
for effect in spawn_with_projectile_effects:
var instance = effect.instantiate()
projectile_effects.append(instance)
add_child(instance)
instance.set_owner(self)
instance.modify_creation(owner, spawn_with_projectile_effects, local_transform)
# Spawn the default projectile
# TODO:: Projectile spawn modification may want to prevent the default from spawning
# how do?
Expand All @@ -23,6 +25,7 @@ func spawn(owner: Node, spawn_with_projectile_effects: Array[ProjectileEffect],
func scale_projectile(new_scale: Vector2):
$Sprite2D.scale = new_scale
$CollisionShape2D.scale = new_scale
scale = new_scale

func _ready():
if scale != Vector2.ONE:
Expand Down

0 comments on commit 066cad0

Please sign in to comment.