Beispiel #1
0
    void Awake()
    {
        laserIndex     = -1;
        lasers         = new List <List <laserNode> >();
        laserQueue     = new List <laserNode>();
        laserHits      = new List <laserHit>();
        laserData      = new laserGrid(gridManager.theGrid.getDimX(), gridManager.theGrid.getDimY());
        laserContainer = new GameObject("laserContainer");
        laserContainer.transform.SetParent(gameObject.transform);
        particleContainer = new GameObject("particleContainer");
        particleContainer.transform.SetParent(gameObject.transform);
        refractHits  = new Dictionary <XY, List <dirHeadPlayer> >();
        particleHits = new Dictionary <XY, List <dirHeadPlayer> >();

        for (int i = 0; i < laserLimit; i++)
        {
            // Line object pool
            GameObject lineObject = new GameObject("lineObject");
            lineObject.transform.SetParent(laserContainer.transform);
            LineRenderer line = lineObject.AddComponent <LineRenderer>();
            line.startWidth     = laserWidth;
            line.endWidth       = laserWidth;
            line.numCapVertices = 5;
            line.material       = laserMaterialP1;
            line.enabled        = false;
            // Particle object pool
            GameObject particleObject = Instantiate(hitEffect);
            particleObject.transform.SetParent(particleContainer.transform);
            particleObject.transform.GetChild(0).GetComponent <ParticleSystem>().Stop();
        }
    }
Beispiel #2
0
    private void simulateLasers()
    {
        // Reset laser data grid, causes ocasional garbage collection spike
        laserData = new laserGrid(gridManager.theGrid.getDimX(), gridManager.theGrid.getDimY());

        // Reset counters and check for placed lasers
        iterationCount = 0; laserIndex = -1;
        bool p1LaserFound = false, p2LaserFound = false;

        for (int i = 0; i < gridManager.theGrid.getDimY(); i++)
        {
            if (gridManager.theGrid.getBuilding(0, i) == Building.Laser)
            {
                laserStartP1 = i; p1LaserFound = true;
            }
            if (gridManager.theGrid.getBuilding(gridManager.theGrid.getDimX() - 1, i) == Building.Laser)
            {
                laserStartP2 = i; p2LaserFound = true;
            }
        }

        // Clear old lasers before starting again
        for (int i = 0; i < lasers.Count; i++)
        {
            lasers[i].Clear();
        }
        for (int i = 0; i < laserCounter; i++)
        {
            laserContainer.transform.GetChild(i).GetComponent <LineRenderer>().enabled = false;
        }
        for (int i = 0; i < particleCounter; i++)
        {
            particleContainer.transform.GetChild(i).transform.GetChild(0).GetComponent <ParticleSystem>().Stop();
        }
        laserCounter    = 0;
        particleCounter = 0;

        // Clear laser hits, refract hits
        laserHits.Clear();
        refractHits.Clear();
        particleHits.Clear();

        // Simulate each player's laser
        if (p1LaserFound)
        {
            laserQueue.Add(new laserNode(0, laserStartP1, 1f, laserHeadingP1, Direction.Right, Player.PlayerOne, ++laserIndex, 0));
        }
        if (p2LaserFound)
        {
            laserQueue.Add(new laserNode(gridManager.theGrid.getDimX() - 1, laserStartP2, 1f, laserHeadingP2, Direction.Left, Player.PlayerTwo, ++laserIndex, 0));
        }

        // Loop through the simulation
        while (laserQueue.Count > 0)
        {
            laserStep(laserQueue[0]);
            laserQueue.RemoveAt(0);
        }

        // Trim down unused list elements
        for (int i = lasers.Count - 1; i >= 0; i--)
        {
            if (lasers[i].Count == 0)
            {
                lasers.RemoveAt(i);
            }
        }

        // Draw lasers
        for (int i = 0; i < lasers.Count; i++)
        {
            int last = lasers[i].Count - 1;
            if (lasers[i][0].getOwner() != lasers[i][last].getOwner())
            {
                for (int j = last; j > 0; j--)
                {
                    if (lasers[i][j].getOwner() != Player.Shared)
                    {
                        drawLaser(lasers[i][0], lasers[i][j]); drawLaser(lasers[i][j + 1], lasers[i][last]); break;
                    }
                }
            }
            else
            {
                drawLaser(lasers[i][0], lasers[i][last]);
            }
        }

        // Particles
        int dimX = gridManager.theGrid.getDimX(); int dimY = gridManager.theGrid.getDimY();

        foreach (KeyValuePair <XY, List <dirHeadPlayer> > keyValue in particleHits)
        {
            for (int i = 0; i < keyValue.Value.Count; i++)
            {
                Transform particle = particleContainer.transform.GetChild(particleCounter);
                particle.localPosition    = new Vector3((-dimX / 2) + keyValue.Key.x + 0.5f, 0.01f, (-dimY / 2) + keyValue.Key.y + 0.5f);;
                particle.localEulerAngles = directionToEular(keyValue.Value[i].direction);
                //particle.transform.GetChild(0).GetComponent<ParticleSystem>(). set color? // hit.Value.player == Player.PlayerOne ? red : green;
                particle.transform.GetChild(0).GetComponent <ParticleSystem>().Play();
                particleCounter++;
            }
        }

        // Set needsUpdate to false
        gridManager.theGrid.updateFinished();
    }