Esempio n. 1
0
        public TiledObjectGroup createObjectGroup(string name, Color color, bool visible, float opacity)
        {
            var group = new TiledObjectGroup(name, color, visible, opacity);

            objectGroups.Add(group);
            return(group);
        }
Esempio n. 2
0
        private void setupEnemies(Nez.Tiled.TiledObjectGroup mapObjectLayer)
        {
            var enemy1Spawn = mapObjectLayer.objectWithName("enemySpawn1");

            enemy1 = createEntity("enemy1");

            //turned off for testing
            //enemy1.setTag((int)Tags.gameEntity);
            enemy1.addComponent(new Enemy("EnemyAgent", 4, backgroundTileMap, true, 4));
            //enemy1.addComponent(new BoxCollider());
            enemy1.scale = new Vector2(2f, 2f);
            enemy1.transform.position = new Vector2(enemy1Spawn.x, enemy1Spawn.y);

            var punk1Spawn = mapObjectLayer.objectWithName("punkSpawn1");
            var punk1      = createEntity("punk1");

            punk1.addComponent(new Actors.Punk("Punk", 4, backgroundTileMap, false, 4));
            punk1.scale = new Vector2(2);
            punk1.transform.position = new Vector2(punk1Spawn.x, punk1Spawn.y);
        }
Esempio n. 3
0
        static TiledObjectGroup ReadObjectGroup(ContentReader reader, TiledMap tiledMap)
        {
            var objectGroup = new TiledObjectGroup(
                reader.ReadString(), reader.ReadColor(), reader.ReadBoolean(), reader.ReadSingle());

            ReadCustomProperties(reader, objectGroup.Properties);

            var objectCount = reader.ReadInt32();

            objectGroup.Objects = new TiledObject[objectCount];
            for (var i = 0; i < objectCount; i++)
            {
                var obj = new TiledObject()
                {
                    Id       = reader.ReadInt32(),
                    Name     = reader.ReadString(),
                    Type     = reader.ReadString(),
                    X        = reader.ReadInt32(),
                    Y        = reader.ReadInt32(),
                    Width    = reader.ReadInt32(),
                    Height   = reader.ReadInt32(),
                    Rotation = reader.ReadInt32(),
                    Gid      = reader.ReadInt32(),
                    Visible  = reader.ReadBoolean()
                };

                var tiledObjectType = reader.ReadString();
                if (tiledObjectType == "ellipse")
                {
                    // ellipse has no extra props
                    obj.TiledObjectType = TiledObjectType.Ellipse;
                }
                else if (tiledObjectType == "image")
                {
                    obj.TiledObjectType = TiledObjectType.Image;
                    throw new NotImplementedException("Image objects are not yet implemented");
                }
                else if (tiledObjectType == "polygon")
                {
                    obj.TiledObjectType = TiledObjectType.Polygon;
                    obj.PolyPoints      = ReadVector2Array(reader);
                }
                else if (tiledObjectType == "polyline")
                {
                    obj.TiledObjectType = TiledObjectType.Polyline;
                    obj.PolyPoints      = ReadVector2Array(reader);
                }
                else
                {
                    obj.TiledObjectType = TiledObjectType.None;
                }

                obj.ObjectType = reader.ReadString();

                ReadCustomProperties(reader, obj.Properties);

                objectGroup.Objects[i] = obj;
            }

            return(objectGroup);
        }
Esempio n. 4
0
        protected override TiledMap Read(ContentReader reader, TiledMap existingInstance)
        {
            var backgroundColor = reader.ReadColor();
            var renderOrder     = (TiledRenderOrder)Enum.Parse(typeof(TiledRenderOrder), reader.ReadString(), true);
            var tiledMap        = new TiledMap(firstGid: reader.ReadInt32(),
                                               width: reader.ReadInt32(),
                                               height: reader.ReadInt32(),
                                               tileWidth: reader.ReadInt32(),
                                               tileHeight: reader.ReadInt32(),
                                               orientation: (TiledMapOrientation)reader.ReadInt32())
            {
                backgroundColor = backgroundColor,
                renderOrder     = renderOrder
            };

            tiledMap.largestTileWidth  = reader.ReadInt32();
            tiledMap.largestTileHeight = reader.ReadInt32();

            // determine if we have some tiles that are larger than our standard tile size and if so mark this map for requiring culling
            if (tiledMap.largestTileWidth > tiledMap.tileWidth || tiledMap.largestTileHeight > tiledMap.tileHeight)
            {
                tiledMap.requiresLargeTileCulling = true;
            }

            readCustomProperties(reader, tiledMap.properties);

            var tilesetCount = reader.ReadInt32();

            for (var i = 0; i < tilesetCount; i++)
            {
                var isStandardTileset = reader.ReadBoolean();

                // image collections will have not texture associated with them
                var       textureName = reader.ReadString();
                Texture2D texture     = null;
                if (textureName != string.Empty)
                {
                    var textureAssetName = reader.getRelativeAssetPath(textureName);
                    texture = reader.ContentManager.Load <Texture2D>(textureAssetName);
                }

                var tileset = tiledMap.createTileset(
                    texture: texture,
                    firstId: reader.ReadInt32(),
                    tileWidth: reader.ReadInt32(),
                    tileHeight: reader.ReadInt32(),
                    isStandardTileset: isStandardTileset,
                    spacing: reader.ReadInt32(),
                    margin: reader.ReadInt32());
                readCustomProperties(reader, tileset.properties);

                // tiledset tile array
                var tileCount = reader.ReadInt32();
                for (var j = 0; j < tileCount; j++)
                {
                    var tile = new TiledTilesetTile(reader.ReadInt32(), tiledMap);

                    var tileAnimationFrameCount = reader.ReadInt32();
                    if (tileAnimationFrameCount > 0)
                    {
                        tile.animationFrames = new List <TiledTileAnimationFrame>(tileAnimationFrameCount);
                    }

                    for (var k = 0; k < tileAnimationFrameCount; k++)
                    {
                        tile.animationFrames.Add(new TiledTileAnimationFrame(reader.ReadInt32(), reader.ReadSingle()));
                    }

                    // image source is optional
                    var isFromImageCollection = reader.ReadBoolean();
                    if (isFromImageCollection)
                    {
                        var rect = new Rectangle(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
                        ((TiledImageCollectionTileset)tileset).setTileTextureRegion(tileset.firstId + tile.id, rect);
                    }

                    int objectGroupCount_ = reader.ReadInt32();
                    for (int k = 0; k < objectGroupCount_; k++)
                    {
                        var objectGroup = new TiledObjectGroup(reader.ReadString(), reader.ReadColor(), reader.ReadBoolean(), reader.ReadSingle());

                        readCustomProperties(reader, objectGroup.properties);

                        var objectCount = reader.ReadInt32();
                        objectGroup.objects = new TiledObject[objectCount];
                        for (var l = 0; l < objectCount; l++)
                        {
                            var obj = new TiledObject()
                            {
                                gid      = reader.ReadInt32(),
                                name     = reader.ReadString(),
                                type     = reader.ReadString(),
                                x        = reader.ReadInt32(),
                                y        = reader.ReadInt32(),
                                width    = reader.ReadInt32(),
                                height   = reader.ReadInt32(),
                                rotation = reader.ReadInt32(),
                                visible  = reader.ReadBoolean()
                            };

                            var tiledObjectType = reader.ReadString();
                            if (tiledObjectType == "ellipse")
                            {
                                // ellipse has no extra props
                                obj.tiledObjectType = TiledObject.TiledObjectType.Ellipse;
                            }
                            else if (tiledObjectType == "image")
                            {
                                obj.tiledObjectType = TiledObject.TiledObjectType.Image;
                                throw new NotImplementedException("Image objects are not yet implemented");
                            }
                            else if (tiledObjectType == "polygon")
                            {
                                obj.tiledObjectType = TiledObject.TiledObjectType.Polygon;
                                obj.polyPoints      = readVector2Array(reader);
                            }
                            else if (tiledObjectType == "polyline")
                            {
                                obj.tiledObjectType = TiledObject.TiledObjectType.Polyline;
                                obj.polyPoints      = readVector2Array(reader);
                            }
                            else
                            {
                                obj.tiledObjectType = TiledObject.TiledObjectType.None;
                            }

                            obj.objectType = reader.ReadString();

                            readCustomProperties(reader, obj.properties);

                            objectGroup.objects[l] = obj;
                            if (tile.objectGroups == null)
                            {
                                tile.objectGroups = new List <TiledObjectGroup>();
                            }
                            tile.objectGroups.Add(objectGroup);
                        }
                    }

                    readCustomProperties(reader, tile.properties);

                    // give the TiledTilesetTile a chance to process and cache any data required
                    tile.processProperties();

                    // if this tile is from an image collection and it has no properties there is no need to keep it around since we
                    // already grabbed the image source above
                    if (!(isFromImageCollection && tile.properties.Count == 0))
                    {
                        tileset.tiles.Add(tile);
                    }
                }
            }

            var layerCount = reader.ReadInt32();

            for (var i = 0; i < layerCount; i++)
            {
                var layer = readLayer(reader, tiledMap);
                readCustomProperties(reader, layer.properties);
            }


            var objectGroupCount = reader.ReadInt32();

            for (var i = 0; i < objectGroupCount; i++)
            {
                readObjectGroup(reader, tiledMap);
            }

            return(tiledMap);
        }
Esempio n. 5
0
        void renderObjectGroup( TiledObjectGroup group, Graphics graphics )
        {
            var renderPosition = entity.transform.position + _localOffset;

            foreach( var obj in group.objects )
            {
                if( !obj.visible )
                    continue;

                switch( obj.tiledObjectType )
                {
                    case TiledObject.TiledObjectType.Ellipse:
                        graphics.batcher.drawCircle( new Vector2( renderPosition.X + obj.x + obj.width * 0.5f, renderPosition.Y + obj.y + obj.height * 0.5f ), obj.width * 0.5f, group.color );
                        break;
                    case TiledObject.TiledObjectType.Image:
                        throw new NotImplementedException( "Image layers are not yet supported" );
                    case TiledObject.TiledObjectType.Polygon:
                        graphics.batcher.drawPoints( renderPosition, obj.polyPoints, group.color, true );
                        break;
                    case TiledObject.TiledObjectType.Polyline:
                        graphics.batcher.drawPoints( renderPosition, obj.polyPoints, group.color, false );
                        break;
                    case TiledObject.TiledObjectType.None:
                        graphics.batcher.drawHollowRect( renderPosition.X + obj.x, renderPosition.Y + obj.y, obj.width, obj.height, group.color );
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
        }