コード例 #1
0
 public static void RemoveObject(RtsObject o)
 {
     lock (RtsObjects)
     {
         rtsObjects.Remove(o);
     }
 }
コード例 #2
0
ファイル: UnitCommands.cs プロジェクト: nubington/rts
 /*public AttackCommand(List<Vector2> wayPoints, RtsObject target)
     : base(wayPoints, 1)
 {
     this.target = target;
 }*/
 public AttackCommand(Unit unit, RtsObject target, bool willingToChangeTarget, bool holdPosition)
     : base(unit, target.CenterPoint)
 {
     Target = target;
     this.willingToChangeTarget = willingToChangeTarget;
     this.holdPosition = holdPosition;
 }
コード例 #3
0
ファイル: Selection.cs プロジェクト: bagnalla/RTS
        public void Remove(RtsObject o)
        {
            objects.Remove(o);

            if (typeCounts.ContainsKey(o.GetType()))
            {
                int count;
                typeCounts.TryGetValue(o.GetType(), out count);
                count--;
                if (count == 0)
                {
                    typeCounts.Remove(o.GetType());
                }
            }

            if (mostPopulousType == o.GetType())
            {
                largestCount = 0;
                foreach (KeyValuePair <Type, int> pair in typeCounts)
                {
                    if (pair.Value > largestCount)
                    {
                        mostPopulousType = pair.Key;
                        largestCount     = pair.Value;
                    }
                }
            }

            if (objects.Count == 0)
            {
                mostPopulousType = null;
                activeType       = null;
                largestCount     = 0;
            }
        }
コード例 #4
0
 /*public AttackCommand(List<Vector2> wayPoints, RtsObject target)
  *  : base(wayPoints, 1)
  * {
  *  this.target = target;
  * }*/
 public AttackCommand(Unit unit, RtsObject target, bool willingToChangeTarget, bool holdPosition)
     : base(unit, target.CenterPoint)
 {
     Target = target;
     this.willingToChangeTarget = willingToChangeTarget;
     this.holdPosition          = holdPosition;
 }
コード例 #5
0
ファイル: Selection.cs プロジェクト: bagnalla/RTS
        /*public Selection(Selection s)
         *  : this(s.objects)
         * {
         * }*/

        public void Add(RtsObject o)
        {
            //if (!objects.Contains(o))
            objects.Add(o);
            sortSelection();

            RtsObjectType type = o.Type;

            if (objects.Count == 1)
            {
                ActiveType = type;
            }

            int count;

            if (!typeCounts.ContainsKey(type))
            {
                typeCounts.Add(type, 1);
                count = 1;
            }
            else
            {
                typeCounts[type]++;
                typeCounts.TryGetValue(type, out count);
            }

            if (count > largestCount)
            {
                mostPopulousType = type;
                largestCount     = count;
            }
        }
コード例 #6
0
ファイル: Selection.cs プロジェクト: bagnalla/RTS
        public void Add(RtsObject o)
        {
            objects.Add(o);

            if (objects.Count == 1)
            {
                activeType = o.GetType();
            }

            int count;

            if (!typeCounts.ContainsKey(o.GetType()))
            {
                typeCounts.Add(o.GetType(), 1);
                count = 1;
            }
            else
            {
                typeCounts[o.GetType()]++;
                typeCounts.TryGetValue(o.GetType(), out count);
            }

            if (count > largestCount)
            {
                mostPopulousType = o.GetType();
                largestCount     = count;
            }
        }
コード例 #7
0
ファイル: RtsBullet.cs プロジェクト: bagnalla/RTS
 public RtsBullet(Unit shooter, RtsObject target, Vector2 position, int size, float speed)
     : base(new Rectangle(0, 0, size, size), new Vector2(speed, speed))
 {
     this.shooter = shooter;
     this.target  = target;
     CenterPoint  = position;
     RtsBullets.Add(this);
 }
コード例 #8
0
ファイル: RtsBullet.cs プロジェクト: nubington/rts
 public RtsBullet(Unit shooter, RtsObject target, Vector2 position, int size, float speed)
     : base(new Rectangle(0, 0, size, size), new Vector2(speed, speed))
 {
     this.shooter = shooter;
     this.target = target;
     CenterPoint = position;
     RtsBullets.Add(this);
 }
コード例 #9
0
        static void AddStructure(Structure s)
        {
            structures.Add(s);

            lock (RtsObject.RtsObjects)
            {
                RtsObject.AddObject(s);
            }
        }
コード例 #10
0
ファイル: Animation.cs プロジェクト: bagnalla/RTS
 public UnitAnimation(RtsObject unit, int size, float duration, float fps, bool stayAfterDeath, Texture2D spriteSheet, int frameWidth, int frameHeight)
     : base(duration, fps, Util.SplitTexture(spriteSheet, frameWidth, frameHeight))
 {
     Unit               = unit;
     Rectangle          = new Rectangle(0, 0, size, size);
     Rectangle.Location = new Point((int)Unit.CenterPoint.X - Rectangle.Width / 2, (int)Unit.CenterPoint.Y - Rectangle.Height / 2);
     Rotation           = Unit.Rotation;
     StayAfterDeath     = stayAfterDeath;
     UnitAnimations.Add(this);
 }
コード例 #11
0
ファイル: Animation.cs プロジェクト: bagnalla/RTS
 public UnitAnimation(RtsObject unit, int size, float duration, float fps, bool stayAfterDeath, params Texture2D[] textures)
     : base(duration, fps, textures)
 {
     Unit               = unit;
     Rectangle          = new Rectangle(0, 0, size, size);
     Rectangle.Location = new Point((int)Unit.CenterPoint.X - Rectangle.Width / 2, (int)Unit.CenterPoint.Y - Rectangle.Height / 2);
     Rotation           = Unit.Rotation;
     StayAfterDeath     = stayAfterDeath;
     UnitAnimations.Add(this);
 }
コード例 #12
0
ファイル: Selection.cs プロジェクト: bagnalla/RTS
 void sortSelection()
 {
     for (int i = 1; i < objects.Count; i++)
     {
         for (int j = i; j >= 1 && objects[j].SelectionSortValue < objects[j - 1].SelectionSortValue; j--)
         {
             RtsObject tempItem = objects[j];
             objects.RemoveAt(j);
             objects.Insert(j - 1, tempItem);
         }
     }
 }
コード例 #13
0
ファイル: Selection.cs プロジェクト: bagnalla/RTS
        public void Remove(RtsObject o)
        {
            if (!objects.Contains(o))
            {
                return;
            }

            objects.Remove(o);

            Rts.selectedUnitsChanged = true;

            RtsObjectType type = o.Type;

            if (typeCounts.ContainsKey(type))
            {
                typeCounts[type]--;
                if (typeCounts[type] == 0)
                {
                    typeCounts.Remove(type);
                    if (activeType == type)
                    {
                        TabActiveType();
                    }
                }
            }

            if (mostPopulousType == type)
            {
                largestCount = 0;
                foreach (KeyValuePair <RtsObjectType, int> pair in typeCounts)
                {
                    if (pair.Value > largestCount)
                    {
                        mostPopulousType = pair.Key;
                        largestCount     = pair.Value;
                    }
                }
            }

            if (objects.Count == 0)
            {
                mostPopulousType = null;
                activeType       = null;
                largestCount     = 0;
            }

            //if (!ContainsType(o))
            //    TabActiveType();
        }
コード例 #14
0
        // find walkable pathnode that is nearest to given RtsObject
        public PathNode FindNearestPathNode(int startY, int startX, RtsObject o)
        {
            // searches outward from start node for walkable nodes
            List <PathNode> walkableNodes = new List <PathNode>();

            for (int i = 1; ; i++)
            {
                for (int x = startX - i; x <= startX + i; x += i * 2)
                {
                    if (x < 0 || x > Map.Width - 1)
                    {
                        continue;
                    }
                    for (int y = startY - i; y <= startY + i; y += i * 2)
                    {
                        if (y < 0 || y > Map.Height - 1)
                        {
                            continue;
                        }
                        if (IsNodeWalkable(y, x))
                        {
                            //return PathNodes[y, x];
                            walkableNodes.Add(PathNodes[y, x]);
                        }
                    }
                }
                if (walkableNodes.Count > 0)
                {
                    break;
                }
            }

            // return discovered path node that is closest to unit
            float shortestDistanceToUnit = float.MaxValue;
            int   shortestIndex          = 0;

            for (int i = 0; i < walkableNodes.Count; i++)
            {
                float distance = Vector2.Distance(walkableNodes[i].Tile.CenterPoint, o.CenterPoint);
                if (distance < shortestDistanceToUnit)
                {
                    shortestDistanceToUnit = distance;
                    shortestIndex          = i;
                }
            }
            return(walkableNodes[shortestIndex]);
        }
コード例 #15
0
        public bool IsTileVisible(Vector2 point1, Vector2 point2, RtsObject o, int numberOfIntermediatePoints)
        {
            float lerpAmountIncrement = 1f / (numberOfIntermediatePoints + 1);

            for (float l = lerpAmountIncrement; l < 1f; l += lerpAmountIncrement)
            //for (float l = 1f - lerpAmountIncrement; l > 0; l -= lerpAmountIncrement)
            {
                Vector2 intermediatePoint = Vector2.Lerp(point1, point2, l);

                if (!IsPointVisible(intermediatePoint, o))
                {
                    return(false);
                }
            }

            return(true);
        }
コード例 #16
0
ファイル: RtsBullet.cs プロジェクト: nubington/rts
        public RtsBullet(BulletType type, Unit shooter, RtsObject target, Vector2 position, int size, float speed)
            : base(new Rectangle(0, 0, size, size), new Vector2(speed, speed))
        {
            Type = type;

            /*if (type.Animated)
                animation = new Animation(0, 1 * Rts.GameSpeed, Util.SplitTexture(type.Texture, type.SheetWidth, type.SheetHeight));
            else
                Texture = type.Texture;*/
            if (type.Textures.Length > 1)
                animation = new Animation(0, 10 * Rts.GameSpeed, type.Textures);
            else
                Texture = type.Textures[0];

            this.shooter = shooter;
            this.target = target;
            CenterPoint = position;
            RtsBullets.Add(this);
        }
コード例 #17
0
        public bool IsPointVisible(Vector2 point, RtsObject o)
        {
            int y = (int)(point.Y / Map.TileSize);
            int x = (int)(point.X / Map.TileSize);

            if ((y < 0 || y >= Map.Height || x < 0 || x >= Map.Width))
            {
                return(false);
            }

            PathNode pathNode = PathNodes[y, x];

            if (o.OccupiedPathNodes.Contains(pathNode))
            {
                return(true);
            }
            else
            {
                return(pathNode.Tile.Walkable && !pathNode.Blocked);
            }
        }
コード例 #18
0
        public RtsBullet(BulletType type, Unit shooter, RtsObject target, Vector2 position, int size, float speed)
            : base(new Rectangle(0, 0, size, size), new Vector2(speed, speed))
        {
            Type = type;

            /*if (type.Animated)
             *  animation = new Animation(0, 1 * Rts.GameSpeed, Util.SplitTexture(type.Texture, type.SheetWidth, type.SheetHeight));
             * else
             *  Texture = type.Texture;*/
            if (type.Textures.Length > 1)
            {
                animation = new Animation(0, 10 * Rts.GameSpeed, type.Textures);
            }
            else
            {
                Texture = type.Textures[0];
            }

            this.shooter = shooter;
            this.target  = target;
            CenterPoint  = position;
            RtsBullets.Add(this);
        }
コード例 #19
0
ファイル: Selection.cs プロジェクト: nubington/rts
        /*public Selection(Selection s)
            : this(s.objects)
        {
        }*/
        public void Add(RtsObject o)
        {
            //if (!objects.Contains(o))
                objects.Add(o);
            sortSelection();

            RtsObjectType type = o.Type;

            if (objects.Count == 1)
                ActiveType = type;

            int count;

            if (!typeCounts.ContainsKey(type))
            {
                typeCounts.Add(type, 1);
                count = 1;
            }
            else
            {
                typeCounts[type]++;
                typeCounts.TryGetValue(type, out count);
            }

            if (count > largestCount)
            {
                mostPopulousType = type;
                largestCount = count;
            }
        }
コード例 #20
0
 /*public AttackCommand(List<Vector2> wayPoints, RtsObject target)
  *  : base(wayPoints, 1)
  * {
  *  this.target = target;
  * }*/
 public AttackCommand(RtsObject target)
     : base(target.CenterPoint, 1)
 {
     this.target = target;
 }
コード例 #21
0
ファイル: Selection.cs プロジェクト: nubington/rts
        public void Remove(RtsObject o)
        {
            objects.Remove(o);

            if (typeCounts.ContainsKey(o.GetType()))
            {
                int count;
                typeCounts.TryGetValue(o.GetType(), out count);
                count--;
                if (count == 0)
                    typeCounts.Remove(o.GetType());
            }

            if (mostPopulousType == o.GetType())
            {
                largestCount = 0;
                foreach (KeyValuePair<Type, int> pair in typeCounts)
                {
                    if (pair.Value > largestCount)
                    {
                        mostPopulousType = pair.Key;
                        largestCount = pair.Value;
                    }
                }
            }

            if (objects.Count == 0)
            {
                mostPopulousType = null;
                activeType = null;
                largestCount = 0;
            }
        }
コード例 #22
0
ファイル: Rts Player Actions.cs プロジェクト: nubington/rts
        // used by rightClick() and giveAttackCommand() to schedule the attack commands
        void scheduleAttackCommands(List<ScheduledUnitCommand> scheduledUnitCommands, RtsObject target)
        {
            // add scheduled actions and send them in batch over network
            NetOutgoingMessage msg = netPeer.CreateMessage();

            msg.Write(MessageID.UNIT_ATTACK_COMMAND_BATCH);
            msg.Write(currentScheduleTime);
            msg.Write(Player.Me.Team);
            msg.Write(usingShift);
            msg.Write((short)scheduledUnitCommands.Count);

            Unit targetUnit = target as Unit;
            if (targetUnit != null)
            {
                msg.Write((short)0);
                msg.Write(targetUnit.ID);
            }

            Structure targetStructure = target as Structure;
            if (targetStructure != null)
            {
                msg.Write((short)1);
                msg.Write(targetStructure.ID);
            }
            msg.Write(target.Team);

            foreach (ScheduledUnitCommand s in scheduledUnitCommands)
            {
                AttackCommand attackCommand = s.UnitCommand as AttackCommand;

                Player.Players[s.Team].ScheduledActions.Add(new ScheduledUnitCommand(currentScheduleTime, attackCommand, s.Queued));
                //Rts.pathFinder.AddHighPriorityPathFindRequest(attackCommand, (int)Vector2.DistanceSquared(attackCommand.Unit.CenterPoint, attackCommand.Destination), false);

                //if (s.Queued)
                //moveCommand.Unit.QueueCommand(moveCommand);
                //else
                //moveCommand.Unit.GiveCommand(moveCommand);

                msg.Write(attackCommand.Unit.ID);
            }

            netPeer.SendMessage(msg, connection, NetDeliveryMethod.ReliableOrdered);
        }
コード例 #23
0
ファイル: PathFinderTools.cs プロジェクト: nubington/rts
        public bool IsPointVisible(Vector2 point, RtsObject o)
        {
            int y = (int)(point.Y / Map.TileSize);
            int x = (int)(point.X / Map.TileSize);

            if ((y < 0 || y >= Map.Height || x < 0 || x >= Map.Width))
                return false;

            PathNode pathNode = PathNodes[y, x];
            if (o.OccupiedPathNodes.Contains(pathNode))
                return true;
            else
                return (pathNode.Tile.Walkable && !pathNode.Blocked);
        }
コード例 #24
0
ファイル: Animation.cs プロジェクト: nubington/rts
 public UnitAnimation(RtsObject unit, int size, float duration, float fps, bool stayAfterDeath, Texture2D spriteSheet, int frameWidth, int frameHeight)
     : base(duration, fps, Util.SplitTexture(spriteSheet, frameWidth, frameHeight))
 {
     Unit = unit;
     Rectangle = new Rectangle(0, 0, size, size);
     Rectangle.Location = new Point((int)Unit.CenterPoint.X - Rectangle.Width / 2, (int)Unit.CenterPoint.Y - Rectangle.Height / 2);
     Rotation = Unit.Rotation;
     StayAfterDeath = stayAfterDeath;
     UnitAnimations.Add(this);
 }
コード例 #25
0
ファイル: Selection.cs プロジェクト: nubington/rts
        public void Remove(RtsObject o)
        {
            if (!objects.Contains(o))
                return;

            objects.Remove(o);

            Rts.selectedUnitsChanged = true;

            RtsObjectType type = o.Type;

            if (typeCounts.ContainsKey(type))
            {
                typeCounts[type]--;
                if (typeCounts[type] == 0)
                {
                    typeCounts.Remove(type);
                    if (activeType == type)
                        TabActiveType();
                }
            }

            if (mostPopulousType == type)
            {
                largestCount = 0;
                foreach (KeyValuePair<RtsObjectType, int> pair in typeCounts)
                {
                    if (pair.Value > largestCount)
                    {
                        mostPopulousType = pair.Key;
                        largestCount = pair.Value;
                    }
                }
            }

            if (objects.Count == 0)
            {
                mostPopulousType = null;
                activeType = null;
                largestCount = 0;
            }

            //if (!ContainsType(o))
            //    TabActiveType();
        }
コード例 #26
0
ファイル: Unit.cs プロジェクト: nubington/rts
 /*public AttackCommand(List<Vector2> wayPoints, RtsObject target)
     : base(wayPoints, 1)
 {
     this.target = target;
 }*/
 public AttackCommand(RtsObject target)
     : base(target.CenterPoint, 1)
 {
     this.target = target;
 }
コード例 #27
0
ファイル: Rts Input Processing.cs プロジェクト: nubington/rts
        void SelectUnits(GameTime gameTime)
        {
            //SelectBox.Box.CalculateCorners();

            bool structureInPreviousSelection = false;
            foreach (RtsObject o in SelectedUnits)
            {
                if (o is Structure)
                    structureInPreviousSelection = true;
            }

            int selectingUnitsCount = SelectingUnits.Count;
            SelectingUnits.Clear();

            timeSinceLastSimpleClick += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

            bool simpleClick = (SelectBox.Box.GreaterOfWidthAndHeight <= simpleClickSize);

            if (SelectBox.IsSelecting)
            {
                selecting = true;
                unitsSelected = false;
                /*foreach (Unit unit in Unit.Units)
                {
                    if ((simpleClick && unit.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) ||
                            (!simpleClick && SelectBox.Box.Rectangle.Intersects(unit.Rectangle)))
                    {
                        if (SelectingUnits.Count < MAXSELECTIONSIZE)
                            SelectingUnits.Add(unit);
                    }
                }
                foreach (Structure structure in Structure.Structures)
                {
                    if ((simpleClick && structure.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) ||
                            (!simpleClick && SelectBox.Box.Rectangle.Intersects(structure.Rectangle)))
                    {
                        if (SelectingUnits.Count < MAXSELECTIONSIZE)
                            SelectingUnits.Add(structure);
                    }
                }*/
                foreach (RtsObject o in RtsObject.RtsObjects)
                {
                    if ((simpleClick && o.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) ||
                            (!simpleClick && SelectBox.Box.Rectangle.Intersects(o.Rectangle)))
                    {
                        if (!simpleClick && o.Team != Player.Me.Team)
                            continue;

                        if (SelectingUnits.Count < MAXSELECTIONSIZE && o.Visible)
                            SelectingUnits.Add(o);
                    }
                }

                /*for (int i = 0; i < SelectingUnits.Count; )
                {
                    RtsObject o = SelectingUnits[i];

                    if (!o.Visible)
                        SelectingUnits.Remove(o);
                    else
                        i++;
                }*/
            }
            else if (unitsSelected == false)
            {
                selecting = false;
                unitsSelected = true;
                selectedUnitsChanged = true;
                unitInSelection = false;
                newUnitInSelection = false;
                myTeamInSelection = false;

                bool objectClicked = false;

                // holding shift
                if (usingShift)
                {
                    // dont do if enemy unit selected
                    if (SelectedUnits.Count == 0 || SelectedUnits[0].Team == Player.Me.Team)
                    {
                        foreach (RtsObject o in RtsObject.RtsObjects)
                        {
                            if (!o.Visible)// || (SelectedUnits.Count > 0 && o.Team != SelectedUnits[0].Team))
                                continue;

                            if ((simpleClick && o.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) ||
                                (!simpleClick && SelectBox.Box.Rectangle.Intersects(o.Rectangle)))
                            {
                                // holding ctrl or double click
                                if ((simpleClick && lastUnitClicked == o && timeSinceLastSimpleClick <= doubleClickDelay) ||
                                    (simpleClick && keyboardState.IsKeyDown(Keys.LeftControl)))
                                {
                                    timeSinceLastSimpleClick = 0;

                                    Unit unit = o as Unit;
                                    if (unit != null)
                                    {
                                        foreach (Unit u in Unit.Units)
                                        {
                                            if (u.Type == unit.Type && u.Team == unit.Team && !u.IsOffScreen(worldViewport, camera))
                                            {
                                                if (SelectedUnits.Count >= MAXSELECTIONSIZE)
                                                    break;

                                                if (!SelectedUnits.Contains(u))
                                                {
                                                    SelectedUnits.Add(u);
                                                    newUnitInSelection = true;
                                                }
                                            }
                                        }
                                    }

                                    Structure structure = o as Structure;
                                    if (structure != null)
                                    {
                                        foreach (Structure s in Structure.Structures)
                                        {
                                            if (s.Type == structure.Type && s.Team == structure.Team && !s.IsOffScreen(worldViewport, camera))
                                            {
                                                if (SelectedUnits.Count >= MAXSELECTIONSIZE)
                                                    break;

                                                if (!SelectedUnits.Contains(s))
                                                    SelectedUnits.Add(s);
                                            }
                                        }
                                    }
                                }
                                // not holding ctrl or double click
                                else
                                {
                                    if (!SelectedUnits.Contains(o))
                                    {
                                        if (SelectedUnits.Count < MAXSELECTIONSIZE)
                                        {
                                            SelectedUnits.Add(o);
                                            if (o is Unit)
                                                newUnitInSelection = true;
                                        }
                                        //selectedUnitsChanged = true;
                                    }
                                    else if (simpleClick)
                                    {
                                        SelectedUnits.Remove(o);
                                        //selectedUnitsChanged = true;
                                    }
                                }
                                lastUnitClicked = o;
                                objectClicked = true;
                            }
                        }
                    }
                }
                // not holding shift
                else
                {
                    SelectedUnits.Clear();
                    //selectedUnitsChanged = true;

                    foreach (RtsObject o in RtsObject.RtsObjects)
                    {
                        if (!o.Visible)
                            continue;

                        if ((simpleClick && o.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) ||
                            (!simpleClick && SelectBox.Box.Rectangle.Intersects(o.Rectangle)))
                        {
                            // holding ctrl or double click
                            if ((simpleClick && lastUnitClicked == o && timeSinceLastSimpleClick <= doubleClickDelay) ||
                                (simpleClick && keyboardState.IsKeyDown(Keys.LeftControl)))
                            {
                                timeSinceLastSimpleClick = 0;

                                //if (o.Team != Player.Me.Team)
                                //    continue;

                                Unit unit = o as Unit;
                                if (unit != null)
                                {
                                    foreach (Unit u in Unit.Units)
                                    {
                                        if (u.Type == unit.Type && u.Team == unit.Team && !u.IsOffScreen(worldViewport, camera))
                                        {
                                            if (SelectedUnits.Count >= MAXSELECTIONSIZE)
                                                break;

                                            if (!SelectedUnits.Contains(u))
                                            {
                                                SelectedUnits.Add(u);
                                                newUnitInSelection = true;
                                            }
                                        }
                                    }
                                }

                                Structure structure = o as Structure;
                                if (structure != null)
                                {
                                    foreach (Structure s in Structure.Structures)
                                    {
                                        if (s.Type == structure.Type && s.Team == structure.Team && !s.IsOffScreen(worldViewport, camera))
                                        {
                                            if (SelectedUnits.Count >= MAXSELECTIONSIZE)
                                                break;

                                            if (!SelectedUnits.Contains(s))
                                                SelectedUnits.Add(s);
                                        }
                                    }
                                }
                            }
                            // not holding ctrl or double click
                            else
                            {
                                if (SelectedUnits.Count < MAXSELECTIONSIZE && !SelectedUnits.Contains(o))
                                {
                                    //if (SelectedUnits.Count == 0 || SelectedUnits[0].Team == Player.Me.Team)
                                    SelectedUnits.Add(o);
                                    if (o is Unit)
                                        newUnitInSelection = true;
                                }
                            }

                            lastUnitClicked = o;
                            objectClicked = true;
                        }
                    }

                    SelectedUnits.SetActiveTypeToMostPopulousType();
                }
                if (simpleClick)
                    timeSinceLastSimpleClick = 0;

                foreach (RtsObject o in SelectedUnits)
                {
                    if (o.Team == Player.Me.Team)
                    {
                        myTeamInSelection = true;
                        break;
                    }
                }

                if (myTeamInSelection)
                {
                    for (int i = 0; i < SelectedUnits.Count; )
                    {
                        RtsObject o = SelectedUnits[i];
                        if (o.Team != Player.Me.Team)
                            SelectedUnits.Remove(o);
                        else
                            i++;
                    }
                }

                foreach (RtsObject o in SelectedUnits)
                {
                    if (o is Unit)
                    {
                        unitInSelection = true;
                        break;
                    }
                }

                if (unitInSelection)
                {
                    if (!usingShift || (!structureInPreviousSelection && newUnitInSelection))
                    {
                        for (int i = 0; i < SelectedUnits.Count; )
                        {
                            RtsObject o = SelectedUnits[i];
                            if (o is Structure)
                                SelectedUnits.Remove(o);
                            else
                                i++;
                        }
                    }
                }

                if (!objectClicked)
                    lastUnitClicked = null;
            }
        }
コード例 #28
0
ファイル: RtsObject.cs プロジェクト: nubington/rts
 public static void RemoveObject(RtsObject o)
 {
     lock (RtsObjects)
     {
         rtsObjects.Remove(o);
     }
 }
コード例 #29
0
ファイル: Selection.cs プロジェクト: nubington/rts
        public void Add(RtsObject o)
        {
            objects.Add(o);

            if (objects.Count == 1)
                activeType = o.GetType();

            int count;

            if (!typeCounts.ContainsKey(o.GetType()))
            {
                typeCounts.Add(o.GetType(), 1);
                count = 1;
            }
            else
            {
                typeCounts[o.GetType()]++;
                typeCounts.TryGetValue(o.GetType(), out count);
            }

            if (count > largestCount)
            {
                mostPopulousType = o.GetType();
                largestCount = count;
            }
        }
コード例 #30
0
 public static void AddObject(RtsObject o)
 {
     rtsObjects.Add(o);
 }
コード例 #31
0
        //bool active;

        public UnitButton(Rectangle rectangle, RtsObject unit)
            : base(rectangle)
        {
            Unit = unit;
            //this.active = active;
        }
コード例 #32
0
ファイル: Selection.cs プロジェクト: nubington/rts
 public bool Contains(RtsObject o)
 {
     return objects.Contains(o);
 }
コード例 #33
0
ファイル: Selection.cs プロジェクト: bagnalla/RTS
 public bool Contains(RtsObject o)
 {
     return(objects.Contains(o));
 }
コード例 #34
0
ファイル: Selection.cs プロジェクト: nubington/rts
 public bool ContainsType(RtsObject o)
 {
     RtsObjectType type = o.Type;
     return typeCounts.ContainsKey(type);
 }
コード例 #35
0
ファイル: Selection.cs プロジェクト: bagnalla/RTS
        public bool ContainsType(RtsObject o)
        {
            RtsObjectType type = o.Type;

            return(typeCounts.ContainsKey(type));
        }
コード例 #36
0
ファイル: Animation.cs プロジェクト: nubington/rts
 public UnitAnimation(RtsObject unit, int size, float duration, float fps, bool stayAfterDeath, params Texture2D[] textures)
     : base(duration, fps, textures)
 {
     Unit = unit;
     Rectangle = new Rectangle(0, 0, size, size);
     Rectangle.Location = new Point((int)Unit.CenterPoint.X - Rectangle.Width / 2, (int)Unit.CenterPoint.Y - Rectangle.Height / 2);
     Rotation = Unit.Rotation;
     StayAfterDeath = stayAfterDeath;
     UnitAnimations.Add(this);
 }
コード例 #37
0
ファイル: Rts Drawing.cs プロジェクト: nubington/rts
        /*void drawSelectionRing(Unit unit, SpriteBatch spriteBatch, Color color)
        {
            line.Colour = color;

            line.Position = unit.CenterPoint;
            line.CreateCircle(unit.Radius, (int)Math.Round(unit.Radius * 2));
            line.Render(spriteBatch);
        }*/
        void drawSelectingRing(RtsObject o, SpriteBatch spriteBatch)
        {
            Unit unit = o as Unit;
            if (unit != null)
            {
                //spriteBatch.Draw(unit.SelectingTexture, new Rectangle((int)unit.CenterPoint.X, (int)unit.CenterPoint.Y, unit.Width, unit.Height), null, Color.White, unit.Rotation, unit.TextureCenterOrigin, SpriteEffects.None, 0f);

                line.ClearVectors();
                line.Colour = Color.Green;
                line.CreateCircle(unit.CenterPoint, unit.Radius);
                line.RenderWithZoom(spriteBatch, camera.Zoom);
            }

            Structure structure = o as Structure;
            if (structure != null)
            {
                //PrimitiveLine line = new PrimitiveLine(GraphicsDevice, 1);
                line.ClearVectors();
                line.Colour = Color.Green;
                line.CreateCircle(structure.CenterPoint, structure.Radius);
                line.RenderWithZoom(spriteBatch, camera.Zoom);
            }
        }
コード例 #38
0
ファイル: PathFinderTools.cs プロジェクト: nubington/rts
        // find walkable pathnode that is nearest to given RtsObject
        public PathNode FindNearestPathNode(int startY, int startX, RtsObject o)
        {
            // searches outward from start node for walkable nodes
            List<PathNode> walkableNodes = new List<PathNode>();
            for (int i = 1; ; i++)
            {
                for (int x = startX - i; x <= startX + i; x += i * 2)
                {
                    if (x < 0 || x > Map.Width - 1)
                        continue;
                    for (int y = startY - i; y <= startY + i; y += i * 2)
                    {
                        if (y < 0 || y > Map.Height - 1)
                            continue;
                        if (IsNodeWalkable(y, x))
                            //return PathNodes[y, x];
                            walkableNodes.Add(PathNodes[y, x]);
                    }
                }
                if (walkableNodes.Count > 0)
                    break;
            }

            // return discovered path node that is closest to unit
            float shortestDistanceToUnit = float.MaxValue;
            int shortestIndex = 0;
            for (int i = 0; i < walkableNodes.Count; i++)
            {
                float distance = Vector2.Distance(walkableNodes[i].Tile.CenterPoint, o.CenterPoint);
                if (distance < shortestDistanceToUnit)
                {
                    shortestDistanceToUnit = distance;
                    shortestIndex = i;
                }
            }
            return walkableNodes[shortestIndex];
        }
コード例 #39
0
ファイル: UnitButton.cs プロジェクト: nubington/rts
 //bool active;
 public UnitButton(Rectangle rectangle, RtsObject unit)
     : base(rectangle)
 {
     Unit = unit;
     //this.active = active;
 }
コード例 #40
0
ファイル: PathFinderTools.cs プロジェクト: nubington/rts
        public bool IsTileVisible(Vector2 point1, Vector2 point2, RtsObject o, int numberOfIntermediatePoints)
        {
            float lerpAmountIncrement = 1f / (numberOfIntermediatePoints + 1);

            for (float l = lerpAmountIncrement; l < 1f; l += lerpAmountIncrement)
            //for (float l = 1f - lerpAmountIncrement; l > 0; l -= lerpAmountIncrement)
            {
                Vector2 intermediatePoint = Vector2.Lerp(point1, point2, l);

                if (!IsPointVisible(intermediatePoint, o))
                    return false;
            }

            return true;
        }
コード例 #41
0
ファイル: Rts Input Processing.cs プロジェクト: bagnalla/RTS
        void SelectUnits(GameTime gameTime)
        {
            //SelectBox.Box.CalculateCorners();

            bool structureInPreviousSelection = false;

            foreach (RtsObject o in SelectedUnits)
            {
                if (o is Structure)
                {
                    structureInPreviousSelection = true;
                }
            }

            int selectingUnitsCount = SelectingUnits.Count;

            SelectingUnits.Clear();

            timeSinceLastSimpleClick += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

            bool simpleClick = (SelectBox.Box.GreaterOfWidthAndHeight <= simpleClickSize);


            if (SelectBox.IsSelecting)
            {
                selecting     = true;
                unitsSelected = false;

                /*foreach (Unit unit in Unit.Units)
                 * {
                 *  if ((simpleClick && unit.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) ||
                 *          (!simpleClick && SelectBox.Box.Rectangle.Intersects(unit.Rectangle)))
                 *  {
                 *      if (SelectingUnits.Count < MAXSELECTIONSIZE)
                 *          SelectingUnits.Add(unit);
                 *  }
                 * }
                 * foreach (Structure structure in Structure.Structures)
                 * {
                 *  if ((simpleClick && structure.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) ||
                 *          (!simpleClick && SelectBox.Box.Rectangle.Intersects(structure.Rectangle)))
                 *  {
                 *      if (SelectingUnits.Count < MAXSELECTIONSIZE)
                 *          SelectingUnits.Add(structure);
                 *  }
                 * }*/
                foreach (RtsObject o in RtsObject.RtsObjects)
                {
                    if ((simpleClick && o.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) ||
                        (!simpleClick && SelectBox.Box.Rectangle.Intersects(o.Rectangle)))
                    {
                        if (!simpleClick && o.Team != Player.Me.Team)
                        {
                            continue;
                        }

                        if (SelectingUnits.Count < MAXSELECTIONSIZE && o.Visible)
                        {
                            SelectingUnits.Add(o);
                        }
                    }
                }

                /*for (int i = 0; i < SelectingUnits.Count; )
                 * {
                 *  RtsObject o = SelectingUnits[i];
                 *
                 *  if (!o.Visible)
                 *      SelectingUnits.Remove(o);
                 *  else
                 *      i++;
                 * }*/
            }
            else if (unitsSelected == false)
            {
                selecting            = false;
                unitsSelected        = true;
                selectedUnitsChanged = true;
                unitInSelection      = false;
                newUnitInSelection   = false;
                myTeamInSelection    = false;

                bool objectClicked = false;

                // holding shift
                if (usingShift)
                {
                    // dont do if enemy unit selected
                    if (SelectedUnits.Count == 0 || SelectedUnits[0].Team == Player.Me.Team)
                    {
                        foreach (RtsObject o in RtsObject.RtsObjects)
                        {
                            if (!o.Visible)// || (SelectedUnits.Count > 0 && o.Team != SelectedUnits[0].Team))
                            {
                                continue;
                            }

                            if ((simpleClick && o.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) ||
                                (!simpleClick && SelectBox.Box.Rectangle.Intersects(o.Rectangle)))
                            {
                                // holding ctrl or double click
                                if ((simpleClick && lastUnitClicked == o && timeSinceLastSimpleClick <= doubleClickDelay) ||
                                    (simpleClick && keyboardState.IsKeyDown(Keys.LeftControl)))
                                {
                                    timeSinceLastSimpleClick = 0;

                                    Unit unit = o as Unit;
                                    if (unit != null)
                                    {
                                        foreach (Unit u in Unit.Units)
                                        {
                                            if (u.Type == unit.Type && u.Team == unit.Team && !u.IsOffScreen(worldViewport, camera))
                                            {
                                                if (SelectedUnits.Count >= MAXSELECTIONSIZE)
                                                {
                                                    break;
                                                }

                                                if (!SelectedUnits.Contains(u))
                                                {
                                                    SelectedUnits.Add(u);
                                                    newUnitInSelection = true;
                                                }
                                            }
                                        }
                                    }

                                    Structure structure = o as Structure;
                                    if (structure != null)
                                    {
                                        foreach (Structure s in Structure.Structures)
                                        {
                                            if (s.Type == structure.Type && s.Team == structure.Team && !s.IsOffScreen(worldViewport, camera))
                                            {
                                                if (SelectedUnits.Count >= MAXSELECTIONSIZE)
                                                {
                                                    break;
                                                }

                                                if (!SelectedUnits.Contains(s))
                                                {
                                                    SelectedUnits.Add(s);
                                                }
                                            }
                                        }
                                    }
                                }
                                // not holding ctrl or double click
                                else
                                {
                                    if (!SelectedUnits.Contains(o))
                                    {
                                        if (SelectedUnits.Count < MAXSELECTIONSIZE)
                                        {
                                            SelectedUnits.Add(o);
                                            if (o is Unit)
                                            {
                                                newUnitInSelection = true;
                                            }
                                        }
                                        //selectedUnitsChanged = true;
                                    }
                                    else if (simpleClick)
                                    {
                                        SelectedUnits.Remove(o);
                                        //selectedUnitsChanged = true;
                                    }
                                }
                                lastUnitClicked = o;
                                objectClicked   = true;
                            }
                        }
                    }
                }
                // not holding shift
                else
                {
                    SelectedUnits.Clear();
                    //selectedUnitsChanged = true;

                    foreach (RtsObject o in RtsObject.RtsObjects)
                    {
                        if (!o.Visible)
                        {
                            continue;
                        }

                        if ((simpleClick && o.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) ||
                            (!simpleClick && SelectBox.Box.Rectangle.Intersects(o.Rectangle)))
                        {
                            // holding ctrl or double click
                            if ((simpleClick && lastUnitClicked == o && timeSinceLastSimpleClick <= doubleClickDelay) ||
                                (simpleClick && keyboardState.IsKeyDown(Keys.LeftControl)))
                            {
                                timeSinceLastSimpleClick = 0;

                                //if (o.Team != Player.Me.Team)
                                //    continue;

                                Unit unit = o as Unit;
                                if (unit != null)
                                {
                                    foreach (Unit u in Unit.Units)
                                    {
                                        if (u.Type == unit.Type && u.Team == unit.Team && !u.IsOffScreen(worldViewport, camera))
                                        {
                                            if (SelectedUnits.Count >= MAXSELECTIONSIZE)
                                            {
                                                break;
                                            }

                                            if (!SelectedUnits.Contains(u))
                                            {
                                                SelectedUnits.Add(u);
                                                newUnitInSelection = true;
                                            }
                                        }
                                    }
                                }

                                Structure structure = o as Structure;
                                if (structure != null)
                                {
                                    foreach (Structure s in Structure.Structures)
                                    {
                                        if (s.Type == structure.Type && s.Team == structure.Team && !s.IsOffScreen(worldViewport, camera))
                                        {
                                            if (SelectedUnits.Count >= MAXSELECTIONSIZE)
                                            {
                                                break;
                                            }

                                            if (!SelectedUnits.Contains(s))
                                            {
                                                SelectedUnits.Add(s);
                                            }
                                        }
                                    }
                                }
                            }
                            // not holding ctrl or double click
                            else
                            {
                                if (SelectedUnits.Count < MAXSELECTIONSIZE && !SelectedUnits.Contains(o))
                                {
                                    //if (SelectedUnits.Count == 0 || SelectedUnits[0].Team == Player.Me.Team)
                                    SelectedUnits.Add(o);
                                    if (o is Unit)
                                    {
                                        newUnitInSelection = true;
                                    }
                                }
                            }

                            lastUnitClicked = o;
                            objectClicked   = true;
                        }
                    }

                    SelectedUnits.SetActiveTypeToMostPopulousType();
                }
                if (simpleClick)
                {
                    timeSinceLastSimpleClick = 0;
                }

                foreach (RtsObject o in SelectedUnits)
                {
                    if (o.Team == Player.Me.Team)
                    {
                        myTeamInSelection = true;
                        break;
                    }
                }

                if (myTeamInSelection)
                {
                    for (int i = 0; i < SelectedUnits.Count;)
                    {
                        RtsObject o = SelectedUnits[i];
                        if (o.Team != Player.Me.Team)
                        {
                            SelectedUnits.Remove(o);
                        }
                        else
                        {
                            i++;
                        }
                    }
                }

                foreach (RtsObject o in SelectedUnits)
                {
                    if (o is Unit)
                    {
                        unitInSelection = true;
                        break;
                    }
                }

                if (unitInSelection)
                {
                    if (!usingShift || (!structureInPreviousSelection && newUnitInSelection))
                    {
                        for (int i = 0; i < SelectedUnits.Count;)
                        {
                            RtsObject o = SelectedUnits[i];
                            if (o is Structure)
                            {
                                SelectedUnits.Remove(o);
                            }
                            else
                            {
                                i++;
                            }
                        }
                    }
                }

                if (!objectClicked)
                {
                    lastUnitClicked = null;
                }
            }
        }
コード例 #42
0
ファイル: RtsObject.cs プロジェクト: nubington/rts
 public static void AddObject(RtsObject o)
 {
     rtsObjects.Add(o);
 }
コード例 #43
0
        public static void RemoveStructure(Structure s)
        {
            structures.Remove(s);

            RtsObject.RemoveObject(s);
        }