Skip to content

Commit

Permalink
Add Huge Laser projectile effect
Browse files Browse the repository at this point in the history
This adds a projectile effect that makes lasers huge.
It includes refactoring to the projectile effect engine to allow
effects to directly influence the parent node's size
  • Loading branch information
Checkroth committed Aug 18, 2024
1 parent e71db57 commit 5b3eb64
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
2 changes: 2 additions & 0 deletions projectiles/huge_laser.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class_name HugeLaser
extends ProjectileEffect
7 changes: 7 additions & 0 deletions projectiles/huge_laser.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_scene load_steps=2 format=3 uid="uid://i6l7jkbauvmf"]

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

[node name="HugeLaser" type="Node"]
script = ExtResource("1_ouvu5")
scale_modifier = 10
42 changes: 27 additions & 15 deletions projectiles/projectile_effect.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,50 @@ 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, projectile_effects: Array[ProjectileEffect], transform: Transform2D):
func modify_creation(owner: Node, projectile_effects: Array, transform: Transform2D):
pass

func _calculate_vector_modification(
base_value: Vector2,
modifier_value: int,
modifier_type: MODIFICATION_TYPE
):
# TODO:: How does godot mutability work? we do NOT want to modify the original modifier
var local_modifier = base_value
if modifier_type == MODIFICATION_TYPE.MULTIPLY:
local_modifier *= modifier_value
elif modifier_type == MODIFICATION_TYPE.ADD:
local_modifier += modifier_value
return local_modifier

# Modify the physics logic of the projectile. This function will be called
# during the _physics_process.
# Uses exported variables by default, and should only be overridden if you're
# doing something really interesting.
func modify_physics(position_modifier: Vector2) -> Vector2:
# TODO:: How does godot mutability work? we do NOT want to modify the original modifier
var return_modifier = position_modifier
if speed_modifier_type == MODIFICATION_TYPE.MULTIPLY:
return_modifier *= speed_modifier
elif speed_modifier_type == MODIFICATION_TYPE.ADD:
return_modifier += speed_modifier
return return_modifier
func modify_physics(physics_vector: Vector2) -> Vector2:
return _calculate_vector_modification(physics_vector, speed_modifier, speed_modifier_type)


# Simple changes to the appearance using built-in features.
# By default,
func modify_appearance():
pass
# By default, modifies the scale of the projectile.
func modify_appearance(scale: Vector2) -> Vector2:
return _calculate_vector_modification(owner.scale, scale_modifier, scale_modifier_type)


# To change the sprite itself, elements of the animation, or otherwise add effects
# Hard changes, rather than additions, will not
# Hard changes, rather than additions, will be subject to effect_priority.
func modify_animation():
pass


# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func _enter_tree():
var projectile = get_owner()
if projectile:
# Projectile scenes are instantiated before being added to the tree
# This function will effectively be called twice, but some operations
# require the parent node to be present first.
projectile.scale_projectile(modify_appearance(projectile.scale))

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
Expand Down
3 changes: 2 additions & 1 deletion units/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ func _add_test_effects():
# 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())
# add_child(double_laser.instantiate())
projectile_effects.append(EffectDoubleLaser.new())
projectile_effects.append(HugeLaser.new())


func fire():
Expand Down
6 changes: 4 additions & 2 deletions units/projectile.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ func spawn(owner: Node, spawn_with_projectile_effects: Array[ProjectileEffect],
# Execute any spawn modification effects for the projectile
print("spawned!")
projectile_effects = spawn_with_projectile_effects
var local_transform = parent_transform
for effect in projectile_effects:
effect.modify_creation(owner, projectile_effects, parent_transform)
add_child(effect.instantiate())
effect.modify_creation(owner, projectile_effects, local_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
transform = local_transform

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

0 comments on commit 5b3eb64

Please sign in to comment.