-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimate.py
More file actions
185 lines (159 loc) · 6.87 KB
/
animate.py
File metadata and controls
185 lines (159 loc) · 6.87 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import kivy
from kivy.animation import Animation
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import NumericProperty, ReferenceListProperty
from kivy.clock import Clock
from kivy.core.window import Window
from random import randint
from random import random
from functools import partial
Window.size = (600, 600)
class BG(Widget):
pass
class Car(Widget):
angle = NumericProperty(0)
r = NumericProperty(0)
g = NumericProperty(0)
b = NumericProperty(0)
def on_angle(self, item, angle):
if angle == 360:
item.angle = 0
class Layout(FloatLayout):
pass
class Inter1App(App):
global doNextMoveF
global doNextMoveR
doNextMoveF = []
doNextMoveR = []
def intersecProg(self, layout, inQueue, outQueue, inter1, *largs):
path = ((0, 0), (1, 0), (1, 1), (0, 1))
c = Window.center
c = [c[0] - 20, c[1] - 20]
roadAlign = 40
pathAnim = [
(c[0] - roadAlign, c[1] + roadAlign),
(c[0] - roadAlign, c[1] - roadAlign),
(c[0] + roadAlign, c[1] - roadAlign),
(c[0] + roadAlign, c[1] + roadAlign),
]
inQueueAnim = [
(c[0] - roadAlign, c[1] + roadAlign * 11),
(c[0] - roadAlign * 11, c[1] - roadAlign),
(c[0] + roadAlign, c[1] - roadAlign * 11),
(c[0] + roadAlign * 11, c[1] + roadAlign),
]
outQueueAnim = [
(c[0] + roadAlign, c[1] + roadAlign * 11),
(c[0] - roadAlign * 11, c[1] + roadAlign),
(c[0] - roadAlign, c[1] - roadAlign * 11),
(c[0] + roadAlign * 11, c[1] - roadAlign),
]
nbrCars = sum([len(way) for way in inQueue])
def moveCar(car, futurePos, ttDur):
oldPos = car[3].pos[:]
angle = car[3].angle
movex, movey = futurePos[0] - oldPos[0], futurePos[1] - oldPos[1]
if movex < 0:
correctAngle = 270
if angle == 0: correctAngle = -90
elif movex > 0:
correctAngle = 90
elif movey < 0:
correctAngle = 0
if angle == 270: correctAngle = 360
else:
correctAngle = 180
def forwardAnim(futurePos, dur, *largs):
doNextMoveR.pop()
anim = Animation(pos=futurePos, duration=ttDur-dur*1.3)
doNextMoveF.append(False)
anim.start(largs[1])
anim.bind(on_complete=partial(popNextMoveF))
dur = 0.15
if correctAngle == angle: dur = 0
doNextMoveR.append(False)
rotat = Animation(angle=correctAngle, duration=dur)
rotat.start(car[3])
rotat.bind(on_complete=partial(forwardAnim, futurePos, dur))
def popNextMoveF(anim, widget):
doNextMoveF.pop()
if len(doNextMoveF)+len(doNextMoveR) == 0:
# move by 1 every car
for pathIndex in range(0, len(path)):
x, y = path[pathIndex]
newInterCase = {}
if inter1[x][y] != {0: []}:
for time, car in inter1[x][y].items():
carDestination = (car[1] + car[2] + 1) % 4
if path[carDestination] == path[(pathIndex+1)%4] and time == 0:
outQueue[carDestination].append(car) # move car to outqueue if reach destination
moveCar(car, outQueueAnim[carDestination], 1)
elif time != 0:
newInterCase.update({time - 1: car}) # move by 1 on timeline in case
inter1[x][y] = newInterCase # change the state of the case
if 0 in inter1[x][y]:
moveCar(newInterCase[0], pathAnim[pathIndex], 1)
newInQueue = []
for wayQueue in inQueue: # remove or reduce timer for cars in inqueue
itemToKeep = []
for i in range(0, len(wayQueue)):
item = wayQueue[i]
if item[1] != 0:
if item[1] != -1:
item[1] -= 1
itemToKeep.append(item)
newInQueue.append(itemToKeep[:])
inQueue = newInQueue[:]
# take care of queue (add paths for cars)
for way in range(0, 4):
for carIndex in range(0, len(inQueue[way])):
if inQueue[way][carIndex][1] == -1:
car = inQueue[way][carIndex][0]
doNotPath = True
markdown = 0
while doNotPath: # search for path
doNotPath = False
markdown += 1
for i in range(way, way + car[2] + 1):
pos = i % 4
x, y = path[pos]
doNotPath = doNotPath or (markdown + i - way) in inter1[x][y]
for i in range(way, way + car[2] + 1): # Add whole path at end timeline
pos = i % 4
x, y = path[pos]
inter1[x][y].update({markdown + i - way: car})
inQueue[way][carIndex][1] = markdown - 1 # Add when car exit inQueue
layout.add_widget(car[3])
anim = Animation(pos=inQueueAnim[car[1]], duration=0)
anim &= Animation(angle=90 * car[1], duration=0)
doNextMoveF.append(False)
oldPos = car[3].pos[:]
anim.start(car[3])
anim.bind(on_complete=partial(popNextMoveF))
'''print "This move:"
for i in range(0, 2):
for j in range(0, 2):
print "In case " + str(i) + ", " + str(j)
list = []
for key, item in inter1[i][j].items():
list.append(str(key)+": "+str(item[0]))
print list'''
def build(self):
inQueue = [[], [], [], []]
inter1 = [
[{}, {}],
[{}, {}]
]
outQueue = [[], [], [], []]
nbrCars = 50
for i in range(1, nbrCars + 1):
startPt = randint(0, 3)
inQueue[startPt].append([[i, startPt, randint(0, 2), Car(r=random()*0.4+0.5, g=random()*0.4+0.5, b=random()*0.4+0.5)], -1])
layout = Layout()
layout.add_widget(BG())
Clock.schedule_interval(partial(self.intersecProg, layout, inQueue, outQueue, inter1), 1/60)
return layout
if __name__ == '__main__':
Inter1App().run()