/// <summary> /// This method, moreover, which adds the object to the field monitors to the object so that the object got its current position in the matrix. /// It is necessary for Cell. When we calculate their direction of movement, we work through a list of cells, /// instead of through the elements matrix (field of this class). That has to be stored in the object coordinates and change them when moving. /// <para></para> /// Этот метод, кроме того, что добавляет объект на поле, следит чтоб объект чтоб объект получил свои текущие координаты в матрице. /// Это нужно для Cell. Когда мы высчитываем их направление движения, мы работаем через список клеток, /// а не через матрицу элементов (речь о полях данного класса). Вот и приходится хранить координаты в самом объекте и менять их при перемещении. /// </summary> void SetMatrixElement(int x, int y, UniverseObject universeObject) { universeMatrix[x, y] = universeObject; if (universeObject != null) { universeObject.SetCords(x, y); } }
/// <summary> /// Extended version of SetMatrixElement. Among other things, monitoring the removal of the prev item. /// <para></para> /// Расширенная версия SetMatrixElement. Кроме прочего, следит за удалением старого элемента. /// </summary> bool AddUniverseObject(int x, int y, UniverseObject universeObject, bool canReSetPrevObject) { if (GetMatrixElement(x, y) == null) { SetMatrixElement(x, y, universeObject); return(true); } else if (canReSetPrevObject || GetMatrixElement(x, y).IsDisposed()) { GetMatrixElement(x, y).Dispose(); SetMatrixElement(x, y, universeObject); return(true); } return(false); }
bool AddUniverseObject(int x, int y, UniverseObject universeObject, bool canReSetPrevObject) { if (universeMatrix[x][y] == null) { SetMatrixElement(x, y, universeObject); universeObject.SetUniverse(this); return(true); } else if (canReSetPrevObject || universeMatrix[x][y].isDisposed()) { universeMatrix[x][y].Dispose(); SetMatrixElement(x, y, universeObject); universeObject.SetUniverse(this); return(true); } return(false); }
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; }
public Universe(int width, int height) { this.width = width; this.height = height; universeMatrix = new UniverseObject[width][]; for (int i = 0; i < width; i++) { universeMatrix[i] = new UniverseObject[height]; } for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { AddUniverseObject(i, j, new UniverseObject(), false); } } Initialize(); }
bool AddUniverseObject(int x, int y, UniverseObject universeObject, bool canReSetPrevObject) { if (!ValidateCords(x, y)) { return(false); } if (GetMatrixElement(x, y) == null) { SetMatrixElement(x, y, universeObject); universeObject.SetUniverse(this); return(true); } if (canReSetPrevObject || GetMatrixElement(x, y).isDisposed()) { GetMatrixElement(x, y).Dispose(); SetMatrixElement(x, y, universeObject); universeObject.SetUniverse(this); return(true); } return(false); }
void RelocateUniverseObject(int x1, int y1, int x2, int y2) { SetMatrixElement(x2, y2, universeMatrix[x1][y1]); universeMatrix[x1][y1] = new UniverseObject(); }
void SetMatrixElement(int x, int y, UniverseObject universeObject) { universeMatrix[x][y] = universeObject; universeObject.SetCords(x, y); }
/// <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); } } } }