コード例 #1
0
ファイル: GameManager.cs プロジェクト: TornOne/Power-Grid
    void FixedUpdate()
    {
        EnergyTransmitter[,] oldState = new EnergyTransmitter[gridXSize, gridYSize];
        float[,] deltas = new float[gridXSize, gridYSize];

        for (int x = 0; x < gridXSize; x++)
        {
            for (int y = 0; y < gridYSize; y++)
            {
                if (grid[x][y].building != null)
                {
                    oldState[x, y] = grid[x][y].building.GetComponent <EnergyTransmitter>();
                }
            }
        }

        for (int x = 0; x < gridXSize; x++)
        {
            for (int y = 0; y < gridYSize; y++)
            {
                if (oldState[x, y] == null)
                {
                    continue;
                }

                List <EnergyTransmitter>        cables   = new List <EnergyTransmitter>();
                List <KeyValuePair <int, int> > cableIds = new List <KeyValuePair <int, int> >();
                if (x > 0 && oldState[x - 1, y] != null && oldState[x - 1, y].currentEnergy < oldState[x, y].currentEnergy)
                {
                    cables.Add(oldState[x - 1, y]);
                    cableIds.Add(new KeyValuePair <int, int>(x - 1, y));
                }
                if (y > 0 && oldState[x, y - 1] != null && oldState[x, y - 1].currentEnergy < oldState[x, y].currentEnergy)
                {
                    cables.Add(oldState[x, y - 1]);
                    cableIds.Add(new KeyValuePair <int, int>(x, y - 1));
                }
                if (x < gridXSize - 1 && oldState[x + 1, y] != null && oldState[x + 1, y].currentEnergy < oldState[x, y].currentEnergy)
                {
                    cables.Add(oldState[x + 1, y]);
                    cableIds.Add(new KeyValuePair <int, int>(x + 1, y));
                }
                if (y < gridYSize - 1 && oldState[x, y + 1] != null && oldState[x, y + 1].currentEnergy < oldState[x, y].currentEnergy)
                {
                    cables.Add(oldState[x, y + 1]);
                    cableIds.Add(new KeyValuePair <int, int>(x, y + 1));
                }

                for (int i = 0; i < cables.Count; i++)
                {
                    KeyValuePair <int, int> cableId = cableIds[i];
                    float delta = Mathf.Min((oldState[x, y].currentEnergy - cables[i].currentEnergy) / (cables.Count + 1), (cables[i].energyCapacity - cables[i].currentEnergy));
                    deltas[cableId.Key, cableId.Value] += delta;
                    deltas[x, y] -= delta;
                }
            }
        }

        for (int x = 0; x < gridXSize; x++)
        {
            for (int y = 0; y < gridYSize; y++)
            {
                if (oldState[x, y] != null)
                {
                    oldState[x, y].currentEnergy += deltas[x, y];
                }
            }
        }
    }
コード例 #2
0
ファイル: EnergyProducer.cs プロジェクト: TornOne/Power-Grid
 void Start()
 {
     transmitter  = GetComponent <EnergyTransmitter>();
     moneyTracker = GameObject.FindGameObjectWithTag("GameManager").GetComponent <MoneyTracker>();
 }