-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.gd
99 lines (85 loc) · 2.82 KB
/
player.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
# Copyright (c) 2021, Ben Niemann <pink@odahoda.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
class_name Player
extends Area2D
const ROT_SPEED = 200
const ACCEL = 500
const MAX_VELOCITY = 400
var velocity = Vector2(0, 0)
var shield_counter = 2
var shield_hit = 0
func _ready() -> void:
$thruster.visible = false
$thruster_smoke.emitting = false
enable_shield()
var _unused = connect("area_entered", self, "_on_collision")
func _process(delta: float) -> void:
if shield_counter >= 0:
update_shield(delta)
if Input.is_action_pressed("ui_right"):
rotation_degrees += ROT_SPEED * delta
if Input.is_action_pressed("ui_left"):
rotation_degrees -= ROT_SPEED * delta
if Input.is_action_pressed("ui_up"):
velocity += Vector2(0, -ACCEL * delta).rotated(rotation)
velocity = min(velocity.length(), MAX_VELOCITY) * velocity.normalized()
$thruster.visible = true
$thruster_smoke.emitting = true
else:
$thruster.visible = false
$thruster_smoke.emitting = false
position += velocity * delta
var screen = get_viewport_rect().size
if position.x < -32:
position.x = screen.x + 32
elif position.x >= screen.x + 32:
position.x = -32
if position.y < -32:
position.y = screen.y + 32
elif position.y >= screen.y + 32:
position.y = -32
func _on_collision(other: Area2D):
if shield_counter > 0 and other is Aster:
shield_hit = 0.4
func enable_shield():
$shield.visible = true
$shield.self_modulate = Color(1, 1, 1, 1)
$shape_shield.disabled = false
$shape_player.disabled = true
shield_counter = 5
func disable_shield():
$shield.visible = false
$shape_shield.disabled = true
$shape_player.disabled = false
shield_counter = 0
func update_shield(delta):
shield_counter = max(shield_counter - delta, 0)
if shield_counter == 0:
disable_shield()
return
if shield_hit > 0:
shield_hit = max(shield_hit - delta, 0)
var v = shield_hit / 0.4
$shield.self_modulate = Color(1, 1 - v, 1 - v, 1)
if shield_counter < 0.5:
var f = int(shield_counter / 0.03)
$shield.visible = (f % 2 == 0)
elif shield_counter < 1.0:
var f = int(shield_counter / 0.05)
$shield.visible = (f % 2 == 0)
elif shield_counter < 2.0:
var f = int(shield_counter / 0.05)
$shield.visible = (f % 4 < 3)