aboutsummaryrefslogtreecommitdiff
path: root/entities/player/Player.gd
blob: ff11b1881889dcdeb6c1abb452a1832b4340b0e1 (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
extends KinematicBody2D

export (int) var run_speed = 200
export (int) var jump_speed = -400
export (int) var gravity = 1200
export var entity_type = "PLAYER"
export (int) var health = 90
export (int) var total_apples_need = 0
var total_apples_collected = 0
var velocity = Vector2()
var jumping = false
onready var sprite = $AnimatedSprite
onready var state_machine = $AnimationTree.get("parameters/playback")
var facing_right = true
func get_input():
	velocity.x = 0
	var right = Input.is_action_pressed('right')
	var left = Input.is_action_pressed('left')
	var jump = Input.is_action_pressed('jump')
	
	if facing_right == true:
		sprite.flip_h = false
	else:
		sprite.flip_h = true
		

	if jump and is_on_floor():
		jumping = true
		velocity.y = jump_speed
	if right:
		velocity.x += run_speed
		facing_right = true
		state_machine.travel("run")
	elif left:
		velocity.x -= run_speed
		facing_right = false
		state_machine.travel("run")

	if velocity.length() == 0:
		state_machine.travel("idle")
		
	if !jumping && !is_on_floor():
		state_machine.travel("jump")
		
func _physics_process(delta):
	get_input()
	velocity.y += gravity * delta
	if jumping and is_on_floor():
		jumping = false
	if total_apples_collected==total_apples_need:
		SignalBus.emit_signal("open_level_door",true)
	velocity = move_and_slide(velocity, Vector2(0, -1))
	print("Total Apples Needed",total_apples_need)
	
func _on_enemyHit(_health):
	state_machine.travel("hit")

func _on_apples_collected(apple_count):
	total_apples_collected += apple_count

func _ready() -> void:
	SignalBus.connect("on_hit", self, "_on_enemyHit")
	SignalBus.connect("on_apples_collected", self, "_on_apples_collected")

func play_animation(animation_name):
	pass