Esempio n. 1
0
 /**
  * Force this person to switch its state
  */
 protected void forceAIState(enAIState aiState)
 {
     // Enable the next coroutine to start
     this.isRunningAI = false;
     // Switch the state
     this.aiState = aiState;
     // Stop the current coroutine
     if (this.runningCoroutine != null) {
         StopCoroutine(this.runningCoroutine);
     }
     this.runningCoroutine = null;
 }
Esempio n. 2
0
    private IEnumerator doAI()
    {
        this.isRunningAI = true;

        if (this.state == enState.bribed && this.aiState != enAIState.talk) {
            int num;

            num = PersonBrain.lvlManager.countFreeWithColor(this.color,
                    this.type);

            if (num > 0) {
                this.aiState = enAIState.pursue;
            }
            else if (!isWhite) {
                // Gradually change the player color to white
                StartCoroutine(fadeToWhite());
            }
        }

        switch (aiState) {
            case enAIState.idle: {
                float time;

                this.rbody.velocity = Vector2.zero;

                time = Random.Range(minIdleTime, maxIdleTime);

                Debug.Log("Idle for " + time.ToString() + "s");
                animator.SetFloat("MovBlend", 0);
                yield return new WaitForSeconds(time);
                Debug.Log("Exiting idle!");

                // Select new state
                if (Random.Range(0, 1) == 0) {
                    this.aiState = enAIState.walk;
                }
                else {
                    this.aiState = enAIState.idle;
                }
            } break;
            case enAIState.walk:
            {
                float time;

                // Set speed
                if (this.transform.position.x <= PersonBrain.minHorPosition) {
                    this.rbody.velocity = new Vector2(this.horizontalSpeed, 0.0f);
                }
                else if (this.transform.position.x >= PersonBrain.maxHorPosition) {
                    this.rbody.velocity = new Vector2(-this.horizontalSpeed, 0.0f);
                }
                else if (Random.Range(0, 1) == 0) {
                    this.rbody.velocity = new Vector2(-this.horizontalSpeed, 0.0f);
                }
                else {
                    this.rbody.velocity = new Vector2(this.horizontalSpeed, 0.0f);
                }

                // Make the person walk more to the oposing direction
                if (this.transform.position.x < PersonBrain.lvlManager.width / 8f) {
                    this.rbody.velocity = new Vector2(this.horizontalSpeed, 0.0f);
                    time = Random.Range(minWalkTime * 1.5f, maxWalkTime * 2f);
                }
                else if (this.transform.position.x > PersonBrain.lvlManager.width / 8f) {
                    this.rbody.velocity = new Vector2(-this.horizontalSpeed, 0.0f);
                    time = Random.Range(minWalkTime * 1.5f, maxWalkTime * 2f);
                }
                else {
                    time = Random.Range(minWalkTime, maxWalkTime);
                }

                Debug.Log("Walking for " + time.ToString() + "s");
                animator.SetFloat("MovBlend", 1);
                yield return new WaitForSeconds(time);
                Debug.Log("Exiting walk!");

                this.rbody.velocity = Vector2.zero;

                // Always go back to idle, after this
                this.aiState = enAIState.idle;
             } break;
            case enAIState.talk: {
                this.rbody.velocity = Vector2.zero;
                // Since both people must talk for the same amount of time they
                // should wait for the same amount of time

                animator.SetTrigger("Bribe");
                yield return new WaitForSeconds(this.influenceTime);

                if (this.state == enState.influenced) {
                    // Gradually change the player color to white
                    StartCoroutine(fadeToWhite());
                }
            } break;
            case enAIState.getBribed: {
                this.rbody.velocity = Vector2.zero;
                // Since both people must talk for the same amount of time they
                // should wait for the same amount of time

                animator.SetTrigger("Bribe");
                yield return new WaitForSeconds(this.influenceTime);

                this.state = enState.bribed;
                PersonBrain.lvlManager.currentFollowers++;
                PersonBrain.lvlManager.accBribed++;
            } break;
            case enAIState.follow: {
                Transform player;

                player = PersonBrain.lvlManager.player;
                animator.SetFloat("MovBlend", 1);
                while (Vector3.Distance(player.transform.position,
                        this.transform.position) > this.maxDistanceFromPlayer) {
                    // Go after the player
                    if (player.transform.position.x < this.transform.position.x &&
                            this.rbody.velocity.x >= 0) {
                        this.rbody.velocity = new Vector2(-this.horizontalSpeed, 0.0f);
                        Debug.Log("Player is to my left");
                    }
                    else if (player.transform.position.x > this.transform.position.x &&
                            this.rbody.velocity.x <= 0) {
                        this.rbody.velocity = new Vector2(this.horizontalSpeed, 0.0f);
                        Debug.Log("Player is to my right");
                    }
                    else {
                        Debug.Log("Going the right direction!");
                    }

                    yield return null;
                }
            } break;
            case enAIState.pursue: {
                PersonBrain target;

                target = PersonBrain.lvlManager.getNextInfluentiable(this.color,
                    this.type);
                Debug.Log("Entered pursue!");

                while (target.state == enState.free) {
                    // Go after that person
                    if (target.transform.position.x < this.transform.position.x &&
                            this.rbody.velocity.x >= 0) {
                        this.rbody.velocity = new Vector2(-this.horizontalSpeed, 0.0f);
                        Debug.Log("Target is to my left");
                    }
                    else if (target.transform.position.x > this.transform.position.x &&
                            this.rbody.velocity.x <= 0) {
                        this.rbody.velocity = new Vector2(this.horizontalSpeed, 0.0f);
                        Debug.Log("Target is to my right");
                    }
                    else {
                        Debug.Log("Going the right direction!");
                    }

                    // Wait until this person overlap its target or the target
                    // gets influenced/bribed by another
                    yield return null;
                }
            } break;
        }
        this.isRunningAI = false;
        this.runningCoroutine = null;

        if (this.state != enState.free) {
            if (Vector3.Distance(PersonBrain.lvlManager.player.transform.position,
                    this.transform.position) > this.maxDistanceFromPlayer) {
                this.aiState = enAIState.follow;
            }
            else {
                this.aiState = enAIState.idle;
            }
        }
    }
Esempio n. 3
0
 /**
  * Removes the AI loop and force this to be inactive
  */
 public void forceStop()
 {
     // Enable the next coroutine to start
     this.isRunningAI = false;
     // Switch the state to idle
     this.aiState = enAIState.idle;
     // Stop the current coroutine
     if (this.runningCoroutine != null) {
         StopCoroutine(this.runningCoroutine);
     }
     this.runningCoroutine = null;
     this.gameObject.SetActive(false);
 }