Commits

Radomir Dopieralski committed 7a57de0

Move behaviors to a separate module

Comments (0)

Files changed (1)

 from jelly import sprites
 from jelly import message
 from jelly import items
-from jelly.util import DX, DY, dist, pos2dir, dist2
+from jelly import behavior
+from jelly.util import DX, DY, dist, pos2dir
 
 
 KEY_DIRS = {
 }
 
 
-def one_in(prob):
-    def decorator(func):
-        def wrapped(*args, **kwargs):
-            if random.randint(0, prob) != 0:
-                return False
-            return func(*args, **kwargs)
-        return wrapped
-    return decorator
-
-
-def sleep_behavior(mob, level):
-    if mob.status.get('asleep', True):
-        return True
-    return False
-
-
-def attack_behavior(mob, level):
-    if dist(level.pc.pos, mob.pos) <= 1:
-        level.attack(mob, pos2dir(mob.pos, level.pc.pos), level.pc)
-        mob.status['asleep'] = False
-        return True
-    return False
-
-
-def fitness_behavior(mob, level, fitness):
-    x, y = mob.pos
-    possible = [d for d in [0, 2, 4, 6, 1, 3, 5, 7]
-                if not level.is_blocked((x + DX[d], y + DY[d]))]
-    if not possible:
-        return False
-    direction = fitness(possible)
-    try:
-        level.walk(mob, direction)
-    except level.Blocked:
-        return False
-    return True
-
-
-def wander_behavior(mob, level):
-    return fitness_behavior(mob, level, random.choice)
-
-
-def passive_behavior(mob, level):
-    return True
-
-
-def slow_behavior(mob, level):
-    mob.status['wait'] = not mob.status.get('wait', False)
-    if mob.status['wait']:
-        return True
-    return False
-
-
-def spawn_behavior(mob, level):
-    x, y = mob.pos
-    possible = [d for d in [0, 2, 4, 6, 1, 3, 5, 7]
-                if not level.is_blocked((x + DX[d], y + DY[d]))]
-    if not possible:
-        return False
-    direction = random.choice(possible)
-    MobClass = random.choice(mob.spawns)
-    pos = (x + DX[direction], y + DY[direction])
-    spawnee = MobClass(pos)
-    level.spawn(spawnee, mob)
-    return True
-
-
-def coward_behavior(mob, level):
-    x, y = mob.pos
-    def coward_fitness(choices):
-        return max(
-            choices,
-            key=lambda d: dist2((x + DX[d], y + DY[d]), level.pc.pos))
-    return fitness_behavior(mob, level, coward_fitness)
-
-
-def aggressive_behavior(mob, level):
-    x, y = mob.pos
-    return fitness_behavior(mob, level, lambda choices: min(choices,
-        key=lambda d: dist((x+DX[d], y+DY[d]), level.pc.pos)))
 
 
 class Mob(object):
     has_light = False
     max_hp = 2
     hunted = False
-    behaviors = [attack_behavior, wander_behavior]
+    behaviors = [behavior.attack_behavior, behavior.wander_behavior]
     spawns = []
 
     def __init__(self, (x, y)):
     color = 2
     max_hp = 3
     animation = [56, 56, 57, 57, 57, 57]
-    behaviors = [attack_behavior, coward_behavior]
+    behaviors = [behavior.attack_behavior, behavior.coward_behavior]
 
 
 class PurpleJelly(Mob):
     color = 1
     max_hp = 2
     animation = [46, 46, 46, 47, 47, 47, 47]
-    behaviors = [attack_behavior, sleep_behavior, wander_behavior]
+    behaviors = [behavior.attack_behavior, behavior.sleep_behavior, behavior.wander_behavior]
 
 
 class RedJelly(Mob):
     color = 3
     max_hp = 1
     animation = [66, 66, 67, 67, 67]
-    behaviors = [attack_behavior, aggressive_behavior]
+    behaviors = [behavior.attack_behavior, behavior.aggressive_behavior]
 
 
 class BlueJelly(Mob):
     color = 0
     max_hp = 5
     animation = [36, 36, 36, 37, 37, 37, 37, 37]
-    behaviors = [slow_behavior, attack_behavior, wander_behavior]
+    behaviors = [behavior.slow_behavior, behavior.attack_behavior, behavior.wander_behavior]
 
 
 class BlackJelly(Mob):
     color = 4
     max_hp = 3
     animation = [76, 76, 76, 77, 77, 77]
-    behaviors = [one_in(15)(spawn_behavior), wander_behavior]
+    behaviors = [behavior.one_in(15)(behavior.spawn_behavior), behavior.wander_behavior]
     hunted = True