changeset 490:0eade58a71b9

Support item dropping (specifically tails). Have monk drop tail.
author Simon Cross <hodgestar@gmail.com>
date Sat, 09 Apr 2011 22:28:03 +0200
parents e5c060dda22a
children 6f110e347f30
files skaapsteker/dialogue.py skaapsteker/gamestate.py skaapsteker/sprites/items.py skaapsteker/sprites/player.py
diffstat 4 files changed, 44 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/skaapsteker/dialogue.py	Sat Apr 09 22:25:43 2011 +0200
+++ b/skaapsteker/dialogue.py	Sat Apr 09 22:28:03 2011 +0200
@@ -1,7 +1,7 @@
 import json
 
 from . import data
-from .engine import OpenDialog
+from .engine import OpenDialog, AddSpriteEvent
 
 
 class DSM(object):
@@ -39,10 +39,15 @@
         """Switch dialogue to another npc."""
         OpenDialog.post(npc_name)
 
-    def _drop_item(self, item):
+    def _drop_item(self, item, shift=(1, 0)):
         """Create a tail of the given type."""
         print "Dropping", item
-        self.world.get_item(item, to_level=self._me.level)
+        to_level = self._me.level
+        to_pos = self._me.pos
+        to_pos = to_pos[0] + shift[0], to_pos[1] + shift[1]
+        gamestate = self.world.gamestate()
+        sprite = gamestate.create_item_sprite(item, to_level=to_level, to_pos=to_pos)
+        AddSpriteEvent.post(sprite)
 
     def event(self, ev):
         my_locals = {
--- a/skaapsteker/gamestate.py	Sat Apr 09 22:25:43 2011 +0200
+++ b/skaapsteker/gamestate.py	Sat Apr 09 22:28:03 2011 +0200
@@ -7,8 +7,9 @@
 
 class StateProxy(object):
 
-    def __init__(self, data):
+    def __init__(self, data, gamestate):
         self.__dict__['_data'] = data # should be a dict
+        self.__dict__['_gamestate'] = gamestate # use sparingly
 
     def __getattr__(self, key):
         try:
@@ -16,7 +17,7 @@
         except KeyError:
             raise AttributeError
         if isinstance(value, dict):
-            return StateProxy(value)
+            return StateProxy(value, None) # surprise people
         else:
             return value
 
@@ -32,6 +33,9 @@
     def copy(self):
         return self._data.copy()
 
+    def gamestate(self):
+        return self._gamestate
+
 
 class GameState(object):
 
@@ -47,7 +51,7 @@
             game_file = self._game_file
         raw_data = open(game_file, "rb").read()
         self.data = json.loads(raw_data, encoding='utf-8')
-        self.world = StateProxy(self.data)
+        self.world = StateProxy(self.data, self)
 
     def new_game(self):
         self.load_game(data.filepath("game.json"))
@@ -81,4 +85,16 @@
                     sprites.append(find_sprite(sprite_dict, stype))
         return sprites
 
+    def create_item_sprite(self, item, to_level=None, to_pos=None):
+        itemdef = self.data['items'].get(item)
 
+        if to_level is not None:
+            itemdef['level'] = to_level
+        if to_pos is not None:
+            itemdef['pos'] = to_pos
+
+        sprite_dict = itemdef.copy()
+        sprite_dict.pop('level')
+        sprite_dict['name'] = item
+        sprite_dict['world'] = self.world
+        return find_sprite(sprite_dict, 'items')
--- a/skaapsteker/sprites/items.py	Sat Apr 09 22:25:43 2011 +0200
+++ b/skaapsteker/sprites/items.py	Sat Apr 09 22:28:03 2011 +0200
@@ -237,6 +237,7 @@
 
 class Tail(Item):
 
+    image_dir = 'icons/tails/'
     image_file = None
     tail_type = None
     help_msg = None
@@ -248,54 +249,54 @@
 
 
 class ShapeshiftTail(Tail):
-    image_file = 'icons/tails/shapeshifted.png'
+    image_file = 'shapeshift.png'
     tail_type = 'shapeshift'
     help_msg = "Shapeshifting tail. Press C to shape shift." \
                " Your powers are more limited while in human form."
 
 
 class FireballTail(Tail):
-    image_file = 'icons/tails/fireball.png'
+    image_file = 'fireball.png'
     tail_type = 'fireball'
     help_msg = "Fireball tail. The X attack key now shoots fireballs."
 
 
 class SprintTail(Tail):
-    image_file = 'icons/tails/sprint.png'
+    image_file = 'sprint.png'
     tail_type = 'sprint'
     help_msg = "Sprint tail. Double-tap the left or right arrow key to" \
                " sprint for a few seconds."
 
 
 class InvisibilityTail(Tail):
-    image_file = 'icons/tails/invisibility.png'
+    image_file = 'invisibility.png'
     tail_type = 'invisibility'
     help_msg = "Invisibility tail. Press V to become invisible for a few seconds." \
                " You become visible again if you attack or interact with others."
 
 
 class FlightTail(Tail):
-    image_file = 'icons/tails/flight.png'
+    image_file = 'flight.png'
     tail_type = 'flight'
     help_msg = "Flight tail. Double-tap up to fly. Press down while on the ground to land." \
                " If you stay in the air too long you'll tire and fall."
 
 
 class ShieldTail(Tail):
-    image_file = 'icons/tails/shield.png'
+    image_file = 'shield.png'
     tail_type = 'shield'
     help_msg = "Shield tail. If you get hit, your mystical shield will protect you for" \
                " one second. After that it'll need a little time to recharge."
 
 
 class StealTail(Tail):
-    image_file = 'icons/tails/steal.png'
+    image_file = 'steal.png'
     tail_type = 'steal'
     help_msg = "Life-stealing tail. If you projectiles hit your enemies, you steal some" \
                " of their life."
 
 
 class LightningTail(Tail):
-    image_file = 'icons/tails/lightning.png'
+    image_file = 'lightning.png'
     tail_type = 'lightning'
     help_msg = "Lightning tail. The Z attack now shoots lightning bolts."
--- a/skaapsteker/sprites/player.py	Sat Apr 09 22:25:43 2011 +0200
+++ b/skaapsteker/sprites/player.py	Sat Apr 09 22:28:03 2011 +0200
@@ -519,15 +519,15 @@
         my_item = self._me.item
         if my_item is None:
             return None
-        world_item = getattr(self.the_world.items, my_item)
+
         if set_level:
-            world_item.level = self._me.level
-            world_item.pos = self.get_tile_pos()
-        sprite_dict = world_item.copy()
-        sprite_dict.pop('level')
-        sprite_dict['name'] = my_item
-        sprite_dict['world'] = self.the_world
-        return find_sprite(sprite_dict, 'items')
+            to_level = self._me.level
+            to_pos = self._me.get_tile_pos()
+        else:
+            to_level, to_pos = None, None
+
+        return self.the_world.create_item_sprite(
+            my_item, to_level=to_level, to_pos=to_pos)
 
 
     def drop_item(self):