예제 #1
0
파일: RangedAI.cs 프로젝트: nmotocic/PPiJ
    // Update is called once per frame
    void Update()
    {
        alarm.Update();
        //Init
        var     my_pos = transform.position;
        Vector3 target;
        //Pick target
        var targetObject = GameObject.FindGameObjectWithTag(targetObjectTag);

        if (targetObject == null) //Mouse
        {
            target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }
        else //Player
        {
            target = targetObject.transform.position;
        }
        //Update animation
        updateAnimation(Mathf.Sign(my_pos.x - target.x));
        target.z = 0;

        if (AiDefaults.getCircleSight(my_pos, projectileObject.GetComponent <CircleCollider2D>().radius, target, true) && state == 0)
        {   //Target obstructed
            setDestination(target, agent);
            rbody2d.velocity = new Vector2(0, 0);
        }
        else
        {                   //State machine
            //Pick state
            if (state != 0) // Stop moving if winding up an attack
            {
                setDestination(my_pos, agent);
            }
            else //Move
            {
                setDestination(target, agent);
                rbody2d.velocity = new Vector2(0, 0);
            }
            //Hurt
            if (state == GameDefaults.hitState())
            {
                if (alarm.isActive())
                {
                    state = 0;
                }
                danger = false;
            }
            //Death
            else if (state == GameDefaults.deatState())
            {
                //Drop stuff, get removed
                danger = false;
                if (alarm.getMax() != GameDefaults.corpseTimeout())
                {
                    alarm.setMax(GameDefaults.corpseTimeout());
                }
                if (alarm.isActive())
                {
                    GameObject.Destroy(gameObject);
                }
            }

            //--------------------------Attacking
            if (state != GameDefaults.deatState())
            {
                if (Vector2.Distance(my_pos, target) <= attackTriggerRange && state == 0)
                { //Prep attack
                    setDestination(my_pos, agent);
                    state     = 1;
                    targetDir = -(my_pos - target);
                    targetDir.Normalize();
                    startPos = my_pos;
                    //Alarm
                    alarm.setMax(attackWindup);
                    alarm.reset();
                }
                else if (state == 1 && alarm.isActive()) //Wait for alarm
                {                                        //Attack - shoot
                    eScript.DispenseObject(projectileObject, my_pos, targetDir, attackSpeed);
                    //Alarm
                    state = 2;
                    alarm.setMax(attackDuration);
                    alarm.reset();
                }
                else if ((state == 2 & alarm.isActive()))
                { //Attack over - stop
                    rbody2d.velocity = new Vector2(0, 0);
                    state            = 3;
                    alarm.setMax(attackCooldown);
                    alarm.reset();
                }
                else if ((state == 3 & alarm.isActive()))
                { //Attack over - stop
                    state = 0;
                }
                //--------------------------Attacking
            }
        }
    }
예제 #2
0
    // Update is called once per frame
    void Update()
    {
        if (permaBuff)
        {
            buff = true;
        }
        alarm.Update();
        aiTimer.Update();
        //Init
        var     my_pos = transform.position;
        Vector3 target;
        //Pick target
        var targetObject = GameObject.FindGameObjectWithTag(targetObjectTag);

        if (targetObject == null) //Mouse
        {
            target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }
        else //Player
        {
            target = targetObject.transform.position;
        }
        //Update animation
        updateAnimation(Mathf.Sign(my_pos.x - target.x));
        target.z = 0;
        //Reset idle
        if (state == 5 && alarm.isActive())
        {
            state = 0;
        }
        //Reset attack alarm
        if (attackType != 0 && state == 0)
        {
            attackType = 0;
            aiTimer.reset();
        }

        //LoS
        if (state == 0 && AiDefaults.getLineSight(my_pos, target))
        {   //Target obstructed
            setDestination(target, agent);
            rbody2d.velocity = new Vector2(0, 0);
        }
        else
        {                   //State machine
            //Pick state
            if (state == 0) // Move
            {
                setDestination(target, agent);
                rbody2d.velocity = new Vector2(0, 0);
            }
            else //Move
            {
            }

            //Hurt
            if (state == GameDefaults.hitState())
            {
                if (alarm.isActive())
                {
                    state = 0;
                }
                danger = false;
            }
            //Death
            else if (state == GameDefaults.deatState())
            {
                //Drop stuff, get removed
                danger = false;
                GameObject.Destroy(myBuff);
                myBuff = null;
            }

            //--------------------------Attacking
            //Pick attack
            if (Vector2.Distance(my_pos, target) <= autoTriggerAtackDist && state == 0 && attackType == 0) //Attack player immediately
            {
                pickAttack();
                while (attackType == -1 || attackType == 4)
                {
                    pickAttack();
                }
                Debug.Log("Picked attack:" + attackType.ToString());
            }
            else if (aiTimer.isActive() && attackType == 0 && state == 0)
            {
                pickAttack();
                while (buff && attackType == 4)
                {
                    pickAttack();
                }
                Debug.Log("Picked attack:" + attackType.ToString());
            }
            //Attacks
            if (attackType > 0)
            {
                //attackType = 4;
                //Dash
                if (attackType == 1)
                {
                    //Windup
                    if (state == 0)
                    {
                        state = 1;
                        alarm.setMax(dashWindup);
                        alarm.reset();
                        //Stop moving
                        setDestination(my_pos, agent);
                    }
                    //Start
                    else if (state == 1 && alarm.isActive())
                    {
                        state  = 2;
                        danger = true;
                        //Set agent speeds
                        if (buff)
                        {
                            agent.speed        = dashSpeed * 1.5f;
                            agent.angularSpeed = dashAngular * 4f;
                            agent.acceleration = dashAccel * 4f;
                            setDestination(target, agent);
                            buff = false;
                            if (myBuff != null)
                            {
                                myBuff.GetComponent <AI_Effect>().deActivate();
                            }
                            alarm.setMax(dashDuration / 2);
                            alarm.reset();
                        }
                        else
                        {
                            agent.speed        = dashSpeed;
                            agent.angularSpeed = dashAngular;
                            agent.acceleration = dashAccel;
                            setDestination(target, agent);
                            alarm.setMax(dashDuration);
                            alarm.reset();
                        }
                    }
                    //End
                    else if ((state == 2) && (alarm.isActive() || !danger))
                    {
                        setState(3); //Stab state
                        alarm.setMax(dashCooldown / 2);
                        alarm.reset();
                        danger = false;
                    }
                    //Cooldown
                    else if ((state == 3) && (alarm.isActive()))
                    {
                        state = 5; //IdleState
                        alarm.reset();
                    }
                }
                //Slash
                else if (attackType == 2)
                {
                    targetDir = -(my_pos - target);
                    targetDir.Normalize();
                    if (state == 0)
                    {
                        state = 1;
                        alarm.setMax(slashWindup);
                        alarm.reset();
                        //Stop moving
                        setDestination(my_pos, agent);

                        if (buff)
                        {
                            attackCombo = 2;
                            buff        = false;
                            if (myBuff != null)
                            {
                                myBuff.GetComponent <AI_Effect>().deActivate();
                            }
                        }
                        else
                        {
                            attackCombo = 0;
                        }
                    }
                    //Start
                    else if (state == 1 && alarm.isActive())
                    {
                        state = 2;
                        alarm.setMax(slashDuration);
                        alarm.reset();
                        eScript.DispenseObject(slashProjectile, my_pos, targetDir, slashProjectileSpeed);
                    }
                    //Swing
                    else if ((state == 2) && (alarm.isActive()))
                    {
                        if (attackCombo > 0)
                        { //Moar swings
                            attackCombo--;
                            state = 1;
                            alarm.setMax(slashWindup / 6);
                            alarm.reset();
                        }
                        else
                        { //Cooldown
                            state = 5;
                            alarm.setMax(slashCooldown);
                            alarm.reset();
                        }
                    }
                }
                //Spin
                else if (attackType == 3)
                {
                    if (state == 0)
                    {
                        state = 1;
                        alarm.setMax(spinWindup);
                        alarm.reset();
                        //Stop moving
                        setDestination(my_pos, agent);
                    }
                    //Start
                    else if (state == 1 && alarm.isActive())
                    {
                        state = 2;
                        alarm.setMax(spinInterval);
                        alarm.reset();

                        if (buff)
                        {
                            attackCombo = spinProjectiles;
                            buff        = false;
                            if (myBuff != null)
                            {
                                myBuff.GetComponent <AI_Effect>().deActivate();
                            }
                            attackCount = 4;
                        }
                        else
                        {
                            attackCombo = spinProjectiles;
                            attackCount = 1;
                        }
                    }
                    //Spin and launch
                    else if ((state == 2) && (alarm.isActive()))
                    {
                        if (attackCombo > 0)
                        { //Keep launching
                            state = 2;
                            alarm.setMax(spinInterval);
                            alarm.reset();
                            attackCombo--;
                            for (int i = 0; i < attackCount; i++)
                            {
                                Vector2 randVector = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f));
                                randVector.Normalize();
                                eScript.DispenseObject(slashProjectile, my_pos, randVector, slashProjectileSpeed);
                            }
                        }
                        else
                        { //Cooldown
                            state = 3;
                            alarm.setMax(spinCooldown / 3);
                            alarm.reset();
                        }
                    }
                    else if (state == 3 && alarm.isActive())
                    {
                        state = 5;
                        alarm.setMax(2 * spinCooldown / 3);
                        alarm.reset();
                    }
                }
                //Buff
                else if (attackType == 4)
                {
                    if (state == 0)
                    {
                        state = 1;
                        alarm.setMax(slamWindup);
                        alarm.reset();
                        //Stop moving
                        setDestination(my_pos, agent);
                    }
                    //Slam
                    else if (state == 1 && alarm.isActive())
                    {
                        state = 2;
                        alarm.setMax(slamDuration);
                        alarm.reset();
                        //Launch 4 projectiles
                        eScript.DispenseObject(slashProjectile, my_pos, Vector2.down, slashProjectileSpeed);
                        eScript.DispenseObject(slashProjectile, my_pos, Vector2.up, slashProjectileSpeed);
                        eScript.DispenseObject(slashProjectile, my_pos, Vector2.left, slashProjectileSpeed);
                        eScript.DispenseObject(slashProjectile, my_pos, Vector2.right, slashProjectileSpeed);
                        buff = true;
                        if (myBuff != null)
                        {
                            myBuff.GetComponent <AI_Effect>().activate();
                        }
                    }
                    //Slam done
                    else if ((state == 2) && (alarm.isActive()))
                    {
                        state = 5;
                        alarm.setMax(slamCooldown);
                        alarm.reset();
                    }
                }
            }
        }
    }