Example #1
0
    public override EnemySteering GetSteering()
    {
        EnemySteering steering = new EnemySteering();

        float rotation = targetOrientation - agent.orientation;

        rotation = MapToRange(rotation);
        float rotationSize = Mathf.Abs(rotation);

        if (rotationSize < targetRadius)
        {
            return(steering);
        }
        float targetRotation = agent.maxRotation;

        if (rotationSize < slowRadius)
        {
            targetRotation = agent.maxRotation * rotationSize / slowRadius;
        }
        targetRotation   *= Mathf.Sign(rotation);
        steering.angular  = targetRotation - agent.rotation;
        steering.angular /= timeToTarget;
        float angularAccel = Mathf.Abs(steering.angular);

        if (angularAccel > agent.maxAngularAccel)
        {
            steering.angular /= angularAccel;
            steering.angular *= agent.maxAngularAccel;
        }

        return(steering);
    }
Example #2
0
    private void Start()
    {
        velocity = Vector2.zero;
        steering = new EnemySteering();

        orientation = -transform.rotation.eulerAngles.z;
        rotation    = 0;
    }
Example #3
0
    public override EnemySteering GetSteering()
    {
        EnemySteering steering = new EnemySteering();

        steering.linear  = transform.rotation * Vector3.up;
        steering.linear *= agent.maxAccel;

        return(steering);
    }
Example #4
0
    public override EnemySteering GetSteering()
    {
        EnemySteering steering = base.GetSteering();
        var           quat     = new Quaternion();

        quat.eulerAngles = new Vector3(0, 0, dirChangeAngle);
        steering.linear  = quat * steering.linear;

        return(steering);
    }
Example #5
0
    public override EnemySteering GetSteering()
    {
        EnemySteering steering = new EnemySteering();

        steering.linear = target.transform.position - transform.position;
        steering.linear.Normalize();
        steering.linear = steering.linear * agent.maxAccel;

        return(steering);
    }
Example #6
0
    //overload for above, use when this enemy is supposed to be following a SteerPath
    public GameObject genMonster(float x, float y, float z, int monsterCode, SteerPath p)
    {
        GameObject    e     = genMonster(x, y, z, monsterCode);
        EnemySteering steer = e.GetComponent <EnemySteering>();

        steer.enabled = true;
        steer.setPath(pathManager, p);
        e.GetComponent <EnemyMover>().enabled = false;

        return(e);
    }
Example #7
0
    public bool sprite_on_child;     //whether the visual representation of this enemy is on its child


    void Start()
    {
        if (!audioz)
        {
            audioz = GameObject.FindWithTag("AudioStorage").GetComponent <AudioStorage>();
        }

        setSizeScale(sizeScale);

        setColliderScale(colliderScale);

        if (!steer)
        {
            steer = GetComponent <EnemySteering>();
        }
    }
Example #8
0
    protected virtual void Update()
    {
        ProceedPauseUnpause();
        if (Pause.Paused || !allowMovement)
        {
            return;
        }

        Vector2 displacement = velocity * Time.deltaTime;

        orientation += rotation * Time.deltaTime;

        orientation %= 360.0f;
        if (orientation < 0.0f)
        {
            orientation += 360.0f;
        }
        transform.Translate(displacement, Space.World);
        transform.rotation = Quaternion.Euler(0, 0, -orientation);

        var behaviors = GetComponents <EnemyBehavior>();

        foreach (var i in behaviors)
        {
            i.CalledUpdate();
        }

        velocity += steering.linear * Time.deltaTime;
        rotation += steering.angular * Time.deltaTime;
        if (velocity.magnitude > maxSpeed)
        {
            velocity.Normalize();
            velocity = velocity * maxSpeed;
        }
        steering = new EnemySteering();
    }
Example #9
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);
    }
Example #10
0
 public void SetSteering(EnemySteering steering, float weight)
 {
     this.steering.linear  += steering.linear * weight;
     this.steering.angular += steering.angular * weight;
 }