private void PlayerDisplacementChunkEvents() { double distance = MVector3.Distance2D(_lastPlayerTriggeredPosition, PlayerManager.Player.Position); //Triggered when player has move a distance of 8 blocks (half chunk distance) if (distance > (AbstractChunk.ChunkSize.X / 2d)) { var newEventNotificationArea = new Range3I { Position = BlockHelper.EntityToChunkPosition(PlayerManager.Player.Position) - _eventNotificationArea.Size / 2, Size = _eventNotificationArea.Size }; var chunks2Syncro = newEventNotificationArea.AllExclude(_eventNotificationArea); if (chunks2Syncro != null) { bool synchroFullyRequested = true; //Get all new chunk in the area that are in a state ready to be requested ! //Check that the concerned chunks are in a correct state to be requested. foreach (var chunkPosition in chunks2Syncro) { if (ResyncChunk(chunkPosition, false) == false) { synchroFullyRequested = false; break; } } if (synchroFullyRequested) { _eventNotificationArea = newEventNotificationArea; } } _lastPlayerTriggeredPosition = PlayerManager.Player.Position; ChunkNeed2BeSorted = true; } }
/// <summary> /// Sort the chunks array if needed /// </summary> private void SortChunks() { if (!ChunkNeed2BeSorted || _camManager.ActiveCamera == null) { return; } //Compute Distance Squared from Chunk Center to Camera foreach (var chunk in Chunks) { chunk.DistanceFromPlayer = MVector3.Distance2D(chunk.ChunkCenter, PlayerManager.CameraWorldPosition); } //Sort by this distance int index = 0; foreach (var chunk in Chunks.OrderBy(x => x.DistanceFromPlayer)) { SortedChunks[index] = chunk; index++; } ChunkNeed2BeSorted = false; }
public void VoxelDraw(DeviceContext context, Matrix viewProjection) { //Applying Correct Render States RenderStatesRepo.ApplyStates(context, DXStates.Rasters.Default, DXStates.Blenders.Disabled, DXStates.DepthStencils.DepthReadWriteEnabled); _voxelModelEffect.Begin(context); _voxelModelEffect.CBPerFrame.Values.SunVector = _skyDome.LightDirection; _voxelModelEffect.CBPerFrame.Values.ViewProjection = Matrix.Transpose(viewProjection); _voxelModelEffect.CBPerFrame.IsDirty = true; _voxelModelEffect.Apply(context); //Draw each buffered Models ===================================== foreach (var modelAndInstances in _models) { //For each instance of the model that have received a body foreach (var pairs in modelAndInstances.Value.Instances.Where(x => x.Value != null)) { var entityToRender = _dynamicEntitiesDico[pairs.Key]; var modelInstance = pairs.Value; //Draw only the entities that are in Client view range //if (_visualWorldParameters.WorldRange.Contains(entityToRender.VisualEntity.Position.ToCubePosition())) bool isInRenderingRange = MVector3.Distance2D(entityToRender.VoxelEntity.Position, _camManager.ActiveCamera.WorldPosition.ValueInterp) <= _staticEntityViewRange; //Can only see dead persons if your are dead yourself or if its your own body ! bool showCharacters = entityToRender.DynamicEntity.HealthState != DynamicEntityHealthState.Dead || IsLocalPlayer(entityToRender.DynamicEntity.DynamicId) || _playerEntityManager.Player.HealthState == DynamicEntityHealthState.Dead; if (isInRenderingRange && showCharacters) { modelInstance.World = Matrix.Scaling(1f / 16) * Matrix.Translation(entityToRender.WorldPosition.ValueInterp.AsVector3()); modelInstance.LightColor = entityToRender.ModelLight.ValueInterp; } else { modelInstance.World = Matrix.Zero; } } if (modelAndInstances.Value.VisualModel != null && modelAndInstances.Value.Instances != null) { var instancesToDraw = modelAndInstances.Value.Instances.Values.Where(x => x.World != Matrix.Zero).ToList(); //Draw only those where the matrix is different than Zero modelAndInstances.Value.VisualModel.DrawInstanced(_d3DEngine.ImmediateContext, _voxelModelEffect, instancesToDraw); } } _voxelToolEffect.Begin(context); _voxelToolEffect.CBPerFrame.Values.LightDirection = _skyDome.LightDirection; _voxelToolEffect.CBPerFrame.Values.ViewProjection = Matrix.Transpose(viewProjection); _voxelToolEffect.CBPerFrame.IsDirty = true; _voxelToolEffect.Apply(context); // draw tools ================ foreach (var pair in _dynamicEntitiesDico) { var charEntity = pair.Value.DynamicEntity as CharacterEntity; if (charEntity != null && pair.Value.ModelInstance != null && pair.Value.ModelInstance.World != Matrix.Zero) { //Take the Tool entity equiped in character Right hand if (charEntity.Equipment.RightTool is CubeResource) { DrawCube(context, (CubeResource)charEntity.Equipment.RightTool, charEntity); } else if (charEntity.Equipment.RightTool is IVoxelEntity) { var voxelItem = charEntity.Equipment.RightTool as IVoxelEntity; if (!string.IsNullOrEmpty(voxelItem.ModelName)) //Check if a voxel model is associated with the entity { DrawTool(voxelItem, charEntity); } } } } }