private void ReadTilesets(ContentReader reader, TiledMap tiledMap)
        {
            var tilesetCount = reader.ReadInt32();

            for (var i = 0; i < tilesetCount; i++)
            {
                var tileset = new TiledTileset();
                tileset.Image.Source        = reader.ReadString();
                tileset.Image.Width         = reader.ReadInt32();
                tileset.Image.Height        = reader.ReadInt32();
                tileset.Image.SpriteTexture = reader.ContentManager.Load <Texture2D>(tileset.Image.Source);
                tileset.FirstGid            = reader.ReadInt32();
                tileset.TileWidth           = reader.ReadInt32();
                tileset.TileHeight          = reader.ReadInt32();
                tileset.Spacing             = reader.ReadInt32();
                tileset.Margin    = reader.ReadInt32();
                tileset.Tilecount = reader.ReadInt32();
                tileset.Columns   = reader.ReadInt32();

                var tileSetTileCount = reader.ReadInt32();
                for (var j = 0; j < tileSetTileCount; j++)
                {
                    var tileId = reader.ReadInt32();
                    tileId = tileset.FirstGid + tileId - 1;

                    var tiledTile = new TiledTile();
                    tiledTile.Id = tileId;
                    // ISSUE #25
                    tileset.Tiles.Add(tiledTile);
                }

                tiledMap.Tilesets.Add(tileset);
            }
        }
Exemple #2
0
        public TiledMap bake()
        {
            // initializing
            TiledMap     map            = new TiledMap(0, Constants.LEVEL_ROWS, Constants.LEVEL_COLUMNS, Constants.TILE_WIDTH, Constants.TILE_HEIGHT);
            Texture2D    tilesetTexture = GlintCore.contentSource.Load <Texture2D>("spritesheet");
            TiledTileset tiledTileset   = map.createTileset(tilesetTexture, 0, Constants.TILE_WIDTH, Constants.TILE_HEIGHT, true, 0, 0, 5, 5);

            // creating the background
            TiledTile[] backgroundTiles = new TiledTile[data.GetLength(0) * data.GetLength(1)];
            int         mw = data.GetLength(0);
            int         mh = data.GetLength(1);

            for (int j = 0; j < mh; j++)
            {
                for (int i = 0; i < mw; i++)
                {
                    backgroundTiles[j * mw + i] = new TiledTile((int)Constants.Id.Air)
                    {
                        tileset = tiledTileset
                    };
                }
            }

            // creating the collidable tiledtile[]
            TiledTile[] collidableTiles = new TiledTile[data.GetLength(0) * data.GetLength(1)];
            for (int j = 0; j < mh; j++)
            {
                for (int i = 0; i < mw; i++)
                {
                    if (data[i, j].id == (int)Constants.Id.Solid)
                    {
                        collidableTiles[j * mw + i] = new TiledTile(data[i, j].id)
                        {
                            tileset = tiledTileset
                        }
                    }
                    ;
                }
            }

            // creating the water tiledtile[]
            TiledTile[] waterTiles = new TiledTile[data.GetLength(0) * data.GetLength(1)];
            for (int i = 0; i < mw; i++)
            {
                for (int j = mh - waterLevel; j < mh; j++)
                {
                    waterTiles[j * mw + i] = new TiledTile((int)Constants.Id.Water)
                    {
                        tileset = tiledTileset
                    };
                }
            }

            // adding the layers together
            map.createTileLayer("background", map.width, map.height, backgroundTiles);
            map.createTileLayer("water", map.width, map.height, waterTiles);
            map.createTileLayer("walls", map.width, map.height, collidableTiles);

            return(map);
        }
Exemple #3
0
        private void addTiledMapComponent( )
        {
            var spriteSheet = scene.content.Load <Texture2D>("images/blocks");
            var tileset     = _tileMap.createTileset(spriteSheet, 0, TILESIZE, TILESIZE, true, 0, 0, TILE_COUNT, TILESET_COLUMNS);

            // Nez' tiledmap implementation will only take 1d arrays,
            // so we have to do this if we want to use the more intuitive
            // 2d array, unfortunatly.
            var _tilesTEMP = new TiledTile[WIDTH * HEIGHT];

            for (int x = 0; x < _tiles.GetLength(0); x++)
            {
                for (int y = 0; y < _tiles.GetLength(1); y++)
                {
                    _tiles[x, y]              = new TiledTile(0);
                    _tiles[x, y].tileset      = tileset;
                    _tilesTEMP[WIDTH * y + x] = _tiles[x, y];
                }
            }

            var layer     = _tileMap.createTileLayer("Default", WIDTH, HEIGHT, _tilesTEMP);
            var component = new TiledMapComponent(_tileMap);

            addComponent(component);
        }
Exemple #4
0
        public static T TileGetProperty <T>(this uint tile, List <TiledTilesetDefinition> tilesets, CompilerContext context, string propertyName, Func <string, T> converter, bool errorOnNotFound = true, T defaultValue = default(T))
        {
            TiledTilesetDefinition tileset = tilesets.GetTilesetByGlobalTileId(tile);
            uint      relativeTile         = tile.TileGetRelativeId(tilesets, context);
            TiledTile chosenTile           = tileset.RealTileset.Tiles.FirstOrDefault(t => t.Id == relativeTile);

            if (chosenTile == null)
            {
                if (errorOnNotFound)
                {
                    context.ExitError("could not find {0} on tile {1}", propertyName, tile);
                }
                context.PushWarning("tile {0}  does not have a {1}, assuming {2}", tile, propertyName, defaultValue);
                return(defaultValue);
            }
            if (chosenTile.Properties.PropertyList == null)
            {
                if (errorOnNotFound)
                {
                    context.ExitError("could not find {0} on tile {1}", propertyName, tile);
                }
                context.PushWarning("tile {0}  does not have a {1}, assuming {2}", tile, propertyName, defaultValue);
                return(defaultValue);
            }
            if (!chosenTile.Properties.PropertyList.Exists(prop => prop.Name == propertyName))
            {
                if (errorOnNotFound)
                {
                    context.ExitError("could not find {0} on tile {1}", propertyName, tile);
                }
                context.PushWarning("tile {0}  does not have a {1}, assuming {2}", tile, propertyName, defaultValue);
                return(defaultValue);
            }
            return(chosenTile.Properties.RetrieveProperty(propertyName, context, chosenTile, converter, errorOnNotFound, defaultValue));
        }
Exemple #5
0
        public static TiledTile[] generateWallMap(int width, int height, TiledTileset tileset)
        {
            var map      = new TiledTile[width * height];
            var wallTile = 0;

            for (var r = 0; r < height; r++)
            {
                for (var c = 0; c < width; c++)
                {
                    if (r == 0 || r == height - 1)
                    {
                        // map.push(wallTile);
                        map[r * width + c] = new TiledTile(wallTile);
                    }
                    else
                    {
                        if (c == 0 || c == (width - 1))
                        {
                            //map.push(wallTile);
                            map[r * width + c] = new TiledTile(wallTile);
                        }
                        else
                        {
                            //map.push(airTile);
                            map[r * width + c] = null;
                        }
                    }
                    if (map[r * width + c] != null)
                    {
                        map[r * width + c].tileset = tileset;
                    }
                }
            }
            return(map);
        }
Exemple #6
0
 public void RemoveTile(TiledTile tile)
 {
     for (var index = 0; index < this.Tiles.Length; index++)
     {
         if (tile == this.Tiles[index])
         {
             this.Tiles[index] = null;
         }
     }
 }
Exemple #7
0
        public bool TileMapCollision(int x, int y)
        {
            TiledTile t = TileCollisionLayer.GetTile(x, y);

            if (t == null)
            {
                return(false);
            }
            return(t.Id != 0);
        }
Exemple #8
0
        public void Load(NezContentManager content, Vector2 ScreenVector)
        {
            myMap = new TiledMap(0, (int)ScreenVector.X, (int)ScreenVector.Y, TileData.TileSize, TileData.TileSize, TiledMapOrientation.Orthogonal);
            //TODO: multilayer support
            TiledTileLayer Floors = (TiledTileLayer)myMap.createTileLayer("Floors", (int)ScreenVector.X, (int)ScreenVector.Y);

            Collidables = (TiledTileLayer)myMap.createTileLayer("Walls", (int)ScreenVector.X, (int)ScreenVector.Y);
            int          XOffset        = (int)Mathf.ceil(0.5f * (ScreenVector.X / TileData.TileSize));
            int          YOffset        = (int)Mathf.ceil(0.5f * (ScreenVector.Y / TileData.TileSize));
            TiledTileset BaseMapTileset = myMap.createTileset(content.Load <Texture2D>(this.MapTextureAtlas), 0, 64, 64, true, 0, 0, this.TextureAtlasTileWidth, this.TextureAtlasTileHeight);

            for (int x = 0; x < Mathf.ceil(ScreenVector.X / TileData.TileSize); x++)
            {
                for (int y = 0; y < Mathf.ceil(ScreenVector.X / TileData.TileSize); x++)
                {
                    TileData  myTileData;
                    TiledTile myTile;
                    Tuple <float, float, float> tuple = new Tuple <float, float, float>(x, y, (float)Math.Ceiling(TileData.MaxHeight * myNoise.GetNoise(x, y)));
                    if (myModifiedTiles.TryGetValue(tuple, out myTileData))
                    {
                        myTile         = new TiledTile(myTileData.TileSetID);
                        myTile.tileset = myMap.createTileset(content.Load <Texture2D>(myTileData.TilesetName), 0, TileData.TileSize, TileData.TileSize, true, 0, 0, myTileData.TextureAtlasTileWidth, myTileData.TextureAtlasTileHeight);
                        if (myTileData.isWall)
                        {
                            Collidables.setTile(myTile);
                        }
                        else
                        {
                            Floors.setTile(myTile);
                        }
                    }
                    else
                    {
                        if (tuple.Item3 > 0)
                        {
                            myTile         = new TiledTile(1);
                            myTile.tileset = BaseMapTileset;
                            Collidables.setTile(myTile);
                        }
                        else if (tuple.Item3 == 0)
                        {
                            myTile         = new TiledTile(0);
                            myTile.tileset = BaseMapTileset;
                            Floors.setTile(myTile);
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
            }
        }
Exemple #9
0
        public int CellAtTileCoord(int tx, int ty)
        {
            if (tx < 0 || tx >= map.Width || ty < 0)
            {
                return(1);
            }
            if (ty >= map.Height)
            {
                return(0);
            }
            TiledTile tile = collisionLayer.GetTile(tx, ty);

            return(tile.Id);
        }
Exemple #10
0
        public TiledTile getTileAt(Vector2 pos)
        {
            TiledTile ret = null;

            var tile_pos = map.worldToTilePosition(pos, false);

            if (tile_pos.X > 0 && tile_pos.X < map.width && tile_pos.Y > 0 && tile_pos.Y < map.height)
            {
                var tilelayer = (TiledTileLayer)map.layers[0];  // FIXME
                ret = tilelayer.getTile(tile_pos.X, tile_pos.Y);
            }

            return(ret);
        }
Exemple #11
0
        public int CellAtTileCoord(int TX, int TY)
        {
            if (TX < 0 || TX >= Map.Width || TY < 0)
            {
                return(1);
            }
            if (TY >= Map.Height)
            {
                return(0);
            }

            TiledTile Tile = CollisionLayer.GetTile(TX, TY);

            return(Tile.Id);
        }
        public int CellAtTileCoord(int tx, int ty)
        {
            if (tx < 0 || tx >= map.Width || ty < 0)
            {
                return(1);
            }
            // let the player drop of the bottom of the screen (this means death)
            if (ty >= map.Height)
            {
                return(0);
            }
            TiledTile tile = collisionLayer.GetTile(tx, ty);

            return(tile.Id);
        }
Exemple #13
0
        public static TiledTile TileGetTileObject(this uint tile, List <TiledTilesetDefinition> tilesets, CompilerContext context)
        {
            TiledTilesetDefinition tileset = tilesets.GetTilesetByGlobalTileId(tile);

            if (tileset == null)
            {
                context.ExitError("could not find tileset of tile {0}", tile);
            }
            TiledTile foundTile = tileset.RealTileset.Tiles.FirstOrDefault(t => t.Id == tile - tileset.FirstGlobalId);

            if (foundTile == null)
            {
                context.ExitError("could not find tile object for tile {0}", tile);
            }
            return(foundTile);
        }
        protected void checkForCollision(CollisionResult res)
        {
            //Find tile collision detects
            TiledTile tile = _mapComponent.getTileAtWorldPosition(entity.position + new Vector2(-16) * res.normal);

            if (tile != null && tile.tilesetTile != null && tile.tilesetTile.isDestructable)   //Destroy tile if isDestructable
            {
                _mapComponent.collisionLayer.removeTile(tile.x, tile.y);
                _mapComponent.removeColliders();
                _mapComponent.addColliders();
            }
            else
            {
                entity.destroy();
            }
        }
        public static TiledTileSet ToTiledTileSet(this JsonTileSet jsonTileSet)
        {
            var tiledTileSet = new TiledTileSet();

            tiledTileSet.FirstgId  = jsonTileSet.FirstgId;
            tiledTileSet.Source    = jsonTileSet.Source;
            tiledTileSet.TileSheet = new TiledTileSheet
            {
                Columns      = jsonTileSet.TileSheet.Columns,
                Image        = jsonTileSet.TileSheet.Image,
                ImageHeight  = jsonTileSet.TileSheet.ImageHeight,
                ImageWidth   = jsonTileSet.TileSheet.ImageWidth,
                Margin       = jsonTileSet.TileSheet.Margin,
                Name         = jsonTileSet.TileSheet.Name,
                Spacing      = jsonTileSet.TileSheet.Spacing,
                TileCount    = jsonTileSet.TileSheet.TileCount,
                TiledVersion = jsonTileSet.TileSheet.TiledVersion,
                TileHeight   = jsonTileSet.TileSheet.TileHeight,
                TileWidth    = jsonTileSet.TileSheet.TileWidth,
                Type         = jsonTileSet.TileSheet.Type,
                Version      = jsonTileSet.TileSheet.Version
            };
            if (jsonTileSet.TileSheet.Tiles != null)
            {
                foreach (var jsonTile in jsonTileSet.TileSheet.Tiles)
                {
                    var tile = new TiledTile()
                    {
                        Id = jsonTile.Id
                    };
                    foreach (var jsonAnimation in jsonTile.Animation)
                    {
                        tile.Animation.Add(new TiledAnimation
                        {
                            Duration = jsonAnimation.Duration,
                            TileId   = jsonAnimation.TileId
                        });
                    }
                    tiledTileSet.TileSheet.Tiles.Add(tile);
                }
            }
            return(tiledTileSet);
        }
Exemple #16
0
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            // Draw background

            GraphicsDevice.Clear(_tiledMap.BackgroundColor ?? Color.Black);

            _spriteBatch.Begin();
            _spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
            _spriteBatch.End();

            _spriteBatch.Begin(transformMatrix: _camera.GetViewMatrix());

            // or you can have more control over drawing each individual layer
            foreach (var layer in _tiledMap.Layers)
            {
                TiledTileLayer tiledTileLayer = layer as TiledTileLayer;
                TiledTile      t = tiledTileLayer.GetTile(0, 24);
                _spriteBatch.Draw(layer, _camera);
            }

            _spriteBatch.Draw(_sprite);              // Draws a sprite over the cursor

            _spriteBatch.End();

            //mainUI.Update (_spriteBatch, _world.WorldCalendar, gameTime);

            //			var textColor = Color.White;
            //			_spriteBatch.Begin(samplerState: SamplerState.PointClamp, blendState: BlendState.AlphaBlend);
            //			_spriteBatch.DrawString(_bitmapFont, "WASD/Arrows: move", new Vector2(5, 32), textColor);
            //			_spriteBatch.DrawString(_bitmapFont, "RF: zoom", new Vector2(5, 32 + _bitmapFont.LineSpacing), textColor);
            //			_spriteBatch.DrawString(_bitmapFont, $"FPS: {_fpsCounter.AverageFramesPerSecond:0}", Vector2.One, Color.AliceBlue);
            //			_spriteBatch.DrawString(_bitmapFont, "Tile: " + mouseOverTile.X + "," + mouseOverTile.Y, new Vector2(5, 32 + _bitmapFont.LineSpacing*2), Color.AliceBlue);
            //			_spriteBatch.DrawString(_bitmapFont, "MouseOver: " + mouseScreenPostion.X + "," + mouseScreenPostion.Y, new Vector2(5, 32 + _bitmapFont.LineSpacing*3), Color.AliceBlue);
            //			_spriteBatch.End();

            base.Draw(gameTime);
        }
Exemple #17
0
        private void CustomizeTile(TiledTile tile, Vector2 pos, Vector2 size)
        {
            if (tile == null)
            {
                return;
            }

            var properties = tile?.tilesetTile?.properties;

            if (properties == null)
            {
                return;
            }
            if (properties.ContainsKey(TiledProperties.EmitsLight))
            {
                var entity = scene.createEntity("world-light", pos + size);
                entity.setScale(0.55f);

                var sprite = entity.addComponent(new Sprite(AssetLoader.GetTexture("effects/lightmask")));
                sprite.material    = Material.blendLighten();
                sprite.color       = new Color(Color.White, 0.4f);
                sprite.renderLayer = Layers.Lights;
            }
        }
        private static List<TiledTile> ReadTilesetMetadata(XElement root)
        {
            var tiles = new List<TiledTile>();
            var tileElements = root.Elements("tile");
            foreach (var tileElement in tileElements)
            {
                var tile = new TiledTile();
                tile.Id = tileElement.ReadAttribute("id", 0);

                PropertyParser.ReadProperties(tile, tileElement);

                tiles.Add(tile);
            }
            return tiles;
        }
 private Microsoft.Xna.Framework.Vector2 getTilePosition(TiledTile tile)
 {
     return(tile.getWorldPosition(entity.scene.findEntity(EntityNames.Map).getComponent <TiledMapComponent>().tiledMap));
 }
Exemple #20
0
 public void SetTile(int x, int y, TiledTile tile)
 {
     this.Tiles[x + y * this.Width] = tile;
 }
        void IUpdatable.update()
        {
            if (Input.isKeyDown(Keys.Left) && !Input.isKeyDown(Keys.Right))
            {
                velocity.X = -(float)Constants.PLAYER_SPEED;
            }
            if (Input.isKeyDown(Keys.Right) && !Input.isKeyDown(Keys.Left))
            {
                velocity.X = (float)Constants.PLAYER_SPEED;
            }
            if (!Input.isKeyDown(Keys.Left) && !Input.isKeyDown(Keys.Right))
            {
                velocity.X = 0;
            }
            if (Input.isKeyDown(Keys.Up) && (grounded || (DateTime.Now - coyoteTime).TotalMilliseconds < Constants.PLAYER_COYOTE_TIME))
            {
                velocity.Y = -(float)Math.Sqrt(2 * Constants.GRAVITY * Constants.PLAYER_JUMP_HEIGHT);
            }

            grounded    = false;
            velocity.Y += (float)Constants.GRAVITY * Time.deltaTime;
            Console.WriteLine(coyoteTime + "\n" + DateTime.Now + "\n");
            // water collision
            TiledTileLayer waterLayer = (TiledTileLayer)entity.scene.entities.findEntity("map_tiles")
                                        .getComponent <TiledMapComponent>()
                                        .tiledMap.getLayer((int)Constants.Layer.Water);
            List <TiledTile> waterTiles = waterLayer.getTilesIntersectingBounds(new Rectangle((int)entity.position.X - Constants.BUFFER_ZONE,
                                                                                              (int)entity.position.Y - Constants.BUFFER_ZONE,
                                                                                              Constants.PLAYER_WIDTH,
                                                                                              Constants.PLAYER_HEIGHT));

            for (int i = 0; i < waterTiles.Count; i++)
            {
                TiledTile tile = waterTiles[i];

                // Console.WriteLine(underWater + " " + i + " " + waterTiles[i].id);
                // Console.WriteLine(waterTiles.Count);
                if (tile.id == (int)Constants.Id.Water)
                {
                    velocity.Y -= Constants.BOUYANT_FORCE * Time.deltaTime;

                    if (underWater == false)
                    {
                        if (Math.Abs(velocity.Y) < Constants.DAMPENING_FORCE_UPON_ENTRY)
                        {
                            velocity.Y = 0;
                        }
                        else
                        {
                            velocity.Y -= Constants.DAMPENING_FORCE_UPON_ENTRY;
                        }
                        underWater = true;
                    }

                    break;
                }
            }

            // If none of the tiles were water
            if (waterTiles.Count == 0)
            {
                underWater = false;
            }

            var motion = velocity * Time.deltaTime;

            // wall collision
            collisions.Clear();
            if (collider.collidesWithAnyMultiple(motion, collisions))
            {
                for (int i = 0; i < collisions.Count; i++)
                {
                    motion -= collisions[i].minimumTranslationVector;
                    // Console.WriteLine(collisions[i].minimumTranslationVector);
                    // Console.WriteLine(entity.position);
                    if (collisions[i].normal.Y == 1)
                    {
                        velocity.Y = 0;
                    }

                    if (collisions[i].normal.Y == -1)
                    {
                        velocity.Y = 0;
                        Console.WriteLine("Hit floor!");
                        grounded   = true;
                        coyoteTime = DateTime.Now;
                    }
                }
            }

            entity.position += motion;
        }