From cb8173427979dda0285170c1541fbb2086519f3a Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Thu, 1 Sep 2022 11:26:34 -0400 Subject: [PATCH] make enemies use the event bus --- godot/characters/enemies/WalkingEnemy.gd | 8 ++++++++ godot/characters/enemies/blightwing/Blightwing.gd | 13 +++++++++++-- godot/characters/enemies/blockface/Blockface.gd | 10 ++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/godot/characters/enemies/WalkingEnemy.gd b/godot/characters/enemies/WalkingEnemy.gd index c1c54b5..94a01b5 100644 --- a/godot/characters/enemies/WalkingEnemy.gd +++ b/godot/characters/enemies/WalkingEnemy.gd @@ -16,6 +16,7 @@ func _ready() -> void: $AnimatedSprite.flip_h = true $FloorChecker.position.x = $CollisionShape2D.shape.get_extents().x * direction $FloorChecker.enabled = detect_edges + Event.connect("level_loaded", self, "_on_level_loaded") func _physics_process(_delta: float) -> void: @@ -33,6 +34,13 @@ func _physics_process(_delta: float) -> void: if collision.collider.name == "Player": emit_signal("player_touched") + Event.emit_signal("object_updated", self.get_name(), "Walking", global_position, velocity) + func squash() -> void: + Event.emit_signal("object_removed", self.get_name()) queue_free() + + +func _on_level_loaded() -> void: + Event.emit_signal("object_created", self.get_name(), "Walking", global_position, Vector2(0, 0)) diff --git a/godot/characters/enemies/blightwing/Blightwing.gd b/godot/characters/enemies/blightwing/Blightwing.gd index 6186bea..1446628 100644 --- a/godot/characters/enemies/blightwing/Blightwing.gd +++ b/godot/characters/enemies/blightwing/Blightwing.gd @@ -22,6 +22,8 @@ func _ready() -> void: if direction == 1: $AnimatedSprite.flip_h = true + Event.connect("level_loaded", self, "_on_level_loaded") + func _physics_process(delta: float) -> void: if $LeftWallChecker.is_colliding(): @@ -29,17 +31,18 @@ func _physics_process(delta: float) -> void: elif $RightWallChecker.is_colliding(): wall_checker_collided($RightWallChecker) + var velocity: Vector2 = Vector2(0, 0) if not follow_path: var target_position = position target_position.x *= 2 * direction position = position.move_toward(target_position, round(speed * delta)) - var velocity = get_velocity_towards_target(delta) + velocity = get_velocity_towards_target(delta) var collision = move_and_collide(velocity, true, true, true) if collision and collision.collider.name != "Player": var _collision = move_and_collide(velocity) else: - var velocity = get_velocity_towards_target(delta) + velocity = get_velocity_towards_target(delta) var collision = move_and_collide(velocity, true, true, true) if collision and collision.collider.name != "Player": @@ -60,6 +63,8 @@ func _physics_process(delta: float) -> void: elif start_position.x + target.x < position.x: $AnimatedSprite.flip_h = false + Event.emit_signal("object_updated", self.get_name(), "Flying", global_position, velocity) + func get_velocity_towards_target(delta: float) -> Vector2: var velocity = Vector2(0, 0) @@ -80,3 +85,7 @@ func wall_checker_collided(wall_checker: RayCast2D) -> void: emit_signal("player_touched") direction *= -1 $AnimatedSprite.flip_h = not $AnimatedSprite.flip_h + + +func _on_level_loaded() -> void: + Event.emit_signal("object_created", self.get_name(), "Flying", global_position, Vector2(0, 0)) diff --git a/godot/characters/enemies/blockface/Blockface.gd b/godot/characters/enemies/blockface/Blockface.gd index b307882..3f52a5a 100644 --- a/godot/characters/enemies/blockface/Blockface.gd +++ b/godot/characters/enemies/blockface/Blockface.gd @@ -11,9 +11,14 @@ export var fall_speed = 75.0 var return_to_start: bool = false +func _ready() -> void: + Event.connect("level_loaded", self, "_on_level_loaded") + + func _physics_process(delta: float) -> void: if return_to_start: position = position.move_toward(start_position, speed * delta) + Event.emit_signal("object_updated", self.get_name(), "Rising", global_position, velocity) else: var collision = move_and_collide(Vector2(0, (position.y + fall_speed) * delta)) if collision: @@ -21,7 +26,12 @@ func _physics_process(delta: float) -> void: $AnimatedSprite.play("normal") if collision.collider.name == "Player": emit_signal("player_touched") + Event.emit_signal("object_updated", self.get_name(), "Falling", global_position, velocity) if position.y <= start_position.y: return_to_start = false $AnimatedSprite.play("angry") + + +func _on_level_loaded() -> void: + Event.emit_signal("object_created", self.get_name(), "Falling", global_position, Vector2(0, 0))