/
world.py
111 lines (87 loc) · 2.37 KB
/
world.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
import encoding
nextid = 1
all = {}
toadd = []
todelete = []
notify = None;
named = {}
class BadDataFormat(Exception):
def __init__(self):
pass
class Entity(object):
id = 0
living = 1
pos = (0, 0, 0)
name = None
type = None
def __init__(self, type):
global nextid
global toadd
self.id = nextid
nextid = nextid + 1;
toadd.append(self)
self.type = type
def step(self):
pass
def die(self):
global todelete
if self.living:
self.living = 0
todelete.append(self)
def onremove(self):
pass
def onadd(self):
pass
def getrep(self):
return ",".join([self.type] + map(str, self.pos))
def unpack(self, rep):
q = rep.split(',', 4)
if (len(q) < 4):
raise BadDataFormat()
self.type = encoding.dequote(q[0])
self.pos = (float(encoding.dequote(q[1])), float(encoding.dequote(q[2])), float(encoding.dequote(q[3])))
if (len(q) > 4):
return q[4]
return ""
def notifylisteners(self, cmd):
global notify
if notify:
notify.command(cmd)
def step():
global toadd
global all
global todelete
global notify
global named
for a in toadd:
print("adding object " + str(a.id) + " " + str(a.name))
if a.name != None:
if named.has_key(a.name):
prev = named[a.name]
print("Removing previous instance of name " + a.name + " id " + str(prev.id) + " for new id " + str(a.id))
if notify:
notify.removeentity(prev)
del(all[prev.id])
prev.onremove()
if notify:
notify.addentity(a)
named[a.name] = a
all[a.id] = a;
a.onadd()
toadd[:] = []
for ent in all.values():
ent.step()
for d in todelete:
print("removing object " + str(d.id) + " " + str(d.name))
if d.name != None:
if named.has_key(d.name):
del(named[d.name])
if notify:
notify.removeentity(d)
if all.has_key(d.id):
del(all[d.id])
d.onremove()
todelete[:] = []
def dumpentities(targ):
for ent in all.values():
targ.dumpentity(ent)