Esempio n. 1
0
 public void AbandonJob()
 {
     NextTile  = DestTile = CurrTile;
     PathAStar = null;
     CurrTile.world.MyJobQueue.Enqueue(MyJob);
     MyJob = null;
 }
Esempio n. 2
0
    void GetNewJob()
    {
        //grab a new job
        myJob = currTile.world.jobQueue.Dequeue();

        if (myJob == null)
        {
            return;
        }

        destTile = myJob.tile;
        myJob.RegisterJobCancelCallback(OnJobEnded);
        myJob.RegisterJobCompleteCallback(OnJobEnded);

        //Check if the job tile is reachable.
        //Note we might not be pathing to it right away due to materials
        //but we still need to verifiy that the final location can be reached
        //generate path to our destination
        pathAStar = new PathAStar(currTile.world, currTile, destTile); // this will calculate a path from current to destination
        if (pathAStar.Length() == 0)
        {
            Debug.LogError("PathAStar returned no path to target job tile");
            AbandonJob();
            destTile = currTile;
        }
    }
Esempio n. 3
0
    void Update_DoMovement(float deltaTime)
    {
        if (currTile == destTile)
        {
            pathAStar = null;
            return; // We're already were we want to be.
        }

        if (nextTile == null || nextTile == currTile)
        {
            // Get the next tile from the pathfinder.
            if (pathAStar == null || pathAStar.Length() == 0)
            {
                // Generate a path to our destination
                pathAStar = new PathAStar(World.world, currTile, destTile); // This will calculate a path from curr to dest.
                if (pathAStar.Length() == 0)
                {
                    Debug.LogError("Path_AStar returned no path to destination!");
                    pathAStar = null;
                    return;
                }
            }

            // Grab the next waypoint from the pathing system!
            nextTile = pathAStar.Dequeue();

            if (nextTile == currTile)
            {
                Debug.LogError("Update_DoMovement - nextTile is currTile?");
            }
        }

        float distToTravel = Mathf.Sqrt(
            Mathf.Pow(currTile.x - nextTile.x, 2) +
            Mathf.Pow(currTile.y - nextTile.y, 2)
            );


        // How much distance can be travel this Update?
        float distThisFrame = speed * deltaTime;

        // How much is that in terms of percentage to our destination?
        float percThisFrame = distThisFrame / distToTravel;

        // Add that to overall percentage travelled.
        movementPercentage += percThisFrame;

        if (movementPercentage >= 1)
        {
            // We have reached our destination

            // TODO: Get the next tile from the pathfinding system.
            //       If there are no more tiles, then we have TRULY
            //       reached our destination.

            currTile           = nextTile;
            movementPercentage = 0;
            // FIXME?  Do we actually want to retain any overshot movement?
        }
    }
Esempio n. 4
0
    //public Nanite( int territory, WorldController world )
    //{
    //    Territory = territory;
    //    _world = world;
    //}

    private void Start()
    {
        _methodHandler   = new Methods(this);
        AvailableMethods = new List <Methods.Methodtype> {
            Methods.Methodtype.CheckTerritory
        };
        TerritoryTiles = new List <Tile>();
        DoStoredJobs();
        ReloadTerritory();
        CurrentTile = WorldController.Instance.GetTileAtWorldCoord(transform.position);
        NextTile    = null;
        _pathAStar  = null;
        World       = WorldController.Instance.world;
    }
Esempio n. 5
0
    //private void Update_DoMovement(float deltaTime) {
    //    if (currTile == destTile) {
    //        path = null;
    //        return;
    //    }

    //    if (nextTile == null || nextTile == currTile) {
    //        if (path == null || path.Length() == 0)
    //        {
    //            GeneratePath();
    //            if (path.Length() == 0) {
    //                Debug.Log("No path returned by A*!");
    //                AbandonJob();
    //                path = null;
    //                return;
    //            }
    //            nextTile = path.Dequeue();
    //        }
    //        //grab the next tile!
    //        nextTile = path.Dequeue();
    //    }


    //    float distToTravel = Mathf.Sqrt(Mathf.Pow(currTile.X - destTile.X, 2) + Mathf.Pow(currTile.Y - destTile.Y, 2));


    //    if (nextTile.movementCost == 0) {
    //        Debug.LogError("FIXME: A character has tried to enter an unwalkable tile.");
    //        nextTile = null;
    //        path = null;

    //    }


    //    float distThisFrame = speed /nextTile.movementCost * deltaTime;

    //    float percThisFrame = distThisFrame / distToTravel;

    //    movementPercentage += percThisFrame;

    //    if (movementPercentage >= 1) {
    //        currTile = destTile;
    //        movementPercentage = 0;
    //        return;
    //    }

    //    //Debug.Log("Im updating " + currTile + " " + destTile + " " + distThisFrame + "/" + distToTravel + " " + movementPercentage + "%" );

    //}


    public void AbandonJob()
    {
        nextTile = null;
        destTile = null;
        path     = null;
        currTile.world.jobQueue.Enqueue(myJob); // was "Requeue" ..reurn the job to the queue.
        myJob.UnregisterJobCancelCallback(OnJobEnded);
        myJob.UnregisterJobCompleteCallback(OnJobEnded);
        myJob = null;

        if (inventory != null)
        {
            currTile.world.inventoryManager.PlaceInventory(currTile, inventory);
        }
    }
Esempio n. 6
0
    //private void Update_DoJob(float deltaTime) {
    //    if (myJob == null) {
    //        myJob = currTile.world.jobQueue.Dequeue();
    //        if (myJob != null) {
    //            SetDestination(myJob.tile);
    //            myJob.RegisterJobCancelCallback(OnJobEnded);
    //            myJob.RegisterJobCompleteCallback(OnJobEnded);
    //        }
    //    }

    //    if (currTile == destTile) {
    //        if (myJob != null) {
    //            myJob.DoWork(deltaTime);
    //        }
    //        path = null;
    //    }

    //}


    private void GeneratePath()
    {
        if (currTile != destTile)
        {
            path = new PathAStar(currTile.world, currTile, destTile);
            if (cbPathChanged != null)
            {
                cbPathChanged(path.path);
            }
        }
        else if (currTile == destTile)
        {
            //we're there! but do i need to do anything?
            Debug.Log("On the tile!");
        }
    }
Esempio n. 7
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3      poss = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            MoveableTile tile = World.world.getTile((int)(poss.x * 4), (int)(poss.y * 4));
            if (tile != null)
            {
                destTile  = tile;
                pathAStar = null;
            }
        }

        Update_DoMovement(Time.deltaTime);
        //Vector3 pos = World.world.tilemap.CellToWorld(new Vector3Int((int)(X / 4), (int)(Y / 4), 0));
        go.transform.position = new Vector3((X / 4), (Y / 4), 0);
    }
Esempio n. 8
0
    void Update_DoJob(float deltaTime)
    {
        // Do I have a job?
        if (myJob == null)
        {
            // Grab a new job.
            GetNewJob();

            if (myJob == null)
            {
                //I couldn't get a new job.
                //Debug.Log("Character:- No Job Available");
                path     = null;
                destTile = null;
                return;
            }
        }
        // We have a job! And it's reachable.

        if (myJob.HasAllMaterials() == false)
        {
            GetJobMaterials();
            return; // we can't get further until we have all items
        }

        // Are we there yet and we can't get here unless the job has all items.
        if (currTile.IsNeighbour(myJob.tile) || currTile == myJob.tile)
        {
            //if ((Mathf.Abs(x - myJob.tile.X) <= 1 && (Mathf.Abs(y - myJob.tile.Y) <= 1))) {
            myJob.DoWork(deltaTime);
            path = null; // stop pathing causde its close enough
            //discard inventory?
            if (inventory != null)
            {
                currTile.world.inventoryManager.PlaceInventory(currTile, inventory);
            }
        }
    }
Esempio n. 9
0
    public void FixedUpdate(float deltaTime, float speed)
    {
        globalState.ExecuteState(this);
        currState.ExecuteState(this);

        if (curr == dest)
        {
            pathfinding = null;
            return;
        }
        if ((next.x == -1 && next.y == -1) || next == curr)
        {
            if (pathfinding == null || pathfinding.Length() == 0)
            {
                pathfinding = new PathAStar(curr, dest);
                if (pathfinding.Length() == 0)
                {
                    // Debug.Log(obj.name  + ": No path to (" + dest.x + ", " + dest.y + ").");
                    pathfinding = null;
                    dest        = curr;
                    return;
                }
            }
            next       = pathfinding.Dequeue();
            distToNext = CityManager.Instance.Distance(curr, next);
        }
        float moveThisFrame = speed * deltaTime;
        float percThisFrame = moveThisFrame / distToNext;

        movePercent += percThisFrame;
        if (movePercent >= 1)
        {
            curr        = next;
            movePercent = 0;
        }
        Move(Vector2.Lerp(curr, next, movePercent));
    }
Esempio n. 10
0
    void Update_DoMovement(float deltaTime)
    {
        if (currTile == destTile || destTile == null || destTile.IsNeighbour(currTile))
        {
            path = null;
            return; // We're already were we want to be.
        }
        if (currTile == null)
        {
            Debug.Log("My current tile is null??");
        }

        if (nextTile == null || nextTile == currTile)
        {
            // Get the next tile from the pathfinder.
            if (path == null || path.Length() == 0)
            {
                // Generate a path to our destination
                GeneratePath(); // This will calculate a path from curr to dest.
                if (path.Length() == 0)
                {
                    Debug.LogError("Path_AStar returned no path to destination!");
                    AbandonJob();
                    path = null;
                    return;
                }
                //knock first tile off list, cause we're already in it!
                nextTile = path.Dequeue();
            }

            // Grab the next waypoint from the pathing system!
            nextTile = path.Dequeue();
            //if (nextTile == currTile) {
            //    //we're now next to our destination "one tile over".
            //    Debug.Log("Update_DoMovement - grabbing next waypoint but nextTile is currTile?" + nextTile + "\n cur: " + currTile);
            //}
        }

        /*		if(pathAStar.Length() == 1) {
         *          return;
         *      }
         */
        // At this point we should have a valid nextTile to move to.

        // What's the total distance from point A to point B?
        // We are going to use Euclidean distance FOR NOW...
        // But when we do the pathfinding system, we'll likely
        // switch to something like Manhattan or Chebyshev distance
        float distToTravel = Mathf.Sqrt(
            Mathf.Pow(currTile.X - nextTile.X, 2) +
            Mathf.Pow(currTile.Y - nextTile.Y, 2)
            );

        switch (nextTile.IsEnterable())
        {
        case ENTERABILITY.Never:
            nextTile = null;
            path     = null;
            return;

        case ENTERABILITY.Soon:

            return;

        case ENTERABILITY.Yes:
            break;
        }


        // How much distance can be travel this Update?
        float distThisFrame = speed / nextTile.movementCost * deltaTime;

        // How much is that in terms of percentage to our destination?
        float percThisFrame = distThisFrame / distToTravel;

        // Add that to overall percentage travelled.
        movementPercentage += percThisFrame;

        if (movementPercentage >= 1)
        {
            // We have reached our destination

            // TODO: Get the next tile from the pathfinding system.
            //       If there are no more tiles, then we have TRULY
            //       reached our destination.

            currTile           = nextTile;
            movementPercentage = 0;
            // FIXME?  Do we actually want to retain any overshot movement?
        }
    }
Esempio n. 11
0
    private void GetNewJob()
    {
        //FIXME: just gets first job.
        if (myJob != null)
        {
            Debug.LogError("Character:- trying tog et a new job even though I have one!");
            return;
        }


        myJob = world.jobQueue.Dequeue();
        if (myJob == null)
        {
            return;
        }


        //If I can't find any items for this job, cancel it!
        string objectType = myJob.GetFirstDesiredInventory().objectType;

        if (objectType != null
            &&
            myJob.canTakeFromStockpile == true
            &&
            world.inventoryManager.GetClosestInventoryOfType(objectType, currTile) == null)
        //trying to include the check here to bin job, becuase no loose items.
        {
            AbandonJob();
            path     = null;
            destTile = currTile;
            return;
        }
        else if (objectType != null
                 &&
                 world.inventoryManager.GetClosestLooseInventoryOfType(objectType, currTile) == null)
        {
            AbandonJob();
            path     = null;
            destTile = currTile;
            return;
        }
        else
        {
            Debug.Log("Character:- GetNewJob() - found some materials, or dont need them!");
        }


        //Same results for either atm.
        myJob.RegisterJobCompleteCallback(OnJobEnded);
        myJob.RegisterJobCancelCallback(OnJobEnded);

        destTile = myJob.tile;

        // check to see if we can reach the job....
        // though maybe this should be done in movement?
        GeneratePath();
        if (path == null || path.Length() == 0)
        {
            Debug.LogError("Could not reach job site!");
            AbandonJob();
            path     = null;
            destTile = currTile;
            return;
        }
    }
Esempio n. 12
0
 public void SetDest(Vector2 dest)
 {
     this.dest   = dest;
     pathfinding = null;
 }
Esempio n. 13
0
    public IEnumerator MoveToTarget(int move)
    {
        if (CurrentTile.X == TargetTile.X && CurrentTile.Z == TargetTile.Z)
        {
            _pathAStar = null;
            // We're already were we want to be.
        }
        else
        {
            if (NextTile == null || CurrentTile.X == TargetTile.X && CurrentTile.Z == TargetTile.Z)
            {
                // Get the next tile from the pathfinder.
                if (_pathAStar == null || _pathAStar.Length() == 0)
                {
                    // Generate a path to our destination
                    _pathAStar = new PathAStar(CurrentTile.world, CurrentTile, TargetTile); // This will calculate a path from curr to dest.
                    if (_pathAStar.Length() == 0)
                    {
                        Debug.LogError("Path_AStar returned no path to destination!");
                    }

                    // Let's ignore the first tile, because that's the tile we're currently in.
                    NextTile = _pathAStar.Dequeue();
                }
                // Grab the next waypoint from the pathing system!
                NextTile = _pathAStar.Dequeue();

                if (NextTile == CurrentTile)
                {
                    Debug.LogError("Update_DoMovement - nextTile is currTile?");
                }
            }
        }
        //if (CurrentTile != TargetTile)
        //{
        //    //Debug.Log(NextTile.X + "-" + NextTile.Z);
        //    NextTile = null;
        //    if (NextTile == null || NextTile == CurrentTile)
        //    {
        //        //get the next tile from the PathFinder
        //        if (_pathAStar == null)
        //        {
        //            //Generate a path to our Destination
        //            _pathAStar = new PathAStar(World, CurrentTile, TargetTile);
        //            if (_pathAStar.Length() == 0)
        //            {
        //                Debug.LogError("The PathAStar returned no Path to Destination");
        //                _pathAStar = null;
        //            }
        //        }
        //        // grab the next waypoint from the Pathing system
        //        if (_pathAStar != null)
        //        {
        //            NextTile = _pathAStar.Dequeue();
        //            Debug.Log("Next Tile : " + NextTile.X + "-" + NextTile.Z);
        //        }
        //        if (NextTile == CurrentTile)
        //        {
        //            Debug.LogError("MoveTaget - NextTile is CurrentTile?");
        //        }
        //    }
        //}
        if (NextTile.X > CurrentTile.X)
        {
            IsMoving = true;
            StartCoroutine(_methodHandler.MoveRight(1));
            //yield return new WaitForFixedUpdate();
        }
        else if (NextTile.X < CurrentTile.X)
        {
            IsMoving = true;
            StartCoroutine(_methodHandler.MoveLeft(1));
            //yield return new WaitForFixedUpdate();
        }
        else if (NextTile.Z > CurrentTile.Z)
        {
            IsMoving = true;
            StartCoroutine(_methodHandler.MoveUp(1));
            //yield return new WaitForFixedUpdate();
        }
        else if (NextTile.Z < CurrentTile.Z)
        {
            IsMoving = true;
            StartCoroutine(_methodHandler.MoveDown(1));
            //yield return new WaitForFixedUpdate();
        }
        if (CurrentTile.X != TargetTile.X && CurrentTile.Z != TargetTile.Z)
        {
            AddMovetoTarget();
        }
        else
        {
            _pathAStar = null;
            WorldController.Instance.ChangeTileColor(TargetTile, Color.white);
            //Debug.Log("Reached Destination: " + TargetTile.X + "-" + TargetTile.Z);
            //Debug.Log("Current Location: " + CurrentTile.X + "-" + CurrentTile.Z);
            TargetTile = null;
            IsMoving   = false;
        }
        //Debug.Log("Current Tile: " + CurrentTile.X + "-" + CurrentTile.Z);
        yield return(new WaitForSeconds(1));
    }
Esempio n. 14
0
    void UpdateDoMovement(float deltaTime)
    {
        if (CurrTile == DestTile)
        {
            PathAStar = null;
            return; // We're already were we want to be.
        }

        // currTile = The tile I am currently in (and may be in the process of leaving)
        // nextTile = The tile I am currently entering
        // destTile = Our final destination -- we never walk here directly, but instead use it for the pathfinding

        if (NextTile == null || NextTile == CurrTile)
        {
            // Get the next tile from the pathfinder.
            if (PathAStar == null || PathAStar.Length() == 0)
            {
                // Generate a path to our destination
                PathAStar = new PathAStar(CurrTile.world, CurrTile, DestTile); // This will calculate a path from curr to dest.
                if (PathAStar.Length() == 0)
                {
                    //Debug.LogError("Path_AStar returned no path to destination!");
                    AbandonJob();
                    PathAStar = null;
                    return;
                }

                // Let's ignore the first tile, because that's the tile we're currently in.
                NextTile = PathAStar.Dequeue();
            }


            // Grab the next waypoint from the pathing system!
            NextTile = PathAStar.Dequeue();

            if (NextTile == CurrTile)
            {
                //Debug.LogError("Update_DoMovement - nextTile is currTile?");
            }
        }

        /*		if(pathAStar.Length() == 1) {
         *          return;
         *      }
         */
        // At this point we should have a valid nextTile to move to.

        // What's the total distance from point A to point B?
        // We are going to use Euclidean distance FOR NOW...
        // But when we do the pathfinding system, we'll likely
        // switch to something like Manhattan or Chebyshev distance
        float DistToTravel = Mathf.Sqrt(
            Mathf.Pow(CurrTile.X - NextTile.X, 2) +
            Mathf.Pow(CurrTile.Y - NextTile.Y, 2)
            );

        if (NextTile.IsEnterable() == ENTERABILITY.Never)
        {
            // Most likely a wall got built, so we just need to reset our pathfinding information.
            // FIXME: Ideally, when a wall gets spawned, we should invalidate our path immediately,
            //		  so that we don't waste a bunch of time walking towards a dead end.
            //		  To save CPU, maybe we can only check every so often?
            //		  Or maybe we should register a callback to the OnTileChanged event?
            //Debug.LogError("FIXME: A character was trying to enter an unwalkable tile.");
            NextTile  = null;   // our next tile is a no-go
            PathAStar = null;   // clearly our pathfinding info is out of date.
            return;
        }
        else if (NextTile.IsEnterable() == ENTERABILITY.Soon)
        {
            // We can't enter the NOW, but we should be able to in the
            // future. This is likely a DOOR.
            // So we DON'T bail on our movement/path, but we do return
            // now and don't actually process the movement.
            return;
        }

        // How much distance can be travel this Update?
        float distThisFrame = Speed / NextTile.movementCost * deltaTime;

        // How much is that in terms of percentage to our destination?
        float percThisFrame = distThisFrame / DistToTravel;

        // Add that to overall percentage travelled.
        MovementPercentage = MovementPercentage + percThisFrame;

        if (MovementPercentage >= 1)
        {
            // We have reached our destination

            // TODO: Get the next tile from the pathfinding system.
            //       If there are no more tiles, then we have TRULY
            //       reached our destination.

            CurrTile           = NextTile;
            MovementPercentage = 0;
            // FIXME?  Do we actually want to retain any overshot movement?
        }
    }
Esempio n. 15
0
    void UpdateDoMovement(float deltaTime)
    {
        if (currTile == destTile)
        {
            pathAStar = null;
            return; // we are already there
        }

        //Curr tile is the tile i'm currently in
        //NextTile is the tile I'm currently entering
        //DestTile = final destination

        if (nextTile == null || nextTile == currTile)
        {
            //get the next tile from the path finder;
            if (pathAStar == null || pathAStar.Length() == 0)
            {
                //generate path to our destination
                pathAStar = new PathAStar(currTile.world, currTile, destTile); // this will calculate a path from current to destination
                if (pathAStar.Length() == 0)
                {
                    Debug.LogError("PathAStar returned no path to destination");
                    AbandonJob();
                    return;
                }
                //Lets ignore the first tile because we are already there
                nextTile = pathAStar.Dequeue();
            }

            //Grab the next waypoint
            nextTile = pathAStar.Dequeue();
            if (nextTile == currTile)
            {
                Debug.LogError("UpdateDoMovement - Next tile is curr tile");
            }
        }

        //if(pathAStar.Length() == 1)
        //{
        //    return;
        //}

        //At this point we should have a valid tile to move to
        //Whats the total distance from point a to point b
        float distToTravel = Mathf.Sqrt(Mathf.Pow(currTile.X - nextTile.X, 2) + Mathf.Pow(currTile.Y - nextTile.Y, 2));

        if (nextTile.IsEnterable() == Enterability.Never)
        {
            //Most likely a wall got built so we just need to reset our pathfinding
            //Fixme -- when a wall gets spawned we should invalidate our path immediatly so we don't
            // waste time walking twards a dead end, to save cpu maybe we can onlny check every so often
            // Maybe register a callback to the ontilechanged event
            Debug.LogError("Fixme: a character was trying to enter an unwalkable tile");
            nextTile  = null; //our next tile is not walkable
            pathAStar = null; //our pathfinding info is out of date
            return;
        }
        else if (nextTile.IsEnterable() == Enterability.Soon)
        {
            //Debug.Log("WE are waiting for the door to open");
            //Tile is technically walkable but are we actually allowed to enter it right now
            return;
        }

        //How much distance can we travel this update?
        float distThisFrame = speed / nextTile.movementCost * deltaTime;
        //How much is that in terms of percentage to our destination?
        float percThisFram = distThisFrame / distToTravel;

        //Add that to overall percentage traveled
        movementPercentage += percThisFram;
        if (movementPercentage >= 1)
        {
            //We have reached our destination

            //ToDo: Get the next Tile from the pathfinding system.
            //If there are no more tiles then we have reached our destination
            currTile           = nextTile;
            movementPercentage = 0;
            //Fixme do we want to retain any overshot movement?
        }
    }