From: Christian Heller Date: Tue, 8 Dec 2020 20:56:23 +0000 (+0100) Subject: Refactor thing addition / removal. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/static/git-logo.png?a=commitdiff_plain;h=856b68988876a63409737dac77b4d4921b9a466b;p=plomrogue2 Refactor thing addition / removal. --- diff --git a/plomrogue/commands.py b/plomrogue/commands.py index dea0717..770fb22 100644 --- a/plomrogue/commands.py +++ b/plomrogue/commands.py @@ -40,9 +40,7 @@ def cmd_LOGIN(game, nick, connection_id): raise GameError('name already in use') if game.get_player(connection_id): raise GameError('cannot log in twice') - t = game.thing_types['Player'](game) - t.position = game.spawn_point - game.things += [t] # TODO refactor into Thing.__init__? + t = game.add_thing('Player', game.spawn_point) t.thing_char = game.get_next_player_char() game.sessions[connection_id] = { 'thing_id': t.id_, @@ -57,7 +55,6 @@ def cmd_LOGIN(game, nick, connection_id): t.position = s.position break game.changed = True - game.changed_fovs = True cmd_LOGIN.argtypes = 'string' def cmd_BECOME_ADMIN(game, password, connection_id): @@ -230,17 +227,8 @@ def cmd_THING(game, big_yx, little_yx, thing_type, thing_id): if thing_type not in game.thing_types: raise GameError('illegal thing type %s' % thing_type) _ = game.get_map(big_yx) - t_old = None - if thing_id > 0: - t_old = game.get_thing(thing_id) - t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx, - little_yx)) - if t_old: - game.things[game.things.index(t_old)] = t_new - else: - game.things += [t_new] + game.add_thing(thing_type, (big_yx, little_yx), id_=thing_id) game.changed = True - game.changed_fovs = True cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg' def cmd_THING_NAME(game, thing_id, name, pw, connection_id): diff --git a/plomrogue/game.py b/plomrogue/game.py index 6e8afdf..10fcb12 100755 --- a/plomrogue/game.py +++ b/plomrogue/game.py @@ -210,6 +210,22 @@ class Game(GameBase): return '/O O\\' + '| oo |' + '\\>-- 0: + t_old = self.get_thing(id_) + t = self.thing_types[type_](self, id_=id_, position=position) + if t_old: + self.things[self.things.index(t_old)] = t + else: + self.things += [t] + self.changed_fovs = True + return t + def send_gamestate(self, connection_id=None): """Send out game state data relevant to clients.""" @@ -294,8 +310,7 @@ class Game(GameBase): t = self.get_player(connection_id) if hasattr(t, 'name'): self.io.send('CHAT ' + quote(t.name + ' left the map.')) - self.things.remove(t) - self.changed_fovs = True + self.remove_thing(t) to_delete += [connection_id] for connection_id in to_delete: del self.sessions[connection_id] diff --git a/plomrogue/tasks.py b/plomrogue/tasks.py index ef5fc5c..08a4a4f 100644 --- a/plomrogue/tasks.py +++ b/plomrogue/tasks.py @@ -123,15 +123,13 @@ class Task_DROP(Task): if t.type_ == 'BottleDeposit' and t.position == self.thing.position]: t.accept() - self.thing.game.things.remove(self.thing.carrying) - self.thing.game.changed_fovs = True + self.thing.game.remove_thing(self.thing.carrying) break elif self.thing.carrying.type_ == 'Hat': for t in [t for t in self.thing.game.things if t.type_ == 'HatRemixer' and t.position == self.thing.position]: t.accept(self.thing.carrying) - self.thing.game.changed_fovs = True break self.thing.carrying = None @@ -232,13 +230,10 @@ class Task_WEAR(Task): def do(self): if self.thing.name in self.thing.game.hats: - t = self.thing.game.thing_types['Hat'](self.thing.game, - position=self.thing.position) - self.thing.game.things += [t] + t = self.thing.game.add_thing('Hat', self.thing.position) t.design = self.thing.game.hats[self.thing.name] del self.thing.game.hats[self.thing.name] self.thing.send_msg('CHAT "You drop your hat."') - self.game.changed_fovs = True # thing addition for remixer in [t for t in self.thing.game.things if t.type_ == 'HatRemixer' and t.position == self.thing.position]: diff --git a/plomrogue/things.py b/plomrogue/things.py index 23f66ad..e1a9bd3 100644 --- a/plomrogue/things.py +++ b/plomrogue/things.py @@ -96,11 +96,8 @@ class ThingSpawner(Thing): for t in [t for t in self.game.things if t != self and t.position == self.position]: return - t = self.game.thing_types[self.child_type](self.game, - position=self.position) - self.game.things += [t] + self.game.add_thing(self.child_type, self.position) self.game.changed = True - self.game.changed_fovs = True @@ -302,8 +299,7 @@ class Thing_BottleDeposit(Thing): if self.bottle_counter >= 3: self.bottle_counter = 0 choice = random.choice(['MusicPlayer', 'Hat']) - t = self.game.thing_types[choice](self.game, position=self.position) - self.game.things += [t] + self.game.add_thing(choice, self.position) msg = 'here is a gift as a reward for ecological consciousness –' if choice == 'MusicPlayer': msg += 'pick it up and then use "command thing" on it!' @@ -311,7 +307,6 @@ class Thing_BottleDeposit(Thing): msg += 'pick it up and then use "(un-)wear" on it!' self.sound('BOTTLE DEPOSITOR', msg) self.game.changed = True - self.game.changed_fovs = True def accept(self): self.bottle_counter += 1