Mercurial > rinkhals
diff gamelib/misc.py @ 456:96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Mon, 23 Nov 2009 10:30:22 +0000 |
parents | 129de5883524 |
children | f84ad10a9625 |
line wrap: on
line diff
--- a/gamelib/misc.py Sun Nov 22 17:56:41 2009 +0000 +++ b/gamelib/misc.py Mon Nov 23 10:30:22 2009 +0000 @@ -9,7 +9,7 @@ import serializer class Position(serializer.Simplifiable): - """2D position / vector""" + """2D/3D position / vector. Assumed immutable.""" SIMPLIFY = ['x', 'y', 'z'] @@ -21,6 +21,9 @@ def to_tile_tuple(self): return self.x, self.y + def to_3d_tuple(self): + return self.x, self.y, self.z + def dist(self, b): """Gives the distance to another position""" @@ -38,8 +41,14 @@ def right_of(self, b): return self.x > b.x + def __hash__(self): + return hash(self.to_3d_tuple()) + def __eq__(self, b): - return self.x == b.x and self.y == b.y and self.z == b.z + return self.to_3d_tuple() == b.to_3d_tuple() + + def __str__(self): + return "<Position: %s>" % (self.to_3d_tuple(),) def intermediate_positions(self, b): """Only operates in two dimensions.""" @@ -63,7 +72,7 @@ for item, weight in weightings: self.weightings.append((item, weight)) self.total += weight - + def choose(self): roll = random.uniform(0, self.total) for item, weight in self.weightings: