Files
net-gunner/scripts/trap.gd

86 lines
2.3 KiB
GDScript

class_name Trap extends Area3D
enum Type{
BOMB,
MINE,
GAS,
FORCE_PANEL,
SWITCH,
PITFALL
}
const range_shapes : Dictionary = {
Trap.Type.BOMB : Vector3(5,1,5),
Trap.Type.GAS : Vector3(1,1,1),
Trap.Type.PITFALL : Vector3(1,1,1),
Trap.Type.FORCE_PANEL : Vector3(1,1,1),
Trap.Type.SWITCH : Vector3(3,1,3),
Trap.Type.MINE : Vector3(5,1,5),
}
const trap_icons : Dictionary = {
Trap.Type.BOMB : preload("res://visuals/images/icons/t-bomb.png"),
Trap.Type.GAS : preload("res://visuals/images/icons/t-gas.png"),
Trap.Type.PITFALL : preload("res://visuals/images/icons/t-pitfall.png"),
Trap.Type.FORCE_PANEL : preload("res://visuals/images/icons/t-force_panel.png"),
Trap.Type.SWITCH : preload("res://visuals/images/icons/t-switch.png"),
Trap.Type.MINE : preload("res://visuals/images/icons/t-mine.png"),
}
const explosion_template = preload("res://templates/explosion.tscn")
@onready var range_area : Area3D = %RangeArea
@onready var range_shape : BoxShape3D = %RangeShape.shape
@onready var model : MeshInstance3D = %Model
@onready var icon : Sprite3D = %Icon
@onready var material : StandardMaterial3D = model.get_surface_override_material(0)
@onready var reveal_timer : Timer = %RevealTimer
var type : Type
var square : Vector3i
var trap_owner : int
var disarming : bool
var disarm_id : int
var damage : int = 10
signal disarmed(type : Trap.Type)
func setup(type : Type, trap_owner : int) -> void:
self.type = type
self.trap_owner = trap_owner
func disarm() -> void:
disarmed.emit(type)
queue_free()
func reveal() -> void:
model.visible = true
reveal_timer.start(5)
func _on_reveal_timeout() -> void:
if Game.level.is_square_detected(square):
reveal_timer.start(5)
else:
model.visible = false
func _ready() -> void:
var owns_trap = trap_owner == Multiplayer.id
icon.texture = trap_icons[type]
model.visible = owns_trap
icon.visible = owns_trap
range_shape.size = range_shapes[type]
material.albedo_color = Color.YELLOW if owns_trap else Color.RED
func activate() -> void:
var exp = explosion_template.instantiate()
Game.level.add_vfx(exp, square)
for body in range_area.get_overlapping_bodies():
body.hurt(damage)
#match(type):
func _on_body_entered(body: Node3D) -> void:
if !disarming or body.id != disarm_id:
if !body.detecting:
activate()