/
TaskEngine.py
112 lines (82 loc) · 3.83 KB
/
TaskEngine.py
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
#####################################################################
# Frets on Fire X (FoFiX) #
# Copyright (C) 2013 FoFiX Team #
# #
# This program is free software; you can redistribute it and/or #
# modify it under the terms of the GNU General Public License #
# as published by the Free Software Foundation; either version 2 #
# of the License, or (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, #
# MA 02110-1301, USA. #
#####################################################################
class TaskEngine(object):
def __init__(self, engine):
self.engine = engine
self.clock = self.engine.clock
# self.tasks contains a list of dictionaries.
# each contain: task instance, synced or not, and paused or not
self.tasks = []
self.currentTask = None
self.deltaTime = 25 # Time between synced task updates. 25 ms
self.timeAccumulator = 0
def checkTask(self, task):
''' Check if a task exists '''
for taskData in self.tasks:
if taskData['task'] is task:
return True
return False
def addTask(self, task, synced = True):
''' Add a task '''
if not self.checkTask(task):
self.tasks.append({'task': task, 'synced': synced, 'paused': False})
task.started()
def removeTask(self, task):
''' Remove a task '''
for taskData in self.tasks:
if taskData['task'] is task:
self.tasks.remove(taskData)
task.stopped()
break
def pauseTask(self, task):
''' Pause a task '''
for taskData in self.tasks:
if taskData['task'] is task:
taskData['paused'] = True
break
def resumeTask(self, task):
''' Resume a paused task '''
for taskData in self.tasks:
if taskData['task'] is task:
taskData['paused'] = False
break
def runTask(self, task, tick=0):
self.currentTask = task
task.run(tick)
self.currentTask = None
def run(self):
''' Run one cycle of the task scheduler engine.'''
if not self.tasks:
return False
self.clock.tick(self.engine.fps)
self.timeAccumulator += self.clock.get_time()
# Synced tasks
while self.timeAccumulator >= self.deltaTime:
for taskData in self.tasks:
if taskData['paused'] or not taskData['synced']:
continue
self.runTask(taskData['task'], tick=self.deltaTime)
self.timeAccumulator -= self.deltaTime
# Unsynced tasks
for taskData in self.tasks:
if taskData['paused']or taskData['synced']:
continue
self.runTask(taskData['task'])
return True