|
| 1 | +--- |
| 2 | +-- This module handles the loading of a map from a savefile and creating an |
| 3 | +-- actual Map object. |
| 4 | +-- @module MapLoader |
| 5 | +-- |
| 6 | + |
| 7 | +-- ------------------------------------------------ |
| 8 | +-- Required Modules |
| 9 | +-- ------------------------------------------------ |
| 10 | + |
| 11 | +local Object = require( 'src.Object' ) |
| 12 | +local TileFactory = require( 'src.map.tiles.TileFactory' ) |
| 13 | +local WorldObjectFactory = require( 'src.map.worldobjects.WorldObjectFactory' ) |
| 14 | + |
| 15 | +-- ------------------------------------------------ |
| 16 | +-- Module |
| 17 | +-- ------------------------------------------------ |
| 18 | + |
| 19 | +local MapLoader = {} |
| 20 | + |
| 21 | +-- ------------------------------------------------ |
| 22 | +-- Constructor |
| 23 | +-- ------------------------------------------------ |
| 24 | + |
| 25 | +function MapLoader.new() |
| 26 | + local self = Object.new():addInstance( 'MapLoader' ) |
| 27 | + |
| 28 | + -- ------------------------------------------------ |
| 29 | + -- Private Methods |
| 30 | + -- ------------------------------------------------ |
| 31 | + |
| 32 | + local function recreateTile( x, y, id ) |
| 33 | + return TileFactory.create( x, y, id ) |
| 34 | + end |
| 35 | + |
| 36 | + local function recreateWorldObject( id, hp, passable, blocksVision, inventory ) |
| 37 | + local worldObject = WorldObjectFactory.create( id ) |
| 38 | + worldObject:setHitPoints( hp ) |
| 39 | + worldObject:setPassable( passable ) |
| 40 | + worldObject:setBlocksVision( blocksVision ) |
| 41 | + if worldObject:isContainer() and inventory then |
| 42 | + worldObject:getInventory():loadItems( inventory ) |
| 43 | + end |
| 44 | + return worldObject |
| 45 | + end |
| 46 | + |
| 47 | + local function loadSavedTiles( savedTiles ) |
| 48 | + local loadedTiles = {} |
| 49 | + for _, tile in ipairs( savedTiles ) do |
| 50 | + -- Recreate the tile. |
| 51 | + local recreatedTile = recreateTile( tile.x, tile.y, tile.id ) |
| 52 | + |
| 53 | + -- Recreate any worldobject that was located on the tile. |
| 54 | + if tile.worldObject then |
| 55 | + local obj = tile.worldObject |
| 56 | + local worldObject = recreateWorldObject( obj.id, obj.hp, obj.passable, obj.blocksVision, obj.inventory ) |
| 57 | + recreatedTile:addWorldObject( worldObject ) |
| 58 | + end |
| 59 | + |
| 60 | + -- Recreate the tile's inventory if it had one. |
| 61 | + if tile.inventory then |
| 62 | + recreatedTile:getInventory():loadItems( tile.inventory ) |
| 63 | + end |
| 64 | + |
| 65 | + -- Set the exploration info of each faction for this tile. |
| 66 | + if tile.explored then |
| 67 | + for i, v in pairs( tile.explored ) do |
| 68 | + recreatedTile:setExplored( i, v ) |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + -- Store tile in the table. |
| 73 | + loadedTiles[tile.x] = loadedTiles[tile.x] or {} |
| 74 | + loadedTiles[tile.x][tile.y] = recreatedTile |
| 75 | + end |
| 76 | + return loadedTiles |
| 77 | + end |
| 78 | + |
| 79 | + -- ------------------------------------------------ |
| 80 | + -- Public Methods |
| 81 | + -- ------------------------------------------------ |
| 82 | + |
| 83 | + --- |
| 84 | + -- Recreates a map from a savefile. |
| 85 | + -- @tparam table savedmap A table containing the saved map information. |
| 86 | + -- @treturn table A table containing the recreated tiles and world objects. |
| 87 | + -- @treturn number The width of the recreated map. |
| 88 | + -- @treturn number The height of the recreated map. |
| 89 | + -- |
| 90 | + function self:recreateMap( savedmap ) |
| 91 | + return loadSavedTiles( savedmap.tiles ), savedmap.width, savedmap.height |
| 92 | + end |
| 93 | + |
| 94 | + return self |
| 95 | +end |
| 96 | + |
| 97 | +return MapLoader |
0 commit comments