Inheritance: ITilesetTable
 public TilesetTableTest()
 {
     var tilesetTableMemoryStream = FileStreams.TilesetTableStream();
     var tilesetTableStreamReader = new StreamReader(tilesetTableMemoryStream);
     tilesetTableStreamReader.DiscardBufferedData();
     tilesetTableMemoryStream.Position = 0;
     var tilesetTable = new TilesetTable(null, tilesetTableStreamReader);
 }
Example #2
0
        public WallsTests()
        {
            Mock<TilesetTable> mockTilesetTable = new Mock<TilesetTable>();
            mockTilesetTable.Setup(x => x.TileNumber(It.IsAny<string>())).Returns(0);
            mockTilesetTable.Setup(x => x.TileName(It.IsAny<int>())).Returns("");
            m_tilesetTable = mockTilesetTable.Object;

            m_wall = new Wall(m_tilesetTable);
        }
 public void UpdateItems(IAtlas atlas, TilesetTable table)
 {
     if (atlas.NewAutoupdateables != null)
     {
         CurrentUpdateRequests.AddRange(atlas.NewAutoupdateables);
         atlas.NewAutoupdateables.Clear();
     }
     foreach (IAutoupdateableGameActor actor in CurrentUpdateRequests)
     {
         actor.Update(atlas, table);
         if (actor.NextUpdateAfter > 0)
             Register(actor, actor.NextUpdateAfter);
     }
     CurrentUpdateRequests.Clear();
 }
Example #4
0
        public MapLoaderTests()
        {
            // initiate streamReaders
            var tmxMemoryStream = FileStreams.SmallTmx();
            var tilesetTableMemoryStream = FileStreams.TilesetTableStream();

            var tilesetTableStreamReader = new StreamReader(tilesetTableMemoryStream);

            var serializer = new TmxSerializer();
            Map map = serializer.Deserialize(tmxMemoryStream);

            var tilesetTable = new TilesetTable(map, tilesetTableStreamReader);

            // create atlas
            m_atlas = MapLoader.LoadMap(map, tilesetTable, (GameActor actor) => { });
        }
Example #5
0
        public ToyWorld(Map tmxDeserializedMap, StreamReader tileTable)
        {
            if (tileTable == null)
                throw new ArgumentNullException("tileTable");
            if (tmxDeserializedMap == null)
                throw new ArgumentNullException("tmxDeserializedMap");
            Contract.EndContractBlock();

            Size = new Vector2I(tmxDeserializedMap.Width, tmxDeserializedMap.Height);
            AutoupdateRegister = new AutoupdateRegister(100000);
            TilesetTable = new TilesetTable(tmxDeserializedMap, tileTable);

            InitAtlas(tmxDeserializedMap);
            InitPhysics();
            TileDetectorRegister = new TileDetectorRegister(Atlas, TilesetTable);
            RegisterSignals();
        }
Example #6
0
        private static ITileLayer FillTileLayer(
            Layer layer,
            LayerType layerType,
            Dictionary<int, StaticTile> staticTilesContainer,
            TilesetTable tilesetTable,
            Action<GameActor> initializer,
            Random random)
        {
            SimpleTileLayer newSimpleLayer = new SimpleTileLayer(layerType, layer.Width, layer.Height, random);
            string[] lines = layer.Data.RawData.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)
                .Where(l => !string.IsNullOrWhiteSpace(l)).ToArray();
            Assembly assembly = Assembly.GetExecutingAssembly();
            Type[] cachedTypes = assembly.GetTypes().Where(x => x.IsSubclassOf(typeof(Tile))).ToArray();
            for (int i = 0; i < lines.Length; i++)
            {
                string[] tiles = lines[i].Split(',');
                for (int j = 0; j < tiles.Length; j++)
                {
                    if (tiles[j].Trim() == "")
                        continue;
                    int tileNumber = int.Parse(tiles[j]);

                    if (tileNumber == 0) continue;

                    int x = j;
                    int y = layer.Height - 1 - i;
                    if (staticTilesContainer.ContainsKey(tileNumber))
                    {
                        newSimpleLayer.AddInternal(x, y, staticTilesContainer[tileNumber]);
                    }
                    else
                    {
                        string tileName = tilesetTable.TileName(tileNumber);
                        if (tileName != null)
                        {
                            Tile newTile = CreateInstance(tileName, tileNumber, cachedTypes, new Vector2I(x, y));
                            initializer.Invoke(newTile);
                            newSimpleLayer.AddInternal(x, y, newTile);
                            if (newTile is StaticTile)
                            {
                                staticTilesContainer.Add(tileNumber, newTile as StaticTile);
                            }
                        }
                        else
                        {
                            Log.Instance.Error("Tile with number " + tileNumber + " was not found in TilesetTable");
                        }
                    }
                }
            }

            if (layer.Properties != null)
            {
                Property render = layer.Properties.PropertiesList.FirstOrDefault(x => x.Name.Trim() == "Render");
                if (render != null)
                    newSimpleLayer.Render = bool.Parse(render.Value);
            }

            return newSimpleLayer;
        }
Example #7
0
        /// <summary>
        /// Loads map from specified path, creates tiles and objects and put them into new Atlas object.
        /// </summary>
        /// <param name="map">Deserialized .tmx file</param>
        /// <param name="tilesetTable">Table of numbers of game object and theirs classes</param>
        /// <param name="initializer"></param>
        /// <returns>Atlas with initial state of ToyWorld</returns>
        public static IAtlas LoadMap(Map map, TilesetTable tilesetTable, Action<GameActor> initializer)
        {
            IAtlas atlas = new Atlas.Layers.Atlas();

            map.Tilesets = map.Tilesets.OrderBy(x => x.Firstgid).ToList();

            foreach (LayerType layerType in Enum.GetValues(typeof(LayerType)).Cast<LayerType>())
            {
                string layerName = Enum.GetName(typeof(LayerType), layerType);

                Debug.Assert(layerName != null);

                int type = (int)layerType;
                bool simpleType = type > 0 && (type & (type - 1)) == 0;
                if (!simpleType) continue;

                if (layerName.Contains("Object"))
                {
                    ObjectGroup objectLayer = map.ObjectGroups.FirstOrDefault(x => x.Name == layerName);
                    if (objectLayer == null)    // TMX does not contain such layer
                    {
                        Log.Instance.Warn("Layer " + layerName + " not found in given .tmx file!");
                        continue;
                    }
                    IObjectLayer filledObjectLayer = FillObjectLayer(
                        atlas,
                        objectLayer,
                        layerType,
                        initializer,
                        map.Tilesets,
                        map.Tilewidth,
                        map.Tileheight,
                        map.Height
                        );
                    atlas.ObjectLayers.Add(
                        filledObjectLayer);
                }
                else
                {
                    Layer tileLayer = map.Layers.FirstOrDefault(x => x.Name == layerName);
                    if (tileLayer == null)  // TMX does not contain such layer
                    {
                        Log.Instance.Warn("Layer " + layerName + " not found in given .tmx file!");
                        continue;
                    }
                    Random rnd = new Random(); // Shared across all layers
                    ITileLayer filledTileLayer = FillTileLayer(
                        tileLayer,
                        layerType,
                        atlas.StaticTilesContainer,
                        tilesetTable,
                        initializer,
                        rnd);
                    atlas.TileLayers.Add(
                        filledTileLayer);
                }
            }

            FillNamedAreas(atlas, map);

            SetTileRelations(atlas, map);

            return atlas;
        }