public static GsRectangle CreateBoxAroundPoint(GsVector pt, GsVector offset, float width, float height) { GsRectangle box = new GsRectangle(pt.X - (width / 2), pt.Y - (height / 2), width, height); box.Offset(offset); return(box); }
public void ChooseTarget(IEnumerable <Invader> invaders, GsVector offset) { Invader bestInvader = null; float bestFuzzyValue = 0.0f; float radiusSquared = Radius * Radius; GsRectangle pieceBounds = GsRectangle.Offset(Bounds, offset); foreach (Invader invader in invaders) { // calculate the position. In the future, we should get this directly from the invader GsVector position = invader.Position + offset + (invader.Size.ToVector() * .5f); GsVector origin = invader.Origin; GsSize size = invader.Size; // calculate the bounds GsRectangle invaderBounds = new GsRectangle(position - (origin * .5f), size); // determine the distance float distance = GsVector.DistanceSquared(pieceBounds.Center, invaderBounds.Center); if (InvalidTarget(distance, radiusSquared, invader, invaderBounds)) { continue; } // determine the fuzzy number float fuzzy = 0.0f; fuzzy += CalculateFuzzyLife(invader) * FuzzyLifeWeight; fuzzy += CalculateFuzzyTime(invader) * FuzzyTimeWeight; fuzzy += CalculateFuzzyDistance(distance) * FuzzyDistanceWeight; // if the fuzzy value is better, then select this invader if (fuzzy > bestFuzzyValue) { bestInvader = invader; bestFuzzyValue = fuzzy; } } if (bestInvader == null) { Target = null; } else if (bestInvader != Target) { mTimeChasingTarget = TimeSpan.Zero; Target = bestInvader; } }
private void InitializeGrid() { int midCol = (NumCols - 1) / 2; int midRow = (NumRows - 1) / 2; int midColLeft = midCol - HalfThroughWay; int midColRight = midCol + HalfThroughWay; int midRowUp = midRow - HalfThroughWay; int midRowDown = midRow + HalfThroughWay; int dc = (NumCols - 1) % 2; int dr = (NumRows - 1) % 2; Grid = new GridCell[NumCols, NumRows]; for (int c = 0; c < NumCols; ++c) { for (int r = 0; r < NumRows; ++r) { bool isOuter = (c == 0 || r == 0 || c == (NumCols - 1) || r == (NumRows - 1)); bool isThroughway = isOuter && (GsMath.InRange(c, midColLeft + dc, midColRight) || GsMath.InRange(r, midRowUp + dr, midRowDown)); GridCell cell = new GridCell(c, r, isOuter, isThroughway); cell.Bounds = new GsRectangle(c * CellWidth, r * CellHeight, CellWidth, CellHeight); Grid[c, r] = cell; } } HorzGoalCell = Grid[NumCols - 1, midRow]; VertGoalCell = Grid[midCol, NumRows - 1]; HorzStartCell = Grid[0, midRow]; VertStartCell = Grid[midCol, 0]; gridBounds = GsRectangle.FromLTRB(Grid[0, 0].X, Grid[0, 0].Y, Grid[NumCols - 1, 0].X + CellWidth, Grid[0, NumRows - 1].Y + CellHeight); gridBounds.Offset(MiddleOffset + Position); }