changeset 308:31bbd0ed9b63

Merge the two orientation queues into one
author Stefano Rivera <stefano@rivera.za.net>
date Fri, 16 Sep 2011 18:30:48 +0200
parents 09ec51b9d385
children 67312228c158
files mamba/snake.py
diffstat 1 files changed, 10 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/mamba/snake.py	Fri Sep 16 18:14:26 2011 +0200
+++ b/mamba/snake.py	Fri Sep 16 18:30:48 2011 +0200
@@ -28,7 +28,6 @@
         self.mutation = None
         self.coiled = True
         self._orientation_changes = []
-        self._already_oriented = False
 
     head = property(fget=lambda self: self.segments[0])
     tail = property(fget=lambda self: self.segments[-1])
@@ -78,9 +77,8 @@
             shifted, ds = self.head.shift_head(ds)
             if shifted:
                 self.coiled = False
-                self._already_oriented = False
                 self.head.shifted_tile()
-                self.head.set_orientation(self.orientation)
+                self._pop_orientation_queue()
             else:
                 break
             if self.pending_segments:
@@ -98,22 +96,23 @@
             segment.shift_pixels(ds)
             world.interact(segment)
 
-        self._set_orientation()
-
     def send_new_direction(self, orientation):
         self._orientation_changes.append(orientation)
-        self._set_orientation()
+        # Cap queue length:
+        self._orientation_changes = self._orientation_changes[:2]
 
-    def _set_orientation(self):
-        if self._already_oriented:
-            return
+    def _pop_orientation_queue(self):
         while self._orientation_changes:
             orientation = self._orientation_changes.pop(0)
             if ((0 == orientation[0] == self.head.orientation[0])
                 or (0 == orientation[1] == self.head.orientation[1])):
                 continue
-            self.orientation = orientation
-            self._already_oriented = True
+            self.set_orientation(orientation)
+            break
+
+    def set_orientation(self, orientation):
+        self.orientation = orientation
+        self.head.set_orientation(orientation)
 
     def check_self_crash(self):
         if self.coiled: