コード例 #1
0
ファイル: Agent.cs プロジェクト: urthegame/main
    /*
     * globaalaas uzvediibas FSM (finite-state-machine)
     * @todo -- stack baased ? ---paartaisiishu, ja atradiishu vismaz 12 iemeslus
     *
     */
    private void thinkAbout()
    {
        Room room = null;

        switch(CurrentState) {
        //---------------------------------------------------------------
        case AgentStates.idling:
            isCraving = -1; //nemeklee nekaadu resursu
            workUnit = null;

            int numResourceShortages = Needs.Shortage.Count(c => c); // ezoteeriskaa veidaa izskaita cik ir TRUE shajaa masiivaa - tik mums ir resursu, kas iet uz galu un jaaiet tos papildinaat
            if(numResourceShortages > 0){ //meklees sev resursus
                CurrentState = AgentStates.choosingResourceDestination;

            } else if(idlingFor > 0){ //pagaidiis bezdarbiibaa

                idlingFor -= Time.deltaTime;
                //bodyAnimator.SetBool("waving", true);

            } else if(workManagerScript.IsThereWorkAvailable()) { //ir kaads darbinsh pieejams
            //    print("ir kaads darbinsh");
                CurrentState = AgentStates.choosingWorkDestination;

            } else { //ja nav jaameklee resursus, tad izveeleesies nejaushu galapunktu un tur pagaidiis
                CurrentState = AgentStates.choosingRandomDestination;
                idlingFor = Random.Range(1, 3);
                //bodyAnimator.SetBool("waving", false);

               // print("STATE:idling + choseRND");
            }
            //print("STATE:idling");
            break;
            //---------------------------------------------------------------
        case AgentStates.choosingRandomDestination:

            room = levelscript.roomWhereIcanDoThis(-1); //dabuus nejaushu telpa

            if(room != null) {
                Vector2 rc = levelscript.randomCubeInThisRoom(room); //randomcube - nejaushss kubiks atrastajaa telpaa
                if(GoThere(rc.x, rc.y) == -1){ // kljuuda - agjents neatrodas uz grida
                    CurrentState = AgentStates.offTheGrid;
                    break;
                }

             //   print("rooom "  + Mathf.RoundToInt(room.transform.position.x) + ", " + Mathf.RoundToInt(room.transform.position.y) + "randCube "  + Mathf.RoundToInt(rc.x) + ", " + Mathf.RoundToInt(rc.y));
              // print("RANDrooom "  + (room.transform.position.x) + ", " + (room.transform.position.y) + "   RANDcube "  + (rc.x) + ", " + (rc.y));

                CurrentState = AgentStates.traveling;
            } else {
                CurrentState = AgentStates.idling; //nav pat 1 nejaushas telpas, nu tad neko - turpina gaidiit
            }

            //print("STATE:choosingRandomDestination");
            break;
            //---------------------------------------------------------------
        case AgentStates.choosingResourceDestination:
            //jaaatrod TUVAAKAA telpa, kas apmierina vajadziibu peec resursa

            for(int need = 0; need < AgentNeeds.numTypes; need++) { //iet cauri visaam agjentvajadziibaam
                if(Needs.Shortage[need]){ //shis resurss truukst, to ir jaaiet mekleet | pirmaas vajadziibas ir svariigaakas (jo taas apskata sekvencionaali, varbuut vajag randomizeet seciibu ikreizi ?)

                    room = levelscript.roomWhereIcanDoThis(need);
                    if(room != null){ //ir atrasta sho vajadziibu apmierinosha telpa
                        isCraving = need; //pieseivos kuru vajadziibu ies apmierinaat - lai zinaatu, ka jaait prom, tikliidz TAA ir apmierinaata
                        break; //neapskata paareejaas vajadziibas
                    }
                }
            }

            if(room != null){  //jaaiet uz atrasto telpu
                Vector2 rc = levelscript.randomCubeInThisRoom(room); //randomcube - nejaushss kubiks atrastajaa telpaa
                if(GoThere(rc.x, rc.y) == -1){ // kljuuda - agjents neatrodas uz grida
                    CurrentState = AgentStates.offTheGrid;
                    break;
                }
                CurrentState = AgentStates.traveling;
               // print("atrada  " + rc.x + ","  + rc.y);
            } else {
                CurrentState = AgentStates.idling; //neko neatrada - jaaiet neko nedariit
              //  print("neatrada");
            }

           // print("STATE:choosingResourceDestination craving=" + isCraving);
            break;
            //---------------------------------------------------------------
        case AgentStates.choosingWorkDestination:
            //print("STATE:choosingWorkDestination ");

            workUnit = null;

            try{
                workUnit = workManagerScript.GetWork(this);
                if(workUnit != null){

                    if(GoThere(workUnit.BestPositionToStandWhileWorking.x, workUnit.BestPositionToStandWhileWorking.y) == -1){ // iet uz noraadiito darbavietu (ja -1 tad Agjents neatrodas uz grida, jaaiet sho glaabt)
                        CurrentState = AgentStates.offTheGrid;
                        break;
                    }

                    CurrentState = AgentStates.traveling;
                    workUnit.ReserveWork(true,this);

                 //   print("ir straadaashana " + workUnit + "  @" + workUnit.parentGameobject.transform + "  bestPOS " + workUnit.BestPositionToStandWhileWorking);

                } else { //nav neviena briiva,pieejama darbinja
                    CurrentState = AgentStates.idling;
                    break;
                }

            } catch(System.Exception e) { // kjer pathfinding iznjeemumu, ka agjents nemaz neatrodas uz grida
                if(e.Message == "not-on-a-grid"){
                    CurrentState = AgentStates.offTheGrid; //agjents ies glaabties
                }

            }

            break;

            //---------------------------------------------------------------
        case AgentStates.traveling:
            // print("STATE:traveling");
            //kad agjents buus nonaacis galaa, tad vinja staavokli nomainiis moveAbout() metodee nevis sheit; nomainiis uz "arriving"

            break;
           //---------------------------------------------------------------
        case AgentStates.offTheGrid:
            //neatrodas uz navgrida, jaaatrod tuvaakais navgridaa esoshais punts un jaaevakueejas taa virzienaa

            if(offTheGridFor > 0){ //pagaidiis kaadu laicinju, lai neceptu procesoru
                offTheGridFor -= Time.deltaTime;
                break;
            }

            if(levelscript.Navgrid.Count > 0){ //ja ir navgridaa punkti, kur mukt
                //print ("provees tikt uz celja");
                Vector2 closestPoint = new Vector2(0,0);
                float closestDistance = -1;
                float x = transform.position.x;
                float y = transform.position.y;

                foreach(KeyValuePair<Vector4, float> p in levelscript.Navgrid) {
                    if(p.Key.y == y){ //meklee tuvaakos punkts tikai shajaa staavaa
                        float d = levelscript.MapDistanceBetweenPoints(p.Key.x,p.Key.y,x,y);//cik taalu shis navgrida punkts no agjenta
                        if(closestDistance == -1 || d < closestDistance){
                            closestPoint = new Vector2(p.Key.x,p.Key.y); //shis ir tuvaakais navgrida punkts
                            closestDistance = d;
                        }
                    }
                }

                if(closestDistance > -1){
                //    print("atradaam kaadu punktu, kur glaabties");
                    destinationNode = closestPoint;
                    actualRoute.Add(closestPoint); //manuaali izveidoju celju (stur tikai 1 punktu - kur skrienu glaabties)
                    CurrentState = AgentStates.traveling; //peec ieshanas automaatiski staavoklis buus IDLING
                    avatarAnimator.SetBool("fast", true); //manuaalie iesaaku animaaciju
                    //print(x + "," + y + " -> " + closestPoint);
                    break;
                } else {
                  //  print("NEatradaam nevienu punktu, kur glaabties");
                    offTheGridFor = 3; //3 sekundes pagaidiis, liidz meegjinaas atkal, varbuut tad buuus kaut kas uzbuuveets
                }

            }
            break;
           //-----------------------------------------------------------------
        case AgentStates.arriving:

            if(workUnit != null){ //te ieradies darba dariishanaas
             //   print("ieradies straaadaaat");
                workingFor = Random.Range(2,5); //dazhas sekundes pastraadaas
                avatarAnimator.SetBool("working", true);
                CurrentState = AgentStates.working;

                break; //break case

            } else if(isCraving >= 0){//sheit ieradies apmierinaat kaadu vajadziibu

                room = levelscript.roomAtThisPosition(transform.position.x,transform.position.y);  //null vai telpa

                if(room == null){ //atnaacis, bet neatrodas telpaa, hmmm
                    CurrentState = AgentStates.idling;
                 //   print("iatnaacis, bet neatrodas telpaa, hmmm");
                } else { //viss kaartiibaa ir telpaa

                    bool roomIsProviding = false;
                    for(int need = 0; need < AgentNeeds.numTypes; need++) { //iet cauri visaam agjentvajadziibaam
                        if(Needs.Shortage[need]){ //shis resurss truukst
                            if(room.AgentNeedsGeneration[need] > 0){ //telpa dod sho resursu
                                roomIsProviding = true;
                                break;
                            }
                        }
                    }

                    if(roomIsProviding){ //telpa deriiga, saakam eest
                      //  print("telpa deriiga, saakam eest");
                        currentRoom = room; //piekesho telpu, kur atnaacis
                        CurrentState = AgentStates.consumingResource;
                    } else { //telpa neko nedod
                        //print("telpa neko nedod");
                        CurrentState = AgentStates.idling;
                    }

                }

            }  else if(isCraving == -1){ //ir bezmeerkjiigi ieradies
                CurrentState = AgentStates.idling;
                break; //break case
            }

            //print("STATE:arriving");
            break;
            //---------------------------------------------------------------
        case AgentStates.consumingResource:
            // currentRoom -- tikai sheit droshi varu lietot sho mainiigo, jo tas ir uzsists ieprieksheejaa kadraa vieniigajaa IFaa, kas ved uz shejieni

            for(int need = 0; need < AgentNeeds.numTypes; need++) { //iet cauri visaam agjentvajadziibaam

                avatarAnimator.SetBool("eating", true); //pagaidaam ir tikai 1 teereeshanas animaacija - eeshana, visus resursus pateeree eedot - arii miedzinju XD

                Needs.Reserve[need] += currentRoom.AgentNeedsGeneration[need] * Time.deltaTime; //eed visus resursus (kas ir 0 vai pozitiivs skaitlis)

                if(Needs.Reserve[need] > Needs.Max[need]){ //neljauj paareesties, ierobesho maximumu
                    Needs.Reserve[need] = Needs.Max[need];
                }

                if(Needs.Reserve[isCraving] == Needs.Max[isCraving]){ // vajadziiba, kuras deelj naaca uz sho telpu, ir apmierinaata
                    CurrentState = AgentStates.idling;
                    avatarAnimator.SetBool("eating", false);
                }

            }

            //print("STATE:consumingResource");
            break;
            //---------------------------------------------------------------
        case AgentStates.working:

            if(!workUnit.IsOn()){ //darbinsh tiek izsleegts
                CurrentState = AgentStates.idling;
                avatarAnimator.SetBool("working", false);
                workUnit.ReserveWork(false); //padara darbinju pieejamu citiem
                break;
            }

            workingFor -= Time.deltaTime;
            workUnit.DoSomeActualMotherFlippingJob();

            if(workingFor < 0){ //buus gana straadaats
                CurrentState = AgentStates.idling;
                avatarAnimator.SetBool("working", false);
                workUnit.ReserveWork(false);//padara darbinju pieejamu citiem
                break;
            }

            //visa magjija un progress notiek workunit skriptaa

            break;

        }
    }