|
@@ -1,3 +1,4 @@
|
|
1
|
+import time
|
1
|
2
|
from synergine2.cycle import CycleManager
|
2
|
3
|
from synergine2.simulation import Simulation
|
3
|
4
|
from synergine2.terminals import TerminalManager
|
|
@@ -10,11 +11,13 @@ class Core(object):
|
10
|
11
|
simulation: Simulation,
|
11
|
12
|
cycle_manager: CycleManager,
|
12
|
13
|
terminal_manager: TerminalManager=None,
|
13
|
|
-
|
|
14
|
+ cycles_per_seconds: int=1,
|
14
|
15
|
):
|
15
|
16
|
self.simulation = simulation
|
16
|
17
|
self.cycle_manager = cycle_manager
|
17
|
18
|
self.terminal_manager = terminal_manager or TerminalManager([])
|
|
19
|
+ self._loop_delta = 1./cycles_per_seconds
|
|
20
|
+ self._current_cycle_start_time = None
|
18
|
21
|
|
19
|
22
|
def run(self):
|
20
|
23
|
try:
|
|
@@ -26,6 +29,8 @@ class Core(object):
|
26
|
29
|
self.terminal_manager.send(start_package)
|
27
|
30
|
|
28
|
31
|
while True:
|
|
32
|
+ self._start_cycle()
|
|
33
|
+
|
29
|
34
|
events = []
|
30
|
35
|
packages = self.terminal_manager.receive()
|
31
|
36
|
for package in packages:
|
|
@@ -47,8 +52,20 @@ class Core(object):
|
47
|
52
|
self.simulation.subjects.adds = []
|
48
|
53
|
self.simulation.subjects.removes = []
|
49
|
54
|
|
50
|
|
- import time
|
51
|
|
- time.sleep(1) # TODO: tick control
|
|
55
|
+ self._end_cycle()
|
52
|
56
|
except KeyboardInterrupt:
|
53
|
57
|
pass # Just stop while
|
54
|
58
|
self.terminal_manager.stop()
|
|
59
|
+
|
|
60
|
+ def _start_cycle(self):
|
|
61
|
+ self._current_cycle_start_time = time.time()
|
|
62
|
+
|
|
63
|
+ def _end_cycle(self) -> None:
|
|
64
|
+ """
|
|
65
|
+ Make a sleep if cycle duration take less time of wanted (see
|
|
66
|
+ cycles_per_seconds constructor parameter)
|
|
67
|
+ """
|
|
68
|
+ cycle_duration = time.time() - self._current_cycle_start_time
|
|
69
|
+ sleep_time = self._loop_delta - cycle_duration
|
|
70
|
+ if sleep_time > 0:
|
|
71
|
+ time.sleep(sleep_time)
|