public CellViewModel this[int x, int y] { get { var cell = this.Cells.FirstOrDefault(c => c.TileX == x && c.TileY == y); if (cell == null) { cell = new CellViewModel(new Cell(){ TileX = x, TileY = y, TemplateIndex = this.DefaultCellTemplateIndex }); } return cell; } }
private void ExecuteChangeTemplate(object o) { if (!this.GameWorld.IsInAddMode) { var mouse = Mouse.GetPosition(this.PanelTiles); var gameObjectViewModel = this.GameWorld.GameObjects.FirstOrDefault(x => IsMouseInGameObject(mouse, x)); if (gameObjectViewModel != null) { this.GameWorld.GameObjects.Remove(gameObjectViewModel); var gameObjectDisplayImage = this.PanelGameObjects.Children.Cast<Image>().First(x => x.DataContext == gameObjectViewModel); this.PanelGameObjects.Children.Remove(gameObjectDisplayImage); } } else if (this.GameWorld.SelectedTemplateIndex < 4) { Vector position = (Vector)o; byte x = Convert.ToByte(position.X); byte y = Convert.ToByte(position.Y); var cell = this.GameWorld.Cells.FirstOrDefault(c => c.TileX == x && c.TileY == y); if (cell == null) { cell = new CellViewModel(new Cell() { TileX = x, TileY = y }); this.GameWorld.Cells.Add(cell); } cell.TemplateIndex = this.GameWorld.SelectedTemplateIndex; } else { var mouse = Mouse.GetPosition(this.PanelTiles); var gameObjectViewModel = new GameObjectViewModel(new GameObject()) { TemplateIndex = this.GameWorld.SelectedTemplateIndex, X = Convert.ToInt32(mouse.X), Y = Convert.ToInt32(mouse.Y) }; this.GameWorld.GameObjects.Add(gameObjectViewModel); DisplayGameObject(gameObjectViewModel); } }