Exemple #1
0
        /// <summary>
        ///     Collider of a wall object
        /// </summary>
        /// <param name="vector">Vector representing an object</param>
        /// <param name="radius">radius player</param>
        /// <returns>bool Collided</returns>
        public override bool Collides(Vector vector, int radius)
        {
            var playerSizeHorizontal = radius * 2.0;
            var playerSizeVertical   = radius * 2.0;

            var playerRect = new Rect(vector.X, vector.Y, playerSizeHorizontal, playerSizeVertical);
            var wallRect   = new Rect(Locatable.Location.X, Locatable.Location.Y, Radius * 2, Radius * 2);

            return(playerRect.IntersectsWith(wallRect));
        }
Exemple #2
0
        private void ButtonNewLevel_OnClick(object sender, RoutedEventArgs e)
        {
            if (_changesMade)
            {
                var result = MessageBox.Show("Are you sure you want to discard this level?", "New Level",
                                             MessageBoxButton.YesNo,
                                             MessageBoxImage.Warning, MessageBoxResult.No);
                if (result == MessageBoxResult.No)
                {
                    return;
                }
            }

            _editorObjects.Clear();
            EditorCanvas.Background = new SolidColorBrush(Colors.DimGray);
            _changesMade            = false;
            _background             = null;
            _startLoc             = null;
            _endLoc               = null;
            TextBoxLevelName.Text = "";
            DrawObjects();
        }
Exemple #3
0
        private void EditorCanvas_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            var pos    = e.GetPosition(EditorCanvasObjects);
            var vector = new Vector(Math.Round(pos.X), Math.Round(pos.Y));

            switch (e.ChangedButton)
            {
            // Left-click creates objects
            case MouseButton.Left:
                if (_startLoc != null && ((ObjectListItem)ComboBoxObjects.SelectedItem).Id == "start")
                {
                    return;
                }
                if (_endLoc != null && ((ObjectListItem)ComboBoxObjects.SelectedItem).Id == "end")
                {
                    return;
                }
                // Aligning placed objects to grid
                if (CheckBoxEnforceGrid.IsChecked ?? false)
                {
                    vector = new Vector(
                        (int)(vector.X / NumericGridWidth.Value) * NumericGridWidth.Value +
                        NumericGridWidth.Value / 2.0,
                        (int)(vector.Y / NumericGridHeight.Value) * NumericGridHeight.Value +
                        NumericGridHeight.Value / 2.0);
                }

                // Creates the representation object and sets some variables
                var newObject = new EditorObjectRepresentation(
                    vector,
                    ((ObjectListItem)ComboBoxObjects.SelectedItem).Id,
                    NumericZIndex.Value
                    );
                if (newObject.Id == "start" || newObject.Id == "end")
                {
                    newObject.ZIndex = 101;
                }
                _editorObjects.Add(newObject);
                if (newObject.Id == "start")
                {
                    _startLoc = vector;
                }
                if (newObject.Id == "end")
                {
                    _endLoc = vector;
                }
                _changesMade = true;
                break;

            // Right-click removes objects
            case MouseButton.Right:
                // Finding the newest object to remove
                EditorObjectRepresentation removable = null;
                foreach (var o in _editorObjects)
                {
                    var oImage = (from i in _objectList
                                  where i.Id == o.Id
                                  select i.ImageBrush).First().ImageSource;
                    var loc  = new Vector(o.Location.X - oImage.Width / 2.0, o.Location.Y - oImage.Height / 2.0);
                    var loc2 = new Vector(loc.X + oImage.Width, loc.Y + oImage.Height);
                    if (vector.X >= loc.X && vector.X < loc2.X &&
                        vector.Y >= loc.Y && vector.Y < loc2.Y)
                    {
                        removable = o;
                    }
                }

                if (removable != null)
                {
                    // Checking for start or end object
                    if (removable.Id == "start")
                    {
                        _startLoc = null;
                    }
                    if (removable.Id == "end")
                    {
                        _endLoc = null;
                    }
                    _editorObjects.Remove(removable);
                }

                _changesMade = true;
                break;
            }

            DrawObjects();
        }
Exemple #4
0
        /// <summary>
        ///     Parses an XML level document for use in the editor window
        /// </summary>
        /// <param name="doc">The <see cref="XmlDocument" /> to load</param>
        private void ParseLevel(XmlDocument doc)
        {
            var root = doc.DocumentElement;

            if (root == null)
            {
                throw new XmlException("Missing root node");
            }

            if (!root.HasAttribute("name"))
            {
                throw new XmlException("Level missing name attribute");
            }
            TextBoxLevelName.Text =
                (root.GetAttribute("name").StartsWith("custom_") ? "" : "custom_") + root.GetAttribute("name");

            if (!root.HasAttribute("background"))
            {
                EditorCanvas.Background = Brushes.DimGray;
                _background             = null;
            }
            else
            {
                EditorCanvas.Background = ResourceManager.Instance.LoadImageBrush(root.GetAttribute("background"));
                _background             = root.GetAttribute("background");
            }

            var objectsXml = doc.SelectSingleNode("//level/objects");

            if (objectsXml == null)
            {
                throw new XmlException("Level missing objects node");
            }
            _startLoc = null;
            _endLoc   = null;
            // Parsing objects
            foreach (var child in objectsXml.ChildNodes)
            {
                if (!(child is XmlNode))
                {
                    continue;
                }
                var childXml = (XmlNode)child;

                if (childXml.LocalName == "start")
                {
                    _startLoc = Level.ParseLocation(childXml.Attributes["location"].InnerText);
                }
                if (childXml.LocalName == "end")
                {
                    _endLoc = Level.ParseLocation(childXml.Attributes["location"].InnerText);
                }

                _editorObjects.Add(new EditorObjectRepresentation(
                                       Level.ParseLocation(childXml.Attributes["location"].InnerText),
                                       childXml.LocalName == "start" || childXml.LocalName == "end"
                        ? childXml.LocalName
                        : childXml.Attributes["name"].InnerText,
                                       20
                                       ));
            }

            _changesMade = false;

            DrawObjects();
        }