public void SetupTiles() { tileHeight = map.TileHeight; levelTileHeight = map.Height; levelTileWidth = map.Width; levelGrid = new Sprite[levelTileWidth, levelTileHeight]; foreach (TiledMapTileLayer layer in map.TileLayers) { if (layer.Name == "Collision") { collisionLayer = layer; } } int columns = 0; int rows = 0; int loadCount = 0; while (loadCount < collisionLayer.Tiles.Count) { if (collisionLayer.Tiles[loadCount].GlobalIdentifier != 0) { Sprite tileSprite = new Sprite(); tileSprite.Position.X = columns * tileHeight; tileSprite.Position.Y = rows * tileHeight; } } }
void ReadTileLayers(ContentReader input, TiledMap map) { var layersCount = input.ReadInt32(); var layers = new TiledMapTileLayer[layersCount]; for (var i = 0; i < layersCount; i += 1) { var layer = new TiledMapTileLayer(); ReadLayer(input, layer); layer.Width = input.ReadInt32(); layer.Height = input.ReadInt32(); layer.TileWidth = map.TileWidth; layer.TileHeight = map.TileHeight; var tiles = new TiledMapTile[layer.Width][]; for (var x = 0; x < layer.Width; x += 1) { tiles[x] = new TiledMapTile[layer.Height]; } for (var y = 0; y < layer.Height; y += 1) { for (var x = 0; x < layer.Width; x += 1) { tiles[x][y] = ReadTile(input); } } layer.Tiles = tiles; layers[i] = layer; } map.TileLayers = layers; }
static TiledMapTileLayer ParseTileLayer(XmlNode layerXml) { var layer = new TiledMapTileLayer(); ParseBaseLayer(layerXml, layer); layer.Width = int.Parse(layerXml.Attributes["width"].Value); layer.Height = int.Parse(layerXml.Attributes["height"].Value); if (layerXml["data"].Attributes["encoding"].Value != "csv") { throw new NotSupportedException("Error while parsing layer " + layer.Name + ". Only CSV encoding is supported."); } // Parsing csv tile values. var tilemapValuesStr = layerXml["data"].InnerText.Split(','); var tilemapValues = new uint[tilemapValuesStr.Length]; for (var i = 0; i < tilemapValues.Length; i += 1) { tilemapValues[i] = uint.Parse(tilemapValuesStr[i]); } // Parsing csv tile values. // Initing tile array. /* * Pipeline cannot work with 2-dimensional arrays, * so we're stuck arrays of arrays. */ var tiles = new TiledMapTile[layer.Width][]; for (var x = 0; x < layer.Width; x += 1) { tiles[x] = new TiledMapTile[layer.Height]; } // Initing tile array. // Filling tilemap with tiles. for (var y = 0; y < layer.Height; y += 1) { for (var x = 0; x < layer.Width; x += 1) { tiles[x][y] = new TiledMapTile(); var tilemapValue = tilemapValues[y * layer.Width + x]; // Tile flip flags are stored in the tile value itself as 3 highest bits. tiles[x][y].FlipHor = ((tilemapValue & (uint)FlipFlags.FlipHor) != 0); tiles[x][y].FlipVer = ((tilemapValue & (uint)FlipFlags.FlipVer) != 0); tiles[x][y].FlipDiag = ((tilemapValue & (uint)FlipFlags.FlipDiag) != 0); tiles[x][y].GID = (int)(tilemapValue & (~(uint)FlipFlags.All)); } } // Filling tilemap with tiles. layer.Tiles = tiles; return(layer); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); player.Load(Content); arialFont = Content.Load <SpriteFont>("Arial"); heart = Content.Load <Texture2D>("heart"); var viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, viewWidth, viewHeight); camera = new Camera2D(viewportAdapter); camera.Position = new Vector2(0, viewHeight); map = Content.Load <TiledMap>("Level"); mapRenderer = new TiledMapRenderer(GraphicsDevice); gameoverSound = Content.Load <Song>("GameOver"); winSound = Content.Load <Song>("Win"); foreach (TiledMapTileLayer layer in map.TileLayers) { if (layer.Name == "Solid") { collisionLayer = layer; } } gameMusic = Content.Load <Song>("SuperHero_original_no_Intro"); // TODO: use this.Content to load your game content here }
private void GenerateTileMap() { _tiledMap = _game.Content.Load <TiledMap>("Test"); _tiledMapRenderer = new TiledMapRenderer(_game.GraphicsDevice, _tiledMap); _mapWidth = _tiledMap.WidthInPixels; _mapHeight = _tiledMap.HeightInPixels; _collisionComponent = new CollisionComponent(new RectangleF(0, 0, _mapWidth, _mapHeight)); TiledMapTileLayer collidables = _tiledMap.GetLayer <TiledMapTileLayer>("Collidable"); if (collidables != null) { foreach (TiledMapTile t in collidables.Tiles) { // Global Identifier 0 means tile is empty if (t.GlobalIdentifier == 0) { continue; } Vector2 pos = Vector2.Zero; pos.X = t.X * collidables.TileWidth; pos.Y = t.Y * collidables.TileHeight; Size2 size = new Size2(collidables.TileWidth, collidables.TileHeight); Wall w = new Wall(pos, size); _collisionComponent.Insert(w); } } }
private static Player MakeWorldAndPlayer(params Rectangle[] immovables) { var world = new World(); var map = new TiledMap("TestMap", Room.NumTilesWidth, Room.NumTilesHeight, Room.TileWidth, Room.TileHeight, TiledMapTileDrawOrder.RightDown, TiledMapOrientation.Orthogonal); var bottomLayer = new TiledMapTileLayer(Dungeon.BottomLayerName, Room.NumTilesWidth, Room.NumTilesHeight, Room.TileWidth, Room.TileHeight); for (int y = 0; y < bottomLayer.Height; ++y) { for (int x = 0; x < bottomLayer.Width; ++x) { bottomLayer.SetTile((ushort)x, (ushort)y, 1); } } map.AddLayer(bottomLayer); var collidables = new TiledMapRectangleObject[immovables.Length]; int i = 0; foreach (Rectangle immovable in immovables) { // Note: Object IDs for Tiled maps start at 1, not 0. collidables[i] = new TiledMapRectangleObject(i + 1, "collidable" + (i + 1), new Size2(immovable.Width, immovable.Height), new Vector2(immovable.X, immovable.Y)); ++i; } var collisionLayer = new TiledMapObjectLayer(Dungeon.CollisionLayerName, collidables); map.AddLayer(collisionLayer); var dungeon = new Dungeon(world, map, null); world.Dungeons.Add(dungeon); var player = new Player(dungeon, new Point(Room.Width / 2, Room.Height / 2), Direction.Down); world.Player = player; return(player); }
private IEnumerable <TiledMapLayerModel> CreateTileLayerModels(TiledMap map, TiledMapTileLayer tileLayer) { var layerModels = new List <TiledMapLayerModel>(); var staticLayerBuilder = new TiledMapStaticLayerModelBuilder(); var animatedLayerBuilder = new TiledMapAnimatedLayerModelBuilder(); foreach (var tileset in map.Tilesets) { var firstGlobalIdentifier = map.GetTilesetFirstGlobalIdentifier(tileset); var lastGlobalIdentifier = tileset.TileCount + firstGlobalIdentifier - 1; var texture = tileset.Texture; foreach (var tile in tileLayer.Tiles.Where(t => firstGlobalIdentifier <= t.GlobalIdentifier && t.GlobalIdentifier <= lastGlobalIdentifier)) { var tileGid = tile.GlobalIdentifier; var localTileIdentifier = tileGid - firstGlobalIdentifier; var position = GetTilePosition(map, tile); var tilesetColumns = tileset.Columns == 0 ? 1 : tileset.Columns; // fixes a problem (what problem exactly?) var sourceRectangle = TiledMapHelper.GetTileSourceRectangle(localTileIdentifier, tileset.TileWidth, tileset.TileHeight, tilesetColumns, tileset.Margin, tileset.Spacing); var flipFlags = tile.Flags; // animated tiles var tilesetTile = tileset.Tiles.FirstOrDefault(x => x.LocalTileIdentifier == localTileIdentifier); if (tilesetTile is TiledMapTilesetAnimatedTile animatedTilesetTile) { animatedLayerBuilder.AddSprite(texture, position, sourceRectangle, flipFlags); animatedLayerBuilder.AnimatedTilesetTiles.Add(animatedTilesetTile); if (animatedLayerBuilder.IsFull) { layerModels.Add(animatedLayerBuilder.Build(_graphicsDevice, texture)); } } else { staticLayerBuilder.AddSprite(texture, position, sourceRectangle, flipFlags); if (staticLayerBuilder.IsFull) { layerModels.Add(staticLayerBuilder.Build(_graphicsDevice, texture)); } } } if (staticLayerBuilder.IsBuildable) { layerModels.Add(staticLayerBuilder.Build(_graphicsDevice, texture)); } if (animatedLayerBuilder.IsBuildable) { layerModels.Add(animatedLayerBuilder.Build(_graphicsDevice, texture)); } } return(layerModels); }
public IEnumerator <IWait> AddLayerToGround(TiledMapTileLayer layer) { var tiles = layer.Tiles.Where(t => !t.IsBlank).ToList(); tiles.Shuffle(this.random); foreach (var tile in tiles) { yield return(new WaitSeconds(0.15F)); this.Entities.Add(new SpawningTile(this, tile, new Vector2(tile.X, tile.Y))); } }
private void PrepareCollisionGrid(TiledMapTileLayer collisionLayer) { //Mapa de tiles colidíveis por padrão é 0 em todas as posições var collisionGridData = new int[this.map.Width * this.map.Height]; //Só mudamos o valor da célula para os tiles colidíveis da camada informada foreach (var c in collisionLayer.Tiles) { collisionGridData[c.X + (c.Y * this.map.Width)] = c.GlobalIdentifier; } this.collisionGrid = this.collisionWorld.CreateGrid(collisionGridData, collisionLayer.Width, collisionLayer.Height, collisionLayer.TileWidth, collisionLayer.TileHeight); }
public void setUpTiles() { tileHight = map.TileHeight; levelTileHight = map.TileHeight; levelTileWidth = map.TileWidth; levelGrid = new Sprite[levelTileWidth, levelTileHight]; foreach (TiledMapTileLayer layer in map.TileLayers) { if (layer.Name == "contact Lay") { collistionLayer = layer; } } }
/// <summary> /// Gets tile info for a specific tile on the map /// </summary> /// <param name="position">map position of the tile</param> /// <returns>tile info, or null when no tile info is available</returns> public TileInfo GetTileInfo(MapPosition position) { if (this.TileMap == null) { return(null); } int mapX = position.X; int mapY = position.Y; if (this.CurrentMap.EdgeType == Map.MapEdgeType.WrapAround) { if (mapX > this.TileMap.Width) { mapX %= this.TileMap.Width; } if (mapY > this.TileMap.Height) { mapY %= this.TileMap.Height; } } if (mapX < 0 || mapY < 0 || mapX >= this.TileMap.Width || mapY >= this.TileMap.Height) { return(null); } TiledMapTileLayer layer = this.TileMap.TileLayers.FirstOrDefault(); if (layer == null) { return(null); } if (!layer.TryGetTile((ushort)mapX, (ushort)mapY, out TiledMapTile? tile) || !tile.HasValue) { return(null); } return(GameData.GetTileInfo(tile.Value.GlobalIdentifier - 1)); }
public void SetUpTiles() { tileHeight = map.TileHeight; levelTileHeight = map.Height; levelTileWidth = map.Width; levelGrid = new Sprite[levelTileWidth, levelTileHeight]; foreach (TiledMapTileLayer layer in map.TileLayers) { if (layer.Name == "Collisions") { collisionLayer = layer; } } int columns = 0; int rows = 0; int loopCount = 0; while (loopCount < collisionLayer.Tiles.Count) { if (collisionLayer.Tiles[loopCount].GlobalIdentifier != 0) { Sprite tileSprite = new Sprite(); tileSprite.position.X = columns * tileHeight; tileSprite.position.Y = rows * tileHeight; tileSprite.width = tileHeight; tileSprite.height = tileHeight; tileSprite.UpdateHitBox(); allCollisionTiles.Add(tileSprite); levelGrid[columns, rows] = tileSprite; } columns++; if (columns == levelTileWidth) { columns = 0; rows++; } loopCount++; } }
private static bool IsRoomEmpty(TiledMapTileLayer layer, int startX, int startY) { for (int y = startY; y < startY + Room.NumTilesHeight; ++y) { for (int x = startX; x < startX + Room.NumTilesWidth; ++x) { // If one tile exists in this Room, then we consider it not empty. TiledMapTile?tile; if (layer.TryGetTile((ushort)x, (ushort)y, out tile)) { if (!tile.Value.IsBlank) { return(false); } } } } return(true); }
protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); // TODO: use this.Content to load your game content here _playerTexture = Content.Load <Texture2D>("ball"); _map = Content.Load <TiledMap>("Plazas/sample"); _tiledMapRenderer = new TiledMapRenderer(GraphicsDevice, _map); _borders = _map.GetLayer <TiledMapTileLayer>("Obstacles"); _objects = _map.GetLayer <TiledMapObjectLayer>("Objects"); var pos = _objects?.Objects?.FirstOrDefault(o => { o.Properties.TryGetValue("player", out string playerPos); return(int.Parse(playerPos) == 1); }); _startPosition = pos.Position; }
public Location FormLocation(Random random, ref ILocationGenerator.CellType[,] map) { uint width = (uint)map.GetLength(0); uint height = (uint)map.GetLength(1); var location = new Location(width, height); var tiles = Storage.Content.Load <TiledMapTileset>("maps/dungeon_tiles"); var floors = new TiledMapTileLayer("floors", (int)width, (int)height, 64, 32, new Vector2(0, -32)); location.TiledMap.AddLayer(floors); var walls = new TiledMapTileLayer("walls", (int)width, (int)height, 64, 32, new Vector2(0, -32)); location.TiledMap.AddLayer(walls); location.TiledMap.AddTileset(tiles, 1); for (ushort x = 1; x < width; x++) { for (ushort y = 1; y < height; y++) { floors.SetTile(x, y, (uint)(location.TiledMap.GetTilesetFirstGlobalIdentifier(tiles) + 74)); } } for (ushort x = 0; x < width; x++) { for (ushort y = 0; y < height; y++) { if (map[x, y] == ILocationGenerator.CellType.Wall) { walls.SetTile(x, y, (uint)(location.TiledMap.GetTilesetFirstGlobalIdentifier(tiles) + 32)); location.SetWall(x, y); } } } return(location); }
protected virtual void BuildLayerCache() { layers.Clear(); layerMap.Clear(); for (var layerIndex = 0; layerIndex < Map.Layers.Count; layerIndex++) { var mooseLayer = Map.Layers[layerIndex] switch { TiledMapTileLayer tileLayer => new TiledMooseTileLayer(tileLayer), TiledMapObjectLayer objectLayer => new TiledMooseObjectLayer(objectLayer, Width, Height), _ => null as ILayer }; if (mooseLayer != null) { AddLayer(mooseLayer); } } BuildFullBlockingMap(); }
public Canopy(TiledMapTileLayer layer) { Layer = layer; }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); player.Load(Content); candaraFont = Content.Load <SpriteFont>("Candara"); heart = Content.Load <Texture2D>("heart"); BoxingViewportAdapter viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, ScreenWidth, ScreenHeight); camera = new Camera2D(viewportAdapter); camera.Position = new Vector2(0, ScreenHeight); gameMusic = Content.Load <Song>("Music/SuperHero_original_no_Intro"); MediaPlayer.Volume = 0.1f; MediaPlayer.Play(gameMusic); map = Content.Load <TiledMap>("samp1"); mapRenderer = new TiledMapRenderer(GraphicsDevice); foreach (TiledMapTileLayer layer in map.TileLayers) { if (layer.Name == "Collisions") { collisionLayer = layer; } } foreach (TiledMapObjectLayer layer in map.ObjectLayers) { if (layer.Name == "Enemies") { foreach (TiledMapObject obj in layer.Objects) { Enemy enemy = new Enemy(this); enemy.Load(Content); enemy.Position = new Vector2(obj.Position.X, obj.Position.Y); enemies.Add(enemy); } } if (layer.Name == "Loot") { TiledMapObject obj = layer.Objects[0]; if (obj != null) { AnimatedTexture anim = new AnimatedTexture(Vector2.Zero, 0, 1, 1); anim.Load(Content, "gem_3", 1, 1); gem = new Sprite(); gem.Add(anim, 0, 5); gem.position = new Vector2(obj.Position.X, obj.Position.Y); } } } // TODO: use this.Content to load your game content here }
public override void Update(ContentManager Content, GameTime gameTime) { if (isLoaded == false) { font = Content.Load <SpriteFont>("Arial"); var viewportAdapter = new BoxingViewportAdapter(game1.Window, game1.GraphicsDevice, ScreenWidth, ScreenHeight); camera = new Camera2D(viewportAdapter); camera.Position = new Vector2(0, ScreenHeight); map = Content.Load <TiledMap>("Project_Map"); mapRenderer = new TiledMapRenderer(game1.GraphicsDevice); player.Load(Content); foreach (TiledMapTileLayer layer in map.TileLayers) { if (layer.Name == "Collision") { collisionLayer = layer; } if (layer.Name == "Spikes") { spikeLayer = layer; } } foreach (TiledMapObjectLayer layer in map.ObjectLayers) { if (layer.Name == "Enemies") { foreach (TiledMapObject obj in layer.Objects) { Enemy enemy = new Enemy(this); enemy.Load(Content); enemy.Position = new Vector2(obj.Position.X, obj.Position.Y); enemies.Add(enemy); } } if (layer.Name == "Goal") { TiledMapObject obj = layer.Objects[0]; if (obj != null) { AnimatedTexture anim = new AnimatedTexture(Vector2.Zero, 0, 1, 1); anim.Load(Content, "chest", 1, 1); goal = new Sprite(); goal.Add(anim, 0, 5); goal.position = new Vector2(obj.Position.X, obj.Position.Y); } } if (layer.Name == "Key") { TiledMapObject key = layer.Objects[0]; if (key != null) { AnimatedTexture keyAnim = new AnimatedTexture(Vector2.Zero, 0, 1, 1); keyAnim.Load(Content, "Key", 1, 1); keys = new Sprite(); keys.Add(keyAnim, 0, 5); keys.position = new Vector2(key.Position.X, key.Position.Y); } } } gameMusic = Content.Load <SoundEffect>("Superhero_violin_no_intro"); gameMusicInstance = gameMusic.CreateInstance(); gameMusicInstance.IsLooped = true; gameMusicInstance.Play(); zombieDeath = Content.Load <SoundEffect>("zombie_death"); zombieDeathInstance = zombieDeath.CreateInstance(); keyJingle = Content.Load <SoundEffect>("key_collect"); keyJingleInstance = keyJingle.CreateInstance(); chestOpen = Content.Load <SoundEffect>("chest_opened"); chestOpenInstance = chestOpen.CreateInstance(); arialFont = Content.Load <SpriteFont>("Arial"); heart = Content.Load <Texture2D>("heart"); keyIcon = Content.Load <Texture2D>("Key - Icon"); lives = 3; isLoaded = true; } float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; player.Update(deltaTime); camera.Position = player.Position - new Vector2(ScreenWidth / 2, ScreenHeight / 2); if (keyLost == true) { timer += deltaTime; if (timer >= 3.0f) { keyLost = false; timer = 0f; } } if (score <= 0) { score = 0; } foreach (Enemy e in enemies) { e.Update(deltaTime); } CheckCollisions(); if (lives <= 0 || keyCollected == true && chestInteracted == true) { if (keyCollected == true && chestInteracted == true) { score += 3; chestOpen.Play(); } _2D_Platformer.StateManager.ChangeState("GAMEOVER"); enemies.Clear(); gameMusicInstance.Stop(); keyCollected = false; isLoaded = false; } }
public Tile GetTileAt(TiledMapTileLayer layer, int x, int y) { return(Map.GetTileAt(layer, x, y)); }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); player.Load(Content); BoxingViewportAdapter viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, ScreenWidth, ScreenHeight); camera = new Camera2D(viewportAdapter); camera.Position = new Vector2(-100, -100); splash = Content.Load <Texture2D>("splashscreen"); healthBar = Content.Load <Texture2D>("healthbar"); map = Content.Load <TiledMap>("dungeon1"); mapRenderer = new TiledMapRenderer(GraphicsDevice); arial = Content.Load <SpriteFont>("Arial"); graveyard = Content.Load <Texture2D>("graveyard"); deathText = Content.Load <Texture2D>("deathtext"); treasure = Content.Load <Texture2D>("treasurepile"); treasureText = Content.Load <Texture2D>("treasuretext"); foreach (TiledMapTileLayer layer in map.TileLayers) { if (layer.Name == "Collisions") { ; } collisionLayer = layer; } foreach (TiledMapObjectLayer layer in map.ObjectLayers) { if (layer.Name == "Zombies") { foreach (TiledMapObject obj in layer.Objects) { Zombie zombie = new Zombie(this); zombie.Load(Content); zombie.GetPlayer = player; zombie.Position = new Vector2(obj.Position.X, obj.Position.Y); zombies.Add(zombie); } } } foreach (TiledMapObjectLayer layer in map.ObjectLayers) { if (layer.Name == "Coins") { foreach (TiledMapObject obj in layer.Objects) { Coin coin = new Coin(this); coin.Load(Content); coin.Position = new Vector2(obj.Position.X, obj.Position.Y); coins.Add(coin); } } } foreach (TiledMapObjectLayer layer in map.ObjectLayers) { if (layer.Name == "Skeletons") { foreach (TiledMapObject obj in layer.Objects) { Skeleton skeleton = new Skeleton(this); skeleton.Load(Content); skeleton.GetPlayer = player; skeleton.Position = new Vector2(obj.Position.X, obj.Position.Y); skeletons.Add(skeleton); } } } foreach (TiledMapObjectLayer layer in map.ObjectLayers) { if (layer.Name == "Turrets") { foreach (TiledMapObject obj in layer.Objects) { Turret turret = new Turret(this); turret.Load(Content); turret.GetPlayer = player; turret.Position = new Vector2(obj.Position.X, obj.Position.Y); turrets.Add(turret); } } } zombieDeathSound = Content.Load <SoundEffect>("zombiedeath"); zombieDeathSoundInstance = zombieDeathSound.CreateInstance(); skeletonDeathSound = Content.Load <SoundEffect>("skeletondeath"); skeletonDeathSoundInstance = skeletonDeathSound.CreateInstance(); playerHurtSound = Content.Load <SoundEffect>("playerhurt"); playerHurtSoundInstance = playerHurtSound.CreateInstance(); playerDeathSound = Content.Load <SoundEffect>("playerdeath"); playerDeathSoundInstance = playerDeathSound.CreateInstance(); turretDeathSound = Content.Load <SoundEffect>("turretDeath"); turretDeathSoundInstance = turretDeathSound.CreateInstance(); coinPickupSound = Content.Load <SoundEffect>("coinSound"); coinPickupSoundInstance = coinPickupSound.CreateInstance(); backgroundMusic = Content.Load <Song>("Metaruka - The End"); }