Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor transmission #96

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
class_name TransmissionResource
extends ValueResource

## Used to know when data is consumed and need a reaction to it
signal consumed
enum ErrorType {NONE = -1, CONSUMED = 0, TRY_AGAIN, FAILED}

@export var transmission_name:StringName
@export var state:ErrorType = ErrorType.NONE

func send_transmission(receiver:AreaReceiver2D)->ErrorType:
## Receiving end must call either one
receiver.emit_signal(transmission_name, self)

# TODO: if signal exists use this assert
#assert(state != ErrorType.NONE)
return state

func consume()->void:
state = ErrorType.CONSUMED

func try_again()->void:
state = ErrorType.TRY_AGAIN

func failed()->void:
state = ErrorType.FAILED
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func get_target_path(from:Vector2, to:Vector2)->PackedVector2Array:
index = 1
else:
index = 0
assert(index < navigation_path.size())

finish_reached = false
if visible:
Expand Down
3 changes: 2 additions & 1 deletion addons/top_down/scenes/pickups/health_pickup.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[sub_resource type="Resource" id="Resource_1v8hw"]
script = ExtResource("3_7bghr")
value = 1.0
effect_signal = &"health"
transmission_name = &""
state = -1
version = 0
not_saved = false

Expand Down
5 changes: 0 additions & 5 deletions addons/top_down/scripts/pickups/EffectTransmision.gd
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
class_name EffectTransmision
extends TransmissionResource


@export var effect_signal:StringName
30 changes: 26 additions & 4 deletions addons/top_down/scripts/pickups/EffectTransmitter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ signal consumed
# TODO: maybe add enabled to manage connections

@export var area_transmitter:AreaTransmitter2D
@export var effect_transmission:EffectTransmision
@export var effect_transmission:TransmissionResource

func _ready()->void:
assert(effect_transmission != null)
Expand All @@ -15,9 +15,31 @@ func _ready()->void:
func on_area_entered(area:Area2D)->void:
if !(area is AreaReceiver2D):
return
var effect_dup:EffectTransmision = effect_transmission.duplicate()
effect_dup.consumed.connect(on_consumed, CONNECT_ONE_SHOT)
(area as AreaReceiver2D).emit_signal(effect_dup.effect_signal, effect_dup)
send(area)

func send(receiver:AreaReceiver2D)->void:
var effect_dup:TransmissionResource = effect_transmission.duplicate()
var err:TransmissionResource.ErrorType = effect_dup.send_transmission(receiver)

# TODO: maybe situations when need to handle FAILED and NONE
if err == TransmissionResource.ErrorType.CONSUMED:
on_consumed()
return
if err == TransmissionResource.ErrorType.TRY_AGAIN:
# If this is called from next frame the calling signal connection is stil active.
# Need to delay somewhere after this function call.
try_again.call_deferred(receiver)
return

## Call transmission next physics frame
func try_again(receiver:AreaReceiver2D)->void:
get_tree().physics_frame.connect(test_receiver.bind(receiver), CONNECT_ONE_SHOT)

func test_receiver(receiver:AreaReceiver2D)->void:
var overlapping_areas:Array[Area2D] = area_transmitter.get_overlapping_areas()
if overlapping_areas.has(receiver):
send(receiver)

## Give notification to other nodes
func on_consumed()->void:
consumed.emit()
5 changes: 5 additions & 0 deletions addons/top_down/scripts/pickups/HealthReceiver.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class_name HealthReceiver
extends Node

@export var enabled:bool = true

@export var area_receiver:AreaReceiver2D
@export var damage_receiver:DamageReceiver
## Resource node holds HealthResource
Expand All @@ -20,5 +22,8 @@ func _ready()->void:
area_receiver.connect("health", receive)

func receive(health_transmision:HealthTransmision)->void:
if !enabled:
health_transmision.failed()
return
## Health transmission takes care of processing health
health_transmision.apply(health_resource)
4 changes: 2 additions & 2 deletions addons/top_down/scripts/pickups/HealthTransmision.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class_name HealthTransmision
extends EffectTransmision
extends TransmissionResource

@export var value:float

Expand All @@ -12,4 +12,4 @@ func apply(health_resource:HealthResource)->void:
return
health_resource.add_hp(value)

consumed.emit()
consume()