ft: extracted mirrorSprite to custom node

This commit is contained in:
2026-02-07 15:41:08 +01:00
parent 57c611a46d
commit 2211130fd3
8 changed files with 114 additions and 114 deletions

View File

@@ -1,34 +1,16 @@
extends AbstractPrey2D
@onready var sprite = $AnimatedSprite2D
@onready var fsm = $StateMachine
@onready var wrapper: WrappingManager = $WrappingManager
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var fsm: StateMachine = $StateMachine
@export var speed = 0.5
var desired_rotation: float = self.rotation
# Mirrored sprites for periodic boundary
var mirrorSprite1: Node2D
var mirrorSprite2: Node2D
var mirrorSprite3: Node2D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
health = maxHealth
sprite.play("Healthy")
mirrorSprite1 = sprite.duplicate()
sprite.play("Healthy")
mirrorSprite2 = sprite.duplicate()
sprite.play("Healthy")
mirrorSprite3 = sprite.duplicate()
sprite.play("Healthy")
add_child(mirrorSprite1)
add_child(mirrorSprite2)
add_child(mirrorSprite3)
# Called every frame. 'delta' is the elapsed time since the previous frame.
@@ -36,9 +18,6 @@ func _process(delta: float) -> void:
# smoothly rotate
if self.rotation != self.desired_rotation:
self.rotation = lerp_angle(self.rotation, self.desired_rotation, clampf(4 * delta, 0, 1))
# Boundary mirroring
_handle_wrapping()
func _physics_process(delta: float) -> void:
pass
@@ -60,94 +39,13 @@ func handle_damage(dmg: int, src: Node) -> void:
func die() -> void:
sprite.play("Dying")
mirrorSprite1.play("Dying")
mirrorSprite2.play("Dying")
mirrorSprite3.play("Dying")
wrapper.play_sprite("Dying")
super.die()
func become_injured() -> void:
sprite.play("Injured")
mirrorSprite1.play("Injured")
mirrorSprite2.play("Injured")
mirrorSprite3.play("Injured")
wrapper.play_sprite("Injured")
func _on_sight_body_entered(body: Node2D) -> void:
if body.is_in_group("predator") or (health < maxHealth and body.is_in_group("player")):
fsm.transition_to_next_state(fsm.States.FLEEING, {"threat": body})
# Mirroring table:
# |---|---|---|---|
# | 4 | 3 | 4 | 3 |
# |---|===|===|---|
# | 1 ǁ 2 | 1 ǁ 2 |
# |---ǁ---|---ǁ---|
# | 4 ǁ 3 | 4 ǁ 3 |
# |---|===|===|---|
# | 1 | 2 | 1 | 2 |
# |---|---|---|---|
# If less than viewport size away from an edge, mirror over that edge (for seamless boundary)
# NOTE: For this to look correctly the camera size should be smaller than half the screen port (in
# any one dimension. Ideally, the difference between camera size and half the screen port is
# at least the size of the prey sprite)
func _handle_wrapping():
mirrorSprite1.visible = false
mirrorSprite2.visible = false
mirrorSprite3.visible = false
# TODO: Assume viewport size << screen size and only draw according to GameManager.viewport_size
# Find corresponding section of the screen
if position.x < GameManager.screen_size.x/2 and position.y < GameManager.screen_size.y/2:
# 2
mirrorSprite1.visible = true
mirrorSprite2.visible = true
mirrorSprite3.visible = true
# Right
mirrorSprite1.global_position = global_position + Vector2(GameManager.screen_size.x, 0)
# Diag
mirrorSprite3.global_position = global_position + Vector2(GameManager.screen_size.x, GameManager.screen_size.y)
# Bottom
mirrorSprite2.global_position = global_position + Vector2(0, GameManager.screen_size.y)
elif position.x < GameManager.screen_size.x/2:
# 3
mirrorSprite1.visible = true
mirrorSprite2.visible = true
mirrorSprite3.visible = true
# Top
mirrorSprite1.global_position = global_position + Vector2(0, - GameManager.screen_size.y)
# Diag
mirrorSprite2.global_position = global_position + Vector2(GameManager.screen_size.x, - GameManager.screen_size.y)
# Right
mirrorSprite3.global_position = global_position + Vector2(GameManager.screen_size.x, 0)
elif position.y < GameManager.screen_size.y/2:
# 1
mirrorSprite1.visible = true
mirrorSprite2.visible = true
mirrorSprite3.visible = true
# Left
mirrorSprite1.global_position = global_position + Vector2(- GameManager.screen_size.x, 0)
# Bottom
mirrorSprite2.global_position = global_position + Vector2(0, GameManager.screen_size.y)
# Diag
mirrorSprite3.global_position = global_position + Vector2(- GameManager.screen_size.x, GameManager.screen_size.y)
else:
# 4
mirrorSprite1.visible = true
mirrorSprite2.visible = true
mirrorSprite3.visible = true
# Left
mirrorSprite1.global_position = global_position + Vector2(- GameManager.screen_size.x, 0)
# Diag
mirrorSprite2.global_position = global_position + Vector2(- GameManager.screen_size.x, - GameManager.screen_size.y)
# Top
mirrorSprite3.global_position = global_position + Vector2(0, - GameManager.screen_size.y)