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