changeset 41:6b1e309eafc3

Make all skeletons move towards player
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sat, 10 Mar 2012 13:22:09 +0100
parents 6d38f7032b0d
children 8d0054b3240a
files mhrok/level.py mhrok/mobs.py mhrok/sprites.py mhrok/util.py
diffstat 4 files changed, 32 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/mhrok/level.py	Sat Mar 10 13:08:48 2012 +0100
+++ b/mhrok/level.py	Sat Mar 10 13:22:09 2012 +0100
@@ -44,6 +44,8 @@
             self.map[y][x] = value
 
     def walk(self, mob, direction):
+        if direction is None:
+            return
         x, y = mob.pos
         target = x + DX[direction], y + DY[direction]
         for m in self.mobs:
@@ -55,5 +57,13 @@
         self.events.append(('mob_walk', mob, direction))
 
     def stand(self, mob, direction):
+        if direction is None:
+            return
         mob.direction = direction
         self.events.append(('mob_stand', mob, direction))
+
+    def is_blocked(self, pos):
+        for m in self.mobs:
+            if m.pos == pos:
+                return True
+        return not self[pos]
--- a/mhrok/mobs.py	Sat Mar 10 13:08:48 2012 +0100
+++ b/mhrok/mobs.py	Sat Mar 10 13:22:09 2012 +0100
@@ -7,6 +7,7 @@
 from pygame import locals as pg
 
 from mhrok import sprites
+from mhrok.util import DX, DY, dist
 
 
 KEY_DIRS = {
@@ -30,13 +31,14 @@
         return self.sprite, shadow, None
 
     def control(self, level):
-        direction = random.choice([None, 0, 1, 2, 3])
-        if direction is not None:
-            try:
-                level.walk(self, direction)
-            except level.Blocked:
-                direction = None
-        self.sprite.direction = direction
+        x, y = self.pos
+        possible = [d for (d, (dx, dy)) in enumerate(zip(DX, DY))
+                    if level[x + dx, y + dy]]
+        direction = random.choice(possible)
+        try:
+            level.walk(self, direction)
+        except level.Blocked:
+            pass
 
 
 class Bat(Mob):
@@ -56,6 +58,15 @@
     name = 'skeleton'
     animation = [35, 35, 35, 36, 36, 36, 35, 35, 35, 37, 37, 37]
 
+    def control(self, level):
+        x, y = self.pos
+        possible = [d for (d, (dx, dy)) in enumerate(zip(DX, DY))
+                    if level[x + dx, y + dy]]
+        direction = min(possible, key=lambda d: dist(level.pc.pos, (x+DX[d], y+DY[d])))
+        try:
+            level.walk(self, direction)
+        except level.Blocked:
+            pass
 
 class Teddybear(Mob):
     name = 'teddybear'
--- a/mhrok/sprites.py	Sat Mar 10 13:08:48 2012 +0100
+++ b/mhrok/sprites.py	Sat Mar 10 13:22:09 2012 +0100
@@ -19,7 +19,6 @@
         self.rect.move_ip(4 + 20 * x, 12 * y)
         self.depth = self.rect.bottom
         self.animation = None
-        self.vision = None
 
     def update(self):
         self.frame = (self.frame + 1) % len(self.steps)
--- a/mhrok/util.py	Sat Mar 10 13:08:48 2012 +0100
+++ b/mhrok/util.py	Sat Mar 10 13:22:09 2012 +0100
@@ -3,3 +3,7 @@
 
 DX = [-1,  0,  1,  0]
 DY = [ 0, -1,  0,  1]
+
+
+def dist((x1, y1), (x2, y2)):
+    return abs(x2 - x1) + abs(y2 - y1)