// Update is called once per frame
 void Update()
 {
     slope = new Vector2(x, y);
     if (trigger)
     {
         con.shoot(transform.position, target.position, slope, isTargetLeft(), gameObject, overShoot);
         trigger = false;
     }
     //con.drawTrajectory (transform.position, velocity, slope, false, line);
 }
Exemple #2
0
    //modify later - to button press
    private void OnMouseDown()
    {
        if (shootingController.isRecharging())
        {
            return;
        }
        spriteRenderer.color = activeColor;
        shootingController.shoot();
        Transform cannonTip = shootingController.getNextTip();

        //instantiate projectile
        GameObject tempProjectile;

        tempProjectile = Instantiate(projectile, cannonTip.transform.position, cannonTip.transform.rotation) as GameObject;
        //assign a tab to the projectile - a word that we want to match with enemy
        tempProjectile.name = article;

        //target position
        Vector2 position;

        if (shootingController.target)
        {
            position = shootingController.target.transform.position - cannonTip.transform.position;
        }
        else
        {
            position = gameObject.transform.forward;
        }
        //control the rigidbody component of projectile to shoot it
        Rigidbody2D tempRigidbody;

        tempRigidbody = tempProjectile.GetComponent <Rigidbody2D>();

        //shoot the projectile
        tempRigidbody.velocity = position * 10;


        //after 2 second projectile is automatically destroyed if not used
        Destroy(tempProjectile, 2.0f);
    }
    //updates before physics updates
    void FixedUpdate()
    {
        //tests of time to next attack is being used
        if (!(timeToNextAttack <= 0))
        {
            timeToNextAttack -= Time.deltaTime;
            //tests if enough time went by
            if (timeToNextAttack <= 0)
            {
                canMove = true;
            }
        }

        //defualt slow is off
        isSlowed = false;

        //tests if should be slowed
        if (timeSlowedLeft > 0)
        {
            timeSlowedLeft -= Time.deltaTime;
            if (timeSlowedLeft <= 0)
            {
                isSlowed = false;
            }
            else
            {
                isSlowed = true;
            }
        }

        //variables for speed in each direction
        float vertical   = 0.0f;
        float horizontal = 0.0f;

        //tests if player is allowed to move
        if (canMove)
        {
            //checks if paused pressed
            if (buttonListen.isPressed(ButtonType.PAUSE))
            {
                //pauses game and stops player
                PauseMenuControl.pause();
                return;
            }

            //gets the angle of how the stick was moved
            horizontal = Stick.Horizontal();
            vertical   = Stick.Vertical();

            //checks attack buttons
            if (buttonListen.isPressed(ButtonType.ATTACK_LIGHT) && timeToNextAttack <= 0)
            {
                attack(lightAttack, lightAttackDamage, false);
                animator.SetTrigger("lightattack");
                timeToNextAttack = 1;
                ASound.playAttackSound();
            }

            if (buttonListen.isPressed(ButtonType.ATTACK_HEAVY) && timeToNextAttack <= 0)
            {
                attack(heavyAttack, heavyAttackDamage, true);
                animator.SetTrigger("heavyattack");
                timeToNextAttack = 2;
                canMove          = false;
                ASound.playAttackSound();
            }

            //Ammo selector checks
            if (buttonListen.isPressed(ButtonType.AMMO_PRIMARY))
            {
                setActiveAmmoPrimary(true);
            }

            if (buttonListen.isPressed(ButtonType.AMMO_SECONDARY))
            {
                setActiveAmmoPrimary(false);
            }
        }


        //checks ranged stick
        //if player is aiming
        if (isAiming())
        {
            //slows player movement
            isSlowed = true;

            //calculates trajectory
            calculateTrajectoryStart(out powerPrev, out isLeftPrev, out slopePrev);

            //draws trajectory line
            lineRender.gameObject.SetActive(true);
            shootCon.drawTrajectory(transform.position, powerPrev, slopePrev, isLeftPrev, lineRender);
        }
        //if player has attempted to shoot
        if (shouldShoot())
        {
            //shoots in trajectory using previous settings
            lineRender.gameObject.SetActive(false);

            //checks if ammo is left
            if (getActiveSelect().canConsume(1))
            {
                //shoots
                shootCon.shoot(transform.position, powerPrev, slopePrev, isLeftPrev, gameObject);
                //updates counter
                getActiveSelect().consume(1);
            }
        }

        //set variable for speed(in each direction) needed to move
        moveDirection = new Vector2(horizontal, vertical);

        //if slowed reduces the speed
        if (isSlowed)
        {
            moveDirection -= (moveDirection * slowPerCent);
        }

        //sets the animation speed variable
        moveDirection *= speed;

        //gets total speed in float
        float speedmove = horizontal + vertical;

        //changes direction sprite is facing

        //checks for facing right and changes to move left (negative value(speed) is facing left)
        if (transform.localScale.x > 0)
        {
            if (speedmove < 0)
            {
                //sets facing direction to the left
                Vector3 scale = gameObject.transform.localScale;
                scale.Set(gameObject.transform.localScale.x * -1, gameObject.transform.localScale.y, gameObject.transform.localScale.z);
                gameObject.transform.localScale = scale;
            }
            //checks for facing left and changes to move right
        }
        else if (transform.localScale.x < 0)
        {
            if (speedmove > 0)
            {
                //sets facing direction to the right
                Vector3 scale = gameObject.transform.localScale;
                scale.Set(gameObject.transform.localScale.x * -1, gameObject.transform.localScale.y, gameObject.transform.localScale.z);
                gameObject.transform.localScale = scale;
            }
        }

        //gets absolute value of speedmove
        speedmove = Mathf.Abs(speedmove);

        //sets animator speed parameter
        animator.SetFloat("speed", speedmove);

        //sets velocity for movement
        controller.velocity = moveDirection;
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        //tests if the enemy is in the middle of dying
        if (onDeath)
        {
            //tests if animations is finished
            timeSinceDeath -= Time.deltaTime;
            if (timeSinceDeath <= 0)
            {
                Despawn();
            }
            return;
        }

        //tests of time to next attack is being used
        if (!(cooldown <= 0))
        {
            cooldown -= Time.deltaTime;
            //tests if enough time went by
            if (cooldown <= 0)
            {
                canMove = true;
            }
        }

        //defualt slow is off
        isSlowed = false;

        //tests if should be slowed
        if (timeSlowedLeft > 0)
        {
            timeSlowedLeft -= Time.deltaTime;
            if (timeSlowedLeft <= 0)
            {
                isSlowed = false;
            }
            else
            {
                isSlowed = true;
            }
        }

        //tests if overlay has been on
        if (timeToStopDamage > 0)
        {
            timeToStopDamage -= Time.deltaTime;
            //tests if enough time has passed
            if (timeToStopDamage <= 0)
            {
                overlay.SetActive(false);
            }
        }

        if (timeToNextShot > 0)
        {
            timeToNextShot -= Time.deltaTime;
        }

        //deciding state
        state = decideState();

        //resets speed and movement direction
        float   speedmove     = 0f;
        Vector2 moveDirection = Vector2.zero;

        //tests if enemy can move
        if (canMove)
        {
            //move away from player decision
            if (state == AiState.DISTANCE_PLAYER)
            {
                //gets line to player
                getReversedTargetDirection(target.position, out moveDirection, out speedmove);
            }

            //move from ally decision
            if (state == AiState.DISTANCE_ALLY)
            {
                getReversedTargetDirection(RunFrom.transform.position, out moveDirection, out speedmove);
            }

            //light attack decision
            if (state == AiState.ATTACK_LIGHT)
            {
                attack(lightAttack, lightAttackDamage, 1, false);
                animator.SetTrigger("lightattack");
            }

            //heavy attack decision
            if (state == AiState.ATTACK_HEAVY)
            {
                attack(heavyAttack, heavyAttackDamage, 2, true);
                animator.SetTrigger("heavyattack");
                canMove = false;
            }

            if (state == AiState.ATTACK_SHOOT)
            {
                float overUnder = 0f;

                //checks if should under shoot or over shoot
                //if player heading to enemy, undershoot, away, overshoot respectively
                if (isPlayerLookingAway())
                {
                    overUnder = overShoot;
                }
                //shoots player
                shootCon.shoot(transform.position, target.position, rangeAngle, target.gameObject.GetComponent <ControllerPlayer>().isLeftTo(point: transform.position), gameObject, overUnder);
            }

            //move to decision
            if (state == AiState.FOLLOW_PLAYER)
            {
                getTargetDirection(target.position, out moveDirection, out speedmove);
            }

            //follow decision
            if (state == AiState.FOLLOW_PATH)
            {
                //tests if has path to follow
                if (follow.hasPathToFollow())
                {
                    //checks if should move on path
                    if (follow.shouldMove(transform.position))
                    {
                        getTargetDirection(follow.getCurrentPathPoint(transform.position), out moveDirection, out speedmove);
                    }
                    else
                    {
                        moveDirection = Vector2.zero;
                        speedmove     = 0;
                    }
                }
                else
                {
                    getTargetDirection(target.position, out moveDirection, out speedmove);
                }
            }
        }

        //after decision



        //checks for facing right and changes to move left
        if (transform.localScale.x > 0)
        {
            if (speedmove < 0)
            {
                //sets to look left
                Vector3 scale = gameObject.transform.localScale;
                scale.Set(gameObject.transform.localScale.x * -1, gameObject.transform.localScale.y, gameObject.transform.localScale.z);
                gameObject.transform.localScale = scale;
            }
        }

        //checks for facing left and changes to move right
        if (transform.localScale.x < 0)
        {
            if (speedmove > 0)
            {
                //sets to move right
                Vector3 scale = gameObject.transform.localScale;
                scale.Set(gameObject.transform.localScale.x * -1, gameObject.transform.localScale.y, gameObject.transform.localScale.z);
                gameObject.transform.localScale = scale;
            }
        }

        //gets the absolute value of speedmove
        speedmove = Mathf.Abs(speedmove);

        //sets animator parameter for speed
        animator.SetFloat("speed", speedmove);

        //checks if speed is too high
        while (Mathf.Abs(moveDirection.x) + Mathf.Abs(moveDirection.y) > 2)
        {
            moveDirection = new Vector2((moveDirection.x * 0.1f), (moveDirection.y * 0.1f));
        }

        //sets speed
        moveDirection *= speed;

        //if slowed reduces the speed
        if (isSlowed)
        {
            moveDirection -= (moveDirection * slowPerCent);
        }

        //sets velocity to move player
        controller.velocity = moveDirection;
    }