public override void Update() { if (!hasHadInitialSet) { Position.Y = Chunk.HEIGHT; if (Raycast.CastVoxel(new Vector3(Position), new Vector3(0, -1, 0), Chunk.HEIGHT, out RayVoxelOut hit)) { var chunkWp = (hit.BlockPosition); Position = chunkWp + new Vector3(0.5f, Chunk.HEIGHT, 0.5f); Debug.Log("Hit block for y pos " + Position.Y); rigidbody = new Rigidbody(this, 70, new BoundingBox(-0.25f, 0.25f, 0, 2, -0.25f, 0.25f)); hasHadInitialSet = true; } } currentWorld.WorldCamera.Position = Position + new Vector3(0, 1.7f, 0); if (hasHadInitialSet) { HandleInput(); var chunkPos = Position.ToChunkPosition(); if (World.GetInstance().TryGetChunkAtPosition((int)chunkPos.X, (int)chunkPos.Z, out Chunk chunk)) { var block = Position.ToChunkSpaceFloored(); isInWater = chunk.GetBlockID((int)block.X, (int)block.Y, (int)block.Z) == GameBlocks.WATER.ID; rigidbody.Drag = isInWater ? UnderWaterDrag : 0; } } if (currentWorld.HasFinishedInitialLoading) { if (lastHungerLossTick + hungerLossTickRate <= Time.GameTime) { hungerLossAmount = isSprinting ? 0.25f : 0.01f; if (currentHunger > 0) { currentHunger -= hungerLossAmount; } else { currentHealth -= 0.0625f; TakeDamage(0); } lastHungerLossTick = Time.GameTime; } if (lastHealthIncreaseTick + healthIncreaseTickRate <= Time.GameTime) { if (currentHealth < MAX_HEALTH && currentHunger == MAX_HUNGER) { SetHealth((int)currentHealth + 1); } lastHealthIncreaseTick = Time.GameTime; } } }
void InputInteract() { if (!controlsEnabled) { return; } if (Raycast.CastVoxel(currentWorld.WorldCamera.Position, currentWorld.WorldCamera.GetForward(), 5, out RayVoxelOut op)) { int x = (int)Math.Floor(GetPositionInChunk().X); int z = (int)Math.Floor(GetPositionInChunk().Z); bool isPlayerAtPos = (int)op.PlacementPosition.X == x && (int)op.PlacementPosition.Z == z; if (currentWorld.TryGetChunkAtPosition((int)op.PlacementChunk.X, (int)op.PlacementChunk.Y, out Chunk chunk) && !isPlayerAtPos) { var stack = inventory.GetItemStackByLocation(inventory.SelectedItemIndex, 0); if (stack != null) { stack.Item.OnInteract(op.PlacementPosition, chunk); inventory.RemoveItemFromStack(stack.Item, stack); currentWorld.RequestChunkUpdate(chunk, true, (int)op.BlockPosition.X, (int)op.BlockPosition.Z); } } } }
void InputJump() { if (!controlsEnabled || isInWater) { return; } if (Raycast.CastVoxel(currentWorld.WorldCamera.Position, new Vector3(0, -1, 0), 2.1f, out RayVoxelOut output)) { rigidbody.AddImpluse(new Vector3(0, 1, 0) * 600); } }
void InputDestroyBlock() { if (!controlsEnabled) { return; } if (Raycast.CastVoxel(currentWorld.WorldCamera.Position, currentWorld.WorldCamera.GetForward(), 5, out RayVoxelOut op)) { if (currentWorld.TryGetChunkAtPosition((int)op.ChunkPosition.X, (int)op.ChunkPosition.Y, out Chunk chunk)) { chunk.DestroyBlock((int)op.BlockPosition.X, (int)op.BlockPosition.Y, (int)op.BlockPosition.Z); var chunkWp = (op.ChunkPosition * Chunk.WIDTH); var wp = new Vector3(chunkWp.X, 0, chunkWp.Y) + op.BlockPosition; BlockDatabase.GetBlock(op.BlockID).OnBreak(wp, op.ChunkPosition); currentWorld.RequestChunkUpdate(chunk, true, (int)op.BlockPosition.X, (int)op.BlockPosition.Z); } } }