-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.gd
56 lines (40 loc) · 1.06 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
extends Node2D
@onready var ray = $RayCast2D
signal hit
var grounded = false
var gravity = 15
var max_y_velocity = 300
var y_velocity = 0
var jumping = false
var jump_speed = -500
func _ready():
hide()
func start():
position = Vector2.ZERO
show()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
if not grounded:
y_velocity = min(max_y_velocity, y_velocity + gravity)
else:
y_velocity = 0
grounded = false
var jump = Input.is_action_just_pressed("jump") and not jumping
if jump:
jumping = true
y_velocity = jump_speed
position.y += y_velocity * delta
if not jump:
if ray.is_colliding():
var origin = ray.global_transform.origin
var collision_point = ray.get_collision_point()
var distance = abs(origin.y - collision_point.y)
var depth = abs(ray.target_position.y - distance)
grounded = true
jumping = false
position.y -= depth
func _on_hit_box_body_entered(body):
hide()
hit.emit()
func _on_head_box_body_entered(body):
y_velocity = max(y_velocity, 0)