-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChoco.gd
124 lines (98 loc) · 3.14 KB
/
Choco.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
112
113
114
115
116
117
118
119
120
121
122
123
124
extends KinematicBody2D
signal player_moving_signal
signal player_stopped_signal
export var walk_speed = 10.0
const TILE_SIZE = 16
onready var anim_tree = $AnimationTree
onready var anim_state = anim_tree.get("parameters/playback")
onready var ray = $RayCast2D
enum PlayerState { IDLE, TURNING, WALKING }
enum FacingDirection { LEFT, RIGHT, UP, DOWN }
var player_state = PlayerState.IDLE
var facing_direction = FacingDirection.DOWN
var score = 0
var cokelat = 0
var initial_position = Vector2(0, 0)
var input_direction = Vector2(0, 0)
var is_moving = false
var percent_moved_to_next_title = 0.0
# Called when the node enters the scene tree for the first time.
func _ready():
anim_tree.active = true
initial_position = position
func _physics_process(delta):
if player_state == PlayerState.TURNING:
return
if is_moving == false:
process_player_input()
elif input_direction != Vector2.ZERO:
anim_state.travel("Walk")
move(delta)
else:
anim_state.travel("Idle")
is_moving = false
func process_player_input():
if input_direction.y == 0:
input_direction.x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))
if input_direction.x == 0:
input_direction.y = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))
if input_direction != Vector2.ZERO:
anim_tree.set("parameters/Idle/blend_position", input_direction)
anim_tree.set("parameters/Walk/blend_position", input_direction)
anim_tree.set("parameters/Turn/blend_position", input_direction)
if need_to_turn():
player_state = PlayerState.TURNING
anim_state.travel("Turn")
else:
initial_position = position
is_moving = true
else :
anim_state.travel("Idle")
func need_to_turn():
var new_facing_direction
if input_direction.x < 0:
new_facing_direction = FacingDirection.LEFT
elif input_direction.x > 0:
new_facing_direction = FacingDirection.RIGHT
elif input_direction.y < 0:
new_facing_direction = FacingDirection.UP
elif input_direction.y > 0:
new_facing_direction = FacingDirection.DOWN
if facing_direction != new_facing_direction:
facing_direction = new_facing_direction
return true
facing_direction = new_facing_direction
return false
func finished_turning():
player_state = PlayerState.IDLE
func move(delta):
var desired_step: Vector2 = input_direction * TILE_SIZE / 2
ray.cast_to = desired_step
ray.force_raycast_update()
if !ray.is_colliding():
if percent_moved_to_next_title == 0:
emit_signal("player_moving_signal")
percent_moved_to_next_title += walk_speed * delta
if percent_moved_to_next_title >= 1.0:
position = initial_position + (input_direction * TILE_SIZE)
percent_moved_to_next_title = 0.0
is_moving = false
emit_signal("player_stopped_signal")
else:
position = initial_position + (input_direction * TILE_SIZE * percent_moved_to_next_title)
else:
is_moving = false
func add_cokelat():
cokelat = cokelat+1
func _on_beruangg_dead():
queue_free()
func _on_beruangg2_dead():
queue_free()
func _on_beruangg3_dead():
queue_free()
func _on_beruangver_dead():
queue_free()
func _on_beruangver2_dead():
queue_free()
func _on_Area2D_finish():
queue_free()