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