-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakeHead.gd
91 lines (73 loc) · 2.28 KB
/
SnakeHead.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
extends KinematicBody2D
export (int) var speed = 200
var _max_speed = 1800
var length = 0
var velocity = Vector2()
signal catch_the_eat(fruit)
enum DirectionState {
ToTop,
ToRight,
ToBottom,
ToLeft
}
var direction;
var prev_dir;
func get_input():
if Input.is_action_just_pressed('ui_right') and (direction != DirectionState.ToLeft):
prev_dir = direction
direction = DirectionState.ToRight
if Input.is_action_just_pressed( 'ui_left') and (direction != DirectionState.ToRight):
prev_dir = direction
direction = DirectionState.ToLeft
if Input.is_action_just_pressed('ui_down') and (direction != DirectionState.ToTop):
prev_dir = direction
direction = DirectionState.ToBottom
if Input.is_action_just_pressed('ui_up') and (direction != DirectionState.ToBottom):
prev_dir = direction
direction = DirectionState.ToTop
if Input.is_action_just_pressed("ui_shift"):
speed += 400
if Input.is_action_just_released("ui_shift"):
speed -= 400
func change_rot(d):
rotation = d
func rotate_head():
if direction != prev_dir:
match direction:
DirectionState.ToBottom: change_rot(PI)
DirectionState.ToLeft: change_rot(-PI/2)
DirectionState.ToTop: change_rot(0)
DirectionState.ToRight: change_rot(PI/2)
func moving():
velocity = Vector2()
get_input()
rotate_head()
if direction == DirectionState.ToBottom:
velocity.y += 1
if direction == DirectionState.ToRight:
velocity.x += 1
if direction == DirectionState.ToLeft:
velocity.x -= 1
if direction == DirectionState.ToTop:
velocity.y -= 1
velocity = velocity.normalized() * speed
func _physics_process(delta):
moving()
velocity = move_and_slide(velocity)
func eat(fruit):
emit_signal("catch_the_eat", fruit)
if fruit["speed"]:
speed += fruit["speed"]
else:
speed += 5
# func move_tale(pos):
# var count = $TaleParts.get_child_count()
# for i in count:
# if i == 0:
# $TaleParts.get_child(0).old_position = $TaleParts.get_child(0).position
# $TaleParts.get_child(0).position = pos
# else:
# $TaleParts.get_child(i).old_position = $TaleParts.get_child(i).position
# $TaleParts.get_child(i).position = $TaleParts.get_child(i - 1).old_position
# #print($TaleParts.get_child(i).position)# = $TaleParts.get_child(i - 2).position
# #$TaleParts.get_child(i - 1).position = global_position