コード例 #1
0
        private void pnlTileMap_MouseClick(object sender, MouseEventArgs e)
        {
            Rectangle mapRect = new Rectangle(0, 0, pnlTileMap.Width, pnlTileMap.Height);
            if (e.Button == MouseButtons.Left)
            {
                Point offset = e.Location;
                offset.X -= pnlTileMap.AutoScrollPosition.X;
                offset.Y -= pnlTileMap.AutoScrollPosition.Y;

                int tempIndexX, tempIndexY;
                tempIndexX = offset.X / tileSize.Width;
                tempIndexY = offset.Y / tileSize.Height;
                int mapTile = (tempIndexY * mapSize.Width) + tempIndexX;
                if (layerMap == 'm')
                {
                    if (tempIndexX < mapSize.Width && tempIndexY < mapSize.Height && mapRect.Contains(e.Location))
                    {
                        if (paintMode == 'p')
                            map[tempIndexX, tempIndexY].TileID = selectedTile;
                        else if (paintMode == 'e')
                            map[tempIndexX, tempIndexY].TileID = 0;
                        else if (paintMode == 'f')
                        {
                            tileFill(tempIndexX, tempIndexY, map[tempIndexX, tempIndexY].TileID);
                        }
                    }
                }
                else if (layerMap == 'c')
                {
                    if (tempIndexX < mapSize.Width && tempIndexY < mapSize.Height && mapRect.Contains(e.Location))
                    {
                        map[tempIndexX, tempIndexY].CollisionID = (int)numCollisionIDS.Value;
                    }
                }
                else if (layerMap == 'e')
                {
                    if (tempIndexX < mapSize.Width && tempIndexY < mapSize.Height && mapRect.Contains(e.Location))
                    {
                        if (eventMode == 'a')
                        {
                            if (EID == null)
                            {
                                EID = new EventDialog(this, tempIndexX, tempIndexY);
                                EID.FormClosed += EID_FormClosed;
                                EID.Show(this);
                            }
                        }
                        else if (eventMode == 'r')
                        {
                            map[tempIndexX, tempIndexY].EventID = "none";
                        }

                    }
                }
                else if (layerMap == 'o')
                {
                    if (tempIndexX < mapSize.Width && tempIndexY < mapSize.Height && mapRect.Contains(e.Location))
                    {
                        if (objectMode == 'o')
                        {
                            if (map[tempIndexX, tempIndexY].ObjectID == '0')
                            {
                                if (cmbObjects.SelectedItem.ToString() == "Player" && !playerSpawned)
                                {
                                    playerSpawned = true;
                                    map[tempIndexX, tempIndexY].ObjectID = 'p';
                                    placeHolder = new ObjectStruct("Player");
                                    placeHolder.WaypointTiles.Add(mapTile);
                                    lstWaypoints.Items.Add(mapTile);
                                    levelObjects.Add(placeHolder);
                                    map[tempIndexX, tempIndexY].ObjectNumber = objectCount;
                                    lstObjects.Items.Add(placeHolder.Name);
                                    lstObjects.SelectedIndex = objectCount;
                                    ++objectCount;
                                }

                                else if (cmbObjects.SelectedItem.ToString() == "Guard")
                                {
                                    map[tempIndexX, tempIndexY].ObjectID = 'g';
                                    placeHolder = new ObjectStruct("Guard");
                                    placeHolder.WaypointTiles.Add(mapTile);
                                    lstWaypoints.Items.Add(mapTile);
                                    levelObjects.Add(placeHolder);
                                    map[tempIndexX, tempIndexY].ObjectNumber = objectCount;
                                    lstObjects.Items.Add(placeHolder.Name);
                                    lstObjects.SelectedIndex = objectCount;
                                    ++objectCount;
                                }

                            }
                        }
                        else if (objectMode == 'j')
                        {
                            if (map[tempIndexX, tempIndexY].ObjectID == 'p')
                                playerSpawned = false;
                            if (map[tempIndexX, tempIndexY].ObjectID == 'p' || map[tempIndexX, tempIndexY].ObjectID == 'g')
                            {
                                objectRemoved = true;
                                levelObjects.RemoveAt(map[tempIndexX, tempIndexY].ObjectNumber);
                                lstObjects.Items.RemoveAt(map[tempIndexX, tempIndexY].ObjectNumber);
                                objectIndexAbove = map[tempIndexX, tempIndexY].ObjectNumber;
                                --objectCount;
                                lstWaypoints.Items.Clear();
                            }
                            map[tempIndexX, tempIndexY].ObjectID = '0';
                        }
                        else if (objectMode == 'w')
                        {
                            if (!levelObjects[lstObjects.SelectedIndex].WaypointTiles.Contains(mapTile))
                            {
                                levelObjects[lstObjects.SelectedIndex].WaypointTiles.Add(mapTile);
                                lstWaypoints.Items.Add(levelObjects[lstObjects.SelectedIndex].WaypointTiles[levelObjects[lstObjects.SelectedIndex].WaypointTiles.Count - 1]);
                            }
                        }
                        else if (objectMode == 'd')
                        {
                            for (int i = 0; i < levelObjects[lstObjects.SelectedIndex].WaypointTiles.Count; ++i)
                            {
                                if (mapTile == levelObjects[lstObjects.SelectedIndex].WaypointTiles[i])
                                {
                                    levelObjects[lstObjects.SelectedIndex].WaypointTiles.RemoveAt(i);
                                    lstWaypoints.Items.RemoveAt(i);
                                }
                            }
                        }
                    }
                }
                if (over != null)
                    over.Render();
            }
            else if (e.Button == MouseButtons.Right)
            {
                Point offset = e.Location;
                offset.X -= pnlTileMap.AutoScrollPosition.X;
                offset.Y -= pnlTileMap.AutoScrollPosition.Y;

                int tempIndexX, tempIndexY;
                tempIndexX = offset.X / tileSize.Width;
                tempIndexY = offset.Y / tileSize.Height;
                if (tempIndexX < mapSize.Width && tempIndexY < mapSize.Height && mapRect.Contains(e.Location))
                {
                    tipMap.ToolTipTitle = "Tile Info";
                    string caption = "Tile ID: " + map[tempIndexX, tempIndexY].TileID.ToString() +
                        "\nTile Collision: " + map[tempIndexX, tempIndexY].CollisionID.ToString() +
                        "\nEvent: " + map[tempIndexX, tempIndexY].EventID +
                        "\nObject ID: " + map[tempIndexX, tempIndexY].ObjectID;
                    tipMap.Show(caption, pnlTileMap, 5000);
                }
            }
        }
コード例 #2
0
        private void openMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Tile Maps(.xml)|*.xml";
            open.InitialDirectory = System.IO.Path.GetFullPath(filePath + "\\TileMaps");

            if (DialogResult.OK == open.ShowDialog())
            {
                XElement root = XElement.Load(open.FileName);
                ObjectStruct tempObjects = new ObjectStruct();

                XAttribute tileSetFile = root.Attribute("tileSetFile");
                imageName = tileSetFile.Value;
                try
                {
                    tileSetID = TM.LoadTexture(filePath + "\\TileSets\\" + tileSetFile.Value);
                }
                catch (Exception)
                {
                    MessageBox.Show("Failed to load the required Tile Set, please check that you have a folder named TileSets");
                    throw;
                }

                int tempX, tempY, tempIndex, tempPixX, tempPixY;
                XElement layer = root.Element("layer");
                XAttribute layerHeight = layer.Attribute("height");
                XAttribute layerWidth = layer.Attribute("width");
                toolTileMapWidth.SelectedIndex = int.Parse(layerWidth.Value) - 1;
                toolTileMapHeight.SelectedIndex = int.Parse(layerHeight.Value) - 1;

                map = new Tile[mapSize.Width, mapSize.Height];

                XElement tileset = root.Element("tileset");
                XAttribute tilesetHeight = tileset.Attribute("height");
                XAttribute tilesetWidth = tileset.Attribute("width");
                toolTileSetHeight.SelectedIndex = int.Parse(tilesetHeight.Value) -1;
                toolTileSetWidth.SelectedIndex = int.Parse(tilesetWidth.Value) - 1;

                XElement tilesize = root.Element("tilesize");
                XAttribute tilesizeHeight = tilesize.Attribute("height");
                XAttribute tilesizeWidth = tilesize.Attribute("width");
                toolTileHeight.SelectedItem = int.Parse(tilesizeHeight.Value);
                toolTileWidth.SelectedItem = int.Parse(tilesizeWidth.Value);

                XElement player = root.Element("player");
                XAttribute playerName = player.Attribute("name");
                XAttribute playerX = player.Attribute("startX");
                XAttribute playerY = player.Attribute("startY");
                tempObjects = new ObjectStruct(playerName.Value);
                tempPixX = int.Parse(playerX.Value);
                tempPixY = int.Parse(playerY.Value);
                tempX = tempPixX / tileSize.Width;
                tempY = tempPixY / tileSize.Height;
                tempIndex = tempY * mapSize.Width + tempX;
                tempObjects.WaypointTiles.Add(tempIndex);
                levelObjects.Add(tempObjects);
                lstObjects.Items.Add(levelObjects[0].Name);

                IEnumerable<XElement> guards = root.Elements("guard");
                int objIndex = 1;
                foreach (XElement guard in guards)
                {
                    XAttribute guardName = guard.Attribute("name");
                    XAttribute guardX = guard.Attribute("startX");
                    XAttribute guardY = guard.Attribute("startY");
                    tempObjects = new ObjectStruct(guardName.Value);
                    tempPixX = int.Parse(guardX.Value);
                    tempPixY = int.Parse(guardY.Value);
                    tempX = tempPixX / tileSize.Width;
                    tempY = tempPixY / tileSize.Height;
                    tempIndex = tempY * mapSize.Width + tempX;
                    tempObjects.WaypointTiles.Add(tempIndex);
                    IEnumerable<XElement> waypoints = guard.Elements("waypoint");

                    foreach (XElement way in waypoints)
                    {
                        if (way.Attribute("tilePoint") != null)
                        {
                            XAttribute point = way.Attribute("tilePoint");
                            tempObjects.WaypointTiles.Add(int.Parse(point.Value));
                        }
                    }
                    levelObjects.Add(tempObjects);
                    lstObjects.Items.Add(levelObjects[objIndex].Name);
                    ++objIndex;
                }

                IEnumerable<XElement> tiles = root.Elements("tiles");
                int x = 0, y = 0;
                foreach (XElement tile in tiles)
                {
                    XAttribute id = tile.Attribute("id");
                    XAttribute col = tile.Attribute("collision");
                    XAttribute eid = tile.Attribute("event");
                    XAttribute fid = tile.Attribute("onfire");
                    XAttribute objID = tile.Attribute("objectID");
                    XAttribute objNum = tile.Attribute("objectNumber");
                    Tile tempTile = new Tile();
                    tempTile.TileID = int.Parse(id.Value);
                    tempTile.CollisionID = int.Parse(col.Value);
                    tempTile.EventID = eid.Value;
                    tempTile.OnFireID = fid.Value;
                    tempTile.ObjectID = char.Parse(objID.Value);
                    tempTile.ObjectNumber = int.Parse(objNum.Value);

                    map[x, y] = tempTile;
                    ++x;
                    if (x >= mapSize.Width)
                    {
                        x = 0;
                        ++y;
                    }
                }
            }
        }