summaryrefslogtreecommitdiffhomepage
path: root/Player.gd
blob: 3196dab9a4025f841128277f8a2492b8cad1a30a (plain)
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
extends KinematicBody2D


signal died(position_record)


export var GRAVITY = 1500.0
export var WALK_SPEED = 400
export var JUMP_FORCE = 400.0
export var HELD_JUMP_BOOST = 0.3

var velocity = Vector2()
var respawn_position
var position_record = []

# Called when the node enters the scene tree for the first time.
func _ready():
	respawn_position = position
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass


func _physics_process(delta):
	position_record.append(position)
	
	if Input.is_action_pressed("ui_left") and Input.is_action_pressed("ui_right"):
		velocity.x = 0
	elif Input.is_action_pressed("ui_left"):
		velocity.x = -WALK_SPEED
	elif Input.is_action_pressed("ui_right"):
		velocity.x =  WALK_SPEED
	else:
		velocity.x = 0
	
	if Input.is_action_just_pressed("jump") and self.is_on_floor():
		velocity.y = -JUMP_FORCE
	
	if Input.is_action_pressed("jump") and not self.is_on_floor():
		velocity.y += delta * -GRAVITY * HELD_JUMP_BOOST
	
	if not self.is_on_floor():
		velocity.y += delta * GRAVITY
	
	if self.is_on_ceiling():
		velocity.y = 1

	# We don't need to multiply velocity by delta because "move_and_slide" already takes delta time into account.

	# The second parameter of "move_and_slide" is the normal pointing up.
	# In the case of a 2D platformer, in Godot, upward is negative y, which translates to -1 as a normal.
	move_and_slide(velocity, Vector2.UP, false, 4, PI/4, false)
	for index in get_slide_count():
		var collision = get_slide_collision(index)
		if collision.collider.is_in_group("bodies"):
			print(collision.normal)
			collision.collider.apply_central_impulse(-collision.normal * velocity.length() * 0.3)

func respawn():
	position = respawn_position
	position_record = []
	velocity.y = 0

func die():
	position_record.append(position)
	emit_signal("died", position_record)
	respawn()
	print('dead')