Example #1
0
    // Update is called once per frame

    //TODO: Move entire movement code block to FixedUpdate().
    void Update()
    {
        if (Idle())
        {
            if (Vector2.Distance(transform.position, player.transform.position) < minDistance)
            {
                fsmState = (int)EFsmState.CHASE;
            }
        }
        else if (Chase())
        {
            //Player is seen, no pathfinding needs to occur.
            if (playerSeen)
            {
                transform.position = Vector2.MoveTowards(transform.position, player.transform.position, Time.deltaTime * movementSpeed);
            }
            //Player is not seen
            else
            {
                if (movePath != null)
                {
                    transform.position = Vector2.MoveTowards(transform.position, movePath [currentStep].Position, Time.deltaTime * movementSpeed);
                }
            }
            //Idle if player is too far away.
            if (Vector2.Distance(transform.position, player.transform.position) > maxDistance)
            {
                fsmState = (int)EFsmState.IDLE;
            }
            //Attack if player is close enough.
            if (Vector2.Distance(transform.position, player.transform.position) < attackDistance)
            {
                fsmState = (int)EFsmState.ATTACK;
            }
        }
        else if (Attack())
        {
            if (Vector2.Distance(transform.position, player.transform.position) > attackDistance)
            {
                fsmState = (int)EFsmState.CHASE;
            }
            else if (attackTime <= 0)
            {
                //Okay. Here's how this works:
                //First, the attack is created at the monster's position.
                MonsterAttack rawr = Instantiate(AttackType, transform.position, Quaternion.identity) as MonsterAttack;

                //Second, the attack is given a power.
                rawr.Initiate(5);

                //Third, the attack is moved towards the player.
                rawr.transform.parent   = transform;
                rawr.transform.position = Vector3.MoveTowards(transform.position, player.transform.position, 1.4f);

                //Fourth, math is done to find the rotation of the attack.
                //This is done so if the monster has a narrow attack, such as a spear or arrow, the attack still looks like it originates from the monster.
                float diffX = (rawr.transform.position.x - player.transform.position.x) * (-1);
                float diffY = (rawr.transform.position.y - player.transform.position.y) * (-1);

                //Fifth, more math. (Same as fourth)
                float rotationRads = Mathf.Tan(diffX / diffY);
                float rotationDegs = rotationRads * Mathf.Rad2Deg;

                //Sixth, the rotation happens.
                rawr.transform.Rotate(new Vector3(0, 0, -rotationDegs), Space.World);

                //Seventh, the attack timer is reset.
                attackTime = 100;
                Debug.Log("An attack happened!");
            }
        }

        //If no health, monster ded.
        if (hp <= 0)
        {
            Destroy(gameObject);
        }
        //Decrement attack timer.
        attackTime--;
    }