private void UpdateVision(VisionPosition mp, float visionRange, int player, short groundLevel) { // clear local cache if (raycastEnabled && _cacheVisible) { temporaryVisibleData.Clear(); } if (!updateMethod) { UpdateVision1(mp, visionRange, player, groundLevel); } else { UpdateVision2(mp, visionRange, player, groundLevel); } }
private void UpdateVision2(VisionPosition mp, float visionRange, int player, short groundLevel) { int radius = Mathf.FloorToInt(visionRange / _localScale.x) - 1; if (radius <= 0) { return; } int x0 = mp.x; int y0 = mp.y; int x = radius; int y = 0; int xChange = 1 - (radius << 1); int yChange = 0; int radiusError = 0; while (x >= y) { for (var i = x0 - x; i <= x0 + x; i++) { DrawPixel(player, x0, y0, i, y0 + y, groundLevel); DrawPixel(player, x0, y0, i, y0 - y, groundLevel); } for (var i = x0 - y; i <= x0 + y; i++) { DrawPixel(player, x0, y0, i, y0 + x, groundLevel); DrawPixel(player, x0, y0, i, y0 - x, groundLevel); } y++; radiusError += yChange; yChange += 2; if (((radiusError << 1) + xChange) > 0) { x--; radiusError += xChange; xChange += 2; } } }
private void UpdateVision1(VisionPosition mp, float visionRange, int player, short groundLevel) { var visionPosition = GetWorldPosition(mp.x, mp.y); var currentRowSize = 0; var currentColSize = 0; var rangeSqr = visionRange * visionRange; var visionWidth = Mathf.RoundToInt(visionRange / _localScale.x); var visionHeight = Mathf.RoundToInt(visionRange / _localScale.y); var maxColSize = visionWidth; var maxRowSize = visionHeight; while (currentRowSize != maxRowSize && currentColSize != maxColSize) { var x = -currentColSize; var y = -currentRowSize; var dx = 1; var dy = 0; while (true) { // check current var mx = mp.x + x; var my = mp.y + y; var p = GetWorldPosition(mx, my); var diff = p - visionPosition; if (mx >= 0 && mx < width && my >= 0 && my < height) { if (diff.sqrMagnitude < rangeSqr) { var blocked = raycastEnabled && IsBlocked(groundLevel, mx, my, mp.x, mp.y); if (!blocked) { visionData.StoreFlagValue(player, mx, my); previousVisionData.StoreFlagValue(player, mx, my); } } } if (x + dx > currentColSize) { dx = 0; dy = 1; } if (y + dy > currentRowSize) { dx = -1; dy = 0; } if (x + dx < -currentColSize) { dx = 0; dy = -1; } if (y + dy < -currentRowSize) { // completed the cycle break; } x += dx; y += dy; } if (currentRowSize < maxRowSize) { currentRowSize++; } if (currentColSize < maxColSize) { currentColSize++; } } }