-
Notifications
You must be signed in to change notification settings - Fork 0
/
Xwing.gd
104 lines (86 loc) · 3.38 KB
/
Xwing.gd
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
extends Area2D
signal hit
export var fire_delay = 0.150
export var speed = 600
var screen_size
var Background
var last_shot_side = 0 # 0 == left, 1 == right
var ms_since_last_shot = fire_delay # ensure we can fire on first frame
var xwing_blast = preload("res://rawAssets/xwing_blast.wav")
var XwingBlast = preload("res://XwingBlast.tscn")
var XwingExplosion = preload("res://Explosion.tscn")
var dead = false
# Called when the node enters the scene tree for the first time.
func _ready():
screen_size = get_viewport_rect().size
position.x = (screen_size.x / 2) - 34
position.y = screen_size.y - 100
Background = get_tree().root.get_child(0).find_node("Background")
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
ms_since_last_shot += delta
if !dead:
var velocity = Vector2.ZERO
if Input.is_action_pressed("ui_focus_next"):
_die()
# TODO: Clean this up, build a map of directions and values to iterate over
if Input.is_action_pressed("move_left"):
velocity.x -= 1
if Input.is_action_just_pressed("move_left"):
_set_Background_velocity("x", Background.camera_velocity.x + 200)
if Input.is_action_just_released("move_left"):
_set_Background_velocity("x", Background.default_velocity.x)
if Input.is_action_pressed("move_right"):
velocity.x += 1
if Input.is_action_just_pressed("move_right"):
_set_Background_velocity("x", Background.camera_velocity.x - 200)
if Input.is_action_just_released("move_right"):
_set_Background_velocity("x", Background.default_velocity.x)
if Input.is_action_pressed("move_up"):
velocity.y -= 1
if Input.is_action_just_pressed("move_up"):
_set_Background_velocity("y", Background.camera_velocity.y + 200)
if Input.is_action_just_released("move_up"):
_set_Background_velocity("y", Background.default_velocity.y)
if Input.is_action_pressed("move_down"):
velocity.y += 1
if Input.is_action_just_pressed("move_down"):
_set_Background_velocity("y", Background.camera_velocity.y - 200)
if Input.is_action_just_released("move_down"):
_set_Background_velocity("y", Background.default_velocity.y)
if (Input.is_action_pressed("fire") && ms_since_last_shot >= fire_delay) || (Input.is_action_just_pressed("fire") && ms_since_last_shot >= (fire_delay * 0.75)):
ms_since_last_shot = 0
$BlasterPlayer.stop()
$BlasterPlayer.play()
var newBlaster = XwingBlast.instance()
newBlaster.position.x = self.position.x + (last_shot_side * 61)
newBlaster.position.y = self.position.y - 15
$Blasters.add_child(newBlaster)
if (last_shot_side == 1):
last_shot_side = 0
else:
last_shot_side = 1
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite.play()
else:
$AnimatedSprite.stop()
Background.camera_velocity = Background.default_velocity
position += velocity * delta
position.x = clamp(position.x, 5, screen_size.x - 68)
position.y = clamp(position.y, 0, screen_size.y - 80)
func _on_Xwing_body_entered(body):
_die()
func _on_Xwing_area_entered(area):
_die()
func _set_Background_velocity(dir, val):
if Background:
Background.camera_velocity[dir] = val
func _die():
$AnimatedSprite.hide()
$WilhelmPlayer.play()
dead = true
emit_signal("hit")
# Must be deferred as we can't change physics properties on a physics callback.
$CollisionPolygon2D.set_deferred("disabled", true)
add_child(XwingExplosion.instance())