Exemple #1
0
        public static void Tick(double Delta)
        {
            Debug.WriteOverlay("Tick: " + Delta.ToString());
            if (!initialized)
            {
                UiMinimap map = new UiMinimap
                {
                    Bounds = new Rectangle(0, ScreenHeight - 300, 300, 300)
                };
                UiElements.Add(map);

                unitsTray = new UiUnitTray
                {
                    Bounds = new Rectangle(300, ScreenHeight - 150, ScreenWidth - 300, 150)
                };
                UiElements.Add(unitsTray);

                UiReinforcementTray reinforcementTray = new UiReinforcementTray
                {
                    Bounds = new Rectangle(0, 150, 300, 300)
                };
                UiElements.Add(reinforcementTray);

                initialized = true;
            }

            mousePressed  = IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON);
            mouseReleased = IsMouseButtonReleased(MouseButton.MOUSE_LEFT_BUTTON);
            mouseUp       = IsMouseButtonUp(MouseButton.MOUSE_LEFT_BUTTON);
            mouseDown     = IsMouseButtonDown(MouseButton.MOUSE_LEFT_BUTTON);
            rightPressed  = IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON);
            rightReleased = IsMouseButtonReleased(MouseButton.MOUSE_RIGHT_BUTTON);
            rightUp       = IsMouseButtonUp(MouseButton.MOUSE_RIGHT_BUTTON);
            rightDown     = IsMouseButtonDown(MouseButton.MOUSE_RIGHT_BUTTON);
            mousePosition = GetMousePosition();
            bool inKeepout = InUi(mousePosition);

            if (inKeepout)
            {
                Debug.WriteOverlay("In UI");
            }

            if (mousePressed && MouseState == MouseStates.Idle)
            {
                downLocationScreen = mousePosition;
                DownLocation       = ScreenToWorld(mousePosition);
            }
            else if (IsMouseButtonDown(MouseButton.MOUSE_LEFT_BUTTON) && !inKeepout)
            {
                Vector2 mousePos = ScreenToWorld(mousePosition);
                if (Vector2.Distance(mousePos, DownLocation) > 64 && !InUi(downLocationScreen))
                {
                    MouseState         = MouseStates.Dragging;
                    SelectionRectangle = RecFromVec(DownLocation, mousePos);
                }
            }
            else if (mouseReleased)
            {
                UpLocation = ScreenToWorld(mousePosition);
                if (MouseState == MouseStates.Dragging)
                {
                    MouseState = MouseStates.DragReleased;
                }
                else
                {
                    if (!inKeepout)
                    {
                        GetSelection();
                    }
                    MouseState = MouseStates.Idle;
                }
            }
            else if (MouseState == MouseStates.DragReleased)
            {
                SelectionRectangle = RecFromVec(DownLocation, UpLocation);
                GetSelection();
                MouseState = MouseStates.Idle;
            }

            if (rightReleased && MouseState == MouseStates.Idle && SelectedUnits.Count != 0)
            {
                IEnumerable <SpaceObject> inSelection = GameManager.Instance.Objects.Where(o =>
                                                                                           o is SpaceShip &&
                                                                                           o.Active &&
                                                                                           Vector2.Distance(o.Location, ScreenToWorld(mousePosition)) < MathF.Max(o.TextureOffset.X, o.TextureOffset.Y)
                                                                                           );

                if (inSelection.Any())
                {
                    foreach (SpaceShipUnit unit in SelectedUnits)
                    {
                        SpaceObject target = inSelection.First();
                        foreach (SpaceObject spaceobj in inSelection)
                        {
                            if (spaceobj is SpaceShipHardpoint)
                            {
                                target = spaceobj;
                                break;
                            }
                        }
                        if (unit.Leader != null)
                        {
                            unit.Leader.Objective = target;
                            unit.Leader.Behavior  = SpaceShip.Behaviors.Attacking;
                        }
                    }
                }
                else
                {
                    foreach (SpaceShipUnit unit in SelectedUnits)
                    {
                        if (unit?.Leader != null)
                        {
                            unit.Leader.Objective = null;
                            //(obj as SpaceShip).Goal =  GetMousePosition();
                            Vector2 mouse = ScreenToWorld(mousePosition);
                            unit.Leader.Goal     = mouse;
                            unit.Leader.Behavior = SpaceShip.Behaviors.Going;
                        }
                    }
                }
            }

            foreach (IUiElement element in UiElements)
            {
                if (CheckCollisionPointRec(mousePosition, element.Bounds))
                {
                    if (mousePressed)
                    {
                        element.MousePressed();
                    }
                    else if (mouseReleased)
                    {
                        element.MouseReleased();
                    }
                    else if (mouseDown)
                    {
                        element.MouseDown();
                    }
                    else
                    {
                        element.MouseHover();
                    }
                }
                element.Tick();
            }
        }
Exemple #2
0
        public static SpaceShip FromXml(XmlResource Xml, SpaceShip DstObject)
        {
            if (Xml == null)
            {
                throw new ArgumentNullException("Xml");
            }
            SpaceShip Result = DstObject;

            if (DstObject == null)
            {
                Result = new SpaceShip();
            }
            Result = SpaceObject.FromXml(Xml, Result) as SpaceShip;

            XmlNode obj = Xml.Xml.LastChild;

            string    baseName   = GetXmlText(obj, "Base", string.Empty);
            SpaceShip baseObject = Result;

            if (!string.IsNullOrEmpty(baseName))
            {
                try
                {
                    baseObject = SpaceShip.FromXml(ResourceManager.Get <XmlResource>(baseName), null);
                }
                catch (KeyNotFoundException e)
                {
                    baseObject = Result;
                    Console.WriteLine("XML Error: Failed to locate XML base " + baseName);
                }
            }

            Result.Hull                    = GetXmlValue(obj, "Hull", baseObject.Hull);
            Result.Shield                  = GetXmlValue(obj, "Shield", baseObject.Shield);
            Result.MaxHull                 = GetXmlValue(obj, "MaxHull", baseObject.MaxHull);
            Result.MaxShield               = GetXmlValue(obj, "MaxShield", baseObject.MaxShield);
            Result.ShieldRegen             = GetXmlValue(obj, "ShieldRegen", baseObject.ShieldRegen);
            Result.MaxThrust               = GetXmlValue(obj, "MaxThrust", baseObject.MaxThrust);
            Result.TurnSpeed               = GetXmlValue(obj, "TurnSpeed", baseObject.TurnSpeed);
            Result.RateOfFire              = GetXmlValue(obj, "RateOfFire", baseObject.RateOfFire);
            Result.ShieldRebootProbability = (int)GetXmlValue(obj, "ShieldRebootProbability", baseObject.ShieldRebootProbability);
            Result.Texture                 = ResourceManager.Get <TextureResource>(GetXmlText(obj, "Texture", baseObject.Texture.Name));
            //Result.Hitbox = Hitbox.Automatic(Result.Texture, (int)Math.Max(2, Result.Scale * Result.Texture.Texture.height / 8));

            List <SpaceObject> hardpoints = GetXmlNested(obj, "Hardpoints", null);

            if (hardpoints != null && hardpoints.Count > 0)
            {
                Result.Hardpoints = new List <SpaceShipHardpoint>();
                foreach (SpaceObject o in hardpoints)
                {
                    SpaceShipHardpoint hp = o as SpaceShipHardpoint;
                    if (hp != null)
                    {
                        hp.Parent = Result;
                        hp.Depth += Result.Depth;
                        hp.Scale *= Result.Scale;
                        Result.Hardpoints.Add(hp);
                    }
                }
                Debug.WriteLine("Loaded hardpoints");
            }

            return(Result);
        }