void CalcTotalUniverseEnergy() { long sum = 0; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { UniverseObject uo = GetMatrixElement(i, j); if (uo != null && !uo.IsDisposed() && typeof(IHasEnergy).IsAssignableFrom(uo.GetType())) { sum += (long)(uo as IHasEnergy).GetEnergyLevel(); } } } totalUniverseEnergy = sum; }
/// <summary> /// /// This method is called from DoUniverseTick for all cells. It is calculated for each cell the direction of its movement. /// And as soon as an event occurs: the cell takes damage, moves ... /// <para></para> /// Этот метод вызывается из DoUniverseTick для всех клеток. В нем для каждой клетки высчитывается направление ее движения. /// И сразу же происходит событие: клетка получает урон, перемещается... /// </summary> void HandleCellMove(Cell cell) { if (cell.IsDisposed()) { return; } int x1 = cell.GetX(), y1 = cell.GetY(), x2, y2; cell.CalcMoveDirectionAspiration(this); MoveDirection md = cell.GetMoveDisperation(); switch (md) { case MoveDirection.stand: return; case MoveDirection.up: x2 = x1; y2 = y1 - 1; break; case MoveDirection.down: x2 = x1; y2 = y1 + 1; break; case MoveDirection.left: x2 = x1 - 1; y2 = y1; break; case MoveDirection.right: x2 = x1 + 1; y2 = y1; break; default: return; } if (!ValidateCords(x2, y2)) { return; } UniverseObject unObj = GetMatrixElement(x2, y2); if (unObj == null || unObj.IsDisposed()) { //Если пустое пространство. RelocateUniverseObject(x1, y1, x2, y2); } else { int desc = unObj.GetDescriptor(); if (desc < 0) { //Если еда или яд. cell.AddEnergy((unObj as Food).GetEnergyLevel()); RelocateUniverseObject(x1, y1, x2, y2); } else if (cell.GetDescriptor() == desc) { //Если клетка - родственник cell.AddEnergy(ConstsUniverseProperty.EnergyLevel_MovesFriendly); (unObj as Cell).AddEnergy(ConstsUniverseProperty.EnergyLevel_MovesFriendly); } else { Cell anotherCell = unObj as Cell; if ((ConstsUniverseProperty.Mutation_AttackChildrenMutantsOfFirstGeneration || cell.GetDescriptor() != anotherCell.GetParentDescriptor()) && (ConstsUniverseProperty.Mutation_AttackParentIfCellIsYouMutant || cell.GetParentDescriptor() != anotherCell.GetDescriptor())) { /*Если это клетка с другим геномом, который является ребенком или родителем генома текущей клетки, и при этом включена атака. * Или же если клетки имеют совсем различное происхождение.*/ cell.AddEnergy((float)(ConstsUniverseProperty.EnergyLevel_MovesAggression * 0.8)); anotherCell.AddEnergy(-ConstsUniverseProperty.EnergyLevel_MovesAggression); } } } }