Commits

Anonymous committed 7365aee

refactoring to make the battle model reusable.

Comments (0)

Files changed (3)

 dff26a5c440ee34e66850b0d722ff8ffdae39d16 pyglet.hg
-9fed674729361f7393d4543585745388f60b5814 rpg_1d6
+e47a09a640d16eb32afd177012dec66efd7574b8 rpg_1d6

hexbattle_logic.py

 independent of the type of user interaction and presentation.
 """
 
-from core import core, Sprite, run
+from core import core, Sprite, run, MethodNotImplemented
 from core_logic import Result, Command
 from os.path import join
 from pyglet.clock import schedule_interval, schedule_once
 from hexbattle_units import *
 
 
-class TerrainTile:
+class TerrainTile(object):
     """A tile of ground with properties."""
     def __init__(self, imagepath):
         self.image = imagepath
 
-class Model:
+class BattleModel(object):
     """The basic game logic. It only knows about hexfields."""
     def __init__(self, hexcenter=(10,10)):
         #: The center of the hexmap: moving outside a circle around this center is not allowed. If you are outside, you are only allowed to move further inside.
         #: Whether the game is finished
         self.finished = False
 
-        # settings
-        self.player_team = "guardians"
-        self.num_woses = 3
-        self.num_rats = 20
-        self.num_goblins = 12
         
     def are_enemies(self, unit, other):
         """Check if the given units are enemies."""
                         new_vectors_tmp.add(pos)
             new_vectors = new_vectors_tmp
                     
-    def setup_playerteam(self):
-        for i in range(self.num_woses):
-            x, y = self.find_free_hex(5, 2)
-            char = Character(self, self.hexmap)
-            char.move_to_hex(x, y)
-            char.team = "guardians"
-            char.show()
-            char.attack = 15
-            self.chars.append(char)
-
     def setup_step(self, dt=0):
         """setup the level."""
-        # if we do not have characters yet, we add all of them.
-        if not self.chars:
-            self.setup_playerteam()
-        # TODO: Make loading a character from a file actually work.
-        #with open(join("data", "units", "rats.yml")) as f: 
-                  #rat_data = f.read()
-        # add rats
-        if self.num_rats > 0:
-            x, y = self.find_free_hex(9, 1)
-            char = Character(self, self.hexmap, image_path = join("graphics", "wesnoth", "giant-rat.png"), source = "tag:1w6.org,2010:Rat")#, template_data = rat_data)
+        raise MethodNotImplemented(func=self.setup_step, 
+                                   implementation="""
+    def setup_step(self, dt=0):
+        if not self.chars: # first step: get the player chars
+            for i in range(self.num_chars):
+                x, y = self.find_free_hex(start_x, start_y)
+                char = Character(self, self.hexmap)
+                char.move_to_hex(x, y)
+                char.team = self.char_team
+                char.show()
+                char.attack = 15
+                self.chars.append(char)
+        # add enemies
+        if self.add_num_enemies > 0:
+            x, y = self.find_free_hex(enemy_start_x, enemy_start_y)
+            char = Character(self, self.hexmap, image_path = join("graphics", "wesnoth", "enemy_type.png"), source = "tag:1w6.org,2010:EnemyType")#, template_data = rat_data)
             char.move_to_hex(x, y)
             # we are the rats!
             char.team = "rats"
             char.attack = 6
             self.chars.append(char)
             self.num_rats -= 1
-        
-        # and add goblins
-        if self.num_goblins > 0:
-            x, y = self.find_free_hex(10, -3)
-            char = Character(self, self.hexmap, image_path = join("graphics", "wesnoth", "impaler.png"), source = "tag:1w6.org,2010:Rat")#, template_data = rat_data)
-            char.move_to_hex(x, y)
-            # we are the rats!
-            char.team = "goblins"
-            char.show()
-            char.attack = 9
-            self.chars.append(char)
-            self.num_goblins -= 1
-        # pass the control to the AI
-        if self.num_goblins <= 0 and self.num_rats <= 0: 
+
+        # when all are added, pass the control to the AI
+        if self.add_num_enemies <= 0: 
             # sort by initiative
             self.chars_by_initiative = [[c.roll_initiative(), c] for c in self.chars]
             self.chars_by_initiative.sort()
             self.phase["player"] = "CPU"
         # if we’re not finished yet, stay in the setup step
         # TODO: Do this in steps be returning a result which only allows one action: next step.
-        else: schedule_once(self.setup_step, 0.02)
-        
+        else: 
+            schedule_once(self.setup_step, 0.02)
+""")
     
     def next_by_initiative(self):
         """Get the char with the highest initiative, reroll if needed."""
         # TODO: return the result of the action instead. Or at least a list of changed characters. Best a list of all characters who changed state and how they changed state.
         return Result("step", commands=[Command(self.switch_to_cpu_turn, "continue")])
 
-class Logic:
-    def __init__(self, hexcenter):
-        self.model = Model(hexcenter=hexcenter)
+
+class BattleLogic(object):
+    def __init__(self, hexcenter=None, model=None):
+        raise MethodNotImplemented(self.__init__, implementation="""
+    def __init__(self, hexcenter, model):
+        self.model = model(hexcenter)
         #: The scene to switch to at the next possible step.
         self.switch = False 
+        """)
 
     def update(self):
         # Update the visible representation of the char.
 from core import core, run
 # ensure that pyglet is available
 try:
-    from pyglet.clock import schedule_interval
+    from pyglet.clock import schedule_interval, schedule_once
 except ImportError: # pyglet subrepo has pyglet as subfolder
     import sys
     sys.path.append("pyglet.hg")
-    from pyglet.clock import schedule_interval
+    from pyglet.clock import schedule_interval, schedule_once
 
 from hexbattle_gui import Interaction
-from hexbattle_logic import Logic
+from hexbattle_logic import BattleLogic, BattleModel
+import hexbattle_units as units
 import hexgrid
+import os.path
+
+class Model(BattleModel):
+    """The basic game logic. It only knows about hexfields."""
+    def __init__(self, hexcenter=(10,10)):
+        super(Model, self).__init__(hexcenter=hexcenter)
+        # settings
+        self.player_team = "guardians"
+        self.num_woses = 3
+        self.num_rats = 20
+        self.num_goblins = 12
+        
+    def setup_playerteam(self):
+        for i in range(self.num_woses):
+            x, y = self.find_free_hex(5, 2)
+            char = units.Character(self, self.hexmap)
+            char.move_to_hex(x, y)
+            char.team = "guardians"
+            char.show()
+            char.attack = 15
+            self.chars.append(char)
+
+    def setup_step(self, dt=0):
+        """setup the level."""
+        # if we do not have characters yet, we add all of them.
+        if not self.chars:
+            self.setup_playerteam()
+        # TODO: Make loading a character from a file actually work.
+        #with open(os.path.join("data", "units", "rats.yml")) as f: 
+                  #rat_data = f.read()
+        # add rats
+        if self.num_rats > 0:
+            x, y = self.find_free_hex(9, 1)
+            char = units.Character(self, self.hexmap, 
+                                   image_path = os.path.join("graphics", "wesnoth", "giant-rat.png"), 
+                                   source = "tag:1w6.org,2010:Rat")#, template_data = rat_data)
+            char.move_to_hex(x, y)
+            # we are the rats!
+            char.team = "rats"
+            char.show()
+            char.attack = 6
+            self.chars.append(char)
+            self.num_rats -= 1
+        
+        # and add goblins
+        if self.num_goblins > 0:
+            x, y = self.find_free_hex(10, -3)
+            char = units.Character(self, self.hexmap, 
+                                   image_path = os.path.join("graphics", "wesnoth", "impaler.png"), 
+                                   source = "tag:1w6.org,2010:Rat")#, template_data = rat_data)
+            char.move_to_hex(x, y)
+            # we are the rats!
+            char.team = "goblins"
+            char.show()
+            char.attack = 9
+            self.chars.append(char)
+            self.num_goblins -= 1
+        # pass the control to the AI
+        if self.num_goblins <= 0 and self.num_rats <= 0: 
+            # sort by initiative
+            self.chars_by_initiative = [[c.roll_initiative(), c] for c in self.chars]
+            self.chars_by_initiative.sort()
+            self.phase["player"] = "CPU"
+        # if we’re not finished yet, stay in the setup step
+        # TODO: Do this in steps be returning a result which only allows one action: next step.
+        else: 
+            schedule_once(self.setup_step, 0.02)
+
+
+class Logic(BattleLogic):
+    """Example Game Logic for the first scene."""
+    def __init__(self, hexcenter):
+        self.model = Model(hexcenter)
+        #: The scene to switch to at the next possible step.
+        self.switch = False 
+
 
 #### Basic Game-Scene API ####