-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.gd
83 lines (67 loc) · 2.18 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
extends KinematicBody2D
export (int) var speed = 200
var velocity = Vector2()
var Bullet = preload("res://Bullet.tscn")
var Explosion = preload("res://Explode.tscn")
var MAX_BULLETS = 5
var shotready = true
signal dead
onready var joystick_move := $Analog/Joystick_Left
onready var joystick_look := $Analog/Joystick_Right
var mode = 'desktop'
func _ready():
connect("dead", get_tree().get_root().get_node("World"), 'restart_scene')
if mode == 'desktop':
$Analog.queue_free()
func joystick_get_input():
if joystick_move.joystick_active:
$Sprite.rotation = joystick_move.joystick_vector.angle() - PI/2
velocity = move_and_slide(-joystick_move.joystick_vector * speed)
if joystick_look.joystick_active:
$Sprite2.rotation = (-joystick_look.joystick_vector).angle() - PI/2
var all_bullets = get_tree().get_nodes_in_group("bullets")
if len(all_bullets) < MAX_BULLETS and shotready:
shoot()
shotready = false
$ShootDelay.start()
func get_input():
$Sprite2.rotation = get_global_mouse_position().angle_to_point(position) - PI/2
velocity = Vector2()
if Input.is_action_pressed('right'):
velocity.x += 1
if Input.is_action_pressed('left'):
velocity.x -= 1
if Input.is_action_pressed('down'):
velocity.y += 1
if Input.is_action_pressed('up'):
velocity.y -= 1
velocity = velocity.normalized() * speed
velocity = move_and_slide(velocity)
$Sprite.rotation = velocity.normalized().angle() - PI/2
if Input.is_action_just_pressed("click"):
var all_bullets = get_tree().get_nodes_in_group("bullets")
print(all_bullets)
if len(all_bullets) < MAX_BULLETS:
shoot()
if Input.is_action_pressed('restart'):
Game.lives = 3
Game.level = 1
get_tree().change_scene("res://Levels/World1.tscn")
func _physics_process(delta):
if mode == 'desktop':
get_input()
if mode == 'mobile':
joystick_get_input()
func shoot():
var b = Bullet.instance()
b.add_to_group('bullets')
b.start($Sprite2.global_position + Vector2(40, 0).rotated($Sprite2.rotation + PI/2), $Sprite2.rotation + PI/2)
get_parent().add_child(b)
func hit():
var e = Explosion.instance()
e.start(position)
get_parent().add_child(e)
emit_signal("dead")
queue_free()
func _on_ShootDelay_timeout():
shotready = true