-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.py
60 lines (47 loc) · 1.54 KB
/
scene.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
import re
import draw
from component import *
from env import Environment
import integrator
def loadConf(string):
''' Parse the config file and load the environment variables.
'''
pat = re.compile(r'\w+:')
attrstrs = [a.strip() for a in pat.split(string) if a.strip()]
clses = [c.replace(':','').strip() for c in pat.findall(string)]
components = []
for clsn, attrs in zip(clses, attrstrs):
attrs = [a.strip() for a in attrs.split('\n') if a.strip()]
clsobj = globals()[clsn]
if clsn == 'Environment':
target_obj = clsobj # overwrite environment setting
else:
target_obj = clsobj() # new a component
for a in attrs:
k,v = a.split('=')
k = k.strip()
v = v.strip()
setattr(target_obj, k, eval(v))
if clsn != 'Environment':
components.append(target_obj)
return components
def loadSceneComponents(confFileName):
fd = open(confFileName)
components = loadConf(fd.read())
fd.close()
return components
class DummyScene(draw.DrawDelegate):
def __init__(self):
self.components = []
def step(self, dt):
pass
class Scene(draw.DrawDelegate):
def __init__(self, components=[]):
self.components = components
@classmethod
def fromConfigFile(cls, confFileName):
scene = cls()
scene.components = loadSceneComponents(confFileName)
def step(self, dt):
for c in self.components:
c.step(dt, integrator.getIntegrator())