public void Selected(bool clear = false)
 {
     if (onSelected != null)
     {
         onSelected(this);
         if (clear)
         {
             onSelected = null;
         }
     }
 }
Example #2
0
        public PreSelectState(Color color, Texture2D title, Texture2D first, Texture2D second, OnSelectHandler onFirst, OnSelectHandler onSecond, bool isOpening)
        {
            this.color = color;
            this.title = title;
            this.first = first;
            this.second = second;
            this.onFirst = onFirst;
            this.onSecond = onSecond;
            this.isOpening = isOpening;

            this.game = LinesGame.instance;
        }
Example #3
0
        /// <summary>
        /// The main game loop
        /// </summary>
        /// <param name="gameTime">A snapshot of timing values</param>
        protected override void Update(GameTime gameTime)
        {
            // If we haven't initialized the map, do so
            if (!CurrentMap.Initialized)
            {
                CurrentMap.MapInit();
            }

            IsMouseVisible = false;

            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            MouseState    ms = Mouse.GetState();
            KeyboardState ks = Keyboard.GetState();

            if (ks.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // Change the view
            Vector3 worldtransform = Vector3.Zero;

            if (ks.IsKeyDown(Keys.Left) || ms.X <= 3)
            {
                worldtransform.X = 1f;
            }
            else if (ks.IsKeyDown(Keys.Right) || ms.X >= graphics.PreferredBackBufferWidth - 4)
            {
                worldtransform.X = -1f;
            }
            if (ks.IsKeyDown(Keys.Up) || ms.Y <= 3)
            {
                worldtransform.Z = 1f;
            }
            else if (ks.IsKeyDown(Keys.Down) || ms.Y >= graphics.PreferredBackBufferHeight - 4)
            {
                worldtransform.Z = -1f;
            }
            if (ks.IsKeyDown(Keys.PageUp))
            {
                worldtransform.Y = -1f;
            }
            else if (ks.IsKeyDown(Keys.PageDown))
            {
                worldtransform.Y = 1f;
            }

            worldtransform    = Vector3.Transform(worldtransform * elapsed, Matrix.CreateScale(75f) * Matrix.Invert(CurrentMap.Rotation));
            CurrentMap.World *= Matrix.CreateTranslation(worldtransform.X, worldtransform.Y, worldtransform.Z);

            // Enforce some kind of map bounds - should be done with respect to the world height

            CurrentMap.World.M41 = MathHelper.Clamp(CurrentMap.World.M41, -(CurrentMap.Width / 2), (CurrentMap.Width / 2));
            CurrentMap.World.M43 = MathHelper.Clamp(CurrentMap.World.M43, -(CurrentMap.Height / 2), (CurrentMap.Height / 2));

            if (ks.IsKeyDown(Keys.Divide))
            {
                CurrentMap.Rotation.Yaw -= (float)(MathHelper.PiOver2 * elapsed);
            }
            else if (ks.IsKeyDown(Keys.Multiply))
            {
                CurrentMap.Rotation.Yaw += (float)(MathHelper.PiOver2 * elapsed);
            }

            // Gets/Sets control groups
            int cgid = GetD(ks);

            if (cgid != -1)
            {
                if (ks.IsKeyDown(Keys.LeftControl))
                {
                    Player.CurrentPlayer.SetControlGroup(cgid);
                }
                else
                {
                    Player.CurrentPlayer.RecallControlGroup(cgid);
                }
            }

            // The last selected units - used for failed OnSelects
            UnitGroup prevselected   = new UnitGroup(Player.CurrentPlayer.Selected.ToArray());
            bool      changeselected = false;


            // A smart event is occuring
            if (prevMouse.RightButton == ButtonState.Pressed && ms.RightButton == ButtonState.Released && ((ms.Y >= 467 && ms.X <= 133) || ms.Y < 457) && Player.CurrentPlayer.Selected.Count > 0 && Player.CurrentPlayer.Selected[0].Owner == Player.CurrentPlayer)
            {
                Unit obj = GetUnitAtScreenPoint(new Vector2(ms.X, ms.Y));

                Vector3 worldspace = Vector3.Zero;
                if (ms.Y >= 467)
                {
                    Vector2 mapspace = new Vector2((ms.X - 69) / 128f * CurrentMap.Width, (ms.Y - 531) / 128f * CurrentMap.Height);
                    worldspace = new Vector3(mapspace.X, CurrentMap.GetHeight(mapspace), mapspace.Y);
                }
                else
                {
                    worldspace = CurrentMap.GetWorldPos(new Point(ms.X, ms.Y)) ?? Vector3.Zero;
                }

                object target = obj;
                if (target == null)
                {
                    target = worldspace;
                }

                foreach (Unit caster in Player.CurrentPlayer.Selected)
                {
                    bool success = false;

                    if (caster != null)
                    {
                        // Check each button for a smart - if it has one and returns true,
                        // the smart is handled.
                        foreach (ContextButton button in caster.Type.Actions)
                        {
                            if (button.TypeOf == typeof(Ability))
                            {
                                string  aid     = (string)button.Tag;
                                Ability ability = Ability.GetAbility(aid);
                                success = Ability.InvokeSmart(ability.Code, aid, caster, target);
                                if (success)
                                {
                                    break;
                                }
                            }
                        }
                    }

                    // If the smart is not handled, we'll do it ourselves
                    if (!success)
                    {
                        if (obj != null)
                        {
                            foreach (Unit selected in Player.CurrentPlayer.Selected)
                            {
                                if (selected.Type.Attacks && obj.Owner != Player.CurrentPlayer)
                                {
                                    selected.OrderAttack(obj);
                                }
                                else
                                {
                                    selected.OrderMove(worldspace);
                                }
                            }
                        }
                        else
                        {
                            foreach (Unit selected in Player.CurrentPlayer.Selected)
                            {
                                if (selected.Type.IsBuilding)
                                {
                                    selected.RallyPoint = new Vector2(worldspace.X, worldspace.Z);
                                }
                                else if (selected.Type.Moves)
                                {
                                    selected.OrderMove(worldspace);
                                }
                            }
                        }
                    }
                }
            }


            if (prevMouse.LeftButton == ButtonState.Pressed && ms.LeftButton == ButtonState.Released && OnClick == null && ms.Y < 457)
            {
                // A single left mouse click has occured - selected the unit at the cursor
                if (startMouse.X == ms.X && startMouse.Y == ms.Y)
                {
                    if (!ks.IsKeyDown(Keys.LeftShift))
                    {
                        Player.CurrentPlayer.Selected.Clear();
                    }

                    Unit obj = GetUnitAtScreenPoint(new Vector2(ms.X, ms.Y));
                    if (obj != null)
                    {
                        Player.CurrentPlayer.Select(obj);
                        Player.CurrentPlayer.RefreshActions();
                        changeselected = true;
                    }
                    else
                    {
                        OnSelect = null;
                    }
                }
                else
                {
                    // A selection rectangle has occured - select all units in it
                    Rectangle r = new Rectangle(Math.Min(startMouse.X, ms.X), Math.Min(startMouse.Y, ms.Y), Math.Max(startMouse.X, ms.X) - Math.Min(startMouse.X, ms.X), Math.Max(startMouse.Y, ms.Y) - Math.Min(startMouse.Y, ms.Y));
                    if (!ks.IsKeyDown(Keys.LeftShift))
                    {
                        Player.CurrentPlayer.Selected.Clear();
                    }

                    foreach (Unit obj in Units)
                    {
                        Vector3 pos = obj.Project();
                        if (r.Contains(new Point((int)pos.X, (int)pos.Y)) || (Player.CurrentPlayer.Selected.Contains(obj) && ks.IsKeyDown(Keys.LeftShift)))
                        {
                            if (!Player.CurrentPlayer.Selected.Contains(obj))
                            {
                                Player.CurrentPlayer.Select(obj);
                                Player.CurrentPlayer.RefreshActions();
                                changeselected = true;
                            }
                        }
                    }

                    if (Player.CurrentPlayer.Selected.Count == 0)
                    {
                        OnSelect = null;
                    }
                }
            }

            // Reverts selection changes if OnSelect was enabled and requested
            if (OnSelect != null && changeselected)
            {
                if (!OnSelect(Player.CurrentPlayer.Selected[0], OnSelectTag))
                {
                    Player.CurrentPlayer.Selected = prevselected;
                }
                else
                {
                    Player.CurrentPlayer.CoerceSelectionSize(1);
                }
                OnSelect = null;
            }


            if (prevMouse.LeftButton == ButtonState.Pressed && ms.LeftButton == ButtonState.Released && OnClick != null && ((ms.Y >= 467 && ms.X <= 133) || ms.Y < 457))
            {
                // An action targeting the ground has occured
                Vector3 worldspace = Vector3.Zero;
                if (ms.Y >= 467)
                {
                    Vector2 mapspace = new Vector2((ms.X - 69) / 128f * CurrentMap.Width, (ms.Y - 531) / 128f * CurrentMap.Height);
                    worldspace = new Vector3(mapspace.X, CurrentMap.GetHeight(mapspace), mapspace.Y);
                }
                else
                {
                    worldspace = CurrentMap.GetWorldPos(new Point(ms.X, ms.Y)) ?? Vector3.Zero;
                }
                if (OnClick(new Point(ms.X, ms.Y), worldspace, OnClickTag))
                {
                    OnClick = null;
                }
            }
            else if (ms.LeftButton == ButtonState.Released)
            {
                startMouse = ms;
            }
            else if (ms.LeftButton == ButtonState.Pressed)
            {
                if (ms.Y >= 467 && ms.X <= 133 && ms.Y <= 595 && ms.X >= 5 && OnClick == null)
                {
                    // someone is moving via minimap
                    Vector2 mapspace = new Vector2((ms.X - 69) / 128f * CurrentMap.Width, (ms.Y - 531) / 128f * CurrentMap.Height);

                    CurrentMap.World.M41 = -mapspace.X;
                    CurrentMap.World.M43 = -mapspace.Y;
                }
                else
                {
                    // There is a selection rectangle in the works - add the lines for it
                    Vector2 topleft  = new Vector2(startMouse.X, startMouse.Y);
                    Vector2 topright = new Vector2(ms.X, startMouse.Y);
                    Vector2 botleft  = new Vector2(startMouse.X, ms.Y);
                    Vector2 botright = new Vector2(ms.X, ms.Y);
                    Interface.Lines.Add(new Line(topleft, topright, Color.LimeGreen));
                    Interface.Lines.Add(new Line(topleft, botleft, Color.LimeGreen));
                    Interface.Lines.Add(new Line(topright, botright, Color.LimeGreen));
                    Interface.Lines.Add(new Line(botleft, botright, Color.LimeGreen));
                }
            }

            prevMouse = ms;

            base.Update(gameTime);
        }
Example #4
0
 public PreSelectState(Color color, Texture2D title, Texture2D first, Texture2D second, OnSelectHandler onFirst, bool isOpening)
     : this(color, title, first, second, onFirst, null, isOpening)
 {
 }