/// <summary> /// Ensures player is above terrain /// </summary> public virtual void Unstuck(bool toSurface = true) { #if UNITY_EDITOR if (env.constructorMode) { return; } #endif if (env.CheckCollision(env.cameraMain.transform.position) || env.CheckCollision(transform.position)) { // try to move to last good position if (m_LastNonCollidingCharacterPos.y < float.MaxValue && !env.CheckCollision(m_LastNonCollidingCharacterPos)) { MoveTo(m_LastNonCollidingCharacterPos); return; } // try up or surface float minAltitude = Mathf.FloorToInt(transform.position.y) + 1.1f; if (toSurface) { minAltitude = Mathf.Max(env.GetTerrainHeight(transform.position), minAltitude); } Vector3 pos = new Vector3(transform.position.x, minAltitude + GetCharacterHeight() * 0.5f, transform.position.z); MoveTo(pos); } }
/// <summary> /// Called by Voxel Play to inform that player has moved onto another chunk so new detail can start generating /// </summary> /// <param name="currentPosition">Current player position.</param> /// <param name="checkOnlyBorders">True means the player has moved to next chunk. False means player position is completely new and all chunks in /// range should be checked for detail in this call.</param> /// <param name="position">Position.</param> public override void ExploreArea(Vector3 position, bool checkOnlyBorders) { int explorationRange = env.visibleChunksDistance + 10; int minz = -explorationRange; int maxz = +explorationRange; int minx = -explorationRange; int maxx = +explorationRange; HoleWorm worm; Vector3 pos = position; for (int z = minz; z <= maxz; z++) { for (int x = minx; x < maxx; x++) { if (checkOnlyBorders && z > minz && z < maxz && x > minx && x < maxx) { continue; } pos.x = position.x + x * 16; pos.z = position.z + z * 16; pos = env.GetChunkPosition(pos); if (WorldRand.GetValue(pos) > 0.98f) { bool born; pos.y = env.GetTerrainHeight(pos); if (pos.y > env.waterLevel && !wormBorn.TryGetValue(pos, out born)) { if (!born) { worm.head = pos; worm.life = 2000; worm.lastX = worm.lastY = worm.lastZ = int.MinValue; worm.ax = worm.ay = worm.az = 0; worms.Add(worm); } wormBorn [pos] = true; } } } } if (!checkOnlyBorders) { for (int k = 0; k < 1000; k++) { if (!DoWork(long.MaxValue)) { break; } } } }
/// <summary> /// Called by Voxel Play to inform that player has moved onto another chunk so new detail can start generating /// </summary> /// <param name="currentPosition">Current player position.</param> /// <param name="checkOnlyBorders">True means the player has moved to next chunk. False means player position is completely new and all chunks in /// range should be checked for detail in this call.</param> /// <param name="position">Position.</param> public override void ExploreArea(Vector3 position, bool checkOnlyBorders) { int explorationRange = env.visibleChunksDistance + 10; int minz = -explorationRange; int maxz = +explorationRange; int minx = -explorationRange; int maxx = +explorationRange; Vector3 pos = position; for (int z = minz; z <= maxz; z++) { for (int x = minx; x < maxx; x++) { if (checkOnlyBorders && z > minz && z < maxz && x > minx && x < maxx) { continue; } pos.x = position.x + x * 16; pos.z = position.z + z * 16; pos = env.GetChunkPosition(pos); if (WorldRand.GetValue(pos) > 0.98f) { BuildingStatus bs; if (!buildingPositions.TryGetValue(pos, out bs)) { float h = env.GetTerrainHeight(pos, false); if (h > env.waterLevel) { bs.height = h; bs.placementStatus = false; // No trees on this chunk VoxelChunk chunk; env.GetChunk(pos, out chunk, false); if (chunk != null) { chunk.allowTrees = false; } } else { bs.placementStatus = true; } buildingPositions[pos] = bs; } } } } }
/// <summary> /// Called by Voxel Play to inform that player has moved onto another chunk so new detail can start generating /// </summary> /// <param name="position">Current player position.</param> /// <param name="checkOnlyBorders">True means the player has moved to next chunk. False means player position is completely new and all chunks in /// range should be checked for detail in this call.</param> /// <param name="endTime">Provides a maximum time frame for execution this frame. Compare this with env.stopwatch milliseconds.</param> public override void ExploreArea(Vector3 position, bool checkOnlyBorders, long endTime) { float prob = Mathf.Clamp01(1f - spawnProbability); int explorationRange = env.visibleChunksDistance + 10; int minz = -explorationRange; int maxz = +explorationRange; int minx = -explorationRange; int maxx = +explorationRange; position = env.GetChunkPosition(position); Vector3 pos = position; bool temp = true; for (int z = minz; z <= maxz; z++) { for (int x = minx; x < maxx; x++) { if (checkOnlyBorders && z > minz && z < maxz && x > minx && x < maxx) { continue; } pos.x = position.x + x * VoxelPlayEnvironment.CHUNK_SIZE; pos.z = position.z + z * VoxelPlayEnvironment.CHUNK_SIZE; temp = true; foreach (KeyValuePair <Vector3, BuildingStatus> kv in buildingPositions) { if (pos != kv.Key) { if (Vector3.Distance(pos, kv.Key) < spawnDistance) { temp = false; break; } } } if (WorldRand.GetValue(pos) > prob && temp) { BuildingStatus bs; if (!buildingPositions.TryGetValue(pos, out bs)) { float h = env.GetTerrainHeight(pos, false); if (h > env.waterLevel) { //added -5 bs.height = h - 5; bs.placementStatus = false; // No trees on this chunk VoxelChunk chunk; env.GetChunk(pos, out chunk, false); if (chunk != null) { chunk.allowTrees = false; } } else { bs.placementStatus = true; } if (temp) { buildingPositions[pos] = bs; } } } } } }