hero-mark-2/objects/player/arrow_projectile.gd

95 lines
2.8 KiB
GDScript

extends Node2D
# "block" graphic
const BlockText := preload("res://objects/hud/blocktext.tscn")
# speed to fly at
export var speed: float = 240.0
# group to kill
export var target_group: String = "enemy_hitbox"
# direction to fly
export var direction: float = 1.0
# whether or not it frees on wall collision
export var breaks_on_wall: float = true
#Edge to check culling, if this edge is offscreen, delete the arrow
onready var cull_edge := Vector2(5.0 * direction, 0.0)
onready var initial_sector: Vector2 = Game.current_sector
func _ready():
if target_group == "enemy_hitbox":
var palette = load("res://graphics/player/palettes/%s.tex" % Game.current_palette)
$Sprite.material.set_shader_param("palette", palette)
#Flip depending on direction
scale.x = direction
func _physics_process(delta):
#Move in right direction
position.x += speed * direction * delta
#Delete when offscreen
if Game.get_sector(global_position + cull_edge) != initial_sector:
_persist_trail()
queue_free()
#Wall Collision
func _on_Hitbox_body_entered(body):
if breaks_on_wall:
if body is TileMap or body is StaticBody2D or body.is_in_group("stop_arrow"):
_make_sparks()
_persist_trail()
queue_free()
# kill entity if in target group
func _on_Hitbox_area_entered(area):
# block if collided area is in "blocks_arrow" group
if area.is_in_group(target_group):
var target = area.get_parent()
# create block text and return if blocked
if area.is_in_group("blocks_arrow"):
var pos = target.global_position
Game.instance_node(BlockText, pos.x, pos.y, target.get_parent())
_make_sparks()
else:
# kill targeted node
target.die()
#decrease arrows if enemy killed
if target_group == "enemy_hitbox":
Game.arrows = max(0, Game.arrows - 1) as int # clamp arrows above 0
_persist_trail()
queue_free()
elif area.is_in_group("arrow"):
_make_sparks()
_persist_trail()
queue_free()
func _persist_trail():
# don't do this twice to prevent crash
if not is_queued_for_deletion():
# make particles a sibling so it lives a bit longer than arrow
var particles = $DustParticles
remove_child(particles)
get_parent().add_child(particles)
particles.global_position = global_position
particles.emitting = false
# free particles once they have gone through their lifetime
get_tree().create_timer(particles.lifetime, false).connect("timeout", particles, "queue_free")
func _make_sparks():
# don't do this twice to prevent crash
if not is_queued_for_deletion():
var particles = $SparkParticles
remove_child(particles)
particles.global_position = global_position + particles.position * scale.x
particles.emitting = true
get_parent().add_child(particles)
var timer = get_tree().create_timer(particles.lifetime, false)
timer.connect("timeout", particles, "queue_free")