changeset 8:add2b6c52187

Rotate towards the nearest monster when nobody to attack
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sun, 11 Mar 2012 21:41:56 +0100
parents d4614029977d
children 48c784b63eaf
files jelly/mobs.py jelly/util.py
diffstat 2 files changed, 15 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/jelly/mobs.py	Sun Mar 11 21:20:44 2012 +0100
+++ b/jelly/mobs.py	Sun Mar 11 21:41:56 2012 +0100
@@ -141,7 +141,7 @@
         return True
 
     def control(self, level):
-
+        direction = None
         keys = pygame.key.get_pressed()
         if keys[pg.K_DOWN] and keys[pg.K_LEFT]:
             direction = 1
@@ -152,12 +152,19 @@
         elif keys[pg.K_DOWN] and keys[pg.K_RIGHT]:
             direction = 7
         elif keys[pg.K_z]:
-            if keys[pg.K_LCTRL]:
+            x, y = self.pos
+            for other in level.mobs:
+                if other.pos == (x + DX[self.direction],
+                                 y + DY[self.direction]):
+                    break
+            else:
+                other = None
+            if other is None:
                 if len(level.mobs) > 1:
-                    mob = min((m for m in level.mobs if m != self),
+                    other = min((m for m in level.mobs if m != self),
                         key=lambda m: dist(self.pos, m.pos))
-                    direction = pos2dir(self.pos, mob.pos)
-            else:
+                    self.direction = pos2dir(self.pos, other.pos)
+            if other is not None and not keys[pg.K_LCTRL]:
                 level.attack(self, self.direction, None)
                 return True
         elif not keys[pg.K_LSHIFT]:
--- a/jelly/util.py	Sun Mar 11 21:20:44 2012 +0100
+++ b/jelly/util.py	Sun Mar 11 21:41:56 2012 +0100
@@ -1,8 +1,8 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
-DX = [0, -1, -1, -1,  0,  1, 1, 1, 0]
-DY = [1,  1,  0, -1, -1, -1, 0, 1, 1]
+DX = [0, -1, -1, -1,  0,  1, 1, 1]
+DY = [1,  1,  0, -1, -1, -1, 0, 1]
 
 
 def dist((x1, y1), (x2, y2)):
@@ -11,5 +11,5 @@
 def pos2dir(pos1, pos2):
     x, y = pos1
     return min(
-            xrange(len(DX)),
+            [0, 2, 4, 6, 1, 3, 5, 7],
             key=lambda d: dist(pos2, (x + DX[d], y + DY[d])))