Skip to content

Commit

Permalink
Add a double laser projectile effect.
Browse files Browse the repository at this point in the history
This effect adds an identical lazer ouput offset from the original
laser.
It also makes some modifications to how the firing mechanism and
effects dispatch is handled, to make it easier to offload effect
mechanism management to each effect.
  • Loading branch information
Checkroth committed Aug 17, 2024
1 parent c5c16ec commit e71db57
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
30 changes: 30 additions & 0 deletions projectiles/double_laser.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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:
# 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):
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)
var doubled_transform = transform
# TODO:: Do we want to offset the laser, or do we want to angle out of the same point of origin?
doubled_transform.origin += laser_offset
p.spawn(owner, non_recursive_effects, doubled_transform)

# Called when the node enters the scene tree for the first time.
func _ready():
Projectile = load("res://units/projectile.tscn")
# Called when the node enters the scene tree for the first time.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
6 changes: 6 additions & 0 deletions projectiles/double_laser.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://dgp68vbl25onx"]

[ext_resource type="Script" path="res://projectiles/double_laser.gd" id="1_wqupk"]

[node name="DoubleLaser" type="Node"]
script = ExtResource("1_wqupk")
2 changes: 1 addition & 1 deletion projectiles/projectile_effect.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum MODIFICATION_TYPE {

# Modify the logic that adds the projectile as a child to the scene.
# This can be used to change spawn locations, quantities, etc.
func modify_creation(owner: Node):
func modify_creation(owner: Node, projectile_effects: Array[ProjectileEffect], transform: Transform2D):
pass

# Modify the physics logic of the projectile. This function will be called
Expand Down
23 changes: 17 additions & 6 deletions units/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,28 @@ const HORIZONTAL_BUFFER = 75

@export var Projectile : PackedScene
@onready var projectile_spawner = $ProjectileSpawner

var projectile_effects: Array[ProjectileEffect] = []

# Called when the node enters the scene tree for the first time.
func _ready():
Projectile = load("res://units/projectile.tscn")
_add_test_effects()

func _add_test_effects():
# Debugging -- these should be added to the list via some user interaction instead.
var double_laser = load("res://projectiles/double_laser.tscn")
# TODO:: Not even sure if adding to the scene tree here is necessary.
# It's necessary now just to trigger projectile_effect._ready, but the
# 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())


func fire():
print('fired!')
var p = Projectile.instantiate()
p.spawn(owner, projectile_effects, transform)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
Expand All @@ -32,9 +47,5 @@ func _physics_process(delta):
position.x += HORIZONTAL_SPEED * delta

if Input.is_action_just_pressed("fire"):
print('fired!')
var p = Projectile.instantiate()
p.spawn(owner, projectile_effects)
# TODO:: This transform should probably be modifiable by projectile effects too?
p.transform = projectile_spawner.global_transform
fire()

6 changes: 4 additions & 2 deletions units/projectile.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ const SPEED = 750
var projectile_effects: Array[ProjectileEffect] = []


func spawn(owner: Node, spawn_with_projectile_effects: Array[ProjectileEffect]):
func spawn(owner: Node, spawn_with_projectile_effects: Array[ProjectileEffect], parent_transform: Transform2D):
# Execute any spawn modification effects for the projectile
print("spawned!")
projectile_effects = spawn_with_projectile_effects
for effect in projectile_effects:
effect.modify_creation(owner)
effect.modify_creation(owner, projectile_effects, parent_transform)
# Spawn the default projectile
# TODO:: Projectile spawn modification may want to prevent the default from spawning
# how do?
owner.add_child(self)
projectile_effects = projectile_effects
transform = parent_transform

func _physics_process(delta):
var position_modifier = transform.x * SPEED * delta
Expand Down

0 comments on commit e71db57

Please sign in to comment.