public TileMap(Vector2 pos, string mapPath, string tileset) { Pos = pos; var map = new TiledSharp.TmxMap(mapPath); var texture = tileset; foreach (var layer in map.Layers) { foreach (var tile in layer.Tiles) { if (tile.Gid == 0) { continue; } var t = new TileSprite { Pos = new Vector2(tile.X * map.TileWidth, tile.Y * map.TileHeight), MapPos = new Point(tile.X, tile.Y), Size = new Point(map.TileWidth, map.TileHeight), TexturePath = texture, Id = tile.Gid }; AddChild(t); } } Size = new Point(map.Width * map.TileWidth, map.Height * map.TileHeight); }
public void OpenMap(string filename) { try { var map = new TiledSharp.TmxMap(filename); Controller = new GameController(this, map); mapView1.Controller = Controller; mapView1.Refresh(); CurrentFilename = filename; } catch (Exception exc) { Console.WriteLine(exc); MessageBox.Show(exc.Message, "Error loading map", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void initLevel(ref World world, ref EnemyContainer eContrainer, ref Player player, ref CollectableContainer collectables) { world = new World(new Vector2(0, 0)); eContrainer = new EnemyContainer(world); collectables = new CollectableContainer(world); Level level = getNextLevel(); TileSpriteList = new List <Drawable>(); CreateShape(new SFML.Graphics.Texture(level.AlphaTexture), world); SFML.Graphics.Texture tilemap = new SFML.Graphics.Texture(level.TilemapImage); test = new TiledSharp.TmxMap(level.TMX); var myTileset = test.Tilesets[level.Tilesetname]; Map newMap = new Map(test.Width, test.Height, test.TileWidth); this.Map = newMap; parseTileLayer(tilemap); parseObjectLayer(world, eContrainer, ref player, collectables); }
private void InitializeSpecialStageObjects(ContentManager content, List <StageObject> stageObjects, TiledSharp.TmxMap tmx, TiledSharp.TmxList <TiledSharp.TmxObjectGroup.TmxObject> objectgroup) { // Initialize Special Background Objects foreach (TiledSharp.TmxObjectGroup.TmxObject tmxObject in objectgroup) { // object's Y-coordinate is oriented to the bottom of the placement of the object, and we need top. // so, we get the tile and look at its height and subtract that from the object's Y-coordinate to detemrine placement of object. float fObjectHeight = 0; StageObject s = null; switch (tmxObject.Type) { case "Bridge": case "EnemySpawnLocation": switch (tmxObject.Type) { case "Bridge": s = new StageBridge(); break; case "EnemySpawnLocation": s = new EnemySpawnLocation("EnemyFootSoldier"); break; default: throw new InvalidDataException(string.Format("Unexpected background object type: {0}", tmxObject.Type)); } s.Initialize(null, this, new Vector2((float)tmxObject.X, (float)tmxObject.Y - fObjectHeight)); if (s is StageBridge) { ((StageBridge)s).InitializeBridge(content, new Point(tmxObject.X, tmxObject.Y), 4); } s.Height = tmxObject.Height; s.Width = tmxObject.Width; break; default: int gid = tmxObject.Tile.Gid; for (int i = 0; i < StageTiles.Count; i++) { if (gid < tmx.Tilesets[i].FirstGid) { break; } fObjectHeight = (float)tmx.Tilesets[i].TileHeight; } s = new StageObject(); s.Initialize(content.Load <Texture2D>(tmxObject.Type), this, new Vector2((float)tmxObject.X, (float)tmxObject.Y - fObjectHeight)); break; } //e.Initialize(content, new Vector2((float)tmxObject.X, (float)tmxObject.Y), this, "Sniper"); stageObjects.Add(s); } }
private void InitializeWaitingEnemies(ContentManager content, TiledSharp.TmxMap tmx) { foreach (TiledSharp.TmxObjectGroup.TmxObject tmxObject in tmx.ObjectGroups[WaitingEnemiesObjectGroupName].Objects) { // object's Y-coordinate is oriented to the bottom of the placement of the object, and we need top. // so, we get the tile and look at its height and subtract that from the object's Y-coordinate to detemrine placement of object. float fObjectHeight = 0; int gid = tmxObject.Tile.Gid; for (int i = 0; i < tmx.Tilesets.Count; i++) { fObjectHeight = (float)tmx.Tilesets[i].TileHeight; if (gid > tmx.Tilesets[i].FirstGid) { continue; } if (gid <= tmx.Tilesets[i].FirstGid) { break; } } Enemy e = null; var enemyLocation = new Vector2((float)tmxObject.X, (float)tmxObject.Y - fObjectHeight); switch (tmxObject.Type) { case "FootSoldier": e = new Actors.FootSoldier(content, enemyLocation, this, tmxObject.Type); break; case "Panel": e = new Actors.Panel(content, enemyLocation, this, tmxObject.Properties["ItemType"]); break; case "Sniper": e = new Actors.Sniper(content, enemyLocation, this, tmxObject.Type); break; case "Turret": e = new Actors.Turret(content, enemyLocation, this, tmxObject.Type); break; case "Level1BossPanel": e = new Actors.Level1BossPanel(content, enemyLocation, this, tmxObject.Type); break; case "Capsule": e = new Actors.Capsule(content, enemyLocation, this, tmxObject.Properties["ItemType"]); break; case "Cannon": e = new Actors.Cannon(content, enemyLocation, this, tmxObject.Type); break; case "Level1BossBomberLeft": case "Level1BossBomberRight": e = new Actors.Level1BossBomber(content, enemyLocation, this, tmxObject.Type); break; default: throw new Exception("Unexpected enemy type encountered: " + tmxObject.Type); } waitingEnemies.Add(e); } }
private void InitializeStageTiles(TiledSharp.TmxMap tmx) { MapHeight = tmx.Height; MapWidth = tmx.Width; for (int i = 0; i < tmx.Layers[0].Tiles.Count; i++) { StageTile st = new StageTile(); st.X = tmx.Layers[0].Tiles[i].X; st.Y = tmx.Layers[0].Tiles[i].Y; st.BackgroundGID = tmx.Layers["Background"].Tiles[i].Gid; if (Game.CurrentGame == Game.GameType.Contra) { st.DestructionLayer1GID = tmx.Layers["Destruction1"].Tiles[i].Gid; //st.DestructionLayer1GID = tmx.Layers["Destruction2"].Tiles[i].Gid; } st.MetaGID = tmx.Layers["Meta"].Tiles[i].Gid; st.Status = StageTile.TileStatus.Active; TiledSharp.TmxTilesetTile t = tmx.GetTmxTilesetTileByGID(tmx.Layers["Meta"].Tiles[i].Gid); if (t != null) { if (t != null & t.Properties != null & t.Properties.ContainsKey("Collision")) { if (t.Properties["Collision"] == "PlatformHalfdrop") { st.CollisionType = StageTile.TileCollisionType.PlatformHalfDrop; } if (t.Properties["Collision"] == "Impassable") { st.CollisionType = StageTile.TileCollisionType.Impassable; } if (t.Properties["Collision"] == "Platform") { st.CollisionType = StageTile.TileCollisionType.Platform; } if (t.Properties["Collision"] == "StairsLeft") { st.CollisionType = StageTile.TileCollisionType.StairsLeft; } if (t.Properties["Collision"] == "StairsRight") { st.CollisionType = StageTile.TileCollisionType.StairsRight; } if (t.Properties["Collision"] == "StairsBottomLeft") { st.CollisionType = StageTile.TileCollisionType.StairsBottomLeft; } if (t.Properties["Collision"] == "StairsBottomRight") { st.CollisionType = StageTile.TileCollisionType.StairsBottomRight; } if ((st.CollisionType == StageTile.TileCollisionType.Impassable || st.CollisionType == StageTile.TileCollisionType.Platform) && st.X > 0 && st.Y > 0 && st.X + 1 < this.MapWidth) { if (this.getStageTileByGridPosition(st.X - 1, st.Y - 1).CollisionType == StageTile.TileCollisionType.StairsLeft) { st.CollisionType = StageTile.TileCollisionType.StairsBottomLeft; } if (this.getStageTileByGridPosition(st.X + 1, st.Y - 1).CollisionType == StageTile.TileCollisionType.StairsRight) { st.CollisionType = StageTile.TileCollisionType.StairsBottomRight; } } } if (t.Properties.ContainsKey("Collision") && t.Properties.ContainsKey("WaterTile")) { if (t.Properties["WaterTile"] == "Yes") { st.CollisionType = StageTile.TileCollisionType.PlatformWater; } } } t = tmx.GetTmxTilesetTileByGID(tmx.Layers["Background"].Tiles[i].Gid); if (t != null) { if (t.Properties.ContainsKey("Portal")) { t.Properties.TryGetValue("Portal", out st.PortalID); } } StageTiles.Add(st); } }
public void Initialize(Game game, ContentManager worldcontent, string stageid, int tilewidth, int tileheight) { StageID = stageid; this.Game = game; TileHeight = tileheight; TileWidth = tilewidth; _worldContent = worldcontent; TiledSharp.TmxMap tmx; string appDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); _playerLifeIcon = game.Content.Load <Texture2D>("Sprites/PlayerLifeIcon"); if (game.CurrentGame == Game.GameType.Contra) { _backgroundTileSource.Add(worldcontent.Load <Texture2D>("StageData/Level1Tileset")); _backgroundTileSource.Add(worldcontent.Load <Texture2D>("StageData/Level1TilesetALT")); _destroyedTileSource = worldcontent.Load <Texture2D>("StageData/boss1paneldestroyed"); //explosionTexture = content.Load<Texture2D>("Sprites/Explosion1"); //explosionSound = content.Load<SoundEffect>("Sounds/Explosion1"); _redalertSound = game.Content.Load <SoundEffect>("Sounds/redalert"); _gameplayMusic = worldcontent.Load <Song>("Music/Contra - Jungle Theme"); _fanfare = worldcontent.Load <Song>("Music/fanfare"); PlayMusic(); string tmxFile = appDirectory + "\\LevelMaps\\" + stageid + ".tmx"; if (File.Exists(tmxFile)) { Trace.TraceInformation("Tmx file located: {0}", tmxFile); tmx = new TiledSharp.TmxMap(tmxFile); _destroyedTileSourceBaseGID = tmx.Tilesets["boss1paneldestroyed"].FirstGid; Trace.TraceInformation("Tmx file loaded."); } else { throw new FileNotFoundException(string.Format("Cannot load file: {0}", tmxFile)); } _screenTileWidth = 8; } else { switch (stageid) { case "Castlevania1-1-1": { _gameplayMusic = worldcontent.Load <Song>("Music/Level1VampireKiller"); PlayMusic(); _backgroundTileSource.Add(worldcontent.Load <Texture2D>("StageData/Level1A")); tmx = new TiledSharp.TmxMap(appDirectory + "\\LevelMaps\\" + stageid + ".tmx"); _screenTileWidth = 16; Stage nextSection = new Stage(worldcontent); nextSection.Initialize(this.Game, worldcontent, "Castlevania1-1-2", this.TileWidth, this.TileHeight); _portals.Add("Castlevania1-1-2", nextSection); break; } case "Castlevania1-1-2": { _backgroundTileSource.Add(worldcontent.Load <Texture2D>("StageData/Level1B")); tmx = new TiledSharp.TmxMap(appDirectory + "\\LevelMaps\\" + stageid + ".tmx"); _screenTileWidth = 16; break; } default: { throw new Exception("Unexpected level ID: " + stageid); } } } // Initialize StageTiles InitializeStageTiles(tmx); if (game.CurrentGame == Game.GameType.Contra) { InitializeWaitingEnemies(worldcontent, tmx); InitializeSpecialStageObjects(worldcontent, uniqueBackgroundObjects, tmx, tmx.ObjectGroups[SpecialStageBackgroundObjectsGroupName].Objects); InitializeSpecialStageObjects(worldcontent, uniqueForegroundObjects, tmx, tmx.ObjectGroups[SpecialStageForegroundObjectsGroupName].Objects); } // Set the time keepers to zero _previousSpawnTime = TimeSpan.Zero; Random r = new Random(); _enemySpawnTime = TimeSpan.FromMilliseconds(r.Next(500, 3000)); // Used to determine how fast enemy respawns //enemySpawnTime = TimeSpan.FromSeconds(3.0f); }