summaryrefslogtreecommitdiff
path: root/Characters/Character.gd
blob: 175094d584b8371cf55e33c025853e3211d53573 (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
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
103
104
105
106
107
extends KinematicBody2D


const SPEED = 130
const JUMPFORCE = -500
const GRAVITY = 20

var velocity = Vector2()
var direction = Enum.DIRECTION.RIGHT

var canDoubleJump = false
var hasPlayedFallStop = false
var jumpHeightModifier = 0


func _physics_process(_delta):
	if Input.is_action_pressed("DIRECTION_LEFT"):
		velocity.x = -SPEED
		direction = Enum.DIRECTION.LEFT
		$Sprite.flip_h = true
		if self.is_running():
			velocity.x *= 2
			$Sprite.play("run")
		else:
			$Sprite.play("walk")
	elif Input.is_action_pressed("DIRECTION_RIGHT"):
		velocity.x = SPEED
		direction = Enum.DIRECTION.RIGHT
		$Sprite.flip_h = false
		if self.is_running():
			velocity.x *= 2
			$Sprite.play("run")
		else:
			$Sprite.play("walk")
	else:
		$Sprite.play("idle")
	
	
	velocity = move_and_slide(velocity, Vector2.UP)
	
	
	# jump and fall animation
	if not is_on_floor():
		$Sprite.play("jump")
		if has_node("CheckFallStop"):
			if velocity.y > -JUMPFORCE:
				$CheckFallLanding.set_enabled(true)
				$CheckFallStop.set_enabled(true)
				if $CheckFallLanding.is_colliding():
					$Sprite.play("fall_stop_landing")
				elif $CheckFallStop.is_colliding():
					$Sprite.play("fall_stop")
				else:
					$Sprite.play("fall")
			else:
				$CheckFallLanding.set_enabled(false)
				$CheckFallStop.set_enabled(true)
	
	
	# fall down
	velocity.y += GRAVITY
	
	
	# jump
	if Input.is_action_just_pressed("JUMP"):
		if is_on_floor():
			canDoubleJump = true
			velocity.y = JUMPFORCE
			jumpHeightModifier = JUMPFORCE
		elif not is_on_floor() and not is_on_wall() and canDoubleJump:
			canDoubleJump = false
			velocity.y = JUMPFORCE
	if Input.is_action_pressed("JUMP"):
		jumpHeightModifier += 10
		if jumpHeightModifier > 0:
			jumpHeightModifier = 0
	if Input.is_action_just_released("JUMP"):
		velocity.y -= jumpHeightModifier
	
	
	if is_on_wall():
		velocity.y *= 0.8
		$Sprite.play("wall")
		
		if Input.is_action_just_pressed("JUMP"):
			velocity.y = JUMPFORCE * 0.75
			if direction == Enum.DIRECTION.LEFT:
				Input.action_release("DIRECTION_LEFT")
				velocity.x = 2000
				$Sprite.flip_h = false
			elif direction == Enum.DIRECTION.RIGHT:
				Input.action_release("DIRECTION_RIGHT")
				velocity.x = -2000
				$Sprite.flip_h = true
	
	
	# stop
	velocity.x = lerp(velocity.x, 0, 0.7)


func is_running():
	return Input.is_action_pressed("RUN") and is_on_floor()


func check_flag():
	# $Sprite.play("dance anim")
	print("FLAG CHECKED")