From: Christian Heller Date: Thu, 21 Feb 2019 01:46:03 +0000 (+0100) Subject: Add multi-class thing type system. X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/day?a=commitdiff_plain;h=faf90001efa004054b41df5e2638b6c7c4c1fd98;p=plomrogue2-experiments Add multi-class thing type system. --- diff --git a/new/example_client.py b/new/example_client.py index a877da8..039d8de 100755 --- a/new/example_client.py +++ b/new/example_client.py @@ -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: diff --git a/new/plomrogue/commands.py b/new/plomrogue/commands.py index 87afd60..6a27f80 100644 --- a/new/plomrogue/commands.py +++ b/new/plomrogue/commands.py @@ -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) diff --git a/new/plomrogue/game.py b/new/plomrogue/game.py index d244582..a031406 100755 --- a/new/plomrogue/game.py +++ b/new/plomrogue/game.py @@ -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): diff --git a/new/plomrogue/things.py b/new/plomrogue/things.py index a80e9e4..2decc67 100644 --- a/new/plomrogue/things.py +++ b/new/plomrogue/things.py @@ -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'