home · contact · privacy
Refactor.
[plomrogue2-experiments] / server_ / game.py
index e24b30aad2958df13fecebd9b10cce416289d686..da2f61184b43bce0052d42139c438fd6fee7e54c 100644 (file)
@@ -1,3 +1,8 @@
+import sys
+sys.path.append('../')
+import game_common
+
+
 class GameError(Exception):
     pass
 
@@ -13,22 +18,12 @@ def move_pos(direction, pos_yx):
         pos_yx[1] -= 1
 
 
-class World:
+class World(game_common.World):
 
     def __init__(self):
-        self.turn = 0
-        self.map_size = (5, 5)
-        self.map_ = 'xxxxx' +\
-                    'x...x' +\
-                    'x.X.x' +\
-                    'x...x' +\
-                    'xxxxx'
-        self.things = [
-            Thing(self, 'human', [3, 3]),
-            Thing(self, 'monster', [1, 1])
-        ]
-        self.player_i = 0
-        self.player = self.things[self.player_i]
+        super().__init__()
+        self.Thing = Thing  # use local Thing class instead of game_common's
+        self.player_id = 0
 
     def proceed_to_next_player_turn(self):
         """Run game world turns until player can decide their next step.
@@ -43,15 +38,17 @@ class World:
         the player's task is finished, the loop breaks.
         """
         while True:
-            for thing in self.things[self.player_i+1:]:
+            for thing in self.things[self.player_id+1:]:
                 thing.proceed()
             self.turn += 1
-            for thing in self.things[:self.player_i]:
+            for thing in self.things[:self.player_id]:
                 thing.proceed()
-            self.player.proceed(is_AI=False)
-            if self.player.task is None:
+            player = self.get_thing(self.player_id)
+            player.proceed(is_AI=False)
+            if player.task is None:
                 break
 
+
 class Task:
 
     def __init__(self, thing, name, args=(), kwargs={}):
@@ -74,17 +71,15 @@ class Task:
                test_pos[1] >= self.thing.world.map_size[1]:
                 raise GameError('would move outside map bounds')
             pos_i = test_pos[0] * self.thing.world.map_size[1] + test_pos[1]
-            map_tile = self.thing.world.map_[pos_i]
+            map_tile = self.thing.world.terrain_map[pos_i]
             if map_tile != '.':
                 raise GameError('would move into illegal terrain')
 
 
-class Thing:
+class Thing(game_common.Thing):
 
-    def __init__(self, world, type_, position):
-        self.world = world
-        self.type_ = type_
-        self.position = position
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
         self.task = Task(self, 'wait')
 
     def task_wait(self):
@@ -101,8 +96,8 @@ class Thing:
         else:
             self.set_task('wait')
 
-    def set_task(self, task, *args, **kwargs):
-        self.task = Task(self, task, args, kwargs)
+    def set_task(self, task_name, *args, **kwargs):
+        self.task = Task(self, task_name, args, kwargs)
         self.task.check()
 
     def proceed(self, is_AI=True):