예제 #1
0
    //this function should only be called by EnemySpawner, as it deals with base level data
    public GameObject getEnemyInstance(int eCode)
    {
        if (enemyMold == null)
        {
            Debug.LogError("enemyMold is null... Can't create enemy instance");
            return(null);
        }

        GameObject e = Instantiate(enemyMold, enemyMold.transform.position, enemyMold.transform.rotation) as GameObject; //duplicate

        //SpriteRenderer
        if (sprite_on_child[eCode])
        {
            e.GetComponent <SpriteRenderer>().enabled = false;
            e.transform.GetChild(0).GetComponent <SpriteRenderer>().enabled = true;

            e.GetComponent <Enemy>().sprite_on_child = true;
        }
        else
        {
            e.GetComponent <Enemy>().sprite_on_child = false;
        }

        //Animator
        Animator animator = e.GetComponent <Animator>();

        if (S0_ANIM[eCode] != null || S1_ANIM[eCode] != null || S2_ANIM[eCode] != null)
        {
            AnimatorOverrideController animatorOverrideController = new AnimatorOverrideController(animator.runtimeAnimatorController);
            animatorOverrideController.name = "overrideEnemyAnimator";

            if (S0_ANIM[eCode] != null)
            {
                animatorOverrideController["State0anim"] = S0_ANIM[eCode];
            }

            if (S1_ANIM[eCode] != null)
            {
                animatorOverrideController["State1anim"] = S1_ANIM[eCode];
            }

            if (S2_ANIM[eCode] != null)
            {
                animatorOverrideController["State2anim"] = S2_ANIM[eCode];
            }

            animator.runtimeAnimatorController = animatorOverrideController;
        }
        animator.Update(0.0f);

        if (initial_sprite[eCode] != "")
        {
            Sprite ini_spr = Resources.Load <Sprite>("Sprites/Enemies/" + initial_sprite[eCode]);

            if (ini_spr == null)
            {
                Debug.LogError("sprite " + initial_sprite[eCode] + " NOT found");
            }
            else
            {//if initial_sprite exists and successfully loaded
                if (sprite_on_child[eCode])
                {
                    Transform childSprite = e.transform.Find("childSprite");
                    childSprite.GetComponent <SpriteRenderer>().sprite = ini_spr;
                }
                else
                {
                    e.GetComponent <SpriteRenderer>().sprite = ini_spr;
                }
            }
        }

        //Enemy stats
        e.name = enemyName[eCode];
        Enemy eScript = e.GetComponent <Enemy>();

        eScript.passReferences(prefabHolder, gameControl);
        //NOTE: it's crucial that setLife is AFTER instantiation!
        eScript.setValues(life[eCode], attack[eCode]);
        eScript.setSizeScale(sizeScale[eCode]);
        //eScript.setColliderScale(colliderScale[eCode]);

        //projectile if exists
        if (projectileName[eCode] != "")
        {
            eScript.setProjectile(projectileMold, projectileSprites[eCode], eScript.attack, projectileType[eCode],
                                  projectileSpeed[eCode], projectileAccl[eCode], projectileShootInterval[eCode], shootNoise[eCode], shootChanceIndividual[eCode],
                                  shootChanceEnemy[eCode]);
        }

        EnemyMover mover = e.GetComponent <EnemyMover>();

        mover.enemyType = movement[eCode];
        mover.setSpeed(0, moveSpeed[eCode]); //TODO for now, setting horizontal speed to 0 (most don't move horizontally anyway)

        EnemySteering steer = e.GetComponent <EnemySteering>();

        steer.velocity = moveSpeed[eCode];


        //PolygonCollider2D;    assumes enemyMold prefab has PolygonCollider2D and PixelCollider attached already
        if (enemyColliders[eCode] == null) //collider for this enemy hasn't been generated and stored previously
        {
            List <List <Vector2> > enemy_Paths = e.GetComponent <PixelCollider2D>().Regenerate(sprite_on_child[eCode]);
            enemyColliders[eCode] = enemy_Paths;
        }
        else
        {
            PolygonCollider2D pol = e.GetComponent <PolygonCollider2D>();

            //transferring the points onto PolyCollider
            pol.pathCount = enemyColliders[eCode].Count;
            for (int p = 0; p < enemyColliders[eCode].Count; p++)
            {
                pol.SetPath(p, enemyColliders[eCode][p].ToArray());
            }
        }

        return(e);
    }