Changeset 189:9d08f99b5ddf


Ignore:
Timestamp:
04/06/11 19:14:26 (12 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Phase:
public
Message:

Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.

Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • data/game.json

    r127 r189  
    11{
     2    "fox": {
     3        "item": null
     4    },
    25    "missions": {
    3         "monk_tea": {
    4             "items": {}
    5         }
     6        "monk_tea": {}
    67    },
    7     "items": [
    8         {"type": "Dummy", "level": "level1", "pos": [8, 3]}
    9     ]
     8    "items": {
     9        "tealeaf": { "type": "TeaLeaf", "level": "level1", "pos": [8, 3] },
     10        "teacup": { "type": "TeaCup", "level": "level1", "pos": [10, 3] },
     11        "teapot": { "type": "TeaPot", "level": "level1", "pos": [12, 3] }
     12    },
     13    "npcs": {
     14        "monk": { "type": "Monk", "level": "level1", "pos": [9, 2], "dsm": "npcs/monk.json" },
     15        "guard": { "type": "Guard", "level": "level2", "pos": [10, 3], "dsm": "npcs/guard.json" }
     16    },
     17    "levels": {
     18        "level1" : "level1",
     19        "level2" : "level2",
     20        "level3" : "level3",
     21        "level4" : "level4",
     22        "level5" : "level5"
     23    }
    1024}
  • skaapsteker/engine.py

    r165 r189  
    1515        self._fpss = [self._framerate] * 100
    1616        self._cur_frame = 0
    17         self.game_state = GameState()
     17        self.game_state = GameState("game.json")
    1818
    1919    def change_scene(self, next_scene):
  • skaapsteker/gamestate.py

    r127 r189  
    55
    66
    7 class struct(dict):
     7class StateProxy(object):
     8
     9    def __init__(self, data):
     10        self.__dict__['_data'] = data # should be a dict
     11
    812    def __getattr__(self, key):
    913        try:
    10             return self[key]
     14            print self, key
     15            value = self._data[key]
    1116        except KeyError:
    1217            raise AttributeError
    13 
     18        if isinstance(value, dict):
     19            return StateProxy(value)
     20        else:
     21            return value
    1422
    1523    def __setattr__(self, key, value):
    16         self[key] = value
     24        self._data[key] = value
     25
     26    def __iter__(self):
     27        return self._data.keys()
     28
     29    def __contains__(self, key):
     30        return key in self._data
     31
     32    def copy(self):
     33        return self._data.copy()
    1734
    1835
    1936class GameState(object):
    20     def __init__(self, game_file=None):
    21         if game_file is None:
    22             game_file = 'game.json'
    23         self.data = json.loads(data.load(game_file).read())
    24         self.build_mission_data()
    25         self.build_item_data()
    26         self.build_level_data()
    27         print self.missions
    28         print self.items
    29         print self.levels
    3037
     38    def __init__(self, game_file, saved=False):
     39        if saved:
     40            raw_data = open(game_file, "rb").read()
     41        else:
     42            raw_data = data.load(game_file).read()
     43        self.data = json.loads(raw_data)
     44        self.world = StateProxy(self.data)
    3145
    32     def build_mission_data(self):
    33         self.missions = struct()
    34         for name, mission in self.data['missions'].items():
    35             self.missions[name] = struct(mission)
     46    def save(self, save_game_file):
     47        json.dumps(self.data, open(save_game_file, "wb"))
    3648
    37 
    38     def build_item_data(self):
    39         self.items = struct()
    40         for item in self.data['items']:
    41             istruct = struct(item)
    42             istruct.missions = struct()
    43             self.items[item['type']] = istruct
    44         for mission_name, mission in self.missions.items():
    45             for name, item in mission['items'].items():
    46                 self.items[name].missions[mission_name] = item
    47 
    48 
    49     def build_level_data(self):
    50         self.levels = struct()
    51         for item in self.items.values():
    52             item_level = item.pop('level')
    53             self.levels.setdefault(item_level, []).append(find_sprite(item, 'items'))
    54 
    55 
     49    def create_sprites(self, level):
     50        sprites = []
     51        for stype, key, needs_world in [
     52            ('items', 'items', False),
     53            ('npcs', 'npcs', True)]:
     54            for sprite_dict in self.data[key].values():
     55                sprite_dict = sprite_dict.copy()
     56                if needs_world:
     57                    sprite_dict['world'] = self.world
     58                if sprite_dict.pop('level') == level:
     59                    sprites.append(find_sprite(sprite_dict, stype))
     60        return sprites
  • skaapsteker/levelscene.py

    r183 r189  
    1414class LevelScene(engine.Scene):
    1515
    16     def __init__(self, game_state, leveldef, player=None):
     16    def __init__(self, game_state, leveldef):
    1717        super(LevelScene, self).__init__(game_state)
    18         if not player:
    19             self._player = sprites.player.Player()
    20         else:
    21             self._player = player
     18
     19        self._player = sprites.player.Player()
    2220        self._level = level.Level(leveldef, self._player)
     21
    2322        self._level_surface = self._level.get_surface()
    2423        self._clip_rect = None
     
    2625        self._paused = False
    2726
    28         # hackity, hack, hack
    29         for sprite in self._level.enemies:
     27        for sprite in self._level.sprites:
    3028            self._world.add(sprite)
    31         for sprite in self._level.tiles:
     29        for sprite in self.game_state.create_sprites(self._level.name):
    3230            self._world.add(sprite)
    33         for sprite in self.game_state.levels.get(self._level.name, []):
    34             self._world.add(sprite)
     31        self._world.add(self._player)
    3532
    36         self._world.add(self._player)
    3733        self._build_action_map()
    3834
  • skaapsteker/sprites/base.py

    r186 r189  
    66from skaapsteker.constants import Layers
    77from skaapsteker import data
     8from skaapsteker import dialogue
    89
    910
     
    1314PC_LAYER = 0
    1415MONSTER_LAYER = 1
     16NPC_LAYER = 2
    1517
    1618
     
    4143
    4244class NPC(Sprite):
    43     pass
     45
     46    image_file = None
     47    collision_layer = NPC_LAYER
     48    collides_with = set([PC_LAYER])
     49
     50    debug_color = (240, 240, 240)
     51
     52    block = True
     53
     54    def __init__(self, pos, **opts):
     55        Sprite.__init__(self)
     56        self.image = data.load_image('sprites/' + self.image_file)
     57        self.starting_tile_pos = pos
     58        self.rect = self.image.get_rect(midbottom=(pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1]))
     59        self.collide_rect = self.rect.move(0, 0)
     60        self._layer = Layers.PLAYER
     61        self.setup(**opts)
     62
     63    def setup(self, dsm, world):
     64        self.dsm = dialogue.DSM(dsm, world)
    4465
    4566
     
    7596    mobile = False
    7697    gravitates = False
    77     collides_with = set([PC_LAYER, MONSTER_LAYER])
     98    collides_with = set([PC_LAYER, MONSTER_LAYER, NPC_LAYER])
    7899    is_ground = True
    79100    bounce_factor = (0.0, 0.0)
  • skaapsteker/sprites/items.py

    r162 r189  
    22
    33
    4 class Dummy(Item):
     4class TeaCup(Item):
     5    image_file = 'teacup_empty.png'
     6
     7
     8class TeaLeaf(Item):
    59    image_file = 'tealeaf.png'
    610
    7     def setup(self, missions):
    8         self.missions = missions
     11
     12class TeaPot(Item):
     13    image_file = 'teapot.png'
Note: See TracChangeset for help on using the changeset viewer.