EnergyUsingObject.Update C# (CSharp) Method

Update() private method

Update this instance. /// Updates the energy use. Does necessary calculations for averages and totals. should be called each frame from the energy hub.
private Update ( ) : void
return void
	void Update () {

		bool shouldDrainBattery = false;

		//get how much energy to add based on average per sec.
		float energyToAdd = powerSupply.getAvgUsePerSec() * Time.deltaTime;

		if (powerSupply.getIsEnergySupplied()) { //hub supplying energy.

			bool shouldChargeBattery = false;

			if (powerSupply.getIsOn()) { //if on.

				if (powerSupply.getIsUsingBattery()) { //using battery.. don't use enrgy.
					shouldDrainBattery = true;
				}

				else { //not using battery, power on, energy supplied... USE ENERGY...!
					//add to total usage.
					powerSupply.addTotalUse(energyToAdd);

					shouldChargeBattery = true;
				}

			}

			else { //turned off.
				shouldChargeBattery = true;
			}


			if (shouldChargeBattery) {
				
				//and charge battery as well.
				int numB = getNumBatteries();
				for (int i=0; i<numB; i++) {
					Battery b = powerSupply.getBatteryList()[i];
					if (!b.getIsFull()) {
						//found a not full battery.. charge this one and break.. so you don't charge the others too.
						b.setEnergy(b.getCurEnergy() + energyToAdd);
						break;
					}
				}
				
			}

		}

		else { //no energy supplied from hub.

			if (powerSupply.getIsOn()) {
				if (powerSupply.getIsUsingBattery()) {
					shouldDrainBattery = true;
				}
			}

		}


		if (shouldDrainBattery) {
			//Debug.Log("Using battery");
			//start depleting the first available battery that's not empty.
			int numB = getNumBatteries();
			for (int i=0; i<numB; i++) {
				Battery b = powerSupply.getBatteryList()[i];
				if (!b.getIsEmpty()) {
					//found a not empty battery.. deplete this one and 'break'.. so you don't empty the others too.
					b.setEnergy(b.getCurEnergy() - energyToAdd);
					break;
				}
			}
		}


	}