Exemplo n.º 1
0
 // Update is called once per frame
 void Update()
 {
     // find the current target dir
     where_to_go = where_to_go.update_target(this);
     if (where_to_go)
     {
         Debug.Log("The target: " + where_to_go.transform.position);
         Debug.Log("The enemy   " + this.transform.position);
         enemy_move_direction = Vector3.Normalize(where_to_go.transform.position - this.transform.position);
         Debug.Log(enemy_move_direction);
         // move toward that direction
         this.transform.position += enemy_speed * enemy_move_direction * Time.deltaTime;
     }
 }
Exemplo n.º 2
0
 // Update is called once per frame
 void Update()
 {
     // find the current target dir
     where_to_go = where_to_go.update_target(this);
     // if died or reached the end point, self-destruction
     if (health <= 0 || where_to_go.tag == "objectDestroyer")
     {
         Destroy(this.gameObject);
     }
     else
     {
         enemy_move_direction = Vector3.Normalize(where_to_go.transform.position - this.transform.position);
         // move toward that direction
         this.transform.position += enemy_speed * enemy_move_direction * Time.deltaTime;
     }
 }
Exemplo n.º 3
0
    // Update is called once per frame
    void Update()
    {
        // for test only
        if (Input.GetKeyDown(KeyCode.N))
        {
            state = ENEMY_NORMAL;
        }
        else if (Input.GetKeyDown(KeyCode.K))
        {
            state = ENEMY_KICKED;
        }
        else if (Input.GetKeyDown(KeyCode.J))
        {
            being_stunned();
        }
        else if (Input.GetKeyDown(KeyCode.L))
        {
            state = ENEMY_ATTRACTED;
        }

        switch (state)
        {
        case ENEMY_NORMAL:
            // find the current target dir
            where_to_go          = where_to_go.update_target(this);
            enemy_move_direction = Vector3.Normalize(where_to_go.transform.position - this.transform.position);
            // move toward that direction
            this.transform.position += enemy_speed * enemy_move_direction * Time.deltaTime;
            break;

        case ENEMY_KICKED:
            /* TODO
             * Get kicked direction from character script, fly to that direction
             * then self-destruction
             * note that if an enemy is in "kicked" state
             * it can never be in another state (ie. cannot be stunned when flying)
             */
            // enemy_speed ^ 2 means flying faster than walking, also represent the
            // enemies' weight (walk slower -> heavier -> fly slower)
            this.transform.position += enemy_speed * enemy_speed * enemy_move_direction * Time.deltaTime;
            this.transform.rotation  = Quaternion.Euler(0, 0, this.transform.rotation.z + rotate_angle);
            rotate_angle            += 10;
            break;

        case ENEMY_STUNNED:
            this.transform.position = this.transform.position + (stunned_range * new Vector3(1, 0, 0));
            stunned_range           = -stunned_range;
            break;

        case ENEMY_ATTRACTED:
            // change the current target to the melon
            enemy_move_direction = Vector3.Normalize(enemy_attracted_pos - this.transform.position);
            // move toward that direction
            this.transform.position += enemy_speed * enemy_move_direction * Time.deltaTime;
            break;

        default:
            state = ENEMY_NORMAL;
            break;
        }

        if (state != ENEMY_DIED)
        {
            /***************************** Moving Animation *****************************/
            // change sprite for animation
            sprite_change_counter++;
            if (sprite_change_counter >= sprite_change_interval)
            {
                if (sprite_switcher)
                {
                    sprite_renderer.sprite = new_sprite;
                }
                else
                {
                    sprite_renderer.sprite = old_sprite;
                }
                // reset counter
                sprite_switcher       = !sprite_switcher;
                sprite_change_counter = 0;
            }

            // detrmine moving direction
            if (enemy_move_direction.x > 0)
            {
                moving_right = true;
            }
            else
            {
                moving_right = false;
            }

            // determine if flip is needed
            // if moving right but not facing right, flip sprite
            if (moving_right && !facing_right)
            {
                this.transform.localScale = new Vector3(-enemyScale, enemyScale, 1);
                facing_right = true; // now it is facing right
            }
            // if moving left but facing right, flip
            else if (!moving_right && facing_right)
            {
                this.transform.localScale = new Vector3(enemyScale, enemyScale, 1);
                facing_right = false;
            }


            // if effect ended, back to normal
            if (effect_ended)
            {
                effect_ended = false;
                state        = ENEMY_NORMAL;
                Debug.Log("back to normal");
            }

            // if died or reached the end point, self-destruction
            if (health <= 0)
            {
                state = ENEMY_DIED;
                sprite_renderer.sprite = dead_sprite;
                if (facing_right)
                {
                    this.transform.localScale = new Vector3(-enemyScale, enemyScale, 1);
                }
                else
                {
                    this.transform.localScale = new Vector3(enemyScale, enemyScale, 1);
                }
                StartCoroutine(die_animation(1));
            }

            if (where_to_go.tag == "objectDestroyer")
            {
                if (!sound_played)
                {
                    StartCoroutine(die_sound(2));
                }
                sound_played = true;
            }
        }
    }
Exemplo n.º 4
0
    // Update is called once per frame
    void Update()
    {
        // for test only

        if (Input.GetKeyDown(KeyCode.N))
        {
            state = ENEMY_NORMAL;
        }
        else if (Input.GetKeyDown(KeyCode.K))
        {
            state = ENEMY_KICKED;
        }
        else if (Input.GetKeyDown(KeyCode.J))
        {
            being_stunned();
        }
        else if (Input.GetKeyDown(KeyCode.L))
        {
            state = ENEMY_ATTRACTED;
        }

        switch (state)
        {
        case ENEMY_NORMAL:
            // find the current target dir
            where_to_go          = where_to_go.update_target(this);
            enemy_move_direction = Vector3.Normalize(where_to_go.transform.position - this.transform.position);
            // move toward that direction
            this.transform.position += enemy_speed * enemy_move_direction * Time.deltaTime;
            break;

        case ENEMY_KICKED:
            /* TODO
             * Get kicked direction from character script, fly to that direction
             * then self-destruction
             * note that if an enemy is in "kicked" state
             * it can never be in another state (ie. cannot be stunned when flying)
             */
            // enemy_speed ^ 2 means flying faster than walking, also represent the
            // enemies' weight (walk slower -> heavier -> fly slower)
            this.transform.position += enemy_speed * enemy_speed * enemy_move_direction * Time.deltaTime;
            break;

        case ENEMY_STUNNED:
            this.transform.position = this.transform.position + (stunned_range * new Vector3(1, 0, 0));
            stunned_range           = -stunned_range;
            break;

        case ENEMY_ATTRACTED:
            // change the current target to the melon
            enemy_move_direction = Vector3.Normalize(enemy_attracted_pos - this.transform.position);
            // move toward that direction
            this.transform.position += enemy_speed * enemy_move_direction * Time.deltaTime;
            break;

        default:
            state = ENEMY_NORMAL;
            break;
        }
        // if effect ended, back to normal
        if (effect_ended)
        {
            effect_ended = false;
            state        = ENEMY_NORMAL;
            Debug.Log("back to normal");
        }

        // if died or reached the end point, self-destruction
        if (health <= 0 || where_to_go.tag == "objectDestroyer")
        {
            Destroy(this.gameObject);
        }
    }