Add bullet shooting mechanics and refactor scenes
This commit is contained in:
parent
8501c18dd1
commit
85155b1c7d
6 changed files with 156 additions and 97 deletions
|
|
@ -1,8 +1,31 @@
|
|||
extends Area2D
|
||||
|
||||
@export var speed = 150
|
||||
@export var cooldown = 0.25
|
||||
@export var bullet_scene : PackedScene
|
||||
var can_shoot = true
|
||||
|
||||
@onready var screensize = get_viewport_rect().size
|
||||
|
||||
func _ready():
|
||||
start()
|
||||
|
||||
func start():
|
||||
position = Vector2(screensize.x / 2, screensize.y - 64)
|
||||
$GunCooldown.wait_time = cooldown
|
||||
|
||||
func shoot():
|
||||
if not can_shoot:
|
||||
return
|
||||
can_shoot = false
|
||||
$GunCooldown.start()
|
||||
var b = bullet_scene.instantiate()
|
||||
get_tree().root.add_child(b)
|
||||
b.start(position + Vector2(0, -8))
|
||||
|
||||
func _on_gun_cooldown_timeout():
|
||||
can_shoot = true
|
||||
|
||||
func _process(delta):
|
||||
var input = Input.get_vector("left", "right", "up", "down")
|
||||
if input.x > 0:
|
||||
|
|
@ -17,3 +40,5 @@ func _process(delta):
|
|||
position += input * speed * delta
|
||||
position = position.clamp(Vector2(8, 8), screensize-Vector2(8, 8))
|
||||
|
||||
if Input.is_action_pressed("shoot"):
|
||||
shoot()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue