48 lines
1.4 KiB
GDScript
48 lines
1.4 KiB
GDScript
class_name Gun
|
|
extends Position2D
|
|
# Represents a weapon that spawns and shoots bullets.
|
|
# The Cooldown timer controls the cooldown duration between shots.
|
|
|
|
|
|
const BULLET_VELOCITY = 350
|
|
const Bullet = preload("res://Projectile.tscn")
|
|
|
|
#onready var sound_shoot = $Shoot
|
|
onready var timer = $Cooldown
|
|
onready var offset_position = Vector2(transform.origin.x, transform.origin.y)
|
|
|
|
# This method is only called by Player.gd.
|
|
func shoot(direction = 1):
|
|
#self.set_position(Vector2(position.x * direction, position.y))
|
|
#transform.x = transform.x * direction
|
|
#Transform2D.FLIP_Y
|
|
#transform.origin.x = transform.origin.x * direction
|
|
# if direction < 0:
|
|
# var t = Transform2D()
|
|
# # Translation
|
|
# t.origin = Vector2(position.x * -1, position.y)
|
|
# transform = t
|
|
# elif direction > 0:
|
|
# var t = Transform2D()
|
|
# # Translation
|
|
# t.origin = Vector2(position.x, position.y)
|
|
# transform = t
|
|
|
|
print("Fire Debug: ", direction, " Gun position: ", position, transform.origin)
|
|
# position.x = position.x * direction #shifting position before fire was unreliable
|
|
if not timer.is_stopped():
|
|
return false
|
|
var bullet = Bullet.instance()
|
|
# if (direction > 0):
|
|
# bullet.global_position = global_position
|
|
# else:
|
|
# bullet.global_position = global_position - Vector2(position.x * 2, 0)
|
|
bullet.global_position = global_position
|
|
bullet.linear_velocity = Vector2(direction * BULLET_VELOCITY, 0)
|
|
|
|
bullet.set_as_toplevel(true)
|
|
add_child(bullet)
|
|
# sound_shoot.play()
|
|
timer.start()
|
|
return true
|