Mercurial > rinkhals
annotate gamelib/misc.py @ 131:4527e09dc620
Add lumberjacks.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Wed, 02 Sep 2009 21:52:52 +0000 |
parents | 672cc5598e77 |
children | 1a7000c8211c |
rev | line source |
---|---|
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
1 # Holder for misc useful classes |
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
2 |
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
3 class Position(object): |
100 | 4 """2D position / vector""" |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
5 |
100 | 6 def __init__(self, x, y): |
7 self.x = x | |
8 self.y = y | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
9 |
100 | 10 def to_tuple(self): |
11 return self.x, self.y | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
12 |
100 | 13 def dist(self, b): |
14 """Gives the distance to another position""" | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
15 |
107 | 16 return max(abs(self.x - b.x), abs(self.y - b.y)) |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
17 |
100 | 18 def __sub__(self, b): |
19 return Position(self.x - b.x, self.y - b.y) | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
20 |
100 | 21 def __add__(self, b): |
22 return Position(self.x + b.x, self.y + b.y) | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
23 |
100 | 24 def left_of(self, b): |
25 return self.x < b.x | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
26 |
100 | 27 def right_of(self, b): |
28 return self.x > b.x | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
29 |
100 | 30 def __eq__(self, b): |
31 return self.x == b.x and self.y == b.y |