-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.gd
82 lines (71 loc) · 2.12 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
extends KinematicBody2D
export var bulletcount = 5
onready var bullet = preload("res://Bullet.tscn")
var speed = 600
var friction = 0.05
var acceleration = 0.1
var velocity = Vector2.ZERO
var screen_size
export var playing = false
signal hit
signal empty
signal shooting
# Called when the node enters the scene tree for the first time.
func _ready():
randomize()
screen_size = get_viewport_rect().size
var player_types = $Sprite.frames.get_animation_names()
$Sprite.animation = player_types[randi() % player_types.size()]
func _physics_process(delta):
if playing:
$Sprite.rotation = 0.0
if bulletcount <= 0:
emit_signal("empty")
if Input.is_action_just_pressed("ui_select"):
if bulletcount > 0:
shoot()
var input_velocity = Vector2.ZERO
# Check input for "desired" velocity
if Input.is_action_pressed("ui_right"):
input_velocity.x += 1
$Sprite.rotation = 0.4
if Input.is_action_pressed("ui_left"):
input_velocity.x -= 1
$Sprite.rotation = -0.4
if Input.is_action_pressed("ui_down"):
input_velocity.y += 1
$Sprite.rotation = 0.8
if Input.is_action_pressed("ui_up"):
input_velocity.y -= 1
$Sprite.rotation = -0.8
input_velocity = input_velocity.normalized() * speed
# If there's input, accelerate to the input velocity
if input_velocity.length() > 0:
velocity = velocity.linear_interpolate(input_velocity, acceleration)
else:
# If there's no input, slow down to (0, 0)
velocity = velocity.linear_interpolate(Vector2.ZERO, friction)
velocity = move_and_slide(velocity)
position.x = clamp(position.x, 10, screen_size.x-10)
position.y = clamp(position.y, 10, screen_size.y-10)
func shoot():
emit_signal("shooting")
print("SHOOTING")
var b = bullet.instance()
b.transform = $BulletPosition.global_transform
owner.add_child(b)
func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false
func plane_died():
playing = false
# area.queue_free()
$CollisionShape2D.set_deferred("disabled", true)
$Explosion.play()
$ExplosionSound.play()
$Sprite.hide()
emit_signal("hit")
yield(get_tree().create_timer(2.0), "timeout")
$Explosion.stop()
$ExplosionSound.stop()