コード例 #1
0
 void letWorkerEnter(WorkerNublet worker)
 {
     Rts.SelectedUnits.Remove(worker);
     workersInside.Add(worker);
     workerTimes.Add(0);
     worker.CenterPoint = centerPoint;
 }
コード例 #2
0
        public bool CheckForEntrance(WorkerNublet worker)
        {
            if (worker.CargoAmount == CARGO_PER_TRIP && worker.CargoType == Type)
            {
                TownHall townHall = findNearestTownHall(worker);
                if (townHall != null)
                {
                    worker.InsertCommand(new ReturnCargoCommand(worker, townHall, this));
                }
                else
                {
                    worker.NextCommand();
                }
                return(false);
            }

            if (Amount - (workersInside.Count * CARGO_PER_TRIP) <= 0)
            {
                return(false);
            }

            if (timeSinceLastEntrance >= ALLOW_ENTRANCE_DELAY)
            {
                timeSinceLastEntrance = 0;

                letWorkerEnter(worker);

                return(true);
            }

            return(false);
        }
コード例 #3
0
ファイル: Rts UI.cs プロジェクト: bagnalla/RTS
        void updatePlacedStructures()
        {
            placedStructures.Clear();

            foreach (Unit unit in Unit.Units)
            {
                WorkerNublet worker = unit as WorkerNublet;
                if (worker != null)
                {
                    if (worker.Commands.Count > 0)
                    {
                        foreach (UnitCommand command in worker.Commands)
                        {
                            BuildStructureCommand buildCommand = command as BuildStructureCommand;
                            if (buildCommand != null)
                            {
                                //placedStructures.Add(new ObjectLink<StructureType, Rectangle>(buildCommand.StructureType, new Rectangle((int)(buildCommand.Destination.X - buildCommand.StructureType.Size * map.TileSize / 2), (int)(buildCommand.Destination.Y - buildCommand.StructureType.Size * map.TileSize / 2), buildCommand.StructureType.Size * map.TileSize, buildCommand.StructureType.Size * map.TileSize)));
                                placedStructures.Add(new PlacedStructure(buildCommand.StructureType, new Rectangle(buildCommand.StructureLocation.X * map.TileSize, buildCommand.StructureLocation.Y * map.TileSize, buildCommand.StructureType.Size * map.TileSize, buildCommand.StructureType.Size * map.TileSize), worker.Team));
                            }
                        }
                    }
                }
            }

            foreach (Structure structure in Structure.Structures)
            {
                if (structure.Builder != null)
                {
                    WorkerNublet worker = structure.Builder as WorkerNublet;
                    if (worker != null)
                    {
                        if (worker.Commands.Count > 0)
                        {
                            foreach (UnitCommand command in worker.Commands)
                            {
                                BuildStructureCommand buildCommand = command as BuildStructureCommand;
                                if (buildCommand != null)
                                {
                                    //placedStructures.Add(new ObjectLink<StructureType, Rectangle>(buildCommand.StructureType, new Rectangle((int)(buildCommand.Destination.X - buildCommand.StructureType.Size * map.TileSize / 2), (int)(buildCommand.Destination.Y - buildCommand.StructureType.Size * map.TileSize / 2), buildCommand.StructureType.Size * map.TileSize, buildCommand.StructureType.Size * map.TileSize)));
                                    placedStructures.Add(new PlacedStructure(buildCommand.StructureType, new Rectangle(buildCommand.StructureLocation.X * map.TileSize, buildCommand.StructureLocation.Y * map.TileSize, buildCommand.StructureType.Size * map.TileSize, buildCommand.StructureType.Size * map.TileSize), worker.Team));
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #4
0
        void processUnitStatusUpdate(NetIncomingMessage msg)
        {
            short   unitID   = msg.ReadInt16();
            short   team     = msg.ReadInt16();
            short   hp       = msg.ReadInt16();
            Vector2 position = new Vector2(msg.ReadFloat(), msg.ReadFloat());
            float   rotation = msg.ReadFloat();
            bool    idle     = msg.ReadBoolean();

            Unit unit = Player.Players[team].UnitArray[unitID];

            if (unit != null)
            {
                if (hp < unit.Hp && !unit.IsDead)
                {
                    unit.Hp = hp;
                    if (hp <= 0)
                    {
                        unit.Die();
                    }
                    return;
                }

                //if (unit.IsIdle)
                if (idle)
                {
                    if (unit.IsIdle)
                    {
                        unit.CenterPoint = position;
                    }

                    if (!unit.IsIdle)
                    {
                        unit.NextCommand();
                    }
                }
                else
                {
                    Vector2 expectedPosition = new Vector2(position.X + unit.Speed * connection.AverageRoundtripTime / 2 * (float)Math.Cos(rotation), position.Y + unit.Speed * connection.AverageRoundtripTime / 2 * (float)Math.Sin(rotation));

                    if (Vector2.Distance(expectedPosition, unit.CenterPoint) > unit.Radius)
                    {
                        //unit.CenterPoint -= new Vector2((unit.CenterPoint.X - expectedPosition.X), (unit.CenterPoint.Y - expectedPosition.Y));
                        unit.CenterPoint = expectedPosition;
                    }
                }

                // read current command ID
                int commandID = msg.ReadInt16();
                // if its not the same as our current command, look for it in queue
                if (commandID != -1 && unit.Commands.Count > 0 && commandID != unit.Commands[0].ID)
                {
                    for (int i = 1; i < unit.Commands.Count; i++)
                    {
                        UnitCommand command = unit.Commands[i];
                        if (command.ID == commandID)
                        {
                            // do NextCommand enough times to catch up
                            for (int s = 0; s < i; s++)
                            {
                                unit.NextCommand();
                            }
                        }
                    }
                }

                // read cargoAmount at end if worker
                WorkerNublet worker = unit as WorkerNublet;
                if (worker != null)
                {
                    short cargoAmount = msg.ReadInt16();

                    worker.CargoAmount = cargoAmount;
                }
            }
        }
コード例 #5
0
ファイル: Resource.cs プロジェクト: nubington/rts
        void releaseWorker(WorkerNublet worker)
        {
            worker.CargoType = Type;

            int oldAmount = Amount;
            Amount -= CARGO_PER_TRIP;
            worker.CargoAmount = oldAmount - Amount;

            // only decrement amount if worker is on my team,
            // else rely on status updates for amount
            if (worker.Team != Player.Me.Team)
                Amount = oldAmount;

            //int newAmount = (int)(MathHelper.Max(Amount - CARGO_PER_TRIP, 0));
            //worker.CargoAmount = Amount - newAmount;
            //Amount = newAmount;

            workersInside.Remove(worker);

            TownHall townHall = findNearestTownHall(worker);

            float angle;
            if (townHall == null)
                angle = 0;
            else
                angle = (float)Math.Atan2(townHall.Rectangle.Y - CenterPoint.Y, townHall.Rectangle.X - CenterPoint.X);

            PathNode closestPathNode = null;
            if (townHall != null)
            {
                float closest = float.MaxValue;

                foreach (PathNode pathNode in exitPathNodes)
                {
                    float distance = Vector2.Distance(pathNode.Tile.CenterPoint, townHall.CenterPoint);
                    if (distance < closest)
                    {
                        closestPathNode = pathNode;
                        closest = distance;
                    }
                }
            }

            if (closestPathNode != null)
                worker.CenterPoint = closestPathNode.Tile.CenterPoint;
            else if (exitPathNodes.Count > 0)
                worker.CenterPoint = exitPathNodes[0].Tile.CenterPoint;
            else
                worker.CenterPoint = centerPoint;

            //worker.CenterPoint = centerPoint + new Vector2((Radius + worker.Radius) * (float)Math.Cos(angle), (Radius + worker.Radius) * (float)Math.Sin(angle));
            worker.Rotation = angle;

            worker.FinishHarvesting();

            if (townHall != null && worker.Commands.Count <= 0)
            {
                //worker.GiveCommand(new ReturnCargoCommand(townHall, 1));
                //worker.QueueCommand(new HarvestCommand(this, 1));
                worker.ReturnCargoToNearestTownHall(this);
            }

            checkForStatusUpdate(Rts.netPeer, Rts.connection, worker.Team);
        }
コード例 #6
0
ファイル: Resource.cs プロジェクト: nubington/rts
 void letWorkerEnter(WorkerNublet worker)
 {
     Rts.SelectedUnits.Remove(worker);
     workersInside.Add(worker);
     workerTimes.Add(0);
     worker.CenterPoint = centerPoint;
 }
コード例 #7
0
ファイル: Resource.cs プロジェクト: nubington/rts
        public bool CheckForEntrance(WorkerNublet worker)
        {
            if (worker.CargoAmount == CARGO_PER_TRIP && worker.CargoType == Type)
            {
                TownHall townHall = findNearestTownHall(worker);
                if (townHall != null)
                    worker.InsertCommand(new ReturnCargoCommand(worker, townHall, this));
                else
                    worker.NextCommand();
                return false;
            }

            if (Amount - (workersInside.Count * CARGO_PER_TRIP) <= 0)
                return false;

            if (timeSinceLastEntrance >= allowEntranceDelay)
            {
                timeSinceLastEntrance = 0;

                letWorkerEnter(worker);

                return true;
            }

            return false;
        }
コード例 #8
0
        void completeBuildQueueItem(BuildQueueItem item)
        {
            BuildUnitButtonType buttonType = item.Type as BuildUnitButtonType;

            if (buttonType != null)
            {
                Unit unit;
                if (buttonType.UnitType == UnitType.MeleeNublet)
                {
                    unit = new MeleeNublet(new Vector2(), Team, item.ID);
                }
                else if (buttonType.UnitType == UnitType.RangedNublet)
                {
                    unit = new RangedNublet(new Vector2(), Team, item.ID);
                }
                else
                {
                    unit = new WorkerNublet(new Vector2(), Team, item.ID);
                }

                float angle = 0;

                Vector2 spawnLocation;
                if (rallyPoints.Count > 0)
                {
                    angle = (float)Math.Atan2(rallyPoints[0].Point.Y - CenterPoint.Y, rallyPoints[0].Point.X - CenterPoint.X);
                    angle = Util.ConvertToPositiveRadians(angle);

                    /*if (angle < MathHelper.TwoPi / 4)
                     *  spawnLocation = new Vector2(Rectangle.X + Rectangle.Width - map.TileSize, Rectangle.Y + Rectangle.Height - map.TileSize / 2);
                     * else if (angle < MathHelper.TwoPi / 2)
                     *  spawnLocation = new Vector2(Rectangle.X + map.TileSize, Rectangle.Y + Rectangle.Height - map.TileSize / 2);
                     * else if (angle < MathHelper.TwoPi * .75f)
                     *  spawnLocation = new Vector2(Rectangle.X + map.TileSize, Rectangle.Y + map.TileSize / 2);
                     * else
                     *  spawnLocation = new Vector2(Rectangle.X + Rectangle.Width - map.TileSize, Rectangle.Y + map.TileSize / 2);*/

                    PathNode closestPathNode = null;
                    float    closest         = float.MaxValue;

                    foreach (PathNode pathNode in exitPathNodes)
                    {
                        float distance = Vector2.Distance(pathNode.Tile.CenterPoint, RallyPoints[0].Point);
                        if (distance < closest)
                        {
                            closestPathNode = pathNode;
                            closest         = distance;
                        }
                    }

                    if (closestPathNode != null)
                    {
                        spawnLocation = closestPathNode.Tile.CenterPoint;
                    }
                    else if (exitPathNodes.Count > 0)
                    {
                        spawnLocation = exitPathNodes[0].Tile.CenterPoint;
                    }
                    else
                    {
                        spawnLocation = new Vector2(Rectangle.X + Rts.map.TileSize, Rectangle.Y + Rectangle.Height - Rts.map.TileSize / 2);
                    }
                }
                else
                {
                    spawnLocation = new Vector2(Rectangle.X + Rts.map.TileSize, Rectangle.Y + Rectangle.Height - Rts.map.TileSize / 2);
                }

                unit.CenterPoint = new Vector2(spawnLocation.X, spawnLocation.Y);
                unit.Rotation    = angle;
                unit.InitializeCurrentPathNode();

                if (rallyPoints.Count == 0)
                {
                    unit.CheckForWallHit();
                    unit.CheckForPush();
                }
                else
                {
                    MoveCommand command = null;

                    if (rallyPoints[0].Resource != null && unit is WorkerNublet)
                    {
                        command = new HarvestCommand(unit, rallyPoints[0].Resource);
                    }
                    else
                    {
                        command = new MoveCommand(unit, RallyPoints[0].Point);
                    }

                    if (command != null)
                    {
                        unit.GiveCommand(command);
                        Rts.pathFinder.AddPathFindRequest(command, false, false, false);
                    }

                    for (int i = 1; i < RallyPoints.Count; i++)
                    {
                        if (rallyPoints[i].Resource != null && unit is WorkerNublet)
                        {
                            unit.QueueCommand(new HarvestCommand(unit, rallyPoints[i].Resource));
                        }
                        else
                        {
                            unit.QueueCommand(new MoveCommand(unit, RallyPoints[i].Point));
                        }
                    }

                    unit.CheckForWallHit();
                }
            }
        }
コード例 #9
0
        void releaseWorker(WorkerNublet worker)
        {
            worker.CargoType = Type;

            int oldAmount = Amount;

            Amount            -= CARGO_PER_TRIP;
            worker.CargoAmount = oldAmount - Amount;

            // only decrement amount if worker is on my team,
            // else rely on status updates for amount
            //if (worker.Team != Player.Me.Team)
            //Amount = oldAmount;

            //int newAmount = (int)(MathHelper.Max(Amount - CARGO_PER_TRIP, 0));
            //worker.CargoAmount = Amount - newAmount;
            //Amount = newAmount;

            workersInside.Remove(worker);

            TownHall townHall = findNearestTownHall(worker);

            float angle;

            if (townHall == null)
            {
                angle = 0;
            }
            else
            {
                angle = (float)Math.Atan2(townHall.Rectangle.Y - CenterPoint.Y, townHall.Rectangle.X - CenterPoint.X);
            }

            PathNode closestPathNode = null;

            if (townHall != null)
            {
                float closest = float.MaxValue;

                foreach (PathNode pathNode in exitPathNodes)
                {
                    float distance = Vector2.Distance(pathNode.Tile.CenterPoint, townHall.CenterPoint);
                    if (distance < closest)
                    {
                        closestPathNode = pathNode;
                        closest         = distance;
                    }
                }
            }

            if (closestPathNode != null)
            {
                worker.CenterPoint = closestPathNode.Tile.CenterPoint;
            }
            else if (exitPathNodes.Count > 0)
            {
                worker.CenterPoint = exitPathNodes[0].Tile.CenterPoint;
            }
            else
            {
                worker.CenterPoint = centerPoint;
            }

            //worker.CenterPoint = centerPoint + new Vector2((Radius + worker.Radius) * (float)Math.Cos(angle), (Radius + worker.Radius) * (float)Math.Sin(angle));
            worker.Rotation = angle;

            worker.FinishHarvesting();

            if (townHall != null && worker.Commands.Count <= 0)
            {
                //worker.GiveCommand(new ReturnCargoCommand(townHall, 1));
                //worker.QueueCommand(new HarvestCommand(this, 1));
                worker.ReturnCargoToNearestTownHall(this);
            }

            checkForStatusUpdate(Rts.netPeer, Rts.connection, worker.Team);
        }
コード例 #10
0
ファイル: Structure.cs プロジェクト: nubington/rts
        void completeBuildQueueItem(BuildQueueItem item)
        {
            BuildUnitButtonType buttonType = item.Type as BuildUnitButtonType;
            if (buttonType != null)
            {
                Unit unit;
                if (buttonType.UnitType == UnitType.MeleeNublet)
                    unit = new MeleeNublet(new Vector2(), Team, item.ID);
                else if (buttonType.UnitType == UnitType.RangedNublet)
                    unit = new RangedNublet(new Vector2(), Team, item.ID);
                else
                    unit = new WorkerNublet(new Vector2(), Team, item.ID);

                float angle = 0;

                Vector2 spawnLocation;
                if (rallyPoints.Count > 0)
                {
                    angle = (float)Math.Atan2(rallyPoints[0].Point.Y - CenterPoint.Y, rallyPoints[0].Point.X - CenterPoint.X);
                    angle = Util.ConvertToPositiveRadians(angle);

                    /*if (angle < MathHelper.TwoPi / 4)
                        spawnLocation = new Vector2(Rectangle.X + Rectangle.Width - map.TileSize, Rectangle.Y + Rectangle.Height - map.TileSize / 2);
                    else if (angle < MathHelper.TwoPi / 2)
                        spawnLocation = new Vector2(Rectangle.X + map.TileSize, Rectangle.Y + Rectangle.Height - map.TileSize / 2);
                    else if (angle < MathHelper.TwoPi * .75f)
                        spawnLocation = new Vector2(Rectangle.X + map.TileSize, Rectangle.Y + map.TileSize / 2);
                    else
                        spawnLocation = new Vector2(Rectangle.X + Rectangle.Width - map.TileSize, Rectangle.Y + map.TileSize / 2);*/

                    PathNode closestPathNode = null;
                    float closest = float.MaxValue;

                    foreach (PathNode pathNode in exitPathNodes)
                    {
                        float distance = Vector2.Distance(pathNode.Tile.CenterPoint, RallyPoints[0].Point);
                        if (distance < closest)
                        {
                            closestPathNode = pathNode;
                            closest = distance;
                        }
                    }

                    if (closestPathNode != null)
                        spawnLocation = closestPathNode.Tile.CenterPoint;
                    else if (exitPathNodes.Count > 0)
                        spawnLocation = exitPathNodes[0].Tile.CenterPoint;
                    else
                        spawnLocation = new Vector2(Rectangle.X + Rts.map.TileSize, Rectangle.Y + Rectangle.Height - Rts.map.TileSize / 2);
                }
                else
                {
                    spawnLocation = new Vector2(Rectangle.X + Rts.map.TileSize, Rectangle.Y + Rectangle.Height - Rts.map.TileSize / 2);
                }

                unit.CenterPoint = new Vector2(spawnLocation.X, spawnLocation.Y);
                unit.Rotation = angle;
                unit.InitializeCurrentPathNode();

                if (rallyPoints.Count == 0)
                {
                    unit.CheckForWallHit();
                    unit.CheckForPush();
                }
                else
                {
                    MoveCommand command = null;

                    if (rallyPoints[0].Resource != null && unit is WorkerNublet)
                        command = new HarvestCommand(unit, rallyPoints[0].Resource);
                    else
                        command = new MoveCommand(unit, RallyPoints[0].Point);

                    if (command != null)
                    {
                        unit.GiveCommand(command);
                        Rts.pathFinder.AddPathFindRequest(command, false, false, false);
                    }

                    for (int i = 1; i < RallyPoints.Count; i++)
                    {
                        if (rallyPoints[i].Resource != null && unit is WorkerNublet)
                            unit.QueueCommand(new HarvestCommand(unit, rallyPoints[i].Resource));
                        else
                            unit.QueueCommand(new MoveCommand(unit, RallyPoints[i].Point));
                    }

                    unit.CheckForWallHit();
                }
            }
        }