private void MoveShipIfPossible(Model.Data.Vector pos, bool rotate) { if (rotate) { _draggedShip.IsHorizontal = !_lastIsHorizontal; _draggedShip.Replace(pos - _difference + _difference.Inverted()); _difference = _difference.Inverted(); } else { _draggedShip.Replace(pos + _difference); } if (CollisionDetection(_draggedShip) || (_draggedShip.Coordinates[0].X < 0) || (_draggedShip.Coordinates[0].Y < 0) || (_draggedShip.Coordinates[_draggedShip.Length - 1].X > 9) || (_draggedShip.Coordinates[_draggedShip.Length - 1].Y > 9)) { Logger.Log("Collision detected..."); _draggedShip.IsHorizontal = _lastIsHorizontal; _draggedShip.Replace(_lastPosition); if (rotate) { _difference = _difference.Inverted(); } } _lastIsHorizontal = _draggedShip.IsHorizontal; _lastPosition = _draggedShip.Coordinates[0]; }
private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Keyboard.Focus(Gridd); TopLabel.Content = "You can rotate the ship by pressing: R"; Logger.Log("dragging true..."); _dragging = true; var pos = new Vector(Grid.GetColumn((Rectangle)sender), Grid.GetRow((Rectangle)sender)); for (int i = 0; i < 5; i++) { foreach (Model.Data.Vector v in _ships[i].Coordinates) { if (v == pos) { _draggedShip = _ships[i]; _lastPosition = pos; _difference = _ships[i].Coordinates[0] - pos; _lastIsHorizontal = _ships[i].IsHorizontal; return; } } } Logger.Log("Ship not found!!!"); _draggedShip = null; }