Exemplo n.º 1
0
 protected void OnSomethingHappened(BuildingCell cellIn, string stringIn, bool boolIn)
 {
     if (this.somethingHappened != null)
     {
         this.somethingHappened(cellIn, stringIn, boolIn);
     }
 }
Exemplo n.º 2
0
        public CellWPF(BuildingCell logicIn)
        {
            InitializeComponent();

            this.gridImage                      = new Image();
            this.gridImage.Height               = this.rectangle.Height;
            this.gridImage.Width                = this.rectangle.Width;

            this.somethingHappenedImage         = new Image();
            this.somethingHappenedImage.Height  = double.NaN;
            this.somethingHappenedImage.Width   = double.NaN;

            this.logic = logicIn;

            logicIn.somethingHappened += setSomethingHappened;

            switch (logicIn.material.name)
            {
                case "Acero": this.rectangle.Fill       = Brushes.SteelBlue; break;
                case "Cemento": this.rectangle.Fill     = Brushes.Gainsboro; break;
                case "Ladrillo": this.rectangle.Fill    = Brushes.Firebrick; break;
                case "Madera": this.rectangle.Fill      = Brushes.Brown; break;
                case "Piedra": this.rectangle.Fill      = Brushes.SlateGray; break;
                default: this.rectangle.Fill            = Brushes.Black; break;
            }

            if (this.logic.name.Contains("Comisaría"))
            {
                this.rectangle.Fill = Brushes.Blue;
                this.gridImage.Source = policeSource;
            }
            else if (this.logic.name.Contains("Bomberos"))
            {
                this.rectangle.Fill = Brushes.Yellow;
                this.gridImage.Source = fireStationSource;
            }
            else if (this.logic.name.Contains("Hospital"))
            {
                this.rectangle.Fill = Brushes.AntiqueWhite;
                this.gridImage.Source = hospitalSource;
            }
            else
            {
                this.gridImage.Source = blockSource;
            }

            this.CellWPF_Grid.Children.Add(this.gridImage);

            this.rectangle.Stroke = Brushes.Black;
        }
Exemplo n.º 3
0
        void setSomethingHappened(BuildingCell buildingCellIn, string stringIn, bool boolIn)
        {
            this.Dispatcher.BeginInvoke((Action)(() =>
            {
                if (stringIn.Equals("Crime") && boolIn)
                {
                    this.somethingHappenedImage.Source = this.crimeSource;
                    this.CellWPF_Grid.Children.Add(this.somethingHappenedImage);
                }
                else if (stringIn.Equals("Fire") && boolIn)
                {
                    this.somethingHappenedImage.Source = this.fireSource;
                    this.CellWPF_Grid.Children.Add(this.somethingHappenedImage);
                }
                else if (stringIn.Equals("Accident") && boolIn)
                {
                    this.somethingHappenedImage.Source = this.accidentSource;
                    this.CellWPF_Grid.Children.Add(this.somethingHappenedImage);
                }
                else if (stringIn.Equals("Sick") && boolIn)
                {
                    this.somethingHappenedImage.Source = this.sicknessSource;
                    this.CellWPF_Grid.Children.Add(this.somethingHappenedImage);
                }

                if (!boolIn && this.CellWPF_Grid.Children.Contains(this.somethingHappenedImage))
                {
                    this.CellWPF_Grid.Children.Remove(this.somethingHappenedImage);
                }
            }));
        }
Exemplo n.º 4
0
 void OnBuildingClicked(BuildingCell buildingCellIn)
 {
     if (this.Building_Clicked != null)
     {
         this.Building_Clicked(buildingCellIn);
     }
 }
Exemplo n.º 5
0
        public void CargarDatos()
        {
            OpenFileDialog loadDialog = new OpenFileDialog();

            loadDialog.ShowDialog();

            Stream stream = loadDialog.OpenFile();
            StreamReader sr = new StreamReader(stream);

            string xmlText = sr.ReadToEnd();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xmlText);

            XmlNodeList cityStats = xmlDoc.GetElementsByTagName("city");
            XmlNodeList buildings = xmlDoc.GetElementsByTagName("building");
            XmlNodeList materials = xmlDoc.GetElementsByTagName("material");
            XmlNodeList streetLights = xmlDoc.GetElementsByTagName("streetlight");

            this.name = ((XmlElement)cityStats[0]).GetAttribute("name");
            this.width = Int32.Parse(((XmlElement)cityStats[0]).GetAttribute("width"));
            this.height = Int32.Parse(((XmlElement)cityStats[0]).GetAttribute("height"));
            this.accidentProbability = Double.Parse(((XmlElement)cityStats[0]).GetAttribute("accident"));
            this.sicknessProbability = Double.Parse(((XmlElement)cityStats[0]).GetAttribute("sickness"));
            this.crimeProbability = Double.Parse(((XmlElement)cityStats[0]).GetAttribute("crime"));
            this.fireProbability = Double.Parse(((XmlElement)cityStats[0]).GetAttribute("fire"));

            this.map = new Cell[width, height];

            #region Materials
            foreach (XmlElement node in materials)
            {
                Material auxMaterial = new Material(node.GetAttribute("name"), Double.Parse(node.GetAttribute("inflamability")), Double.Parse(node.GetAttribute("sick")));

                this.materialsList.Add(auxMaterial);
            }
            #endregion

            #region Buildings
            foreach (XmlElement node in buildings)
            {
                string auxName = node.GetAttribute("name");

                Building auxBuilding;
                if (auxName.Contains("Comisaría"))
                {
                    auxBuilding = new PoliceStation(auxName);
                    this.policeStation = (PoliceStation)auxBuilding;
                }
                else if (auxName.Contains("Bomberos"))
                {
                    auxBuilding = new FireStation(auxName);
                    this.fireStation = (FireStation)auxBuilding;
                }
                else if (auxName.Contains("Hospital"))
                {
                    auxBuilding = new Hospital(auxName);
                    this.hospital = (Hospital)auxBuilding;
                }
                else
                {
                    auxBuilding = new Building(node.GetAttribute("name"));
                }

                XmlNodeList currentCells = node.GetElementsByTagName("cell");

                foreach (XmlElement cell in currentCells)
                {
                    int i = Int32.Parse(cell.GetAttribute("i"));
                    int j = Int32.Parse(cell.GetAttribute("j"));

                    string auxMaterial = cell.GetAttribute("material");

                    for (int k = 0; k < this.materialsList.Count; k++)
                    {
                        if (this.materialsList[k].name.Equals(auxMaterial))
                        {
                            BuildingCell auxCell = new BuildingCell(auxBuilding.name, i, j, this.materialsList[k]);
                            auxBuilding.cellList.Add(auxCell);
                            this.map[i, j] = auxCell;
                            break;
                        }
                    }
                }

                this.buildingsList.Add(auxBuilding);
            }
            #endregion

            #region StreetLights
            foreach (XmlElement node in streetLights)
            {
                StreetLight auxStreetLight = new StreetLight(Int32.Parse(node.GetAttribute("time")));

                XmlNodeList currentCells = node.GetElementsByTagName("cell");

                foreach (XmlElement cell in currentCells)
                {
                    int i = Int32.Parse(cell.GetAttribute("i"));
                    int j = Int32.Parse(cell.GetAttribute("j"));

                    StreetLightCell auxCell = new StreetLightCell(i, j);

                    auxStreetLight.cellList.Add(auxCell);

                    this.map[i, j] = auxCell;
                }

                this.streetLightsList.Add(auxStreetLight);
            }
            #endregion

            #region Streets
            for (int i = 0; i < this.width; i++)
            {
                for (int j = 0; j < this.height; j++)
                {
                    if (this.map[i, j] == null)
                    {
                        StreetCell auxStreetCell = new StreetCell(i, j);

                        this.map[i, j] = auxStreetCell;
                        this.streetList.Add(auxStreetCell);
                    }
                }
            }

            #region CasoBase
            for (int i = 0; i < this.streetList.Count; i++)
            {
                int J = this.streetList[i].coordJ;
                int I = this.streetList[i].coordI;

                if (J > 0 && I > 0 && J < this.map.GetLength(1) - 1 && I < this.map.GetLength(0) - 1)
                {
                    if (!this.map[I - 1, J].transitable)
                    {
                        this.streetList[i].verticalVector = 1;
                    }

                    if (!this.map[I + 1, J].transitable)
                    {
                        this.streetList[i].verticalVector = -1;
                    }

                    if (!this.map[I, J - 1].transitable)
                    {
                        this.streetList[i].horizontalVector = -1;
                    }

                    if (!this.map[I, J + 1].transitable)
                    {
                        this.streetList[i].horizontalVector = 1;
                    }
                }
            }
            #endregion

            #region CasoParticular
            for (int i = 0; i < this.streetList.Count; i++)
            {
                int J = this.streetList[i].coordJ;
                int I = this.streetList[i].coordI;

                if (J > 0 && I > 0 && J < this.map.GetLength(1) - 1 && I < this.map.GetLength(0) - 1)
                {
                    if (this.map[I, J - 1].transitable && this.map[I + 1, J].transitable && this.map[I - 1, J].transitable && this.map[I, J + 1].transitable)
                    {
                        if (((StreetCell)(this.map[I, J - 1])).verticalVector == -1 && ((StreetCell)(this.map[I + 1, J])).horizontalVector == -1)
                        {
                            this.streetList[i].verticalVector = -1;
                            this.streetList[i].horizontalVector = 0;
                        }
                    }

                    if (this.map[I, J - 1].transitable && this.map[I + 1, J].transitable && !this.map[I - 1, J].transitable && !this.map[I, J + 1].transitable)
                    {
                        if (((StreetCell)(this.map[I, J - 1])).verticalVector == 1 && ((StreetCell)(this.map[I + 1, J])).horizontalVector == 1)
                        {
                            this.streetList[i].horizontalVector = 1;
                            this.streetList[i].verticalVector = 0;
                        }
                    }

                    if (this.map[I, J - 1].transitable && this.map[I - 1, J].transitable && this.map[I + 1, J].transitable && this.map[I, J + 1].transitable)
                    {
                        if (((StreetCell)(this.map[I, J - 1])).verticalVector == -1 && ((StreetCell)(this.map[I - 1, J])).horizontalVector == -1)
                        {
                            this.streetList[i].horizontalVector = -1;
                            this.streetList[i].verticalVector = 0;
                        }
                    }

                    if (this.map[I, J - 1].transitable && this.map[I - 1, J].transitable && !this.map[I + 1, J].transitable && !this.map[I, J + 1].transitable)
                    {
                        if (((StreetCell)(this.map[I, J - 1])).verticalVector == 1 && ((StreetCell)(this.map[I - 1, J])).horizontalVector == 1)
                        {
                            this.streetList[i].verticalVector = 1;
                            this.streetList[i].horizontalVector = 0;
                        }
                    }

                    if (this.map[I, J + 1].transitable && this.map[I + 1, J].transitable && !this.map[I - 1, J].transitable && !this.map[I, J - 1].transitable)
                    {
                        if (((StreetCell)(this.map[I, J + 1])).verticalVector == -1 && ((StreetCell)(this.map[I + 1, J])).horizontalVector == -1)
                        {
                            this.streetList[i].verticalVector = -1;
                            this.streetList[i].horizontalVector = 0;
                        }
                    }

                    if (this.map[I, J + 1].transitable && this.map[I + 1, J].transitable && this.map[I, J - 1].transitable && this.map[I - 1, J].transitable)
                    {
                        if (((StreetCell)(this.map[I, J + 1])).verticalVector == 1 && ((StreetCell)(this.map[I + 1, J])).horizontalVector == 1)
                        {
                            this.streetList[i].horizontalVector = 1;
                            this.streetList[i].verticalVector = 0;
                        }
                    }

                    if (this.map[I, J + 1].transitable && this.map[I - 1, J].transitable && this.map[I, J - 1].transitable && this.map[I + 1, J].transitable)
                    {
                        if (((StreetCell)(this.map[I, J + 1])).verticalVector == -1 && ((StreetCell)(this.map[I - 1, J])).horizontalVector == 1)
                        {
                            this.streetList[i].verticalVector = 1;
                            this.streetList[i].horizontalVector = 0;
                        }
                    }

                    if (this.map[I, J + 1].transitable && this.map[I - 1, J].transitable && !this.map[I, J - 1].transitable && !this.map[I + 1, J].transitable)
                    {
                        if (((StreetCell)(this.map[I, J + 1])).verticalVector == 1 && ((StreetCell)(this.map[I - 1, J])).horizontalVector == -1)
                        {
                            this.streetList[i].verticalVector = 1;
                            this.streetList[i].horizontalVector = 0;
                        }
                    }
                }
            }
            #endregion

            #region EntradasYSalidas
            for (int i = 0; i < this.streetList.Count; i++)
            {
                int J = this.streetList[i].coordJ;
                int I = this.streetList[i].coordI;

                if (I == 0)
                {
                    if (this.map[I + 1, J].transitable)
                    {
                        if (((StreetCell)this.map[I + 1, J]).horizontalVector == 1)
                        {
                            ((StreetCell)this.map[I, J]).entrance = true;
                        }
                        else if (((StreetCell)this.map[I + 1, J]).horizontalVector == -1)
                        {
                            ((StreetCell)this.map[I, J]).exit = true;
                        }

                        ((StreetCell)this.map[I, J]).horizontalVector = ((StreetCell)this.map[I + 1, J]).horizontalVector;
                    }
                }

                if (I == this.map.GetLength(0) - 1)
                {
                    if (this.map[I - 1, J].transitable)
                    {
                        if (((StreetCell)this.map[I - 1, J]).horizontalVector == 1)
                        {
                            ((StreetCell)this.map[I, J]).exit = true;
                        }
                        else if (((StreetCell)this.map[I - 1, J]).horizontalVector == -1)
                        {
                            ((StreetCell)this.map[I, J]).entrance = true;
                        }

                        ((StreetCell)this.map[I, J]).horizontalVector = ((StreetCell)this.map[I - 1, J]).horizontalVector;
                    }
                }

                if (J == 0)
                {
                    if (this.map[I, J + 1].transitable)
                    {
                        if (((StreetCell)this.map[I, J + 1]).verticalVector == 1)
                        {
                            ((StreetCell)this.map[I, J]).entrance = true;
                        }
                        else if (((StreetCell)this.map[I, J + 1]).verticalVector == -1)
                        {
                            ((StreetCell)this.map[I, J]).exit = true;
                        }

                        ((StreetCell)this.map[I, J]).verticalVector = ((StreetCell)this.map[I, J + 1]).verticalVector;
                    }
                }

                if (J == this.map.GetLength(1) - 1)
                {
                    if (this.map[I, J - 1].transitable)
                    {
                        if (((StreetCell)this.map[I, J - 1]).verticalVector == 1)
                        {
                            ((StreetCell)this.map[I, J]).exit = true;
                        }
                        else if (((StreetCell)this.map[I, J - 1]).verticalVector == -1)
                        {
                            ((StreetCell)this.map[I, J]).entrance = true;
                        }

                        ((StreetCell)this.map[I, J]).verticalVector = ((StreetCell)this.map[I, J - 1]).verticalVector;
                    }
                }
            }
            #endregion
            #endregion

            #region Adjust StreetLights
            for (int i = 0; i < this.streetLightsList.Count; i++)
            {
                for (int j = 0; j < this.streetLightsList[i].cellList.Count; j++)
                {
                    int I = streetLightsList[i].cellList[j].coordI;
                    int J = streetLightsList[i].cellList[j].coordJ;

                    if (I != 0 && I != (this.map.GetLength(0) - 1) && J != 0 && (J != this.map.GetLength(1) - 1))
                    {
                        if (this.map[I + 1, J].transitable)
                        {
                            if (((StreetCell)this.map[I + 1, J]).horizontalVector == -1)
                            {
                                this.streetLightsList[i].cellList[j].NE = true;
                                this.streetLightsList[i].cellList[j].verticalVector = -1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }
                        else if (this.map[I, J - 1].transitable)
                        {
                            if (((StreetCell)this.map[I, J - 1]).verticalVector == -1)
                            {
                                this.streetLightsList[i].cellList[j].NE = true;
                                this.streetLightsList[i].cellList[j].verticalVector = -1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }

                        if (this.streetLightsList[i].cellList[j].NE && !this.map[I, J - 1].transitable)
                        {
                            this.streetLightsList[i].cellList[j].verticalVector = 0;
                            this.streetLightsList[i].cellList[j].horizontalVector = -1;
                            this.streetLightsList[i].cellList[j].anomalous = true;
                        }

                        if (this.map[I - 1, J].transitable)
                        {
                            if (((StreetCell)this.map[I - 1, J]).horizontalVector == -1)
                            {
                                this.streetLightsList[i].cellList[j].NW = true;
                                this.streetLightsList[i].cellList[j].verticalVector = 1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }
                        else if (this.map[I, J - 1].transitable)
                        {
                            if (((StreetCell)this.map[I, J - 1]).verticalVector == 1)
                            {
                                this.streetLightsList[i].cellList[j].NW = true;
                                this.streetLightsList[i].cellList[j].verticalVector = 1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }

                        if (this.streetLightsList[i].cellList[j].NW && !this.map[I - 1, J].transitable)
                        {
                            this.streetLightsList[i].cellList[j].verticalVector = 1;
                            this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            this.streetLightsList[i].cellList[j].anomalous = true;
                        }

                        if (this.map[I + 1, J].transitable)
                        {
                            if (((StreetCell)this.map[I + 1, J]).horizontalVector == 1)
                            {
                                this.streetLightsList[i].cellList[j].SE = true;
                                this.streetLightsList[i].cellList[j].verticalVector = -1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }
                        else if (this.map[I, J + 1].transitable)
                        {
                            if (((StreetCell)this.map[I, J + 1]).verticalVector == -1)
                            {
                                this.streetLightsList[i].cellList[j].SE = true;
                                this.streetLightsList[i].cellList[j].verticalVector = -1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }

                        if (this.streetLightsList[i].cellList[j].SE && !this.map[I + 1, J].transitable)
                        {
                            this.streetLightsList[i].cellList[j].verticalVector = -1;
                            this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            this.streetLightsList[i].cellList[j].anomalous = true;
                        }

                        if (this.map[I - 1, J].transitable)
                        {
                            if (((StreetCell)this.map[I - 1, J]).horizontalVector == 1)
                            {
                                this.streetLightsList[i].cellList[j].SW = true;
                                this.streetLightsList[i].cellList[j].verticalVector = 1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }
                        else if (this.map[I, J + 1].transitable)
                        {
                            if (((StreetCell)this.map[I, J + 1]).verticalVector == 1)
                            {
                                this.streetLightsList[i].cellList[j].SW = true;
                                this.streetLightsList[i].cellList[j].verticalVector = 1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }

                        if (this.streetLightsList[i].cellList[j].SW && !this.map[I, J + 1].transitable)
                        {
                            this.streetLightsList[i].cellList[j].verticalVector = 0;
                            this.streetLightsList[i].cellList[j].horizontalVector = -1;
                            this.streetLightsList[i].cellList[j].anomalous = true;
                        }
                    }
                }
            }
            #endregion

            #region Create Parking Areas
            for (int i = 0; i < this.buildingsList.Count; i++)
            {
                this.buildingsList[i].createParking(this.map);
            }
            #endregion
        }