void SwordAnimation(carDirection dir, SpriteRenderer renderer, Sprite[] animation, int fps)
    {
        // float animation_progression;

        //Debug.Log("sword animate");
        float animation_start_time;

        animation_start_time = Time.time;

        int animation_length    = animation.Length;
        int current_frame_index = ((int)((Time.time - animation_start_time) / (1.0 / fps)) % animation_length);

        renderer.sprite = animation[1];
    }
Esempio n. 2
0
    override public void Move()
    {
        ///Implements random movement for goriya

        if (currentState != EntityState.KNOCKBACK && currentState != EntityState.STUNNED)
        {
            ////Implement speed based movement
            if (target == Vector3.down)
            {
                current_direction = carDirection.SOUTH;
                GetComponent <SpriteRenderer>().sprite = goriya_run_down[0];
            }
            else if (target == Vector3.up)
            {
                current_direction = carDirection.NORTH;
                GetComponent <SpriteRenderer>().sprite = goriya_run_up[0];
            }
            else if (target == Vector3.right)
            {
                current_direction = carDirection.EAST;
                GetComponent <SpriteRenderer>().sprite = goriya_run_right[0];
            }
            else if (target == Vector3.left)
            {
                current_direction = carDirection.WEST;
                GetComponent <SpriteRenderer>().sprite = goriya_run_left[0];
            }



            if (navigation)
            {
                //Rounds the position to the grid on change so that enemy is always in place
                ///multiplying by 2 and then rounding sets enemy to the closest .5
                // print("nav");
                int rand = Random.Range(0, 3);
                if (rand == 0)
                {
                    target             = Vector3.down;
                    transform.position = roundGrid(transform.position, "V");
                    current_direction  = carDirection.SOUTH;
                }

                if (rand == 1)
                {
                    target             = Vector3.up;
                    transform.position = roundGrid(transform.position, "V");
                    current_direction  = carDirection.NORTH;
                }

                if (rand == 2)
                {
                    target             = Vector3.right;
                    transform.position = roundGrid(transform.position, "H");
                    current_direction  = carDirection.EAST;
                }
                if (rand == 3)
                {
                    target             = Vector3.left;
                    transform.position = roundGrid(transform.position, "H");
                    current_direction  = carDirection.WEST;
                }
                navigation = false;
            }

            GetComponent <Rigidbody>().velocity = target * speed;

            int reset_rand = Random.Range(0, 1000);

            ///randomly reset the nav bool to reset 2D movement, should happen 1% of frames
            if (reset_rand > 990)
            {
                //print("update");
                navigation = true;
            }
        }
        else if (currentState == EntityState.STUNNED)
        {
            //print("stunned");
            GetComponent <Rigidbody>().velocity = Vector3.zero;
            if (Time.time > stunTime)
            {
                currentState = EntityState.NORMAL;
            }
        }
        else     //// need to add stuff here as elseif
                 ///else player is locked into knockback

        {
            GetComponent <Rigidbody>().velocity = knockbackDir * force;
            //print("knockbackupdating");
            //print(GetComponent<Rigidbody>().velocity);
            if (Time.time > knockbackTimer)
            {
                currentState = EntityState.NORMAL;
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A) && Time.time > nextAttack)
        {
            //print("poop");
            current_state = EntityState.ATTACKING;
            nextAttack    = Time.time + swordRate;
            if (current_direction == carDirection.SOUTH)
            {
                SwordAnimation(current_direction, GetComponent <SpriteRenderer>(), link_weapon_down, 6);
            }
            else if (current_direction == carDirection.NORTH)
            {
                SwordAnimation(current_direction, GetComponent <SpriteRenderer>(), link_weapon_up, 6);
            }
            else if (current_direction == carDirection.EAST)
            {
                SwordAnimation(current_direction, GetComponent <SpriteRenderer>(), link_weapon_right, 6);
            }
            else if (current_direction == carDirection.WEST)
            {
                SwordAnimation(current_direction, GetComponent <SpriteRenderer>(), link_weapon_left, 6);
            }
        }

        if (Time.time > nextAttack && current_state == EntityState.ATTACKING)
        {
            current_state = EntityState.NORMAL;
            if (current_direction == carDirection.SOUTH)
            {
                animation_state_machine.ChangeState(new StateIdleWithSprite(this, GetComponent <SpriteRenderer>(), link_run_down[0]));
            }
            else if (current_direction == carDirection.NORTH)
            {
                animation_state_machine.ChangeState(new StateIdleWithSprite(this, GetComponent <SpriteRenderer>(), link_run_up[0]));
            }
            else if (current_direction == carDirection.EAST)
            {
                animation_state_machine.ChangeState(new StateIdleWithSprite(this, GetComponent <SpriteRenderer>(), link_run_right[0]));
            }
            else if (current_direction == carDirection.WEST)
            {
                animation_state_machine.ChangeState(new StateIdleWithSprite(this, GetComponent <SpriteRenderer>(), link_run_left[0]));
            }
            //updates back to idle
            //current_state = EntityState.NORMAL;
            // Debug.Log("wow");
        }
        animation_state_machine.Update();
        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            //D//ebug.Log("down");
            current_direction = carDirection.SOUTH;
        }
        else if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            // Debug.Log("up");
            current_direction = carDirection.NORTH;
        }
        else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            // Debug.Log("right");
            current_direction = carDirection.EAST;
        }
        else if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            //Debug.Log("left");
            current_direction = carDirection.WEST;
        }
    }