home · contact · privacy
Add multi-class thing type system.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 21 Feb 2019 01:46:03 +0000 (02:46 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 21 Feb 2019 01:46:03 +0000 (02:46 +0100)
new/example_client.py
new/plomrogue/commands.py
new/plomrogue/game.py
new/plomrogue/things.py

index a877da84f603bc0de3e69cfbc2beca7439aaf1c5..039d8de6bc134f9835c4f70a130a3a4879fb7890 100755 (executable)
@@ -3,7 +3,7 @@ import curses
 import socket
 import threading
 from plomrogue.parser import ArgError, Parser
-from plomrogue.commands import cmd_MAP, cmd_THING_TYPE, cmd_THING_POS
+from plomrogue.commands import cmd_MAP, cmd_THING_POS
 from plomrogue.game import Game, WorldBase
 from plomrogue.mapping import MapBase
 from plomrogue.io import PlomSocket
@@ -105,6 +105,11 @@ def cmd_GAME_STATE_COMPLETE(self):
     self.to_update['turn'] = True
     self.to_update['map'] = True
 
+def cmd_THING_TYPE(game, i, type_):
+    t = game.world.get_thing(i)
+    t.type_ = type_
+cmd_THING_TYPE.argtypes = 'int:nonneg string'
+
 
 class Game:
 
index 87afd605314847da0395249704f94611e73edcfe..6a27f80075afda9904262942d16023506dd51010 100644 (file)
@@ -16,9 +16,25 @@ def cmd_MAP(game, yx):
 cmd_MAP.argtypes = 'yx_tuple:pos'
 
 def cmd_THING_TYPE(game, i, type_):
-    t = game.world.get_thing(i)
-    t.type_ = type_
-cmd_THING_TYPE.argtypes = 'int:nonneg string'
+    t_old = game.world.get_thing(i)
+    t_new = game.thing_types[type_](game.world, i)
+    #attr_names_of_old = [name for name in dir(t_old) where name[:2] != '__']
+    #attr_names_of_new = [name for name in dir(t_new) where name[:2] != '__']
+    #class_new = type(t_new)
+    #for attr_name in [v for v in attr_names_of_old if v in attr_names_of_new]:
+    #    if hasattr(class_new, attr_name):
+    #        attr_new = getattr(class_new, attr_name)
+    #        if type(attr_new) == property and attr_new.fset is None:
+    #            continue  # ignore read-only properties on t_new
+    #    attr_old = getattr(t_old, attr_name)
+    #    attr_new = getattr(t_new, attr_name)
+    #    if type(attr_old) != type(attr_new):
+    #        continue
+    #    setattr(t_new, attr_name, attr_old)
+    t_new.position = t_old.position
+    t_old_index = game.world.things.index(t_old)
+    game.world.things[t_old_index] = t_new
+cmd_THING_TYPE.argtypes = 'int:nonneg string:thingtype'
 
 def cmd_THING_POS(game, i, yx):
     t = game.world.get_thing(i)
index d244582deed90cb227bd5caa5cc7dc5e55f4d036..a031406b19e32a36432bea01131df305df0a3c5f 100755 (executable)
@@ -8,7 +8,7 @@ from plomrogue.mapping import MapHex
 from plomrogue.parser import Parser
 from plomrogue.io import GameIO
 from plomrogue.misc import quote, stringify_yx
-from plomrogue.things import Thing
+from plomrogue.things import Thing, ThingMonster, ThingHuman
 
 
 
@@ -77,12 +77,10 @@ class World(WorldBase):
                 self.map_[pos] = '#'
                 continue
             self.map_[pos] = random.choice(('.', '.', '.', '.', 'x'))
-        player = self.game.thing_type(self, 0)
-        player.type_ = 'human'
+        player = self.game.thing_types['human'](self, 0)
         player.position = [random.randint(0, yx[0] -1),
                            random.randint(0, yx[1] - 1)]
-        npc = self.game.thing_type(self, 1)
-        npc.type_ = 'monster'
+        npc = self.game.thing_types['monster'](self, 1)
         npc.position = [random.randint(0, yx[0] -1),
                         random.randint(0, yx[1] -1)]
         self.things = [player, npc]
@@ -109,10 +107,13 @@ class Game:
         self.world_type = World
         self.world = self.world_type(self)
         self.thing_type = Thing
+        self.thing_types = {'human': ThingHuman, 'monster': ThingMonster}
 
     def get_string_options(self, string_option_type):
         if string_option_type == 'direction':
             return self.world.map_.get_directions()
+        elif string_option_type == 'thingtype':
+            return list(self.thing_types.keys())
         return None
 
     def send_gamestate(self, connection_id=None):
index a80e9e40913e9eac6e06221577cfe5815477a1a5..2decc6709f747113e888edc9327f406c8e43739c 100644 (file)
@@ -3,11 +3,11 @@ from plomrogue.errors import GameError
 
 
 class ThingBase:
+    type_ = '?'
 
-    def __init__(self, world, id_, type_='?', position=[0,0]):
+    def __init__(self, world, id_, position=[0,0]):
         self.world = world
         self.id_ = id_
-        self.type_ = type_
         self.position = position
 
 
@@ -126,3 +126,13 @@ class Thing(ThingBase):
             if stencil[thing.position] == '.':
                 visible_things += [thing]
         return visible_things
+
+
+
+class ThingHuman(Thing):
+    type_ = 'human'
+
+
+
+class ThingMonster(Thing):
+    type_ = 'monster'