Example #1
0
    // Update is called once per frame
    void Update()
    {
        InputDevice inputDevice = InputManager.ActiveDevice;

        //Select what spell is active
        if (inputDevice.LeftBumper.IsPressed)
        {
            selectedSpell = 0;
        }
        else if (inputDevice.RightBumper.IsPressed)
        {
            selectedSpell = 1;
        }
        else if (inputDevice.LeftTrigger.IsPressed)
        {
            selectedSpell = 2;
        }
        else if (inputDevice.RightTrigger.IsPressed)
        {
            selectedSpell = 3;
        }

        //Deadzone adjustment to the thumbstick input
        bool    aiming;
        float   deadzone   = 0.25f;
        Vector2 stickInput = new Vector2(inputDevice.RightStickX, inputDevice.RightStickY);

        if (stickInput.magnitude < deadzone)
        {
            stickInput = Vector2.zero;
            aiming     = false;
        }
        else
        {
            stickInput = stickInput.normalized * ((stickInput.magnitude - deadzone) / (1 - deadzone));
            aiming     = true;
        }

        float newRotation = Mathf.Atan2(stickInput.y, stickInput.x) * Mathf.Rad2Deg;                 //Calculate the new angle to rotate to

        this.transform.Rotate(0f, 0f, newRotation - this.transform.eulerAngles.z + 90f);             //Rotate

        if (aiming && shotCool <= 0f)                                                                //If the shot has cooled down and the player is aiming
        {
            shotCool += 100f - (stats.frequency * 10f);                                              //Add a value to the cooldown variable equal to the frequency stat times a flat multiplier
            GameObject mySpell   = (GameObject)Instantiate(spell, origin.position, origin.rotation); //Instantiate a spell
            Vector2    direction = origin.position - this.transform.position;                        //Calculate a vector2 of direction based on player position and wand position
            mySpell.rigidbody2D.AddForce(direction * 100f);                                          //Add the force in the direction calculated times the shotspeed stat
            mySpell.GetComponent <SpellShotScript> ().stats   = stats.ReturnCopy();
            mySpell.GetComponent <SpellShotScript> ().myCombo = myCombos [selectedSpell];
        }


        if (shotCool > 0f)
        {
            shotCool -= 1f;             //if the shot is still on cooldown, decriment the shot cooldown
        }
    }
Example #2
0
    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.collider.gameObject.tag == "Enemy")             //On collision, if the object hit is an enemy, begin logic
        {
            List <GameObject> mySpell = new List <GameObject> (); //Create a handle to a new game object

            switch (myCombo.shape)                                //Conditional branch based on what shape the current spell is
            {
            case 0:                                               //If circle
                //Instantiate the circle prefab at the position and rotation of the enemy and set handle of the new object to mySpell
                mySpell.Add((GameObject)Instantiate(circleShape, other.transform.position, other.transform.rotation));
                break;

            case 1:                                     //If line
                //Instantiate a line prefab at the position of the enemy and player rotation
                mySpell.Add((GameObject)Instantiate(lineShape, other.transform.position, rotationAtFire));
                break;

            case 2:                                     //If cluster
                for (int ii = 0; ii < 4; ii++)
                {
                    Vector3 rand = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), Random.Range(-1f, 1f));
                    mySpell.Add((GameObject)Instantiate(clusterShape, other.transform.position + rand, other.transform.rotation));
                }
                break;
            }

            switch (myCombo.element)                    //Conditional branch based on what the current element is
            {
            case 0:                                     //If fire
                foreach (GameObject spell in mySpell)
                {
                    spell.AddComponent <SpellFireScript> ();                                                            //Add a fire script component to the spell
                }
                break;

            case 1:                                     //If spark
                //Add a spark script to the spell
                foreach (GameObject spell in mySpell)
                {
                    spell.AddComponent <SpellShockScript> ();                                                            //Add a fire script component to the spell
                }
                break;

            case 2:                                     //If ice
                                                        //Add an ice script to the spell
                foreach (GameObject spell in mySpell)
                {
                    spell.AddComponent <SpellFrostScript> ();                                                            //Add a fire script component to the spell
                }
                break;

            case 3:                                     //If poison
                                                        //Add a poison script to the spell
                foreach (GameObject spell in mySpell)
                {
                    spell.AddComponent <SpellPoisonScript> ();                                                            //Add a fire script component to the spell
                }
                break;

            case 4:                                     //If wind
                                                        //Add a wind script to the spell
                foreach (GameObject spell in mySpell)
                {
                    spell.AddComponent <SpellWindScript> ();                                                            //Add a fire script component to the spell
                }
                break;

            case 5:                                     //If earth
                                                        //Add an earth script to the spell
                foreach (GameObject spell in mySpell)
                {
                    spell.AddComponent <SpellEarthScript> ();                                                            //Add a fire script component to the spell
                }
                break;
            }

            foreach (GameObject spell in mySpell)
            {
                //Change the scale of the spell to the size stat of the player
                spell.transform.localScale = new Vector3(stats.stats [1], stats.stats [1], stats.stats [1]);
                //Use inheritance to initialize the stat values of damage and duration on the spell script assigned to the spell.
                //All spell scripts inherit from SpellParentScript and inherit the InitializeValues function, so polymorphism works here.
                spell.GetComponent <SpellParentScript> ().stats  = stats.ReturnCopy();
                spell.GetComponent <SpellParentScript> ().myWand = myWand;
            }

            //Destroy the spell shot object
            Destroy(this.gameObject);
        }
        else                            //If it collides with a non-enemy
        //Destroy spell shot with no effect
        {
            Destroy(this.gameObject);
        }
    }