Represent flow direction in water tile rendering.
authorNeil Moore <neil@s-z.org>
Sat, 31 May 2008 07:28:27 +0000 (03:28 -0400)
committerNeil Moore <neil@s-z.org>
Sat, 31 May 2008 07:28:27 +0000 (03:28 -0400)
New method loc.Water.recalc_flow to compute the rendering symbol based
  on flowx and flowy, saving its value in self.flowsign.

Use self.flowsign rather than '~' in Water.render_bg.

Call recalc_flow from Water.__init__ and Water.setattr.

Remove unused method loc.Water.set_flow (holdover from before setattr).

loc.py

diff --git a/loc.py b/loc.py
index 890271e..00c9809 100644 (file)
--- a/loc.py
+++ b/loc.py
@@ -326,23 +326,39 @@ class Water(Tile):
         Tile.__init__(self, lvl, y, x)
         self.flowy = flowy
         self.flowx = flowx
-        pic = ('~', ui.Color('Blue').cp)
+        self.recalc_flow()
 
-    def set_flow(self, flowy, flowx):
-        self.flowy = flowy
-        self.flowx = flowx
+    def recalc_flow(self):
+        """Set the appearance of this tile based on the direction of water
+        flow.  Must be called whenever flowy or flowx is changed."""
+        if self.flowy or self.flowx:
+            # Find the sector of the circle at which the flow points:
+            #   ,     ,
+            #  5 \ 6 / 7 
+            # '--_\ /_--'
+            #  4 _=x=_ 0
+            # _-- / \ --_
+            #  3 / 2 \ 1
+            #   '     `
+            angle = math.atan2(self.flowy, self.flowx)
+            idx = int(round(4.0 * angle / math.pi)) % 8
+            self.flowsign = (">\\v/<\\^/")[idx]
+        else:
+            self.flowsign = '~'
 
     def render_bg(self):
-        return ('~', random.choice((
+        return (self.flowsign, random.choice((
             ui.Color('Blue').cp, ui.Color('Blue').cp, ui.Color('Blue').cp,
             ui.Color('Cyan').cp, ui.Color('BrightBlue').cp, ui.Color('BrightBlue').cp,
             ui.Color('BrightCyan').cp, ui.Color('White').cp,
             )))
 
     def setattr(self, attr, value):
-        if not attr in ("flowy", "flowx"):
+        if attr in ("flowy", "flowx"):
+            self.__setattr__(attr, value)
+            self.recalc_flow()
+        else:
             raise Exception("Unknown attr %s for %s" % (attr, self.__class__))
-        self.__setattr__(attr, value)
 
     def passable_by(self, thing):
         return thing.is_item()  or  thing.is_creature() and thing.can_swim()