75 lines
1.6 KiB
GDScript3
75 lines
1.6 KiB
GDScript3
|
|
extends CharacterBody3D
|
||
|
|
class_name Projectile
|
||
|
|
|
||
|
|
const ENEMY : int = 1
|
||
|
|
const ALLY : int = 2
|
||
|
|
|
||
|
|
const MINIMUN_AREA : float = .1
|
||
|
|
|
||
|
|
@export var speed : int = 20
|
||
|
|
@export_range(MINIMUN_AREA, 1000) var radius : float = MINIMUN_AREA
|
||
|
|
@export var isAOE := false
|
||
|
|
@export_flags("Enemies", "Alliés") var allowedTargets : int = ENEMY
|
||
|
|
|
||
|
|
|
||
|
|
@onready var projectileArea : CollisionShape3D = $Impact/ProjectileArea
|
||
|
|
@onready var projectileSize : CollisionShape3D = $Impact/ProjectileSize
|
||
|
|
|
||
|
|
|
||
|
|
var target : PhysicsBody3D
|
||
|
|
var damage : int = 1
|
||
|
|
var bodiesInRange : Array[Node3D]
|
||
|
|
var enemiesInRange : Array[Enemy]
|
||
|
|
var allyInRange : Array[Tower]
|
||
|
|
|
||
|
|
|
||
|
|
func _ready() -> void:
|
||
|
|
enemiesInRange.append(target)
|
||
|
|
|
||
|
|
|
||
|
|
func _physics_process(delta: float) -> void:
|
||
|
|
if is_instance_valid(target):
|
||
|
|
velocity = global_position.direction_to(target.global_position) * speed
|
||
|
|
look_at(target.global_position)
|
||
|
|
|
||
|
|
move_and_slide()
|
||
|
|
else:
|
||
|
|
queue_free()
|
||
|
|
|
||
|
|
|
||
|
|
func onBodyEnteredDamageArea(body: Node3D) -> void:
|
||
|
|
print(body.get_class())
|
||
|
|
if isAOE && targetable(body):
|
||
|
|
if body is Enemy:
|
||
|
|
enemiesInRange.append(body)
|
||
|
|
if body is Tower:
|
||
|
|
allyInRange.append(body)
|
||
|
|
|
||
|
|
|
||
|
|
func OnBodyExitedDamageArea(body: Node3D) -> void:
|
||
|
|
if body is Enemy:
|
||
|
|
enemiesInRange.erase(body)
|
||
|
|
if body is Tower:
|
||
|
|
allyInRange.erase(body)
|
||
|
|
|
||
|
|
|
||
|
|
func onBodyCollideWithProjectile(body : Node3D) -> void:
|
||
|
|
if body == target:
|
||
|
|
resolveDamages()
|
||
|
|
|
||
|
|
|
||
|
|
func targetable(body: Node3D) -> bool:
|
||
|
|
if body is Enemy:
|
||
|
|
return ENEMY & allowedTargets
|
||
|
|
if body is Tower:
|
||
|
|
return ALLY & allowedTargets
|
||
|
|
|
||
|
|
return false
|
||
|
|
|
||
|
|
|
||
|
|
func resolveDamages() -> void:
|
||
|
|
print(enemiesInRange)
|
||
|
|
for enemy in enemiesInRange:
|
||
|
|
enemy.take_damage(damage)
|
||
|
|
queue_free()
|