public TileMap(int x, int y, int size, int xtiles, int ytiles, Texture2D pixel, ToolMap tools, SpriteFont font, String filename) { this.hasmonsters = false; this.lastspawnedmonster = 0; this.filename = filename; this.toolmap = tools; this.size = size; this.xtiles = xtiles; this.ytiles = ytiles; this.font = font; double tss = VIEW_HEIGHT / size; int tilesidesize = (int)Math.Floor(tss); totalmapsize = tilesidesize * size; curxtilemin = 0; curytilemin = 0; battlemap = Map.BATTLE_GENERIC; //int curtoolx = tools.Length-1; //int curtooly = tools[0].Length-1; pixelsperside = tilesidesize; center = (int)Math.Ceiling(size / 2.0); displaytiles = new Rectangle[size][]; monstertiles = new List<Tile>(); npctiles = new Dictionary<String, Tile>(); for (int i = 0; i < size; i++) { displaytiles[i] = new Rectangle[size]; for (int j = 0; j < size; j++) { displaytiles[i][j] = new Rectangle(x + i*tilesidesize, y + j*tilesidesize, pixelsperside, pixelsperside); } } //Random randval = new Random(); map = new Tile[xtiles][]; for (int i = 0; i < xtiles; i++) { map[i] = new Tile[ytiles]; for (int j = 0; j < ytiles; j++) { //int currandx = randval.Next(0, 2); //int currandy = randval.Next(0, 3); map[i][j] = new Tile(i, j, (x + i * tilesidesize), (y + j * tilesidesize), tilesidesize, toolmap.getDefaultTool()); } } }
public void Update(ToolMap toolmap, GameTime gameTime) { Tool selectedTool = toolmap.getSelected(); MouseState mouseState = Mouse.GetState(); int mousex = mouseState.X; int mousey = mouseState.Y; //if mouse is within the map, do mouse over... if ((mousex > 10 && mousex < totalmapsize + 10) && (mousey > 10 && mousey < totalmapsize + 10)) { double tempx = (mousex - 10) / pixelsperside; int tilex = (int)Math.Floor(tempx); double tempy = (mousey - 10) / pixelsperside; int tiley = (int)Math.Floor(tempy); if (mouseState.LeftButton == ButtonState.Pressed && selectedTool != null) { WorldTile selectedType = selectedTool.getType(); if (selectedType == WorldTile.PLAYER) { Tile cur = map[tilex][tiley]; if (cur.getType() != WorldTile.WALL) { if (playertile == null) playertile = new Tile(tilex, tiley, cur.getX(), cur.getY(), cur.getLength(), selectedTool); else { playerx = tilex; playery = tiley; playertile.setMapX(tilex); playertile.setMapY(tiley); } } } else if (selectedType == WorldTile.MONSTER) { Tile cur = map[tilex][tiley]; if (cur.getType() != WorldTile.WALL) { monstertiles.Add(new Tile(tilex, tiley, cur.getX(), cur.getY(), cur.getLength(), selectedTool)); } } else { if (selectedTool.getType() == WorldTile.SELECT) { //if (selectedtile != null && selectedtile != map[curxtilemin + tilex][curytilemin + tiley]) // selectedtile.setColor(Color.White); toolmap.setSelectedTile(map[curxtilemin + tilex][curytilemin + tiley]); //selectedtile.setColor(Color.DarkGray); } else { map[curxtilemin + tilex][curytilemin + tiley].applyTool(selectedTool, toolmap); } } } if (highlighted != null) highlighted.unhighlight(); highlighted = map[curxtilemin + tilex][curytilemin + tiley]; highlighted.highlight(); } }
public bool LoadMap(StreamReader file, String filename, ToolMap toolmap) { this.filename = filename; bool success = false; bool gotxandy = false; int x = 0; int y = 0; int playerx = -1; int playery = -1; using (XmlReader reader = XmlReader.Create(file)) { while (reader.Read() && !gotxandy) { switch (reader.NodeType) { case XmlNodeType.Element: if (reader.Name == "MAP") { x = Convert.ToInt32(reader["x"]); y = Convert.ToInt32(reader["y"]); if(reader["playerx"] != null && reader["playery"] != null) { playerx = Convert.ToInt32(reader["playerx"]); playery = Convert.ToInt32(reader["playery"]); } gotxandy = true; } break; } } } xtiles = x; ytiles = y; map = new Tile[x][]; for(int i = 0; i < x; i++) map[i] = new Tile[y]; file.BaseStream.Position = 0; int tilex = 0; int tiley = 0; bool inEvent = false; Event current = new Event(); using (XmlReader reader = XmlReader.Create(file)) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: if (reader.Name == "MONSTER") { //file.WriteLine("<MONSTER type=\"" + monstertiles[i].getType() + "\" x=\"" + // monstertiles[i].getMapX() + "\" y=\"" + monstertiles[i].getMapY() + "\" />"); hasmonsters = true; String type = reader["type"]; tilex = Convert.ToInt32(reader["x"]); tiley = Convert.ToInt32(reader["y"]); if (Game1.DEBUG) Console.WriteLine("Adding Monster!!, monstertiles.Count=" + monstertiles.Count); monstertiles.Add(new Tile(tilex, tiley, 0, 0, 0, toolmap.getTool(type))); } if (reader.Name == "TILE") { tilex = Convert.ToInt32(reader["x"]); tiley = Convert.ToInt32(reader["y"]); String type = reader["type"]; Tool tiletool = toolmap.getTool(type); map[tilex][tiley] = new Tile(tilex, tiley, tilex, tiley, 0, tiletool); } else if (reader.Name == "EVENT") { String eventtype = reader["type"]; current.setEventType((EventType)Enum.Parse(typeof(EventType), eventtype)); inEvent = true; } else if (reader.Name == "DATA") { if (inEvent) { XmlReader tempreader = reader.ReadSubtree(); String propname = ""; while(tempreader.Read()) switch (tempreader.NodeType) { case XmlNodeType.Element: propname = tempreader.Name; break; case XmlNodeType.Text: current.addProperty(propname, tempreader.Value); break; } } } break; case XmlNodeType.EndElement: if (reader.Name == "EVENT") { if(Game1.DEBUG) Console.WriteLine("Adding event!"); map[tilex][tiley].addEvent(current); inEvent = false; current = new Event(); } break; } } } if (playertile == null) playertile = toolmap.getPlayerTile(); if (playerx > 0 && playery > 0) setPlayerLocation(map[playerx][playery]); success = true; return success; }
/// <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); whitepixel = new Texture2D(GraphicsDevice, 1, 1); whitepixel.SetData(new Color[] { Color.White }); astartiles = new List<Tile>(); astarwaypoint = Content.Load<Texture2D>("AStarWayPoint"); edited = true; font = Content.Load<SpriteFont>("gameFont"); titlefont = Content.Load<SpriteFont>("titleFont"); helpText = Content.Load<SpriteFont>("worldText"); evindicator = Content.Load<Texture2D>("AStarWayPoint"); cave = Content.Load<Song>("cavemusic"); town = Content.Load<Song>("Townmusic"); battle = Content.Load<Song>("battlemusic"); currSong = town; fire = Content.Load<Texture2D>("Tiles/FireBall"); heal = Content.Load<Texture2D>("Tiles/HealBall"); sword = Content.Load<Texture2D>("Tiles/SwordAttack1"); firesound = Content.Load<SoundEffect>("FireballSound"); healSound = Content.Load<SoundEffect>("Healing"); swordSound = Content.Load<SoundEffect>("SwordAttack"); /*tools = new Tool[2][]; tools[0] = new Tool[4]; tools[1] = new Tool[4]; tools[0][0] = new Tool(TileType.GRASS, Content.Load<Texture2D>("Tiles/Grass"), false, 1); tools[0][1] = new Tool(TileType.TREES, Content.Load<Texture2D>("Tiles/Trees"), false, 2); tools[0][2] = new Tool(TileType.SWAMP, Content.Load<Texture2D>("Tiles/Swamp"), false, 4); tools[0][3] = new Tool(TileType.PLAYER, Content.Load<Texture2D>("Tiles/Player"), false, 0); tools[1][0] = new Tool(TileType.WATER, Content.Load<Texture2D>("Tiles/Water"), false, 6); tools[1][1] = new Tool(TileType.ROCKS, Content.Load<Texture2D>("Tiles/LavaRocks"), false, 8); tools[1][2] = new Tool(TileType.WALL, Content.Load<Texture2D>("Tiles/Wall"), true, 0); tools[1][3] = new Tool(TileType.MONSTER, Content.Load<Texture2D>("Tiles/Monster"), false, 0); */ System.Collections.ArrayList tiles = new System.Collections.ArrayList(); foreach (WorldTile t in Enum.GetValues(typeof(WorldTile))) { tiles.Add(t); if(DEBUG) Console.WriteLine(t.GetInformation()); try { Texture2D cur = Content.Load<Texture2D>(t.GetTexture()); if (cur != null && !texmap.ContainsKey(t.GetTexture())) texmap.Add(t.GetTexture(), cur); } catch (Microsoft.Xna.Framework.Content.ContentLoadException e) { } } worldtiles = tiles.ToArray(typeof(WorldTile)) as WorldTile[]; Array.Sort(worldtiles, delegate(WorldTile a, WorldTile b) { return a.ToString().CompareTo(b.ToString()); }); if(DEBUG) Console.WriteLine(worldtiles.Length); //map = new TileMap(10, 10, 17, 512, 512, whitepixel, tools[0][0]); toolmap = new ToolMap(578, 100, whitepixel, texmap, worldtiles, font, Window.Handle); if (!STARTPLAY) { toolmap.enable(); } map = new TileMap(10, 10, 17, DEFAULT_X_TILES, DEFAULT_Y_TILES, whitepixel, toolmap, font, FIRSTMAP); if (STARTPLAY) { FileStream fileStream = new FileStream(@FIRSTMAP, FileMode.Open); StreamReader reader = new StreamReader(fileStream); map.LoadMap(reader, FIRSTMAP, toolmap); maps.Add(FIRSTMAP, map); reader.Close(); fileStream.Close(); state = GameState.TITLE; } PlayMusic(Content.Load<Song>("Townmusic")); // TODO: use this.Content to load your game content here }