-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldStates.py
54 lines (47 loc) · 1.38 KB
/
WorldStates.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
from Robots import Robot
from Sprites import Missile
import Vector
class objectInView:
def __init__(self, sprite, relpos):
self.relativePosition = relpos
self.colour = -1
if sprite != None:
self.colour = sprite.colour
self.direction = sprite.direction
self.isARobot = isinstance(sprite, Robot)
self.isAMissile = isinstance(sprite, Missile)
self.signal = 0
if self.isARobot:
self.signal = sprite.signal
else:
self.direction = Vector.Vec2d(0,0)
def pack(self):
return [ self.colour, self.direction.x, self.relativePosition.x, self.isARobot, self.isAMissile, self.signal ]
@staticmethod
def unpack(packed):
ret = objectInView(None, Vector.Vec2d(0,0))
ret.colour = packed[0]
ret.direction.x = packed[1]
ret.relativePosition.x = packed[2]
ret.isARobot = packed[3]
ret.isAMissile = packed[4]
ret.signal = packed[5]
return ret
# Object representing the view a robot has of the world
class WorldState:
objectsInView = []
def __init__(self):
self.objectsInView = []
def addObjectInView(self, ob):
self.objectsInView.append(ob)
def pack(self):
ret = []
for o in self.objectsInView:
ret.append( o.pack() )
return ret
@staticmethod
def unpack(packed):
ret = WorldState()
for po in packed:
ret.addObjectInView( objectInView.unpack(po) )
return ret