internal TileLayer(string name, int width, int height, bool visible, float opacity, PropertyCollection properties, Map map, int[] data) : base(name, width, height, visible, opacity, properties) { Tiles = new Tile[width, height]; // data is left-to-right, top-to-bottom for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int index = data[y * width + x]; Tiles[x, y] = map.Tiles[index]; } } }
protected override void UpdateScreen(GameTime gameTime, DisplayOrientation displayOrientation) { if (player.CurrentHP < 1) { changeScreenDelegate(ScreenState.GameOver); this.LoadScreenContent(content); } if (playerCreated && !inBattle) { GameplayTurnCheck(); } else if (inBattle) { if (!bossBattle) { hud.Update(player); minion.Update(gameTime); if (input.CheckMouseRelease(attackButton) && input.PreviousMouseState.LeftButton == ButtonState.Pressed && player.TurnBattle && !minion.dead) { minion.HP -= random.Next(0, player.DAM); player.TurnBattle = false; } if (input.CheckMouseRelease(magicButton) && input.PreviousMouseState.LeftButton == ButtonState.Pressed && player.TurnBattle && !minion.dead && player.CurrentMP >= player.MAGCOS) { minion.HP -= random.Next(0, player.MAGDAM); player.CurrentMP -= player.MAGCOS; player.TurnBattle = false; } if (input.CheckMouseRelease(potionButton) && input.PreviousMouseState.LeftButton == ButtonState.Pressed && player.TurnBattle && !minion.dead && player.Potions > 0) { player.CurrentHP = player.MaxHP; player.TurnBattle = false; player.Potions--; } if (!player.TurnBattle) { player.CurrentHP -= random.Next(0, minion.DAM); player.TurnBattle = true; } if (minion.HP <= 0) { minion.dead = true; player.CurrentXP += minion.XP; inBattle = false; minion.Reset(); } } else { hud.Update(player); boss.Update(gameTime); if (input.CheckMouseRelease(attackButton) && input.PreviousMouseState.LeftButton == ButtonState.Pressed && player.TurnBattle && !boss.dead) { boss.HP -= random.Next(0, player.DAM); player.TurnBattle = false; } if (input.CheckMouseRelease(magicButton) && input.PreviousMouseState.LeftButton == ButtonState.Pressed && player.TurnBattle && !boss.dead && player.CurrentMP >= player.MAGCOS) { boss.HP -= random.Next(0, player.MAGDAM); player.CurrentMP -= player.MAGCOS; player.TurnBattle = false; } if (input.CheckMouseRelease(potionButton) && input.PreviousMouseState.LeftButton == ButtonState.Pressed && player.TurnBattle && !boss.dead && player.Potions > 0) { player.CurrentHP = player.MaxHP; player.TurnBattle = false; player.Potions--; } if (!player.TurnBattle) { player.CurrentHP -= random.Next(0, boss.DAM); player.TurnBattle = true; } if (boss.HP <= 0) { if (mapNumber < 6) { player.Potions += 2; boss.dead = true; player.Pieces++; player.CurrentXP += boss.XP; inBattle = false; boss.Reset(); player.TilePosition = new Vector2(1, 17); mapNumber++; tileLayer = null; copyTileLayer = map[mapNumber].GetLayer("Layer") as TileLayer; tileLayer = copyTileLayer; blankTile = tileLayer.Tiles[19, 0]; walkableTile = tileLayer.Tiles[19, 1]; enemyTile = tileLayer.Tiles[19, 2]; exitTile = tileLayer.Tiles[19, 3]; enemykilledTile = tileLayer.Tiles[19, 4]; bossBattle = false; for (int y = 0; y < tileLayer.Height; y++) { for (int x = 0; x < 19; x++) { if (tileLayer.Tiles[x, y] == enemykilledTile) { tileLayer.Tiles[x, y] = enemyTile; } } } } else { changeScreenDelegate(ScreenState.GameWin); LoadScreenContent(content); } } } } else { if (!nameComplete) { currentState = Keyboard.GetState(); if (currentState.IsKeyDown(Keys.Back)) { keydown = '.'; } if (currentState.IsKeyUp(Keys.Back) && keydown == '.' && charAmt >= 0) { keydown = (char)0; enteredName[charAmt] = ' '; if (charAmt != 0) charAmt--; } char tempc = GetCharFromKeyboard(currentState); if (charAmt < 8 && tempc != (char)0) { enteredName[charAmt] = tempc; charAmt++; } enteredName[0] = char.ToUpper(enteredName[0]); playername.ChangeText(new string(enteredName)); if (input.CheckMouseRelease(confirmnameButton) && input.PreviousMouseState.LeftButton == ButtonState.Pressed) { nameComplete = true; } } else if (!classComplete) { if (input.CheckMouseRelease(paladinButton) && input.PreviousMouseState.LeftButton == ButtonState.Pressed) { player.CreatePlayer(0, new string(enteredName).ToString()); classComplete = true; } if (input.CheckMouseRelease(casterButton) && input.PreviousMouseState.LeftButton == ButtonState.Pressed) { player.CreatePlayer(1, new string(enteredName).ToString()); classComplete = true; } if (input.CheckMouseRelease(rogueButton) && input.PreviousMouseState.LeftButton == ButtonState.Pressed) { player.CreatePlayer(2, new string(enteredName).ToString()); classComplete = true; } } else { if (input.CheckMouseRelease(storyContinueButton) && input.PreviousMouseState.LeftButton == ButtonState.Pressed) { if (storyCount < 2) { storyCount++; } else { playerCreated = true; } } } } }
protected override void LoadScreenContent(ContentManager content) { background = new Background(content, "Images/GameplayBackground"); map = new Map[7]; map[0] = content.Load<Map>("Maps/map01"); map[1] = content.Load<Map>("Maps/map02"); map[2] = content.Load<Map>("Maps/map03"); map[3] = content.Load<Map>("Maps/map04"); map[4] = content.Load<Map>("Maps/map05"); map[5] = content.Load<Map>("Maps/map06"); map[6] = content.Load<Map>("Maps/map07"); player = new Player(content); player.TilePosition = new Vector2(1, 17); hud = new HUD(content, player); mapNumber = 0; leftButton = new Button(content, "", new Vector2(800, 650), Color.White, Color.White, "Images/LeftButton"); rightButton = new Button(content, "", new Vector2(900, 650), Color.White, Color.White, "Images/RightButton"); upButton = new Button(content, "", new Vector2(850, 595), Color.White, Color.White, "Images/UpButton"); downButton = new Button(content, "", new Vector2(850, 705), Color.White, Color.White, "Images/DownButton"); copyTileLayer = map[mapNumber].GetLayer("Layer") as TileLayer; tileLayer = copyTileLayer; blankTile = tileLayer.Tiles[19, 0]; walkableTile = tileLayer.Tiles[19, 1]; enemyTile = tileLayer.Tiles[19, 2]; exitTile = tileLayer.Tiles[19, 3]; enemykilledTile = tileLayer.Tiles[19, 4]; for (int y = 0; y < tileLayer.Height; y++) { for (int x = 0; x < 19; x++) { if (tileLayer.Tiles[x, y] == enemykilledTile) { tileLayer.Tiles[x, y] = enemyTile; } } } attackButton = new Button(content, "Attack", new Vector2(ScreenWidth / 2 + 220, 250), Color.Blue, Color.White); magicButton = new Button(content, "Magic", new Vector2(ScreenWidth / 2 + 220, 325), Color.Blue, Color.White); potionButton = new Button(content, "Potion", new Vector2(ScreenWidth / 2 + 220, 400), Color.Blue, Color.White); minion = new Minion(content); boss = new Boss(content); inBattle = false; bossBattle = false; #region Pregame playerCreated = false; keydown = (char)0; enterplayernameText = new Text(content.Load<SpriteFont>("Fonts/buttonFont"), "Enter Characters Name:", new Vector2(350, 200)); confirmnameButton = new Button(content, "Enter", new Vector2(425, 550), Color.Blue, Color.White); textenterbox = content.Load<Texture2D>("Images/TextInputBar"); playername = new Text(content.Load<SpriteFont>("Fonts/buttonFont"),"", new Vector2(375, 400), Color.Blue); charAmt = 0; nameComplete = false; enteredName = new char[8]; for (int i = 0; i < enteredName.Length; i++) { enteredName[i] = ' '; } chooseclasstext = new Text(content.Load<SpriteFont>("Fonts/buttonFont"), "Choose Characters Class:", new Vector2(340, 200)); paladinButton = new Button(content, "Paladin", new Vector2(425, 400), Color.Blue, Color.White); casterButton = new Button(content, "Caster", new Vector2(425, 500), Color.Blue, Color.White); rogueButton = new Button(content, "Rogue", new Vector2(425, 600), Color.Blue, Color.White); storyContinueButton = new Button(content, "Continue", new Vector2(800, 700), Color.Blue, Color.White); classComplete = false; storytexts = new Texture2D[3]; storyCount = 0; storytexts[0] = content.Load<Texture2D>("Images/Storytext1"); storytexts[1] = content.Load<Texture2D>("Images/Storytext2"); storytexts[2] = content.Load<Texture2D>("Images/Storytext3"); #endregion }
//private Layer collisionLayer; // Construct a TiledLib.Map from a TmxReader.map public Map(ContentManager content, string mapName) { string mapPath = AppDomain.CurrentDomain.BaseDirectory + "Content/" + mapName + ".tmx"; TmxMap tmx = new TmxMap(mapPath); Version = new Version(tmx.Version); switch(tmx.Orientation) { case TmxMap.OrientationType.Isometric: Orientation = Orientation.Isometric; break; case TmxMap.OrientationType.Orthogonal: Orientation = Orientation.Orthogonal; break; case TmxMap.OrientationType.Staggered: throw new Exception( "TiledLib doesn't support maps with Staggered " + "orientation."); } Width = tmx.Width; Height = tmx.Height; TileWidth = tmx.TileWidth; TileHeight = tmx.TileHeight; Properties = new PropertyCollection(); foreach(KeyValuePair<string, string> kvp in tmx.Properties) { Properties.Add(kvp); } // Create a list for our tiles List<Tile> tiles = new List<Tile>(); Tiles = new Collection<Tile>(tiles); // Read in each TileSet foreach (TmxTileset ts in tmx.Tilesets) { string tilesetName = ts.Name; Texture2D texture = content.Load<Texture2D>(ts.Image.Source); bool collisionSet = ts.Properties.ContainsKey("CollisionSet") && ts.Properties["CollisionSet"] == "True" ? true : false; /* TODO: Add this intelligence to TiledSharp. * If the texture is bigger than a individual tile, infer all the * additional tiles that must be created. */ if (texture.Width > ts.TileWidth || texture.Height > ts.TileHeight) { // Deconstruct tileset to individual tiles int id = 0; for (int y = 0; y < texture.Height / ts.TileHeight; y++) { for (int x = 0; x < texture.Width / ts.TileWidth; x++) { Rectangle source = new Rectangle( x * ts.TileWidth, y * ts.TileHeight, ts.TileWidth, ts.TileHeight ); Color[] collisionData = null; bool[] collisionBitData = null; PropertyCollection props = new PropertyCollection(); TmxTilesetTile tsTile = ts.Tiles.Find(i => i.Id == id); if(tsTile != null) { foreach (KeyValuePair<string, string> kvp in tsTile.Properties) { props.Add(kvp); // Inherit tilesets collision bool collidable = collisionSet; if (kvp.Key == "CollisionSet") { // Allow override per tile if (kvp.Value == "True") { collidable = true; } else { collidable = false; } } if (collidable) { int numOfBytes = ts.TileWidth * ts.TileHeight; collisionData = new Color[numOfBytes]; collisionBitData = new bool[numOfBytes]; texture.GetData<Color>( 0, source, collisionData, 0, numOfBytes ); for (int col = 0; col < numOfBytes; col++) { if (collisionData[col].A > 0) { collisionBitData[col] = true; } } collisionData = null; } } } Tile t = new Tile( texture, source, props, collisionBitData ); while (id >= tiles.Count) { tiles.Add(null); } tiles.Insert(id + ts.FirstGid, t); id++; } } } } // Process Map Items (layers, objectgroups, etc.) List<Layer> layers = new List<Layer>(); Layers = new Collection<Layer>(layers); foreach(TmxLayer l in tmx.Layers) { Layer layer = null; PropertyCollection props = new PropertyCollection(); foreach(KeyValuePair<string, string> kvp in l.Properties) { props.Add(kvp); } layer = new TileLayer( l.Name, tmx.Width, // As of TiledQT always same as layer. tmx.Height, // As of TiledQT always same as layer. l.Visible, (float) l.Opacity, props, this, l.Tiles ); layers.Add(layer); namedLayers.Add(l.Name, layer); } foreach(TmxObjectGroup og in tmx.ObjectGroups) { Layer layer = null; PropertyCollection props = new PropertyCollection(); foreach(KeyValuePair<string, string> kvp in og.Properties) { props.Add(kvp); } List<MapObject> objects = new List<MapObject>(); // read in all of our objects foreach(TmxObjectGroup.TmxObject i in og.Objects) { Rectangle objLoc = new Rectangle( i.X, i.Y, i.Width, i.Height ); List<Point> objPoints = new List<Point>(); if (i.Points != null) { foreach (Tuple<int, int> tuple in i.Points) { objPoints.Add(new Point(tuple.Item1, tuple.Item2)); } } PropertyCollection objProps = new PropertyCollection(); foreach(KeyValuePair<string, string> kvp in i.Properties) { objProps.Add(kvp); } objects.Add( new MapObject( i.Name, i.Type, objLoc, objPoints, objProps) ); } layer = new MapObjectLayer( og.Name, tmx.Width, tmx.Height, og.Visible, (float) og.Opacity, props, objects ); layers.Add(layer); namedLayers.Add(og.Name, layer); } }
internal Map(ContentReader reader) { // read in the basic map information Version = new Version(reader.ReadString()); Orientation = (Orientation)reader.ReadByte(); Width = reader.ReadInt32(); Height = reader.ReadInt32(); TileWidth = reader.ReadInt32(); TileHeight = reader.ReadInt32(); Properties = new PropertyCollection(); Properties.Read(reader); // create a list for our tiles List<Tile> tiles = new List<Tile>(); Tiles = new Collection<Tile>(tiles); // read in each tile set int numTileSets = reader.ReadInt32(); for (int i = 0; i < numTileSets; i++) { // get the id and texture int firstId = reader.ReadInt32(); string tilesetName = reader.ReadString(); bool collisionSet = reader.ReadBoolean(); Texture2D texture = reader.ReadExternalReference<Texture2D>(); // read in each individual tile int numTiles = reader.ReadInt32(); for (int j = 0; j < numTiles; j++) { int id = firstId + j; // Read the source rectangle from the file. Rectangle source = reader.ReadObject<Rectangle>(); PropertyCollection props = new PropertyCollection(); props.Read(reader); // Read in color data for collision purposes // You'll probably want to limit this to just the tilesets that are used for collision // I'm checking for the name of my tileset that contains wall tiles // Color data takes up a fair bit of RAM Color[] collisionData = null; bool[] collisionBitData = null; if (collisionSet) { int numOfBytes = TileWidth * TileHeight; collisionData = new Color[numOfBytes]; collisionBitData = new bool[numOfBytes]; texture.GetData<Color>( 0, source, collisionData, 0, numOfBytes ); for (int col = 0; col < numOfBytes; col++) { if (collisionData[col].A > 0) { collisionBitData[col] = true; } } collisionData = null; } Tile t = new Tile(texture, source, props, collisionBitData); while (id >= tiles.Count) { tiles.Add(null); } tiles.Insert(id, t); } } // read in all the layers List<Layer> layers = new List<Layer>(); Layers = new Collection<Layer>(layers); int numLayers = reader.ReadInt32(); for (int i = 0; i < numLayers; i++) { Layer layer = null; // read generic layer data string type = reader.ReadString(); string name = reader.ReadString(); int width = reader.ReadInt32(); int height = reader.ReadInt32(); bool visible = reader.ReadBoolean(); float opacity = reader.ReadSingle(); PropertyCollection props = new PropertyCollection(); props.Read(reader); // using the type, figure out which object to create if (type == "layer") { int[] data = reader.ReadObject<int[]>(); layer = new TileLayer(name, width, height, visible, opacity, props, this, data); } else if (type == "objectgroup") { List<MapObject> objects = new List<MapObject>(); // read in all of our objects int numObjects = reader.ReadInt32(); for (int j = 0; j < numObjects; j++) { string objName = reader.ReadString(); string objType = reader.ReadString(); Rectangle objLoc = reader.ReadObject<Rectangle>(); List<Point> objPoints = reader.ReadObject<List<Point>>(); PropertyCollection objProps = new PropertyCollection(); objProps.Read(reader); objects.Add(new MapObject(objName, objType, objLoc, objPoints, objProps)); } layer = new MapObjectLayer(name, width, height, visible, opacity, props, objects); } else { throw new Exception("Invalid type: " + type); } layers.Add(layer); namedLayers.Add(name, layer); } }
/// <summary> /// Iterates all tile sets and generates the correct source rectangles for all tiles inside the tilesets. /// Additionally reads in the property information for the tiles and stores them alongside the source rectangles /// in a Tile object stored in the TileSet's Tiles list. /// </summary> public static void GenerateTileSourceRectangles(MapContent input, string textureRoot = "") { // do some processing on tile sets to load external textures and figure out tile regions foreach (var tileSet in input.TileSets) { // get the real path to the image string path = Path.Combine(textureRoot, tileSet.Image); // load the image so we can compute the individual tile source rectangles int imageWidth = 0; int imageHeight = 0; using (Image image = Image.FromFile(path)) { imageWidth = image.Width; imageHeight = image.Height; } // remove the margins from our calculations imageWidth -= tileSet.Margin * 2; imageHeight -= tileSet.Margin * 2; // figure out how many tiles fit on the X axis int tileCountX = 0; while (tileCountX * tileSet.TileWidth < imageWidth) { tileCountX++; imageWidth -= tileSet.Spacing; } // figure out how many tiles fit on the Y axis int tileCountY = 0; while (tileCountY * tileSet.TileHeight < imageHeight) { tileCountY++; imageHeight -= tileSet.Spacing; } // make our tiles. tiles are numbered by row, left to right. for (int y = 0; y < tileCountY; y++) { for (int x = 0; x < tileCountX; x++) { Tile tile = new Tile(); // calculate the source rectangle int rx = tileSet.Margin + x * (tileSet.TileWidth + tileSet.Spacing); int ry = tileSet.Margin + y * (tileSet.TileHeight + tileSet.Spacing); tile.Source = new Microsoft.Xna.Framework.Rectangle(rx, ry, tileSet.TileWidth, tileSet.TileHeight); // get any properties from the tile set int index = tileSet.FirstId + (y * tileCountX + x); if (tileSet.TileProperties.ContainsKey(index)) { tile.Properties = tileSet.TileProperties[index]; } // save the tile tileSet.Tiles.Add(tile); } } } }
internal Map(ContentReader reader) { // read in the basic map information Version = new Version(reader.ReadString()); Orientation = (Orientation)reader.ReadByte(); WidthInTiles = reader.ReadInt32(); HeightInTiles = reader.ReadInt32(); TileWidth = reader.ReadInt32(); TileHeight = reader.ReadInt32(); Properties = new PropertyCollection(reader); bool makeTilesUnique = reader.ReadBoolean(); // create a list for our tiles List<Tile> tiles = new List<Tile>(); Tiles = new ReadOnlyCollection<Tile>(tiles); // read in each tile set int numTileSets = reader.ReadInt32(); for (int i = 0; i < numTileSets; i++) { // get the id and texture int firstId = reader.ReadInt32(); Texture2D texture = reader.ReadExternalReference<Texture2D>(); // read in each individual tile int numTiles = reader.ReadInt32(); for (int j = 0; j < numTiles; j++) { int id = firstId + j; Rectangle source = reader.ReadObject<Rectangle>(); PropertyCollection props = new PropertyCollection(reader); Tile t = new Tile(texture, source, props); while (id >= tiles.Count) { tiles.Add(null); } tiles.Insert(id, t); } } // read in all the layers List<Layer> layers = new List<Layer>(); Layers = new ReadOnlyCollection<Layer>(layers); int numLayers = reader.ReadInt32(); for (int i = 0; i < numLayers; i++) { Layer layer = null; // read generic layer data string type = reader.ReadString(); string name = reader.ReadString(); int width = reader.ReadInt32(); int height = reader.ReadInt32(); bool visible = reader.ReadBoolean(); float opacity = reader.ReadSingle(); PropertyCollection props = new PropertyCollection(reader); // calculate the default layer depth of the layer float layerDepth = 1f - (LayerDepthSpacing * i); // using the type, figure out which object to create if (type == "layer") { uint[] data = reader.ReadObject<uint[]>(); layer = new TileLayer(name, width, height, layerDepth, visible, opacity, props, this, data, makeTilesUnique); } else if (type == "objectgroup") { List<MapObject> objects = new List<MapObject>(); // read in all of our objects int numObjects = reader.ReadInt32(); for (int j = 0; j < numObjects; j++) { string objName = reader.ReadString(); string objType = reader.ReadString(); Rectangle objLoc = reader.ReadObject<Rectangle>(); PropertyCollection objProps = new PropertyCollection(reader); objects.Add(new MapObject(objName, objType, objLoc, objProps)); } layer = new MapObjectLayer(name, width, height, layerDepth, visible, opacity, props, objects); // read in the layer's color (layer as MapObjectLayer).Color = reader.ReadColor(); } else { throw new Exception("Invalid type: " + type); } layers.Add(layer); namedLayers.Add(name, layer); } }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); blank = new Texture2D(GraphicsDevice, 1, 1); blank.SetData(new[] { Color.White }); // load the map map = Content.Load<Map>("Map"); // find the "Box" and "Point" objects we made in the level MapObjectLayer objects = map.GetLayer("Objects") as MapObjectLayer; point = objects.GetObject("Point"); box = objects.GetObject("Box"); // attempt to read the box color from the box object properties try { boxColor.R = (byte)box.Properties["R"]; boxColor.G = (byte)box.Properties["G"]; boxColor.B = (byte)box.Properties["B"]; boxColor.A = 255; } catch { // on failure, default to yellow boxColor = Color.Yellow; } // find one tile from our tile layer TileLayer tileLayer = map.GetLayer("Tiles") as TileLayer; tile = tileLayer.Tiles[0, 0]; }