giants-godot/entities/genericprojectile.gd
2021-10-12 23:11:23 +02:00

95 lines
2.5 KiB
GDScript

extends Entity
class_name GenericProjectile
export var bullet_speed: float = 1
export var bullet_damage: float = 15
export var lifetime: float = 0
export var fx_explode: PackedScene
var current_fx_explode: Particles
export var proximity_collision_area_path: NodePath
onready var proximity_collision_area: Area = get_node(proximity_collision_area_path)
var lifetime_timer = 0
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,
}
var from: Transform
var to: Vector3 = Vector3.ZERO
export(int, FLAGS) var properties
func _physics_process(delta):
if lifetime != 0:
lifetime_timer += delta
if lifetime_timer >= lifetime:
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)
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"):
print("trigger by body_entered TRIGGER_GROUND")
trigger(body)
if properties & WeaponProperties.TRIGGER_OBJECT and body.is_in_group("objects"):
print("trigger by body_entered TRIGGER_OBJECT")
trigger(body)
func proximity_body_entered(body: Node):
if properties & WeaponProperties.TRIGGER_OBJECT:
if body.is_in_group("objects"):
print("trigger by proximity_body_entered")
trigger(body)
func trigger(body: Node):
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")
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
sleeping = true
else:
queue_free()
func _process(delta):
if current_fx_explode and not current_fx_explode.emitting:
current_fx_explode.queue_free()
queue_free()