private void NotifyOriginShift() { if (OnOriginShift != null) { OnOriginShift.Invoke(this, new FloatingOriginShiftEventArgs() { ShiftAmount = maxOriginPoint }); } }
public static void KeepOriginForPosition(Vector2 position) { Point pos = position.Settle(); //shift when appropriate Point shiftDirection = Point.Zero; if (pos.X > REGION_SIZE_IN_METERS) { shiftDirection = new Point(pos.X / REGION_SIZE_IN_METERS, 0); } else if (pos.X < -REGION_SIZE_IN_METERS) { shiftDirection = new Point(pos.X / REGION_SIZE_IN_METERS, 0); } // up to 100,000 from origin ok on nvidia 500 series... let's just shift every 10,000k then //todo if (shiftDirection != Point.Zero) //FIXME origin shift atm is slow and naive { // get previous regions range int prevAbsRegionsStart = currentOriginInRegions; int prevAbsRegionsEnd = currentOriginInRegions + 4; // store regions shift amount currentOriginInRegions += shiftDirection.X; // get current regions range int curAbsRegionsStart = currentOriginInRegions; int curAbsRegionsEnd = currentOriginInRegions + 4; // cleanup regions outta range for (var i = 0; i < chunks_.Length; i++) { Chunk chunk = chunks_[i]; int relRegionX = i >> REGION_LOG_SIZE_IN_CHUNKS; //if chunk abs region is not in current bounds if (chunk.absRegionX < curAbsRegionsStart || chunk.absRegionX >= curAbsRegionsEnd) { chunk.UnloadChunk(relRegionX); } } // translate all entities Vector2 shift = shiftDirection.ToVector2() * -REGION_SIZE_IN_METERS; Entity.ShiftAllEntities(shift); OnOriginShift?.Invoke(shift); // mark new chunks for loading for (var i = 0; i < chunks_.Length; i++) { int relRegionX = i >> REGION_LOG_SIZE_IN_CHUNKS; int absRegionX = currentOriginInRegions + relRegionX; Chunk chunk = chunks_[i]; chunk.SetRegionX(absRegionX); //load new chunks if (chunk.absRegionX < prevAbsRegionsStart || chunk.absRegionX >= prevAbsRegionsEnd) { chunk.MarkAsNew(); } int relChunkX = i % REGION_SIZE_IN_CHUNKS; DebugHelper.AddDebugText(absRegionX.ToString(), chunk.GetWorldCenter(relRegionX) - Vector2.UnitY * 8, Color.White); DebugHelper.AddDebugText(relChunkX.ToString(), chunk.GetWorldCenter(relRegionX) - Vector2.UnitY * 12, Color.Lime); DebugHelper.AddDebugText(chunk.absRegionX.ToString(), chunk.GetWorldCenter(relRegionX) - Vector2.UnitY * 18, Color.Yellow); } } // load as appropriate TODO culling for (var i = 0; i < chunks_.Length; i++) { chunks_[i].TryLoadChunk(i >> REGION_LOG_SIZE_IN_CHUNKS); } }
private void HandleOriginShift() { UpdateOrigin(); CenterPlayerToOrigin(); OnOriginShift?.Invoke(); }