]> Untitled Git - star-foxy.git/blob - player/player.gd
Added alternating bullets
[star-foxy.git] / player / player.gd
1 extends KinematicBody
2
3 const SPEED_FORWARD = 20
4 const SPEED_MAX = 20
5 const SPEED_TURN = 120
6 const SPEED_AIM = PI / 2
7
8 var bullet_alterating = 0
9 var velocity = Vector3()
10 var scene_bullet = preload("res://player/bullet.tscn")
11
12 # Called when the node enters the scene tree for the first time.
13 func _ready():
14         pass # Replace with function body.
15
16 func get_player_input(delta):
17         var vec_lateral = transform.basis.x
18         var vec_vertical = transform.basis.y
19         
20         
21         if Input.is_action_pressed("player_left"):
22                 velocity += SPEED_TURN * -vec_lateral * delta
23         elif Input.is_action_pressed("player_right"):
24                 velocity += SPEED_TURN * vec_lateral * delta
25                 
26         if Input.is_action_pressed("player_up"):
27                 velocity += SPEED_TURN * vec_vertical * delta
28         elif Input.is_action_pressed("player_down"):
29                 velocity += SPEED_TURN * -vec_vertical * delta
30         
31         if Input.is_action_pressed("player_aim_up"):
32                 rotate_object_local(Vector3.RIGHT, SPEED_AIM * delta)
33         elif Input.is_action_pressed("player_aim_down"):
34                 rotate_object_local(Vector3.RIGHT, -SPEED_AIM * delta)  
35                 
36         if Input.is_action_pressed("player_aim_left"):
37                 rotate(Vector3.UP, SPEED_AIM * delta)
38         elif Input.is_action_pressed("player_aim_right"):
39                 rotate(Vector3.UP, -SPEED_AIM * delta)  
40                 
41         # offense
42         if Input.is_action_pressed("player_fire"):
43                 if $bullet_timer.time_left == 0:
44                         fire_bullet()
45                 
46 func fire_bullet():
47         var bullet = scene_bullet.instance()
48         $"..".add_child(bullet)
49         
50         bullet_alterating = (bullet_alterating + 1) % 2
51         bullet.translation = translation - transform.basis.x * (4 * bullet_alterating - 2)
52         bullet.rotation = rotation
53         bullet.direction = -transform.basis.z
54         
55         $bullet_timer.start()
56         
57 func _process(delta):
58         # get velocity changes player asks for
59         get_player_input(delta)
60         
61         # move foward according to the camera's POV
62         var vec_forward = transform.basis.z
63         velocity += SPEED_FORWARD * -vec_forward * delta
64         
65         # air friction
66         var friction = SPEED_MAX / velocity.length()
67         velocity *= friction
68
69 func _physics_process(_delta):
70         velocity = move_and_slide(velocity, Vector3.UP)