-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.gd
102 lines (76 loc) · 2.7 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
extends KinematicBody2D
# https://godotengine.org/qa/25897/2d-platformer-how-to-control-jump-height-mario-like-jumps
#AnimationPlayer
onready var animationPlayer = $AnimationPlayer
#Jump
export var fall_gravity_scale := 150.0
export var low_jump_gravity_scale := 100.0
export var jump_power := 500.0
export var move_speed := 350.0
var jump_released = false
var jump = false
var game_started = false
#Physics
var velocity = Vector2()
var earth_gravity = 9.807 # m/s^2
export var gravity_scale := 100.0
var on_floor = false
var multiplier = 1.0
export(Array, AudioStream) var audio_sources = null
onready var pling_audio = $AudioStreamPlayer2D
var current_area = 0
func _physics_process(delta):
velocity.x = 0
if (!game_started and !Input.is_action_pressed("jump")):
animationPlayer.play("idle")
return
if Input.is_action_just_released("jump"):
jump_released = true
#Applying gravity to player
velocity += Vector2.DOWN * earth_gravity * gravity_scale * delta
var inAir = false
if (velocity.y != 0 and not is_on_floor()):
inAir = true
#Jump Physics
if velocity.y > 0: #Player is falling
#Falling action is faster than jumping action | Like in mario
#On falling we apply a second gravity to the player
#We apply ((gravity_scale + fall_gravity_scale) * earth_gravity) gravity on the player
velocity += Vector2.DOWN * earth_gravity * fall_gravity_scale * delta
elif velocity.y < 0 && jump_released: #Player is jumping
#Jump Height depends on how long you will hold key
#If we release the jump before reaching the max height
#We apply ((gravity_scale + low_jump_gravity_scale) * earth_gravity) gravity on the player
#It result on a lower jump
velocity += Vector2.DOWN * earth_gravity * low_jump_gravity_scale * delta
var factor = 1
if not (is_on_floor()):
factor = 0.75
if (Input.is_action_pressed("right")):
velocity.x += move_speed * factor
if not inAir:
animationPlayer.play("moveRight")
if (Input.is_action_pressed("left")):
velocity.x -= move_speed * factor
if not inAir:
animationPlayer.play("moveLeft")
if (is_on_floor() and velocity.x == 0 and not inAir):
animationPlayer.play("idle")
if is_on_floor() or jump:
if Input.is_action_just_pressed("jump"):
velocity = Vector2.UP * jump_power #Normal Jump action
jump_released = false
animationPlayer.play("jump")
if inAir:
if (velocity.x > 0):
$character.flip_h = false
elif (velocity.x < 0):
$character.flip_h = true
velocity.x *= multiplier
velocity = move_and_slide(velocity, Vector2.UP, true)
if is_on_floor(): on_floor = true
else: on_floor = false
func play_pling():
var sound = rand_range(0, audio_sources.size())
pling_audio.set_stream(audio_sources[sound])
pling_audio.play()