-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.gd
112 lines (87 loc) · 2.84 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
103
104
105
106
107
108
109
110
111
extends KinematicBody2D
enum DIRECTION { Right, Left }
const UP := Vector2(0, -1)
const CELL_DIGGING_TIME = 0.25
export var gravity := 20
export var speed := 200
export var jump_height := -200
export var hole_size := Vector2(7,7)
signal on_chicken_catch
var motion := Vector2()
var is_diging = false
var is_in_underground = false
var digging_cell = null
var digging_time = CELL_DIGGING_TIME
var direction = DIRECTION.Right
onready var sfx_chicken_1 := $"SFX/chicken-1"
onready var sfx_chicken_2 := $"SFX/chicken-2"
onready var ground_tm: Node2D = get_tree().get_root().get_node("Root/Ground")
onready var dig_player := $SFX/Digging
onready var jump_player := $SFX/Jump
onready var sprite := $Sprite
func changeDirection(new_direction: int):
if (direction != new_direction):
direction = new_direction
sprite.set_flip_h(new_direction)
func set_digging_cell(cell):
digging_cell = cell
digging_time = CELL_DIGGING_TIME
func _physics_process(delta):
is_in_underground = true if global_position.y > 0 else false
motion.y = 0 if is_in_underground else motion.y + gravity
motion.x = 0
if Input.is_action_pressed("ui_right"):
motion.x = speed
changeDirection(DIRECTION.Right)
if Input.is_action_pressed("ui_left"):
motion.x = -speed
changeDirection(DIRECTION.Left)
if is_in_underground and Input.is_action_pressed("ui_down"):
motion.y = speed
if is_in_underground and Input.is_action_pressed("ui_up"):
motion.y = -speed
if not is_in_underground and is_on_floor():
if Input.is_action_just_pressed("ui_up"):
if jump_player.playing:
jump_player.stop()
jump_player.play()
motion.y = jump_height
if Input.is_key_pressed(KEY_SPACE):
is_diging = true
else:
is_diging = false
set_digging_cell(null)
if is_diging and digging_cell == null:
var dir: Vector2
var border: Vector2
if Input.is_action_pressed("ui_down"):
dir = Vector2(0, 1)
border = global_position + Vector2(0, hole_size.y)
elif Input.is_action_pressed("ui_up"):
dir = Vector2(0, -1)
border = global_position + Vector2(0, -hole_size.y)
elif Input.is_action_pressed("ui_left"):
dir = Vector2(-1, 0)
border = global_position + Vector2(-hole_size.x, 0)
elif Input.is_action_pressed("ui_right"):
dir = Vector2(1, 0)
border = global_position + Vector2(hole_size.x, 0)
if dir:
var cell = ground_tm.get_direction_cell(dir, border)
set_digging_cell(cell)
if is_diging and digging_cell != null:
digging_time -= delta
if digging_time < 0:
var removed = ground_tm.remove_cell(digging_cell)
if removed:
if dig_player.playing:
dig_player.stop()
dig_player.play()
set_digging_cell(null)
motion = move_and_slide(motion, UP)
func _on_EnemyDetector_body_shape_entered(body_id, body, body_shape, area_shape):
if (rand_range(-1, 1) > 0):
sfx_chicken_1.play(0)
else:
sfx_chicken_2.play(0)
emit_signal("on_chicken_catch", body)