Implemented Mediator Pattern. This could be great for state driven function calls.
This commit is contained in:
parent
6dc01326c5
commit
b091bdc143
26
src/components/GenericReceiver.gd
Normal file
26
src/components/GenericReceiver.gd
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
extends Node
|
||||
|
||||
export(String) var body_entered_function = ""
|
||||
|
||||
var call_function: FuncRef
|
||||
|
||||
# Declare member variables here. Examples:
|
||||
# var a = 2
|
||||
# var b = "text"
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
if body_entered_function:
|
||||
call_function = funcref(owner, body_entered_function)
|
||||
#call_function.call_func()
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
#func _process(delta):
|
||||
# pass
|
||||
|
||||
func on_body_entered(sender):
|
||||
print("Receiving call from " + sender.name)
|
||||
if body_entered_function:
|
||||
call_function.call_func(sender)
|
||||
|
||||
6
src/components/GenericReceiver.tscn
Normal file
6
src/components/GenericReceiver.tscn
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[gd_scene load_steps=2 format=2]
|
||||
|
||||
[ext_resource path="res://src/components/GenericReceiver.gd" type="Script" id=1]
|
||||
|
||||
[node name="GenericReceiver" type="Node"]
|
||||
script = ExtResource( 1 )
|
||||
41
src/components/GenericSender.gd
Normal file
41
src/components/GenericSender.gd
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
extends Area2D
|
||||
|
||||
export(String) var receiver_node_name = ""
|
||||
|
||||
# Declare member variables here. Examples:
|
||||
# var a = 2
|
||||
# var b = "text"
|
||||
|
||||
|
||||
## Called when the node enters the scene tree for the first time.
|
||||
#func _ready():
|
||||
# pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
#func _process(delta):
|
||||
# pass
|
||||
|
||||
##TODO: maybe guarantee type with receiver node type instead of just the name.
|
||||
# The the name helps keep the receiver type generic.
|
||||
|
||||
func _on_GenericSender_body_entered(body):
|
||||
if body.has_node(receiver_node_name):
|
||||
body.get_node(receiver_node_name).on_body_entered(self)
|
||||
|
||||
|
||||
#func _on_Hurtbox_Component_area_shape_entered(area_rid, area, area_shape_index, local_shape_index):
|
||||
# #print(area.get_parent().name, "hit", owner.name)
|
||||
## if area.get_parent().has_method("gotcha"):
|
||||
# print(area.get_parent().name, " you can do it.", owner.name)
|
||||
#
|
||||
# if owner.has_method("_on_Hurtbox_area_entered"):
|
||||
# owner.call("_on_Hurtbox_area_entered")
|
||||
# print(area.get_parent().name, " just hit me: ", owner.name)
|
||||
|
||||
|
||||
|
||||
func _on_GenericSender_area_shape_entered(area_rid, area, area_shape_index, local_shape_index):
|
||||
if area.get_parent().has_node(receiver_node_name):
|
||||
area.get_parent().get_node(receiver_node_name).on_body_entered(self)
|
||||
|
||||
11
src/components/GenericSender.tscn
Normal file
11
src/components/GenericSender.tscn
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
[gd_scene load_steps=2 format=2]
|
||||
|
||||
[ext_resource path="res://src/components/GenericSender.gd" type="Script" id=1]
|
||||
|
||||
[node name="GenericSender" type="Area2D"]
|
||||
collision_layer = 0
|
||||
collision_mask = 32768
|
||||
script = ExtResource( 1 )
|
||||
|
||||
[connection signal="area_shape_entered" from="." to="." method="_on_GenericSender_area_shape_entered"]
|
||||
[connection signal="body_entered" from="." to="." method="_on_GenericSender_body_entered"]
|
||||
8
src/components/Hitbox_Component.gd
Normal file
8
src/components/Hitbox_Component.gd
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
extends Area2D
|
||||
|
||||
func set_hitbox(enabled: bool):
|
||||
for child in get_children():
|
||||
if child is CollisionShape2D:
|
||||
child.set_deferred("disabled", !enabled)
|
||||
## TODO: This breaks now I guess because I moved the process functions out to parent class
|
||||
#player_hurtbox.set_deferred("disabled", !enabled) #WTF Apparently this is how to do it
|
||||
8
src/components/Hitbox_Component.tscn
Normal file
8
src/components/Hitbox_Component.tscn
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[gd_scene load_steps=2 format=2]
|
||||
|
||||
[ext_resource path="res://src/components/Hitbox_Component.gd" type="Script" id=1]
|
||||
|
||||
[node name="Hitbox_Component" type="Area2D"]
|
||||
collision_layer = 128
|
||||
collision_mask = 0
|
||||
script = ExtResource( 1 )
|
||||
23
src/components/Hurtbox_Component.gd
Normal file
23
src/components/Hurtbox_Component.gd
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
extends Area2D
|
||||
|
||||
func set_hittbox(enabled: bool):
|
||||
for child in get_children():
|
||||
if child is CollisionShape2D:
|
||||
child.set_deferred("disabled", !enabled)
|
||||
|
||||
# This one doesn't seem to work
|
||||
func _on_Hurtbox_Component_body_entered(body):
|
||||
print(body.name, "hit", owner.name)
|
||||
if owner.has_method("gotcha"):
|
||||
print(body.name, " you can do it.", owner.name)
|
||||
|
||||
|
||||
# well this one works.
|
||||
func _on_Hurtbox_Component_area_shape_entered(area_rid, area, area_shape_index, local_shape_index):
|
||||
#print(area.get_parent().name, "hit", owner.name)
|
||||
# if area.get_parent().has_method("gotcha"):
|
||||
print(area.get_parent().name, " you can do it.", owner.name)
|
||||
|
||||
if owner.has_method("_on_Hurtbox_area_entered"):
|
||||
owner.call("_on_Hurtbox_area_entered")
|
||||
print(area.get_parent().name, " just hit me: ", owner.name)
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
[gd_scene load_steps=27 format=2]
|
||||
[gd_scene load_steps=29 format=2]
|
||||
|
||||
[ext_resource path="res://src/templates/Actor/ActorTemplate.tscn" type="PackedScene" id=1]
|
||||
[ext_resource path="res://src/enemyC/enemyC_movement_component.gd" type="Script" id=2]
|
||||
[ext_resource path="res://src/enemyC/attack.gd" type="Script" id=3]
|
||||
[ext_resource path="res://src/components/Hitbox_Component.tscn" type="PackedScene" id=4]
|
||||
[ext_resource path="res://src/components/GenericSender.tscn" type="PackedScene" id=5]
|
||||
|
||||
[sub_resource type="StreamTexture" id=1]
|
||||
load_path = "res://.import/botEnemy.png-46efd539a38730ff15fb4ddb087329c6.stex"
|
||||
|
|
@ -114,6 +115,9 @@ extents = Vector2( 10, 16 )
|
|||
[sub_resource type="RectangleShape2D" id=23]
|
||||
extents = Vector2( 9, 3.5 )
|
||||
|
||||
[sub_resource type="CapsuleShape2D" id=24]
|
||||
height = 10.0
|
||||
|
||||
[node name="EnemyC" instance=ExtResource( 1 )]
|
||||
actor_type = "Enemy"
|
||||
|
||||
|
|
@ -121,7 +125,7 @@ actor_type = "Enemy"
|
|||
position = Vector2( 11, -7 )
|
||||
frames = SubResource( 20 )
|
||||
animation = "idle"
|
||||
frame = 0
|
||||
frame = 1
|
||||
__meta__ = {
|
||||
"_aseprite_wizard_config_": {
|
||||
"layer": "",
|
||||
|
|
@ -187,3 +191,10 @@ modulate = Color( 1, 0.596078, 0.121569, 1 )
|
|||
position = Vector2( 18, -8.5 )
|
||||
shape = SubResource( 23 )
|
||||
disabled = true
|
||||
|
||||
[node name="GenericSender" parent="." index="9" instance=ExtResource( 5 )]
|
||||
receiver_node_name = "GenericReceiver"
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="GenericSender" index="0"]
|
||||
position = Vector2( 48, 0 )
|
||||
shape = SubResource( 24 )
|
||||
|
|
|
|||
42
src/enemyC/attack.gd
Normal file
42
src/enemyC/attack.gd
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
extends StateAnimatedActor
|
||||
|
||||
export (NodePath) var fall_node
|
||||
export (NodePath) var idle_node
|
||||
|
||||
onready var fall_state: State = get_node(fall_node)
|
||||
onready var idle_state: State = get_node(idle_node)
|
||||
|
||||
func enter() -> void:
|
||||
.enter()
|
||||
# parent.set_hurtbox(true)
|
||||
if state_timeout:
|
||||
state_timeout.start()
|
||||
move_component.velocity.x = 0
|
||||
if debug_state:
|
||||
print(owner.name, " Move Component: ", move_component.desired_movement_vector.x)
|
||||
|
||||
func process_frame(_delta: float) -> State:
|
||||
if move_component.wants_shoot() == false and state_timeout.time_left == 0:
|
||||
return idle_state
|
||||
|
||||
# if animations.frame > 1:
|
||||
# parent.set_hitbox(true)
|
||||
# else:
|
||||
# parent.set_hitbox(false)
|
||||
|
||||
|
||||
return null
|
||||
|
||||
|
||||
func process_physics(delta: float) -> State:
|
||||
|
||||
# parent.velocity.y += gravity * delta
|
||||
# #parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1))
|
||||
# parent.velocity.x = move_component.desired_movement_vector.x * move_speed
|
||||
# parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1))
|
||||
|
||||
move_actor_as_desired(delta)
|
||||
|
||||
if !parent.is_on_floor():
|
||||
return fall_state
|
||||
return null
|
||||
|
|
@ -36,3 +36,6 @@ func shoot_projectile():
|
|||
|
||||
func gotcha():
|
||||
print("oh! You got me.")
|
||||
|
||||
func heylookatme(sender):
|
||||
print("does it work!")
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
[gd_scene load_steps=91 format=2]
|
||||
[gd_scene load_steps=92 format=2]
|
||||
|
||||
[ext_resource path="res://src/playerC/Player-KinematicBody2D.gd" type="Script" id=1]
|
||||
[ext_resource path="res://src/playerC/states/idle.gd" type="Script" id=2]
|
||||
|
|
@ -14,6 +14,7 @@
|
|||
[ext_resource path="res://src/components/Hurtbox_Component.tscn" type="PackedScene" id=12]
|
||||
[ext_resource path="res://src/state_modifier.gd" type="Script" id=13]
|
||||
[ext_resource path="res://src/playerC/sm.gd" type="Script" id=14]
|
||||
[ext_resource path="res://src/components/GenericReceiver.tscn" type="PackedScene" id=15]
|
||||
|
||||
[sub_resource type="Resource" id=148]
|
||||
script = ExtResource( 11 )
|
||||
|
|
@ -509,8 +510,9 @@ timeout_seconds = 2.0
|
|||
animation_suffix = true
|
||||
|
||||
[node name="Hurtbox_Component" parent="." instance=ExtResource( 12 )]
|
||||
collision_layer = 0
|
||||
collision_layer = 32768
|
||||
collision_mask = 128
|
||||
monitoring = false
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Hurtbox_Component"]
|
||||
modulate = Color( 1, 0, 0, 1 )
|
||||
|
|
@ -532,3 +534,6 @@ width = 2.0
|
|||
default_color = Color( 1, 0.607843, 0, 1 )
|
||||
|
||||
[node name="SE_Player" type="AudioStreamPlayer" parent="."]
|
||||
|
||||
[node name="GenericReceiver" parent="." instance=ExtResource( 15 )]
|
||||
body_entered_function = "heylookatme"
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user