giants-godot/entities/genericprojectile.gd

96 lines
2.6 KiB
GDScript3
Raw Normal View History

2021-09-08 18:36:41 +02:00
extends Entity
class_name GenericProjectile
2021-10-10 15:51:46 +02:00
export var bullet_speed: float = 1
export var bullet_damage: float = 15
2021-09-08 18:36:41 +02:00
2021-10-10 15:51:46 +02:00
export var lifetime: float = 0
export var fx_explode: PackedScene
var current_fx_explode: Particles
export var max_range: float
2021-10-10 15:51:46 +02:00
export var proximity_collision_area_path: NodePath
onready var proximity_collision_area: Area = get_node(proximity_collision_area_path)
var lifetime_timer = 0
2021-09-08 18:36:41 +02:00
2021-09-22 18:04:03 +02:00
func _init():
gravity_scale = 0
enum WeaponProperties {
HOMING = 0x00000001,
BOUNCE = 0x00000002,
PROXIMITY = 0x00000004,
AREA_HURT = 0x00000008,
TRIGGER_OBJECT = 0x00000010,
TRIGGER_GROUND = 0x00000020,
TRIGGER_TIMEOUT = 0x00000040,
EXPLODE = 0x00000080,
DO_ORIENTATION = 0x00000100,
COLLIDE_GROUND = 0x00000200,
GROUND_RIPPLE = 0x00000400,
DEFLECTABLE = 0x00000800,
SHOOTABLE = 0x00001000,
FIRST_FIRE_DELAY = 0x00002000,
FLARE = 0x00004000,
INCENDIARY = 0x00008000,
EMP = 0x00010000,
}
2021-09-08 18:36:41 +02:00
var from: Transform
var to: Vector3 = Vector3.ZERO
2021-09-22 18:04:03 +02:00
export(int, FLAGS) var properties
2021-09-08 18:36:41 +02:00
func _physics_process(delta):
2021-10-10 15:51:46 +02:00
if lifetime != 0:
lifetime_timer += delta
if lifetime_timer >= lifetime:
2021-09-08 18:36:41 +02:00
queue_free()
func create(_from: Transform, _to: Vector3, exclude_collisions: Array):
from = _from
to = _to
global_transform = from
for object in exclude_collisions:
add_collision_exception_with(object)
2021-10-10 15:51:46 +02:00
add_central_force((to - from.origin).normalized() * bullet_speed)
func body_entered(body: Node):
if properties & WeaponProperties.TRIGGER_GROUND and body.is_in_group("world"):
2021-10-12 23:11:23 +02:00
print("trigger by body_entered TRIGGER_GROUND")
2021-10-10 15:51:46 +02:00
trigger(body)
if properties & WeaponProperties.TRIGGER_OBJECT and body.is_in_group("objects"):
2021-10-12 23:11:23 +02:00
print("trigger by body_entered TRIGGER_OBJECT")
2021-10-10 15:51:46 +02:00
trigger(body)
func proximity_body_entered(body: Node):
if properties & WeaponProperties.TRIGGER_OBJECT:
if body.is_in_group("objects"):
2021-10-12 23:11:23 +02:00
print("trigger by proximity_body_entered")
2021-10-10 15:51:46 +02:00
trigger(body)
func trigger(body: Node):
2021-10-12 23:11:23 +02:00
if properties & WeaponProperties.EXPLODE:
# create decal
var decal: MeshInstance = preload("res://effects/explosion_decal.tscn").instance()
decal.global_transform = global_transform
decal.scale = Vector3(20, 2, 20)
get_parent().add_child(decal)
print("Created decal")
2021-10-10 15:51:46 +02:00
if fx_explode:
var fx: Particles = fx_explode.instance()
fx.global_transform = global_transform
fx.emitting = true
get_parent().add_child(fx)
current_fx_explode = fx
visible = false # needed because _process must still destroy the fx
2021-10-12 23:11:23 +02:00
sleeping = true
2021-10-10 15:51:46 +02:00
else:
queue_free()
func _process(delta):
if current_fx_explode and not current_fx_explode.emitting:
current_fx_explode.queue_free()
queue_free()