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); } }
private bool IsMouseInGameObject(Point mouse, GameObjectViewModel gameObject) { var templateComponent = this.GameWorld.Templates[gameObject.TemplateIndex].Components[0]; var gameObjectRect = new Rect(new Point(gameObject.X - templateComponent.Width / 2, gameObject.Y - templateComponent.Height / 2), new Vector(templateComponent.Width, templateComponent.Height)); return gameObjectRect.Contains(mouse); }
private void DisplayGameObject(GameObjectViewModel gameObjectViewModel) { var image = new Image(); var template = this.GameWorld.Templates[gameObjectViewModel.TemplateIndex]; image.DataContext = gameObjectViewModel; image.Width = template.Components[0].Width; image.Height = template.Components[0].Height; image.Opacity = template.Components[0].Alpha; image.RenderTransform = new RotateTransform(( template.Angle / Math.PI ) * 180, image.Width / 2, image.Height / 2); image.Source = template.BitmapImage; image.Margin = new Thickness(gameObjectViewModel.X - image.Width / 2, gameObjectViewModel.Y - image.Height / 2, 0, 0); image.HorizontalAlignment = HorizontalAlignment.Left; image.VerticalAlignment = VerticalAlignment.Top; this.PanelGameObjects.Children.Add(image); }