1 | extends KinematicBody
|
---|
2 |
|
---|
3 | # Popup menu
|
---|
4 |
|
---|
5 | var Pause_Popup_tscn = preload("res://Pause_Popup.tscn")
|
---|
6 | var Pause_Popup
|
---|
7 |
|
---|
8 | ###################-VARIABLES-####################
|
---|
9 |
|
---|
10 | # Camera
|
---|
11 | export(float) var mouse_sensitivity = 8.0
|
---|
12 | export(NodePath) var head_path = "Head"
|
---|
13 | export(NodePath) var cam_path = "Head/Camera"
|
---|
14 | export(float) var FOV = 80.0
|
---|
15 | var mouse_axis := Vector2()
|
---|
16 | onready var head: Spatial = get_node(head_path)
|
---|
17 | onready var cam: Camera = get_node(cam_path)
|
---|
18 | # Move
|
---|
19 | var velocity := Vector3()
|
---|
20 | var direction := Vector3()
|
---|
21 | var move_axis := Vector2()
|
---|
22 | var snap := Vector3() # Connecting to ground
|
---|
23 | var falling := Vector3()
|
---|
24 | var sprint_enabled := true
|
---|
25 | var sprinting := false
|
---|
26 |
|
---|
27 | # Damage
|
---|
28 | var max_fall := Vector3() # Total disatance falled down
|
---|
29 | var fall_threshold_damage = 17 # distance down a player has to fall before taking damage
|
---|
30 | var myvariable
|
---|
31 |
|
---|
32 | # Walk
|
---|
33 | const FLOOR_MAX_ANGLE: float = deg2rad(46.0)
|
---|
34 | export(float) var gravity = 20.0
|
---|
35 | export(int) var walk_speed = 10
|
---|
36 | export(int) var sprint_speed = 16
|
---|
37 | export(int) var acceleration = 8
|
---|
38 | export(int) var deacceleration = 10
|
---|
39 | export(float, 0.0, 1.0, 0.05) var air_control = 0.3
|
---|
40 | export(int) var jump_height = 12
|
---|
41 | var _speed: int
|
---|
42 | var _is_sprinting_input := false
|
---|
43 | var _is_jumping_input := false
|
---|
44 | # Fall damage
|
---|
45 | enum GROUNDED_STATE {
|
---|
46 | GROUNDED,
|
---|
47 | MIDAIR,
|
---|
48 | TOUCHDOWN
|
---|
49 | }
|
---|
50 | var player_state = GROUNDED_STATE.GROUNDED
|
---|
51 | # Player Stats
|
---|
52 | var health = 100
|
---|
53 | var stamina = 100
|
---|
54 | var stamina_delay = 10
|
---|
55 |
|
---|
56 | ##################################################
|
---|
57 |
|
---|
58 | # Called when the node enters the scene tree
|
---|
59 | func _ready() -> void:
|
---|
60 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
---|
61 | cam.fov = FOV
|
---|
62 |
|
---|
63 | # Called every frame. 'delta' is the elapsed time since the previous frame
|
---|
64 | func _process(_delta: float) -> void:
|
---|
65 | move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
|
---|
66 | move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
|
---|
67 |
|
---|
68 | if Input.is_action_just_pressed("move_jump"):
|
---|
69 | _is_jumping_input = true
|
---|
70 |
|
---|
71 | if Input.is_action_pressed("move_sprint"):
|
---|
72 | _is_sprinting_input = true
|
---|
73 |
|
---|
74 | # Recapture mouse when resuming
|
---|
75 | if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
|
---|
76 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
---|
77 |
|
---|
78 |
|
---|
79 | # Called every physics tick. 'delta' is constant
|
---|
80 | func _physics_process(delta: float) -> void:
|
---|
81 | walk(delta)
|
---|
82 |
|
---|
83 | # Fall damage calculation
|
---|
84 | match player_state:
|
---|
85 | GROUNDED_STATE.GROUNDED:
|
---|
86 | falling = Vector3.ZERO
|
---|
87 | if not is_on_floor():
|
---|
88 | player_state = GROUNDED_STATE.MIDAIR
|
---|
89 | GROUNDED_STATE.MIDAIR:
|
---|
90 | falling += Vector3.DOWN * gravity * delta
|
---|
91 | if velocity.y <= -fall_threshold_damage:
|
---|
92 | max_fall = velocity
|
---|
93 | if is_on_floor():
|
---|
94 | player_state = GROUNDED_STATE.TOUCHDOWN
|
---|
95 | GROUNDED_STATE.TOUCHDOWN:
|
---|
96 | if falling.length() >= 17 and max_fall.y <= -fall_threshold_damage:
|
---|
97 | health -= round(-(max_fall.y + fall_threshold_damage))
|
---|
98 | if round(-(max_fall.y + fall_threshold_damage)) > 5:
|
---|
99 | health -= round(-(max_fall.y + fall_threshold_damage)*9)
|
---|
100 | else:
|
---|
101 | if round(-(max_fall.y + fall_threshold_damage)) > 10:
|
---|
102 | health -= round(-(max_fall.y + fall_threshold_damage)*15)
|
---|
103 | get_node("Head/AnimationPlayer").play("FallDamage")
|
---|
104 | get_node("HUD/Control/HealthBar").value = health
|
---|
105 | max_fall = Vector3.ZERO
|
---|
106 | player_state = GROUNDED_STATE.GROUNDED
|
---|
107 |
|
---|
108 |
|
---|
109 |
|
---|
110 | # if velocity.y > 5:
|
---|
111 | # falling = true
|
---|
112 | # if is_on_floor() and falling:
|
---|
113 | # health -+ 1
|
---|
114 | # print("fall damage taken")
|
---|
115 | # falling = false
|
---|
116 |
|
---|
117 |
|
---|
118 | # Called when there is an input event
|
---|
119 | func _input(event: InputEvent) -> void:
|
---|
120 | if event is InputEventMouseMotion:
|
---|
121 | mouse_axis = event.relative
|
---|
122 | camera_rotation()
|
---|
123 |
|
---|
124 | # Testing tool to instent regen health
|
---|
125 | if Input.is_action_just_pressed("Fire"):
|
---|
126 | health = 100
|
---|
127 | get_node("HUD/Control/HealthBar").value = health
|
---|
128 |
|
---|
129 | func walk(delta: float) -> void:
|
---|
130 | direction_input()
|
---|
131 |
|
---|
132 | if is_on_floor():
|
---|
133 | snap = -get_floor_normal() - get_floor_velocity() * delta
|
---|
134 |
|
---|
135 | # Workaround for sliding down after jump on slope
|
---|
136 | if velocity.y < 0:
|
---|
137 | velocity.y = 0
|
---|
138 |
|
---|
139 | jump()
|
---|
140 | else:
|
---|
141 | # Workaround for 'vertical bump' when going off platform
|
---|
142 | if snap != Vector3.ZERO && velocity.y != 0:
|
---|
143 | velocity.y = 0
|
---|
144 |
|
---|
145 | snap = Vector3.ZERO
|
---|
146 |
|
---|
147 | velocity.y -= gravity * delta
|
---|
148 |
|
---|
149 | sprint(delta)
|
---|
150 |
|
---|
151 | accelerate(delta)
|
---|
152 |
|
---|
153 | velocity = move_and_slide_with_snap(velocity, snap, Vector3.UP, true, 4, FLOOR_MAX_ANGLE)
|
---|
154 | _is_jumping_input = false
|
---|
155 | _is_sprinting_input = false
|
---|
156 |
|
---|
157 |
|
---|
158 | func camera_rotation() -> void:
|
---|
159 | if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
|
---|
160 | return
|
---|
161 | if mouse_axis.length() > 0:
|
---|
162 | var horizontal: float = -mouse_axis.x * (mouse_sensitivity / 100)
|
---|
163 | var vertical: float = -mouse_axis.y * (mouse_sensitivity / 100)
|
---|
164 |
|
---|
165 | mouse_axis = Vector2()
|
---|
166 |
|
---|
167 | rotate_y(deg2rad(horizontal))
|
---|
168 | head.rotate_x(deg2rad(vertical))
|
---|
169 |
|
---|
170 | # Clamp mouse rotation
|
---|
171 | var temp_rot: Vector3 = head.rotation_degrees
|
---|
172 | temp_rot.x = clamp(temp_rot.x, -90, 90)
|
---|
173 | head.rotation_degrees = temp_rot
|
---|
174 |
|
---|
175 |
|
---|
176 | func direction_input() -> void:
|
---|
177 | direction = Vector3()
|
---|
178 | var aim: Basis = get_global_transform().basis
|
---|
179 | if move_axis.x >= 0.5:
|
---|
180 | direction -= aim.z
|
---|
181 | if move_axis.x <= -0.5:
|
---|
182 | direction += aim.z
|
---|
183 | if move_axis.y <= -0.5:
|
---|
184 | direction -= aim.x
|
---|
185 | if move_axis.y >= 0.5:
|
---|
186 | direction += aim.x
|
---|
187 | direction.y = 0
|
---|
188 | direction = direction.normalized()
|
---|
189 |
|
---|
190 |
|
---|
191 | func accelerate(delta: float) -> void:
|
---|
192 | # Where would the player go
|
---|
193 | var _temp_vel: Vector3 = velocity
|
---|
194 | var _temp_accel: float
|
---|
195 | var _target: Vector3 = direction * _speed
|
---|
196 |
|
---|
197 | _temp_vel.y = 0
|
---|
198 | if direction.dot(_temp_vel) > 0:
|
---|
199 | _temp_accel = acceleration
|
---|
200 |
|
---|
201 | else:
|
---|
202 | _temp_accel = deacceleration
|
---|
203 |
|
---|
204 | if not is_on_floor():
|
---|
205 | _temp_accel *= air_control
|
---|
206 |
|
---|
207 | # Interpolation
|
---|
208 | _temp_vel = _temp_vel.linear_interpolate(_target, _temp_accel * delta)
|
---|
209 |
|
---|
210 | velocity.x = _temp_vel.x
|
---|
211 | velocity.z = _temp_vel.z
|
---|
212 |
|
---|
213 | # Make too low values zero
|
---|
214 | if direction.dot(velocity) == 0:
|
---|
215 | var _vel_clamp := 0.01
|
---|
216 | if abs(velocity.x) < _vel_clamp:
|
---|
217 | velocity.x = 0
|
---|
218 | if abs(velocity.z) < _vel_clamp:
|
---|
219 | velocity.z = 0
|
---|
220 |
|
---|
221 |
|
---|
222 | func jump() -> void:
|
---|
223 | if _is_jumping_input:
|
---|
224 | velocity.y = jump_height
|
---|
225 | snap = Vector3.ZERO
|
---|
226 |
|
---|
227 |
|
---|
228 | func sprint(delta: float) -> void:
|
---|
229 | if can_sprint():
|
---|
230 | if stamina >= 0 && stamina_delay >= 0:
|
---|
231 | _speed = sprint_speed
|
---|
232 | cam.set_fov(lerp(cam.fov, FOV * 1.25, delta * 8))
|
---|
233 | sprinting = true
|
---|
234 | stamina -= 0.5
|
---|
235 | if stamina >= 1:
|
---|
236 | stamina_delay = 10
|
---|
237 | get_node("HUD/Control/StaminaBar").value = stamina
|
---|
238 | else:
|
---|
239 | _speed = walk_speed
|
---|
240 | cam.set_fov(lerp(cam.fov, FOV, delta * 8))
|
---|
241 | sprinting = false
|
---|
242 | if stamina_delay >= 1:
|
---|
243 | stamina_delay -= 0.1
|
---|
244 | else:
|
---|
245 | if stamina <= 100:
|
---|
246 | stamina += 0.1
|
---|
247 | get_node("HUD/Control/StaminaBar").value = stamina
|
---|
248 |
|
---|
249 | func can_sprint() -> bool:
|
---|
250 | return (sprint_enabled and is_on_floor() and _is_sprinting_input and move_axis.x >= 0.5)
|
---|