public override void Tick() { Direction.X += MouseHandler.MouseDelta.X; Direction.Y += MouseHandler.MouseDelta.Y; TickMovement(ClientMain.Delta); ClientMain.CameraEye = Position + new Location(0, 0, Maxes.Z); Location forward = Utilities.ForwardVector_Deg(Direction.X, Direction.Y); ClientMain.CameraTarget = ClientMain.CameraEye + forward; ltime += ClientMain.Delta; Location seltarg = ClientMain.CameraEye + forward * 10; SelectedBlock = Collision.BoxRayTrace(new Location(-0.001), new Location(0.001), ClientMain.CameraEye, seltarg, -1); if (SelectedBlock == seltarg) { SelectedBlock = Location.NaN; } if ((Forward != pForward || Backward != pBackward || Leftward != pLeftward || Rightward != pRightward || Upward != pUpward || Downward != pDownward || Slow != pSlow || Use != pUse || Attack != pAttack || Secondary != pSecondary || Direction.X != pDirection.X || Direction.Y != pDirection.Y) || ltime >= 0.1) { pForward = Forward; pBackward = Backward; pLeftward = Leftward; pRightward = Rightward; pUpward = Upward; pDownward = Downward; pDirection = Direction; pSlow = Slow; pUse = Use; pAttack = Attack; pSecondary = Secondary; ltime = 0; AddMS(); if (ClientNetworkBase.Connected) { ClientNetworkBase.SendPacket(new MoveKeysPacketOut(MoveStates[MoveStates.Count - 1])); } if (MoveStates.Count > 250) { MoveStates.RemoveRange(0, 50); } } }
public override void Tick() { if (tdisco) { return; } if (!IsValid) { return; } int pc = PacketsToApply.Count; for (int i = 0; i < pc; i++) { PacketsToApply[i].Apply(); } PacketsToApply.RemoveRange(0, pc); TickMovement(InWorld.Delta); // Manage Selection Location forward = Utilities.ForwardVector_Deg(Direction.X, Direction.Y); Location eye = Position + new Location(0, 0, Maxes.Z); Location seltarg = eye + forward * 10; SelectedBlock = Collision.BoxRayTrace(InWorld, new Location(-0.001), new Location(0.001), eye, seltarg, -1); if (SelectedBlock == seltarg) { SelectedBlock = Location.NaN; } // Break if (Attack && !SelectedBlock.IsNaN()) { if (ServerMain.GlobalTickTime - 0.5 >= LastBreak) { LastBreak = ServerMain.GlobalTickTime; Location sel_block = SelectedBlock.GetBlockLocation(); Chunk ch = InWorld.LoadChunk(World.GetChunkLocation(sel_block)); if (ch.Blocks[(int)(sel_block.X - ch.X * 30), (int)(sel_block.Y - ch.Y * 30), (int)(sel_block.Z - ch.Z * 30)].Type != 0) { ch.SetBlock((int)(sel_block.X - ch.X * 30), (int)(sel_block.Y - ch.Y * 30), (int)(sel_block.Z - ch.Z * 30), (ushort)Material.AIR); InWorld.BroadcastBlock(sel_block); } } } if (!Attack) { LastBreak = 0; } // Place Item HeldItem = GetItemForSlot(QuickBarPos); if (Secondary && HeldItem.Material != Material.AIR) { if (ServerMain.GlobalTickTime - 0.3 >= LastPlace) { Location back_a_block = Collision.BoxRayTrace(InWorld, new Location(-0.001), new Location(0.001), eye, seltarg, 1); LastPlace = ServerMain.GlobalTickTime; Location sel_block = back_a_block.GetBlockLocation(); Chunk ch = InWorld.LoadChunk(World.GetChunkLocation(sel_block)); if (ch.Blocks[(int)(sel_block.X - ch.X * 30), (int)(sel_block.Y - ch.Y * 30), (int)(sel_block.Z - ch.Z * 30)].Type != (ushort)HeldItem.Material) { ch.SetBlock((int)(sel_block.X - ch.X * 30), (int)(sel_block.Y - ch.Y * 30), (int)(sel_block.Z - ch.Z * 30), (ushort)HeldItem.Material); InWorld.BroadcastBlock(sel_block); } } } if (!Secondary) { LastPlace = 0; } // TODO: Better tracking of what chunks to send List <Location> locs = GetChunksNear(World.GetChunkLocation(Position)); foreach (Location loc in locs) { if (!ChunksAware.Contains(loc)) { ToSend.Add(loc); ChunksAware.Add(loc); } } for (int i = 0; i < ChunksAware.Count; i++) { if (!locs.Contains(ChunksAware[i])) { ToSend.Remove(ChunksAware[i]); ChunksAware.RemoveAt(i--); } } sendtimer += InWorld.Delta; if (sendtimer >= 0.05f) { sendtimer = 0.05f; if (ToSend.Count > 0) { Chunk ch = InWorld.LoadChunk(ToSend[0]); SendToSecondary(new ChunkPacketOut(ch)); for (int i = 0; i < ch.Entities.Count; i++) { if (ch.Entities[i] != this) { Send(new NewEntityPacketOut(ch.Entities[i])); } } ToSend.RemoveAt(0); } } // Handle networking lock (Packets) { for (int i = 0; i < Packets.Count; i++) { try { Packets[i].Apply(); } catch (Exception ex) { SysConsole.Output(OutputType.ERROR, "Networking / player / receive " + (Packets[i].IsChunkConnection ? "C-" : "") + "packet: " + ex.ToString()); Kick("Invalid packet " + Packets[i].GetType().Name); // TODO: Less internal-ish packet ID handling? } } Packets.Clear(); } }
/// <summary> /// Ticks player movement. /// </summary> /// <param name="delta">The tick delta</param> /// <param name="custom">Whether tihis is a custom tick call (IE, anything outside the normal tick)</param> public void TickMovement(double delta, bool custom = false) { if (delta == 0) { return; } while (Direction.X < 0) { Direction.X += 360; } while (Direction.X > 360) { Direction.X -= 360; } if (Direction.Y > 89.9f) { Direction.Y = 89.9f; } if (Direction.Y < -89.9f) { Direction.Y = -89.9f; } Location movement = Location.Zero; if (Leftward) { movement.Y = -1; } if (Rightward) { movement.Y = 1; } if (Backward) { movement.X = 1; } if (Forward) { movement.X = -1; } if (Downward) { Maxes.Z = 0.1; } else { Maxes.Z = 1.5; } bool on_ground = Collision.Box(InWorld, Position - (DefaultHalfSize + new Location(0, 0, 0.1)), Position + DefaultHalfSize) && Velocity.Z < 0.01; if (Upward) { if (on_ground && !Jumped) { Velocity.Z += 10; Jumped = true; } } else { Jumped = false; } if (movement.LengthSquared() > 0) { movement = Utilities.RotateVector(movement, Direction.X * Utilities.PI180); } float MoveSpeed = 15; Velocity.X += ((movement.X * MoveSpeed * (Slow || Downward ? 0.5 : 1)) - Velocity.X) * delta * 8; Velocity.Y += ((movement.Y * MoveSpeed * (Slow || Downward ? 0.5 : 1)) - Velocity.Y) * delta * 8; Velocity.Z += delta * -9.8 / 0.5666; // 1 unit = 0.5666 meters Location ppos = Position; Location target = Position + Velocity * delta; Location pos = Position; if (target != pos) { // TODO: Better handling (Based on impact normal) pos = Collision.BoxRayTrace(InWorld, -DefaultHalfSize, DefaultHalfSize, pos, new Location(target.X, target.Y, pos.Z), 1); pos = Collision.BoxRayTrace(InWorld, -DefaultHalfSize, DefaultHalfSize, pos, new Location(target.X, pos.Y, pos.Z), 1); pos = Collision.BoxRayTrace(InWorld, -DefaultHalfSize, DefaultHalfSize, pos, new Location(pos.X, target.Y, pos.Z), 1); pos = Collision.BoxRayTrace(InWorld, -DefaultHalfSize, DefaultHalfSize, pos, new Location(pos.X, pos.Y, target.Z), 1); Reposition(pos); Velocity = (pos - ppos) / delta; } }