mirror of
https://github.com/kodzukye/omens-ascent.git
synced 2026-08-10 05:13:10 +00:00
70 lines
2.0 KiB
GDScript
70 lines
2.0 KiB
GDScript
class_name Spikes
|
|
extends Area2D
|
|
|
|
enum OrientationEnum { BOTTOM, TOP, LEFT, RIGHT }
|
|
enum State { NORMAL, BLOOD }
|
|
|
|
const _COLLISION_OFFSET: float = 3.0
|
|
|
|
@export var orientation: OrientationEnum = OrientationEnum.BOTTOM
|
|
|
|
@export var _normal_bottom: Texture2D
|
|
@export var _normal_top: Texture2D
|
|
@export var _normal_left: Texture2D
|
|
@export var _normal_right: Texture2D
|
|
|
|
@export var _blood_bottom: Texture2D
|
|
@export var _blood_top: Texture2D
|
|
@export var _blood_left: Texture2D
|
|
@export var _blood_right: Texture2D
|
|
|
|
@onready var _sprite: Sprite2D = $Sprite2D
|
|
@onready var _collision: CollisionShape2D = $CollisionShape2D
|
|
|
|
var _state: State = State.NORMAL
|
|
|
|
func _ready() -> void:
|
|
body_entered.connect(_on_body_entered)
|
|
if global_position in EventBus.triggered_spike_positions:
|
|
_state = State.BLOOD
|
|
_update_visuals()
|
|
|
|
func _on_body_entered(body: Node2D) -> void:
|
|
if not body.is_in_group("player"):
|
|
return
|
|
|
|
_state = State.BLOOD
|
|
EventBus.triggered_spike_positions.append(global_position)
|
|
_update_visuals()
|
|
body.kill()
|
|
|
|
func _update_visuals() -> void:
|
|
if not is_node_ready():
|
|
return
|
|
_sprite.texture = _get_texture()
|
|
_update_collision_transform()
|
|
|
|
func _get_texture() -> Texture2D:
|
|
var textures_by_orientation: Dictionary = {
|
|
OrientationEnum.BOTTOM: [_normal_bottom, _blood_bottom],
|
|
OrientationEnum.TOP: [_normal_top, _blood_top],
|
|
OrientationEnum.LEFT: [_normal_left, _blood_left],
|
|
OrientationEnum.RIGHT: [_normal_right, _blood_right],
|
|
}
|
|
return textures_by_orientation[orientation][_state]
|
|
|
|
func _update_collision_transform() -> void:
|
|
match orientation:
|
|
OrientationEnum.BOTTOM:
|
|
_collision.rotation_degrees = 0.0
|
|
_collision.position = Vector2(0.0, _COLLISION_OFFSET)
|
|
OrientationEnum.TOP:
|
|
_collision.rotation_degrees = 0.0
|
|
_collision.position = Vector2(0.0, -_COLLISION_OFFSET)
|
|
OrientationEnum.LEFT:
|
|
_collision.rotation_degrees = 90.0
|
|
_collision.position = Vector2(_COLLISION_OFFSET, 0.0)
|
|
OrientationEnum.RIGHT:
|
|
_collision.rotation_degrees = 90.0
|
|
_collision.position = Vector2(-_COLLISION_OFFSET, 0.0)
|