コード例 #1
0
ファイル: WeatherSystem.cs プロジェクト: cribin/GameLab2017
        private void CreateLightningEffect()
        {
            float   startX = random.Next((int)baseScreenSize.X);
            Vector2 endPos = new Vector2(random.Next((int)baseScreenSize.X), baseScreenSize.Y - 10);

            int burnFoliageProb = random.Next(3);

            if (burnFoliageProb < 2 && foliages.Count > 0)
            {
                Foliage chosenFoliage = foliages[random.Next(foliages.Count)];
                chosenFoliage.StartBurning();
                Vector3 tmp  = chosenFoliage.GetPosition();
                Vector3 size = chosenFoliage.GetSize();
                if (chosenFoliage.Moving)
                {
                    endPos = new Vector2(tmp.X, tmp.Y + size.Y);
                }
                else
                {
                    endPos = new Vector2(tmp.X, tmp.Y);
                }
            }

            branchLightning = new BranchLightning(new Vector2(startX, -2),
                                                  endPos);
            scene.LightningManager.CurrentLightnings.Add(branchLightning);
        }
コード例 #2
0
    // Update is called once per frame
    void Update()
    {
        if (time <= 0)
        {
            CreateBranch();
            ResetTime();
        }

        time = time - Time.deltaTime;

        for (int i = branches.Count - 1; i >= 0; i--)
        {
            //pull the branch lightning component
            BranchLightning branchComponent = branches[i].GetComponent <BranchLightning>();

            //If it's faded out already
            if (branchComponent.IsComplete)
            {
                //destroy it
                Destroy(branches[i]);

                //take it out of our list
                branches.RemoveAt(i);

                //move on to the next branch
                continue;
            }

            //draw and update the branch
            branchComponent.UpdateBranch();
            branchComponent.Draw();
        }
    }
コード例 #3
0
    void summonLightning(Enemy targetEnemy)
    {
        lightningAudio.Play();
        List <Enemy> enemyPoolInstant = new List <Enemy>();

        foreach (Enemy enemy in EnemyPool.enemyPool)
        {
            enemyPoolInstant.Add(enemy);
        }

        enemyList.Add(targetEnemy);
        StartCoroutine(delayUntilClear(targetEnemy));

        foreach (Enemy enemy in enemyPoolInstant)
        {
            if (enemy != targetEnemy && Vector2.Distance(targetEnemy.transform.position, enemy.transform.position) <= 5 && !enemyList.Contains(enemy))
            {
                enemyList.Add(enemy);
                GameObject      branchObj       = (GameObject)GameObject.Instantiate(branchLightningPrefab);
                BranchLightning branchLightning = branchObj.GetComponent <BranchLightning>();
                branchLightning.Initialize(targetEnemy.transform.position, enemy.transform.position, boltPrefab);
                branchLightnings.Add(branchLightning);
                enemy.dealDamage(5);
                StartCoroutine(delayUntilClear(enemy));
            }
        }
    }
コード例 #4
0
    void CreateBranch()
    {
        GameObject      branchObj       = (GameObject)GameObject.Instantiate(branchPrefab);
        BranchLightning branchComponent = branchObj.GetComponent <BranchLightning>();

        var pos1 = topLeft.transform.position;
        var pos2 = bottomRight.transform.position;

        var x = Random.Range(pos1.x, pos2.x);
        var y = Random.Range(pos2.y, pos2.y);

        var start = new Vector2(x, y + Random.Range(heightMin, heightMax));
        var end   = new Vector2(x + Random.Range(-diagonal, diagonal), y);

        branchComponent.Initialize(start, end, boltPrefab);

        branches.Add(branchObj);
    }
コード例 #5
0
    private void Update()
    {
        for (int i = branchLightnings.Count - 1; i >= 0; i--)
        {
            BranchLightning branchComponent = branchLightnings[i];

            if (branchComponent.IsComplete)
            {
                Destroy(branchLightnings[i]);
                branchLightnings.RemoveAt(i);

                continue;
            }

            branchComponent.UpdateBranch();
            branchComponent.Draw();
        }
    }
コード例 #6
0
    void Update()
    {
        //Declare variables for use later
        GameObject    boltObj;
        LightningBolt boltComponent;

        //store off the count for effeciency
        int activeLineCount = activeBoltsObj.Count;

        //loop through active lines (backwards because we'll be removing from the list)
        for (int i = activeLineCount - 1; i >= 0; i--)
        {
            //pull GameObject
            boltObj = activeBoltsObj[i];

            //get the LightningBolt component
            boltComponent = boltObj.GetComponent <LightningBolt>();

            //if the bolt has faded out
            if (boltComponent.IsComplete)
            {
                //deactive the segments it contains
                boltComponent.DeactivateSegments();

                //set it inactive
                boltObj.SetActive(false);

                //move it to the inactive list
                activeBoltsObj.RemoveAt(i);
                inactiveBoltsObj.Add(boltObj);
            }
        }

        //check for key press and set mode accordingly
        if (Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1))
        {
            shouldText  = false;
            currentMode = Mode.bolt;
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2) || Input.GetKeyDown(KeyCode.Keypad2))
        {
            shouldText  = false;
            currentMode = Mode.branch;
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3) || Input.GetKeyDown(KeyCode.Keypad3))
        {
            shouldText  = false;
            currentMode = Mode.moving;
        }
        else if (Input.GetKeyDown(KeyCode.Alpha4) || Input.GetKeyDown(KeyCode.Keypad4))
        {
            shouldText  = true;
            currentMode = Mode.text;
        }
        else if (Input.GetKeyDown(KeyCode.Alpha5) || Input.GetKeyDown(KeyCode.Keypad5))
        {
            shouldText  = false;
            currentMode = Mode.nodes;
        }
        else if (Input.GetKeyDown(KeyCode.Alpha6) || Input.GetKeyDown(KeyCode.Keypad6))
        {
            shouldText  = false;
            currentMode = Mode.burst;
        }

        //If left mouse button pressed
        if (Input.GetMouseButtonDown(0))
        {
            //if first click
            if (clicks == 0)
            {
                //store starting position
                Vector3 temp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                pos1 = new Vector2(temp.x, temp.y);
            }
            else if (clicks == 1)             //second click
            {
                //store end position
                Vector3 temp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                pos2 = new Vector2(temp.x, temp.y);

                //Handle the current mode appropriately
                switch (currentMode)
                {
                case Mode.bolt:
                    //create a (pooled) bolt from pos1 to pos2
                    CreatePooledBolt(pos1, pos2, Color.white, 1f);
                    break;

                case Mode.branch:
                    //instantiate from our branch prefab
                    GameObject branchObj = (GameObject)GameObject.Instantiate(BranchPrefab);

                    //get the branch component
                    BranchLightning branchComponent = branchObj.GetComponent <BranchLightning>();

                    //initialize the branch component using our position data
                    branchComponent.Initialize(pos1, pos2, BoltPrefab);

                    //add it to the list of active branches
                    branchesObj.Add(branchObj);
                    break;

                case Mode.moving:
                    //Prevent from getting a 0 magnitude (0 causes errors
                    if (Vector2.Distance(pos1, pos2) <= 0)
                    {
                        //Try a random position
                        Vector2 adjust = Random.insideUnitCircle;

                        //failsafe
                        if (adjust.magnitude <= 0)
                        {
                            adjust.x += .1f;
                        }

                        //Adjust the end position
                        pos2 += adjust;
                    }

                    //Clear out any old moving bolt (this is designed for one moving bolt at a time)
                    for (int i = movingBolt.Count - 1; i >= 0; i--)
                    {
                        Destroy(movingBolt[i]);
                        movingBolt.RemoveAt(i);
                    }

                    //get the "velocity" so we know what direction to send the bolt in after initial creation
                    lightningVelocity = (pos2 - pos1).normalized;

                    //instantiate from our bolt prefab
                    boltObj = (GameObject)GameObject.Instantiate(BoltPrefab);

                    //get the bolt component
                    boltComponent = boltObj.GetComponent <LightningBolt>();

                    //initialize it with 5 max segments
                    boltComponent.Initialize(5);

                    //activate the bolt using our position data
                    boltComponent.ActivateBolt(pos1, pos2, Color.white, 1f);

                    //add it to our list
                    movingBolt.Add(boltObj);
                    break;

                case Mode.burst:
                    //get the difference between our two positions (destination - source = vector from source to destination)
                    Vector2 diff = pos2 - pos1;

                    //define how many bolts we want in our circle
                    int boltsInBurst = 10;

                    for (int i = 0; i < boltsInBurst; i++)
                    {
                        //rotate around the z axis to the appropriate angle
                        Quaternion rot = Quaternion.AngleAxis((360f / boltsInBurst) * i, new Vector3(0, 0, 1));

                        //Calculate the end position for the bolt
                        Vector2 boltEnd = (Vector2)(rot * diff) + pos1;

                        //create a (pooled) bolt from pos1 to boltEnd
                        CreatePooledBolt(pos1, boltEnd, Color.white, 1f);
                    }

                    break;
                }
            }

            //increment our tick count
            clicks++;

            //restart the count after 2 clicks
            if (clicks > 1)
            {
                clicks = 0;
            }
        }

        //if in node mode
        if (currentMode == Mode.nodes)
        {
            //constantly create a (pooled) bolt between the two assigned positions
            CreatePooledBolt(pos1, pos2, Color.white, 1f);
        }

        //loop through any active branches
        for (int i = branchesObj.Count - 1; i >= 0; i--)
        {
            //pull the branch lightning component
            BranchLightning branchComponent = branchesObj[i].GetComponent <BranchLightning>();

            //If it's faded out already
            if (branchComponent.IsComplete)
            {
                //destroy it
                Destroy(branchesObj[i]);

                //take it out of our list
                branchesObj.RemoveAt(i);

                //move on to the next branch
                continue;
            }

            //draw and update the branch
            branchComponent.UpdateBranch();
            branchComponent.Draw();
        }

        //loop through all of our bolts that make up the moving bolt
        for (int i = movingBolt.Count - 1; i >= 0; i--)
        {
            //get the bolt component
            boltComponent = movingBolt[i].GetComponent <LightningBolt>();

            //if the bolt has faded out
            if (boltComponent.IsComplete)
            {
                //destroy it
                Destroy(movingBolt[i]);

                //remove it from our list
                movingBolt.RemoveAt(i);

                //on to the next one, on on to the next one
                continue;
            }

            //update and draw bolt
            boltComponent.UpdateBolt();
            boltComponent.Draw();
        }

        //if our moving bolt is active
        if (movingBolt.Count > 0)
        {
            //calculate where it currently ends
            lightningEnd = movingBolt[movingBolt.Count - 1].GetComponent <LightningBolt>().End;

            //if the end of the bolt is within 25 units of the camera
            if (Vector2.Distance(lightningEnd, (Vector2)Camera.main.transform.position) < 25)
            {
                //instantiate from our bolt prefab
                boltObj = (GameObject)GameObject.Instantiate(BoltPrefab);

                //get the bolt component
                boltComponent = boltObj.GetComponent <LightningBolt>();

                //initialize it with a maximum of 5 segments
                boltComponent.Initialize(5);

                //activate the bolt using our position data (from the current end of our moving bolt to the current end + velocity)
                boltComponent.ActivateBolt(lightningEnd, lightningEnd + lightningVelocity, Color.white, 1f);

                //add it to our list
                movingBolt.Add(boltObj);

                //update and draw our new bolt
                boltComponent.UpdateBolt();
                boltComponent.Draw();
            }
        }

        //if in text mode
        if (shouldText)
        {
            //go through the points we capture earlier
            foreach (Vector2 point in textPoints)
            {
                //randomly ignore certain points
                if (Random.Range(0, 75) != 0)
                {
                    continue;
                }

                //placeholder values
                Vector2 nearestParticle    = Vector2.zero;
                float   nearestDistSquared = float.MaxValue;

                for (int i = 0; i < 50; i++)
                {
                    //select a random point
                    Vector2 other = textPoints[Random.Range(0, textPoints.Count)];

                    //calculate the distance (squared for performance benefits) between the two points
                    float distSquared = DistanceSquared(point, other);

                    //If this point is the nearest point (but not too near!)
                    if (distSquared < nearestDistSquared && distSquared > 3 * 3)
                    {
                        //store off the data
                        nearestDistSquared = distSquared;
                        nearestParticle    = other;
                    }
                }

                //if the point we found isn't too near/far
                if (nearestDistSquared < 25 * 25 && nearestDistSquared > 3 * 3)
                {
                    //create a (pooled) bolt at the corresponding screen position
                    CreatePooledBolt((point * scaleText) + positionText, (nearestParticle * scaleText) + positionText, new Color(Random.value, Random.value, Random.value, 1f), 1f);
                }
            }
        }

        //update and draw active bolts
        for (int i = 0; i < activeBoltsObj.Count; i++)
        {
            activeBoltsObj[i].GetComponent <LightningBolt>().UpdateBolt();
            activeBoltsObj[i].GetComponent <LightningBolt>().Draw();
        }
    }
コード例 #7
0
    void Update()
    {
        bolttimer -= Time.deltaTime;
        //Declare variables for use later
        GameObject    boltObj;
        LightningBolt boltComponent;

        //store off the count for effeciency
        int activeLineCount = activeBoltsObj.Count;

        //loop through active lines (backwards because we'll be removing from the list)
        for (int i = activeLineCount - 1; i >= 0; i--)
        {
            //pull GameObject
            boltObj = activeBoltsObj[i];

            //get the LightningBolt component
            boltComponent = boltObj.GetComponent <LightningBolt>();

            //if the bolt has faded out
            if (boltComponent.IsComplete)
            {
                //deactive the segments it contains
                boltComponent.DeactivateSegments();

                //set it inactive
                boltObj.SetActive(false);

                //move it to the inactive list
                activeBoltsObj.RemoveAt(i);
                inactiveBoltsObj.Add(boltObj);
            }
        }

        //If left mouse button pressed
        if (bolttimer <= 0)
        {
            sfxMan.Lightning.Play();
            bolttimer = timebetweenbolts * Random.Range(1.0f, 2.0f);
            ///Here we need to say if our timer is up we get our camera locations and set up our start/end
            ///THen add it to our active branches
            /////////////////////////////////////////////////////////////////////////////////////////////////
            ///Under our update
            cameraCenterOffsetX = Random.Range(-1.0f, 1.0f);
            StartP = new Vector3
                         (theCameraController.transform.position.x + (halfWidth * cameraCenterOffsetX),
                         theCameraController.transform.position.y + halfHeight,
                         theCameraController.transform.position.z);
            cameraCenterOffsetY = Random.Range(0.5f, 1.0f);
            EndP = new Vector3
                       (theCameraController.transform.position.x + (halfWidth * cameraCenterOffsetX),
                       theCameraController.transform.position.y * cameraCenterOffsetY,
                       theCameraController.transform.position.z);
            //Store Start pos
            pos1 = new Vector2(StartP.x, StartP.y);
            //Store End pos
            pos2 = new Vector2(EndP.x, EndP.y);

            //instantiate from our branch prefab
            GameObject branchObj = (GameObject)GameObject.Instantiate(BranchPrefab);

            //get the branch component
            BranchLightning branchComponent = branchObj.GetComponent <BranchLightning>();

            //initialize the branch component using our position data
            branchComponent.Initialize(pos1, pos2, BoltPrefab);

            //add it to the list of active branches
            branchesObj.Add(branchObj);
        }

        //loop through any active branches
        for (int i = branchesObj.Count - 1; i >= 0; i--)
        {
            //pull the branch lightning component
            BranchLightning branchComponent = branchesObj[i].GetComponent <BranchLightning>();

            //If it's faded out already
            if (branchComponent.IsComplete)
            {
                //destroy it
                Destroy(branchesObj[i]);

                //take it out of our list
                branchesObj.RemoveAt(i);

                //move on to the next branch
                continue;
            }

            //draw and update the branch
            branchComponent.UpdateBranch();
            branchComponent.Draw();
        }

        //update and draw active bolts
        for (int i = 0; i < activeBoltsObj.Count; i++)
        {
            activeBoltsObj[i].GetComponent <LightningBolt>().UpdateBolt();
            activeBoltsObj[i].GetComponent <LightningBolt>().Draw();
        }
    }