changeset 302:78220c989e6a

Add supporting for flicking between speaking NPCs.
author Simon Cross <hodgestar@gmail.com>
date Fri, 08 Apr 2011 23:27:43 +0200
parents 922155cc0efd
children e94f89b7ec5b
files scripts/npc-test skaapsteker/dialogue.py skaapsteker/levelscene.py skaapsteker/sprites/base.py
diffstat 4 files changed, 22 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/npc-test	Fri Apr 08 23:16:11 2011 +0200
+++ b/scripts/npc-test	Fri Apr 08 23:27:43 2011 +0200
@@ -57,8 +57,17 @@
     dsm = DSM(npc_name, game.world, npc.dsm, npc.state)
     print "  Loaded %s." % (npc.dsm)
 
-    my_locals = { "world": dsm.world, "state": dsm.states, "npcs": dsm.world.npcs }
+    def test_switch_to(npc_name):
+        assert npc_name in game.world.npcs
+
+    my_locals = {
+        "world": dsm.world,
+        "state": dsm.states,
+        "npcs": dsm.world.npcs,
+        "switch_to": test_switch_to,
+    }
     my_locals.update(DsmEvent().items)
+
     for state_name, state in dsm.states.items():
         print "  Testing triggers for state %s" % state_name
         for trigger in state.triggers:
--- a/skaapsteker/dialogue.py	Fri Apr 08 23:16:11 2011 +0200
+++ b/skaapsteker/dialogue.py	Fri Apr 08 23:27:43 2011 +0200
@@ -1,6 +1,7 @@
 import json
 
 from . import data
+from .engine import OpenDialog
 
 
 class DSM(object):
@@ -32,11 +33,16 @@
         self.poke()
         return bool(self.states[self.state].text)
 
+    def _switch_dialogue_to(self, npc_name):
+        """Switch dialogue to another npc."""
+        OpenDialog.post(npc_name)
+
     def event(self, ev):
         my_locals = {
             "state": self.states,
             "world": self.world,
             "npcs": self.world.npcs,
+            "switch_to": self._switch_dialogue_to,
         }
         my_locals.update(ev.items)
         state = self.states[self.state]
--- a/skaapsteker/levelscene.py	Fri Apr 08 23:16:11 2011 +0200
+++ b/skaapsteker/levelscene.py	Fri Apr 08 23:27:43 2011 +0200
@@ -37,7 +37,9 @@
 
         for sprite in self._level.sprites:
             self._world.add(sprite)
-        for sprite in self.game_state.create_sprites(self._level.name):
+        npcs_and_items = game_state.create_sprites(self._level.name)
+        self._npcs = dict((s.name, s) for s in npcs_and_items if hasattr(s, 'dsm'))
+        for sprite in npcs_and_items:
             self._world.add(sprite)
         self._world.add(self._player)
 
@@ -112,6 +114,8 @@
             self._paused = True
 
     def _open_dialogue(self, npc):
+        if isinstance(npc, basestring):
+            npc = self._npcs[npc]
         if npc.dsm.has_text():
             if self._dialogue is not None:
                 self._dialogue.close()
--- a/skaapsteker/sprites/base.py	Fri Apr 08 23:16:11 2011 +0200
+++ b/skaapsteker/sprites/base.py	Fri Apr 08 23:27:43 2011 +0200
@@ -204,6 +204,7 @@
         self._layer = Layers.PLAYER
 
     def setup(self, name, world, dsm, state):
+        self.name = name
         self.dsm = dialogue.DSM(name, world, dsm, state)
 
     def player_action(self, player):