-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.gd
161 lines (138 loc) · 4.75 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
extends KinematicBody
signal player_health_changed
signal player_just_damaged
enum {
IDLE = 0,
ALERT = 1,
SHOOTING = 2,
SEARCHING_COVER = 3,
IN_COVER = 4,
SHOOTING_COVER = 5,
}
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
export var health = 100
export var MAX_HEALTH = 100
export var PLAYER_SPEED = 25;
export var MAX_SPEED = 9999;
export var ACCELERATION = 8;
export var DEACCELERATION = 10;
export var gravity = -50
export var mouse_sensitivity = 0.001;
export var movement_enabled = false
export var in_ship = true
var air_time = 0
var velocity = Vector3.ZERO
var movement = Vector3()
var camera_direction = Vector2()
var charge_target = null
var charging = false
var charge_cooldown = 100
func die():
health = 0
$Head/DeathCamera.current = true
func hit(damage):
if charging == false:
health = clamp(health - damage,0.0,MAX_HEALTH)
print(health)
$Head/Camera/GunManager.health = health
emit_signal("player_just_damaged",health)
func heal(hp):
health = clamp(health+hp,0.0,MAX_HEALTH)
print(health)
$Head/Camera/GunManager.health = health
emit_signal("player_health_changed",health)
func get_ammo(type,amount):
if type == "Pistol":
$Head/Camera/GunManager.get_ammo("Pistol",amount)
elif type == "Shotgun":
$Head/Camera/GunManager.get_ammo("Shotgun",amount)
func check_health():
health = clamp(health,0,MAX_HEALTH)
if health == 0:
die()
func get_movement_input():
movement = Vector3()
var camera_basis = $Head.get_global_transform().basis
if(Input.is_action_pressed("ui_up")):
movement += camera_basis.z;
if(Input.is_action_pressed("ui_down")):
movement -= camera_basis.z;
if(Input.is_action_pressed("ui_left")):
movement += get_global_transform().basis.x;
if(Input.is_action_pressed("ui_right")):
movement -= get_global_transform().basis.x;
$Head/Camera/GunManager.movement = movement.normalized()
return movement.normalized()
func _ready():
emit_signal("player_health_changed",health)
$Head/Camera/Knife.hide()
func _physics_process(delta):
if movement_enabled == true:
$HUD/Data.show()
check_health()
if health != 0:
if charging == false:
charge_cooldown = clamp(charge_cooldown+(delta*10),0,100)
$HUD.update_charge_bar(charge_cooldown)
movement = get_movement_input()
velocity.y += gravity * delta
if is_on_floor():
if velocity.x < 1.5 and velocity.x > -1.5 and velocity.z < 1.5 and velocity.z > -1.5:
velocity.x = 0
velocity.z = 0
velocity.y = 0
if movement != Vector3(0,0,0):
velocity = lerp(velocity,movement*PLAYER_SPEED,delta*ACCELERATION)
else:
velocity = lerp(velocity,movement*PLAYER_SPEED,delta*DEACCELERATION)
if is_on_floor() and Input.is_key_pressed(KEY_SPACE):
velocity.y = 25
velocity = move_and_slide(velocity,Vector3.UP)
if is_on_floor():
air_time = 0
else:
air_time += delta
if Input.is_key_pressed(KEY_Q) and charge_cooldown == 100:
var cast = $Head/Camera/GunManager/RayCast.get_collider()
if cast != null and cast.name == "EnemyHitDetector":
charge_target = cast.get_node("../..")
if charge_target.state != IN_COVER or charge_target.state != SHOOTING_COVER:
$Head/Camera/Knife.show()
$Head/Camera/GunManager.guns_enabled = false
$ChargeTimer.start()
charging = true
charge_cooldown = 0
if charging == true:
$Head/Camera.look_at(charge_target.get_node("EnemyElements/HeadLoc").global_transform.origin,Vector3.UP)
$Head/Camera.rotate_object_local(Vector3(0,1,0), PI)
transform = transform.interpolate_with(charge_target.global_transform,4*delta)
if (charge_target.global_transform.origin - global_transform.origin).length() < 8:
charge_target.hit(3000,$Head/Camera.global_transform.basis.z * 50)
$Head/Camera/Knife/AnimationPlayer.play("Cube|Slice",-1,6)
charge_target = null
charging = false
func _knife_callback():
$Head/Camera/Knife.hide()
$Head/Camera/GunManager.guns_enabled = true
$Head/Camera/GunManager.switchWeapons($Head/Camera/GunManager.current_weapon)
func _on_ChargeTimer_timeout():
$Head/Camera/GunManager.guns_enabled = true
charging = false
charge_target = null
$Head/Camera/Knife.hide()
velocity = Vector3()
func _input(event):
if event is InputEventMouseMotion and charging == false:
camera_direction.x += -event.relative.x*mouse_sensitivity
camera_direction.y += event.relative.y*mouse_sensitivity
camera_direction.y = clamp(camera_direction.y,-1.5,1.55)
transform.basis = Basis()
$Head/Camera.transform.basis = Basis()
rotate_object_local(Vector3(0,1,0),camera_direction.x)
$Head/Camera.rotate_object_local(Vector3(1,0,0),camera_direction.y)
transform = transform.orthonormalized()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass