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

69 lines
2.1 KiB
GDScript

extends Node2D
export var speed = 240.0
# group to kill
var target_group = "enemy_hitbox"
# direction to fly
var direction = 1.0
#Edge to check culling, if this edge is offscreen, delete the arrow
onready var cull_edge = Vector2(5 * direction,0)
onready var player = get_parent().get_node("Player")
onready var initial_sector = Game.current_sector
func _ready():
#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:
queue_free()
#Wall Collision
func _on_Hitbox_body_entered(body):
if body is TileMap or body is StaticBody2D:
_make_sparks()
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(Game.block_text, 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) # clamp arrows above 0
queue_free()
elif area.is_in_group("arrow"):
_make_sparks()
queue_free()
func _exit_tree():
# make sure particles node sticks around until particles decay
var particles = $DustParticles
remove_child(particles)
get_parent().add_child(particles)
particles.global_position = global_position
particles.emitting = false
get_tree().create_timer(particles.lifetime, false).connect("timeout", particles, "queue_free")
func _make_sparks():
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")