-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.gd
78 lines (61 loc) · 1.93 KB
/
character.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
extends KinematicBody2D
var walkspeed = 400
var targetPosition
var path
var moving = false
var cube
var cubePath
var lastStructure
var lastResource
func _ready():
set_fixed_process(true)
func _fixed_process(delta):
if moving && (get_pos() - targetPosition).length() <= 10:
nextPosition()
if moving:
var direction = (targetPosition - get_pos()).normalized()
var motion = direction * walkspeed * delta
move(motion)
func nextPosition():
var root = get_tree().get_root().get_node("mainNode")
if (lastStructure != null):
lastStructure.set_opacity(1)
if (lastResource != null):
lastResource.set_opacity(1)
if !path.empty():
root.cleanSelectedNodes()
targetPosition = path[0]
path.pop_front()
moving = true
if (targetPosition.y < get_pos().y):
if (targetPosition >= get_pos()):
get_node("Sprite").set_flip_h(false)
else:
get_node("Sprite").set_flip_h(true)
get_node("Sprite").play("backward")
else:
if (targetPosition <= get_pos()):
get_node("Sprite").set_flip_h(false)
else:
get_node("Sprite").set_flip_h(true)
get_node("Sprite").play("forward")
else:
moving = false
get_node("Sprite").stop()
get_node("Sprite").set_animation("stop")
root.hidePath(cubePath)
refreshOverlap()
get_tree().get_root().get_node("mainNode").showAdjacentTilesByCords(targetPosition)
func refreshOverlap():
var neighbors = get_tree().get_root().get_node("mainNode").neighbors(cube)
if (neighbors.bottom != null && neighbors.bottom.node.structure != null):
lastStructure = neighbors.bottom.node.get_node(neighbors.bottom.node.structure)
lastStructure.set_opacity(0.5)
if (neighbors.bottom != null && neighbors.bottom.node.resource != null):
lastResource = neighbors.bottom.node.get_node(neighbors.bottom.node.resource)
lastResource.set_opacity(0.5)
func moveTo(_path_, _cubePath_):
path = _path_
cubePath = []
cubePath = [] + _cubePath_
nextPosition()