示例#1
0
    // Update is called once per frame
    public override void Update()
    {
        base.Update();
        bool bWasInDangerOfStarving = AmInDangerOfStarving();
        bool bWasInDangerOfBreaking = AmInDangerOfBreaking();

        afNeeds[(int)Need.FOOD]          -= 0.005f * Time.deltaTime;
        afNeeds[(int)Need.HUMAN_FOOD]    -= 0.0025f * Time.deltaTime;
        afNeeds[(int)Need.ENTERTAINMENT] -= 0.0025f * Time.deltaTime;

        float minNeed = 0.0f;

        if (state == State.WORKING)
        {
            minNeed = 0.1f;
        }

        for (int i = 0; i < 3; i++)
        {
            afNeeds[i] = Mathf.Clamp(afNeeds[i], minNeed, 1.0f);
        }


        if (currentTask == null || currentTask.type != Task.Type.GO_MAD)
        {
            if (!bWasInDangerOfStarving && AmInDangerOfStarving())
            {
                TextTicker.AddLine("<color=orange>" + lizardName + " is starving. Get them some food</color>");
                AbandonTask();
                if (state != State.IDLE)
                {
                    SetState(State.IDLE);
                }
            }
            if (!bWasInDangerOfBreaking && AmInDangerOfBreaking())
            {
                TextTicker.AddLine("<color=orange>" + lizardName + " is going mad. Get them some TV or fancy human food</color>");
                AbandonTask();
                if (state != State.IDLE)
                {
                    SetState(State.IDLE);
                }
            }

            if (ShouldDie())
            {
                AbandonTask();
                TextTicker.AddLine("<color=red>" + lizardName + " died of starvation</color>");
                Core.theTM.lizards[assignment].Remove(this);
                Destroy();
            }

            if (ShouldGoMad())
            {
                if (currentTask == null || currentTask.type != Task.Type.GO_MAD)
                {
                    AbandonTask();
                    TextTicker.AddLine("<color=red>" + lizardName + " has gone mad and is trying to leave</color>");
                    SetAndLockAnim(climbAnim);
                    currentTask = new Task(Task.Type.GO_MAD);
                    DoTask();
                }
            }
        }

        switch (state)
        {
        case State.IDLE:
            if (ShouldEat())
            {
                // Check to see if there are reachable storerooms
                TileManager.TestTile del = delegate(TileBase tile)
                {
                    return(tile.FindResource(Resource.ResourceType.MUSHROOMS) != null || tile.FindResource(Resource.ResourceType.HUMAN_FOOD) != null);
                };

                var pathToFoods = Path.GetPath(currentTile.GetKVPair(), mgr.GetTiles(del));
                if (pathToFoods != null)
                {
                    TileBase targetTile = mgr.GetTileBase(pathToFoods.endX, pathToFoods.endY);
                    foreach (Resource res in targetTile.clutteredResources)
                    {
                        if (!res.isClaimed && (res.type == Resource.ResourceType.MUSHROOMS || res.type == Resource.ResourceType.HUMAN_FOOD))
                        {
                            res.Claim(this);
                            break;
                        }
                    }
                    if (claimed == null)
                    {
                        foreach (Resource res in targetTile.tidyResources)
                        {
                            if (res != null && !res.isClaimed && (res.type == Resource.ResourceType.MUSHROOMS || res.type == Resource.ResourceType.HUMAN_FOOD))
                            {
                                res.Claim(this);
                                break;
                            }
                        }
                    }

                    if (claimed != null)
                    {
                        AbandonTask();
                        SetState(State.RETRIEVING_RESOURCE);
                        currentTask = new Task(Task.Type.EAT);
                        SetPath(pathToFoods);
                        break;
                    }
                }
            }
            if (ShouldFindEntertainment())
            {
                // Check to see if there are reachable storerooms
                TileManager.TestTile del = delegate(TileBase tile)
                {
                    return(tile.Type() == TileBase.TileType.TVROOM);
                };

                var pathToTV = Path.GetPath(currentTile.GetKVPair(), mgr.GetTiles(del));
                if (pathToTV != null)
                {
                    TileBase targetTile = mgr.GetTileBase(pathToTV.endX, pathToTV.endY);

                    SetState(State.TRAVELLING_TO_TASK);
                    currentTask = new Task(Task.Type.RELAX);
                    currentTask.associatedTile = targetTile;
                    currentTask.assignedLizard = this;

                    SetPath(pathToTV);
                    break;
                }
            }
            if (!AmInDangerOfStarving() && !AmInDangerOfBreaking())
            {
                if (Player.thePlayer.pendingTasks[(int)assignment].Count != 0)
                {
                    foreach (Task task in Player.thePlayer.pendingTasks[(int)assignment])
                    {
                        // Check to see if this lizard can reach the task
                        if (Path.GetPath(currentTile.GetKVPair(), task.associatedTile) != null)
                        {
                            currentTask = task;
                            break;
                        }
                    }
                    if (currentTask != null)
                    {
                        currentTask.assignedLizard = this;
                        Player.thePlayer.pendingTasks[(int)assignment].Remove(currentTask);
                        DoTask();
                    }

                    break;
                }
                //Debug.Log("Calling GetPath to find dropped resources!");
                var clutterPath = Path.GetPath(currentTile.GetKVPair(), mgr.GetClutteredTiles());
                if (clutterPath != null)
                {
                    // Check to see if there are reachable storerooms
                    TileManager.TestTile del = delegate(TileBase tile)
                    {
                        return(tile.isConstructed && tile.Type() == TileBase.TileType.STORAGE && tile.NEmptyResourceSlots() > 0);
                    };
                    //Debug.Log("Calling GetPath to check for a store room");
                    var storeroomPath = Path.GetPath(currentTile.GetKVPair(), mgr.GetTiles(del));
                    if (storeroomPath == null)
                    {
                        break;
                    }
                    SetState(State.RETRIEVING_RESOURCE);
                    TileBase targetTile = mgr.GetTileBase(clutterPath.endX, clutterPath.endY);
                    foreach (Resource res in targetTile.clutteredResources)
                    {
                        if (!res.isClaimed)
                        {
                            res.Claim(this);
                            break;
                        }
                    }
                    SetPath(clutterPath);
                    break;
                }

                fIdleTime -= Time.deltaTime;
                if (!targetSet)
                {
                    if (!currentTile.IsLizardy())
                    {
                        TileManager.TestTile del = delegate(TileBase tile) { return(tile.IsLizardy()); };
                        if (!SetPath(Path.GetPath(currentTile.GetKVPair(), currentTile.GetAdjacentTiles(del))))
                        {
                            AbandonTask();
                            TextTicker.AddLine("<color=red>" + lizardName + " was crushed</color>");
                            Core.theTM.lizards[assignment].Remove(this);
                            Destroy();
                        }
                    }
                    else if (fIdleTime < 0)
                    {
                        SetTarget(GetTileCenter(currentTile) + new Vector3(Random.Range(-0.35f, 0.35f), 0.0f, 0.0f));
                    }
                }
                else if (Move())
                {
                    // Set a cooldown timer
                    fIdleTime = Random.Range(1.0f, 5.0f);
                }
            }
            break;

        case State.TRAVELLING_TO_TASK:
            if (Move())
            {
                if (currentTask.type == Task.Type.GO_MAD)
                {
                    TextTicker.AddLine("<color=red>" + lizardName + " went mad and escaped!</color>");
                    Core.theTM.lizards[assignment].Remove(this);
                    Player.thePlayer.AddSuspicion(25.0f);
                    deathClip = null;
                    Destroy();
                }
                else
                {
                    if (currentTask.associatedTile != null)
                    {
                        currentTask.associatedTile.SetTaskActive(true);
                    }
                    SetState(State.WORKING);
                }
            }
            break;

        case State.WORKING:
            switch (currentTask.type)
            {
            case Task.Type.BUILD:
                currentTask.UseResources();
                if (currentTile.Build(this))
                {
                    FinishTask();
                    UI_HUD.instance.PlansFinished(currentTile.x, currentTile.y);

                    if (currentTile.Type() == TileBase.TileType.FILLED)
                    {
                    }
                    SetState(State.IDLE);
                }
                break;

            case Task.Type.BREED:
                if (currentTile is Hatchery)
                {
                    if ((currentTile as Hatchery).Breed(this))
                    {
                        FinishTask();
                        SetState(State.IDLE);
                    }
                }
                break;

            case Task.Type.FARM:
                if (currentTile is MushroomFarm)
                {
                    if ((currentTile as MushroomFarm).Farm())
                    {
                        FinishTask();
                        SetState(State.IDLE);
                    }
                }
                break;

            case Task.Type.TRAP:
                if (currentTile is Trap)
                {
                    if ((currentTile as Trap).Farm())
                    {
                        FinishTask();
                        SetState(State.IDLE);
                    }
                }
                break;

            case Task.Type.TAILOR:
                if (currentTile is Tailor)
                {
                    if ((currentTile as Tailor).Farm())
                    {
                        FinishTask();
                        SetState(State.IDLE);
                    }
                }
                break;

            case Task.Type.RELAX:
                if (currentTile is TVRoom)
                {
                    afNeeds[(int)Need.ENTERTAINMENT] += 0.1f * Time.deltaTime;
                    (currentTile as TVRoom).IncrementTVBill(0.1f * Time.deltaTime);
                    if (afNeeds[(int)Need.ENTERTAINMENT] > 0.9f)
                    {
                        FinishTask();
                        SetState(State.IDLE);
                    }
                }
                break;

            case Task.Type.SELL_RESOURCE:
                currentTask.UseResources();
                int value = 0;
                foreach (KeyValuePair <Resource.ResourceType, int> count in currentTask.requiredResources)
                {
                    value += Player.thePlayer.GetValue(count.Key) * count.Value;
                }
                Player.thePlayer.money += value;
                FinishTask();
                // Maybe put in some animation stuff in time? James?
                // e.g. transport the resource up to the actual hut
                SetState(State.IDLE);
                break;

            case Task.Type.EAT:
            case Task.Type.WORK_ROOM:
            case Task.Type.GO_MAD:
                FinishTask();
                break;
            }
            break;

        case State.RETRIEVING_RESOURCE:
            if (Move())
            {
                if (currentTask == null)
                {
                    // Find a storeroom with an empty slot
                    TileManager.TestTile del = delegate(TileBase tile)
                    {
                        return(tile.Type() == TileBase.TileType.STORAGE && tile.NEmptyResourceSlots() > 0);
                    };
                    //Debug.Log("Calling GetPath to go to a storeroom");
                    var storePath = Path.GetPath(currentTile.GetKVPair(), mgr.GetTiles(del));
                    if (storePath == null)
                    {
                        SetState(State.IDLE);
                        break;
                    }
                    else
                    {
                        SetPath(storePath);
                    }
                }
                else if (currentTask.associatedTile == null)
                {
                    // No tile. Must be a self task
                    switch (currentTask.type)
                    {
                    case Task.Type.EAT:
                        Consume(claimed);
                        break;

                    default:
                        Debug.Assert(false, "This task should have an associated tile");
                        break;
                    }
                    SetState(State.IDLE);
                    return;
                }
                else
                {
                    // Debug.Log("Calling GetPath to go to the target tile");
                    var tilePath = Path.GetPath(currentTile.GetKVPair(), currentTask.associatedTile);
                    if (tilePath == null)
                    {
                        AbandonTask();
                    }
                    else
                    {
                        SetPath(tilePath);
                    }
                }

                if (claimed != null)
                {
                    claimed.GiveToLizard(this);
                    SetState(State.RETURNING_RESOURCE);
                }
                else
                {
                    // Someone nicked it!
                    SetState(State.IDLE);
                }
            }
            break;

        case State.RETURNING_RESOURCE:
            if (Move())
            {
                if (currentTask == null)
                {
                    //Debug.Log("Calling StoreResource");
                    carrying.PutInRoom(currentTile);

                    SetState(State.IDLE);
                }
                else
                {
                    currentTask.AddResource(carrying);
                    DoTask();
                }
            }
            break;
        }
    }
示例#2
0
    // Update is called once per frame
    public override void Update()
    {
        base.Update();
        switch (phase)
        {
        case Phase.MOVE:
            if (Mathf.Abs(transform.position.x - (iTargetX - TileManager.width / 2 + 0.5f)) > 0.1f)
            {
                // Walk to target
                transform.localPosition += new Vector3(Time.deltaTime * fSpeed * (bFlip ? -1.0f : 1.0f), 0.0f, 0.0f);
            }
            else
            {
                transform.localPosition = new Vector3(iTargetX - TileManager.width / 2 + 0.45f, 0.625f, -3.0f);
                SetAnim(unloadAnim);
                fTimeUntilNextPhase = 5.0f;
                phase = Phase.UNLOAD;
            }
            break;

        case Phase.UNLOAD:
            fTimeUntilNextPhase -= Time.deltaTime;
            if (fTimeUntilNextPhase <= 0.0f)
            {
                SetAnim(drillAnim);
                phase = Phase.DRILL;
                // Redo based on player depth and duration of game so far
                iTargetDepth = Random.Range(1, 7);

                // Spawn plans
                for (int i = 0; i < iTargetDepth; i++)
                {
                    Transform plans = Instantiate <Transform>(drillPlansPrefab);
                    plans.SetParent(transform);
                    plans.localPosition = new Vector3(0.05f, -1.03125f - i, 0.2f);

                    drillPlans.Add(plans);
                }

                Transform end = Instantiate <Transform>(drillPlansEndPrefab);
                end.SetParent(transform);
                end.localPosition = new Vector3(0.05f, -1.03125f - iTargetDepth, 0.2f);

                drillPlans.Add(end);

                drillBit = Instantiate <Transform>(drillBitEndPrefab);
                drillBit.SetParent(transform);
                drillBit.localPosition = new Vector3(0.05f, -0.625f, 0.1f);

                drillPlans.Add(drillBit);

                // Warn!
                for (int i = 0; i < iTargetDepth; i++)
                {
                    TileBase tb = Core.theTM.GetTileBase(iTargetX, i);

                    if (!tb.IsLizardy() && tb.Type() != TileBase.TileType.FILLED)
                    {
                        // Something solid is in the way
                        iTargetDepth = i + 1;
                    }

                    tb.bWarning = true;
                    if (tb.IsLizardy())
                    {
                        TextTicker.AddLine("Warning: Humans are about to drill into our " + tb.printName);
                    }
                }
            }
            break;

        case Phase.DRILL:

            // Do some drilling
            fDrillProgress += fDrillSpeed * Time.deltaTime;

            int   iDrillProgress = Mathf.FloorToInt(fDrillProgress);
            float fRemainder     = fDrillProgress - iDrillProgress;
            if (iDrillProgress >= drills.Count)
            {
                drills.Add(Instantiate <SpriteRenderer>(drillBitPrefab));
                topDrill = drills[drills.Count - 1];
                topDrill.transform.SetParent(transform);

                // We just hit a new spot.

                if (iDrillProgress >= 1)
                {
                    if (!Core.theTM.HumanDigTile(iTargetX, iDrillProgress - 1))
                    {
                        CeaseDrilling();
                    }
                }

                if (iDrillProgress > iTargetDepth)
                {
                    CeaseDrilling();
                }
            }

            for (int i = 0; i < drills.Count; i++)
            {
                drills[i].transform.localPosition = new Vector3(0.05f, -0.03125f - i - fRemainder, 0.1f);
            }

            drillBit.localPosition = new Vector3(0.05f, -0.625f - fDrillProgress, 0.1f);

            break;

        case Phase.LEAVE:
            transform.localPosition += new Vector3(Time.deltaTime * fSpeed * (bFlip ? -1.0f : 1.0f), 0.0f, 0.0f);
            if (transform.position.x > 12.0f || transform.position.x < -12.0f)
            {
                Destroy(gameObject);
            }
            break;
        }
    }