コード例 #1
0
ファイル: Pawn.cs プロジェクト: gabriel-jones/Cartel
        void UpdateMovement(float deltaTime)
        {
            if (IsIdling && destinationCell == currentCell)
            {
                if (idleCooldown <= 0)
                {
                    Cell   randomCell = null;
                    Random rand       = new Random();
                    while (randomCell == null)
                    {
                        int x = currentCell.X;
                        int y = currentCell.Y;
                        x         += rand.Next(-2, 2);
                        y         += rand.Next(-2, 2);
                        randomCell = world.GetCellAt(x, y);
                    }
                    pathfinder      = new PathfinderAStar(world, currentCell, randomCell);
                    destinationCell = pathfinder.LastCell();
                    idleCooldown    = 3f;
                }
                else
                {
                    idleCooldown -= deltaTime;
                }
                return;
            }

            // If pawn is at destination, remove pathfinder
            if (currentCell == destinationCell)
            {
                pathfinder = null;
                isIdling   = currentJob == null;
                return;
            }

            // Calculate next cell to go to, if there isn't one already in progress
            if (nextCell == null || nextCell == currentCell)
            {
                if (pathfinder == null || pathfinder.Length == 0)
                {
                    bool withNeighbors = currentJob != null && currentJob.cell == destinationCell;
                    pathfinder = FindPathToCurrentJob(withNeighbors);
                    if (pathfinder == null)
                    {
                        if (!IsIdling)
                        {
                            AbandonJob();
                        }
                        return;
                    }
                }
                NextCell = pathfinder.Dequeue();
            }

            // Increase movement to destination cell
            int   dX             = currentCell.X - nextCell.X;
            int   dY             = currentCell.Y - nextCell.Y;
            float travelDistance = (float)Math.Sqrt(Math.Pow(dX, 2) + Math.Pow(dY, 2));

            float frameDistance   = (speed * (IsIdling ? 0.5f : 1f)) / nextCell.MovementCost * deltaTime;
            float framePercentage = frameDistance / travelDistance;

            movementPercentage += framePercentage;

            if (movementPercentage >= 1)
            {
                CurrentCell        = nextCell;
                movementPercentage = 0f;                 // TODO: set to 0? test this out
            }
        }
コード例 #2
0
ファイル: Pawn.cs プロジェクト: gabriel-jones/Cartel
        void UpdateJob(float deltaTime)
        {
            // Find Job
            if (currentJob == null && IsIdling)
            {
                jobSearchCooldown -= deltaTime;
                if (jobSearchCooldown > 0)
                {
                    return;
                }

                FindJob();

                if (currentJob == null)
                {
                    jobSearchCooldown = 0.5f;
                    //destinationCell = currentCell;
                    return;
                }
            }

            if (!IsIdling && currentJob == null)
            {
                return;
            }

            if (!currentJob.MeetsBuildRequirements())
            {
                SoftObject next = currentJob.NextRequirement();
                if (carrying != null)                                                                                    // We are carrying something
                {
                    if (carrying.Type == next.Type)                                                                      // We are carrying the thing that's needed
                    {
                        if (currentCell == currentJob.cell || currentCell.GetNeighbors(false).Contains(currentJob.cell)) // We are in the job cell, TODO: understand / fix everything in this branch not sure it works with count and stackCount for soft objects
                        {
                            currentJob.DepositSoftObject(carrying);
                            carrying.ReduceCount(next.Count);
                            if (carrying.Count == 0)
                            {
                                carrying = null;
                            }
                            else
                            {
                                world.DropSoftObject(this, currentCell, Carrying.Count);
                            }
                        }
                        else                             // We aren't in the job cell, keep moving towards the destination
                        {
                            destinationCell = currentJob.cell;
                            return;
                        }
                    }
                    else                         // We don't have required inventory, drop current thing
                    {
                        world.DropSoftObject(this, currentCell, Carrying.Count);
                    }
                }
                else                                                                                // We aren't carrying anything
                {
                    if (currentCell.SoftObject != null && currentCell.SoftObject.Type == next.Type) // We're standing on top of a cell with required inventory
                    {
                        world.PickupSoftObject(this, currentCell, currentCell.SoftObject.Count);
                    }
                    else                             // Need to walk to nearest required inventory
                    {
                        if (currentCell != nextCell) // We're moving somewhere rn so keep doing that
                        {
                            return;
                        }

                        Cell lastCell = pathfinder == null ? null : pathfinder.LastCell();
                        if (pathfinder == null || lastCell == null || lastCell.SoftObject == null || lastCell.SoftObject.Type != next.Type)                           // we ain't going somewhere useful so recalculate
                        {
                            PathfinderAStar newPathfinder = world.PathForNearestSoftObject(next.Type, currentCell);
                            if (newPathfinder == null || newPathfinder.Length == 0)
                            {
                                Console.WriteLine("Abandoning because pathfinder unsuccessful for soft object search");
                                AbandonJob();
                                return;
                            }
                            pathfinder      = newPathfinder;
                            destinationCell = pathfinder.LastCell();
                            NextCell        = pathfinder.Dequeue();
                        }
                    }
                }
                return;
            }

            // Work Job
            // destinationCell = currentJob.cell;
            if (currentCell == currentJob.cell || currentCell.GetNeighbors(false).Contains(currentJob.cell))
            {
                currentJob.Progress(deltaTime, this);
            }
        }