-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathship.gd
More file actions
56 lines (45 loc) · 1.59 KB
/
ship.gd
File metadata and controls
56 lines (45 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
extends Area2D
@export var speed = 150
@export var bullet_cooldown = 0.13
@export var bullet_scene: PackedScene
@onready var screen_size = get_viewport_rect().size
@onready var animation_player = $AnimationPlayer
signal hit
var Y_OFFSET = 100
var BULLET_OFFSET = -8
var can_shoot = true
# Called when the node enters the scene tree for the first time.
func _ready():
start(Vector2(screen_size.x/2, screen_size.y - Y_OFFSET))
$BulletCooldown.wait_time = bullet_cooldown
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
var input = Input.get_vector("move_left", "move_right", "move_up", "move_down")
position += input*speed*delta
position = position.clamp(Vector2(16,16), screen_size - Vector2(16,16))
if (Input.is_action_just_pressed("move_left")):
animation_player.play("move_left")
if (Input.is_action_just_released("move_left")):
animation_player.play("move_left_reversed")
if (Input.is_action_just_pressed("move_right")):
animation_player.play("move_right")
if (Input.is_action_just_released("move_right")):
animation_player.play("move_right_reversed")
if (Input.is_action_pressed("shoot")):
shoot()
func shoot():
if not can_shoot:
return
can_shoot = false
$BulletCooldown.start()
var bullet = bullet_scene.instantiate()
get_tree().root.add_child(bullet)
bullet.start(position + Vector2(0, BULLET_OFFSET))
func _on_body_entered(body):
hit.emit()
#$CollisionShapeShip.set_deferred("disable", true)
func start(pos : Vector2):
position = pos
#$CollisionShapeShip.disabled = false
func _on_bullet_cooldown_timeout():
can_shoot = true