diff --git a/ClassicalSharp.csproj b/ClassicalSharp.csproj index 3e74cc4ef..588d459f1 100644 --- a/ClassicalSharp.csproj +++ b/ClassicalSharp.csproj @@ -39,7 +39,7 @@ obj\$(Configuration)\ - x86 + AnyCPU False Auto 4194304 @@ -65,7 +65,7 @@ TRACE - AnyCPU + x86 output\debug\ true Full diff --git a/Entities/EntityList.cs b/Entities/EntityList.cs index 9cedac58f..39bfe1e41 100644 --- a/Entities/EntityList.cs +++ b/Entities/EntityList.cs @@ -32,7 +32,10 @@ namespace ClassicalSharp { } } - public byte GetClosetPlayer( Vector3 eyePos, Vector3 dir ) { + public byte GetClosetPlayer( Game game ) { + Player localP = game.LocalPlayer; + Vector3 eyePos = localP.EyePosition; + Vector3 dir = Utils.GetDirVector( localP.YawRadians, localP.PitchRadians ); float closestDist = float.PositiveInfinity; byte targetId = 255; diff --git a/Game/Game.InputHandling.cs b/Game/Game.InputHandling.cs index 25954bfb4..9c5dae70d 100644 --- a/Game/Game.InputHandling.cs +++ b/Game/Game.InputHandling.cs @@ -30,10 +30,20 @@ namespace ClassicalSharp { Mouse.ButtonUp += MouseButtonUp; } + bool[] buttonsDown = new bool[3]; void MouseButtonUp( object sender, MouseButtonEventArgs e ) { if( activeScreen == null || !activeScreen.HandlesMouseUp( e.X, e.Y, e.Button ) ) { + if( SendPlayerClick( e.Button ) && buttonsDown[(int)e.Button] ) { + byte targetId = Players.GetClosetPlayer( this ); + Network.SendPlayerClick( e.Button, false, targetId, SelectedPos ); + buttonsDown[(int)e.Button] = false; + } } } + + bool SendPlayerClick( MouseButton button ) { + return Network.UsingPlayerClick && button <= MouseButton.Middle; + } void MouseButtonDown( object sender, MouseButtonEventArgs e ) { if( activeScreen == null || !activeScreen.HandlesMouseClick( e.X, e.Y, e.Button ) ) { @@ -121,12 +131,14 @@ namespace ClassicalSharp { DateTime lastClick = DateTime.MinValue; void PickBlocks( bool cooldown, bool left, bool right, bool middle ) { - int buttonsDown = ( left ? 1 : 0 ) + ( right ? 1 : 0 ) + ( middle ? 1 : 0 ); - if( !SelectedPos.Valid || buttonsDown > 1 || ScreenLockedInput || HeldBlock == Block.Air ) return; DateTime now = DateTime.UtcNow; double delta = ( now - lastClick ).TotalMilliseconds; if( cooldown && delta < 250 ) return; // 4 times per second lastClick = now; + ButtonStateChanged( !cooldown, left, right, middle ); + + int buttonsDown = ( left ? 1 : 0 ) + ( right ? 1 : 0 ) + ( middle ? 1 : 0 ); + if( !SelectedPos.Valid || buttonsDown > 1 || ScreenLockedInput || HeldBlock == Block.Air ) return; if( middle ) { Vector3I pos = SelectedPos.BlockPos; @@ -154,6 +166,19 @@ namespace ClassicalSharp { } } + void ButtonStateChanged( bool fromClick, bool left, bool right, bool middle ) { + if( !Network.UsingPlayerClick ) return; + + byte targetId = Players.GetClosetPlayer( this ); + for( int i = 0; i < buttonsDown.Length; i++ ) { + if( !buttonsDown[i] ) continue; + + if( !fromClick ) + Network.SendPlayerClick( (MouseButton)i, false, targetId, SelectedPos ); + Network.SendPlayerClick( (MouseButton)i, true, targetId, SelectedPos ); + } + } + internal bool CanPick( byte block ) { return !( block == 0 || ( BlockInfo.IsLiquid( block ) && !CanPlace[block] && !CanDelete[block] ) ); } diff --git a/Game/Game.cs b/Game/Game.cs index 85e06e40e..7599d35a1 100644 --- a/Game/Game.cs +++ b/Game/Game.cs @@ -238,7 +238,7 @@ namespace ClassicalSharp { bool middle = IsMousePressed( MouseButton.Middle ); PickBlocks( true, left, right, middle ); } else { - SelectedPos.Valid = false; + SelectedPos.SetAsInvalid(); } Graphics.Mode2D( Width, Height ); @@ -325,6 +325,14 @@ namespace ClassicalSharp { screen.Window = this; screen.Init(); } + if( Network.UsingPlayerClick ) { + byte targetId = Players.GetClosetPlayer( this ); + for( int i = 0; i < buttonsDown.Length; i++ ) { + if( !buttonsDown[i] ) continue; + Network.SendPlayerClick( (MouseButton)i, false, targetId, SelectedPos ); + buttonsDown[i] = false; + } + } } public void SetCamera( bool thirdPerson ) { diff --git a/Network/NetworkProcessor.cs b/Network/NetworkProcessor.cs index 5ee5371a8..274d02028 100644 --- a/Network/NetworkProcessor.cs +++ b/Network/NetworkProcessor.cs @@ -70,6 +70,7 @@ namespace ClassicalSharp { public void SendPlayerClick( MouseButton button, bool buttonDown, byte targetId, PickedPos pos ) { Player p = Window.LocalPlayer; + Console.WriteLine( "CLICK: " + button + "," + buttonDown ); MakePlayerClick( (byte)button, buttonDown, p.YawDegrees, p.PitchDegrees, targetId, pos.BlockPos, pos.BlockFace ); } diff --git a/Physics/Picking.cs b/Physics/Picking.cs index 3c0b10743..6e6c5e313 100644 --- a/Physics/Picking.cs +++ b/Physics/Picking.cs @@ -80,9 +80,7 @@ namespace ClassicalSharp { float dz = Math.Min( Math.Abs( origin.Z - min.Z ), Math.Abs( origin.Z - max.Z ) ); if( dx * dx + dy * dy + dz * dz > reachSquared ) { - pickedPos.BlockPos = Vector3I.MinusOne; - pickedPos.BlockFace = (CpeBlockFace)255; - pickedPos.Valid = false; + pickedPos.SetAsInvalid(); return; } @@ -91,7 +89,8 @@ namespace ClassicalSharp { // since some blocks do not occupy a whole cell. float t0, t1; if( Intersection.RayIntersectsBox( origin, dir, min, max, out t0, out t1 ) ) { - pickedPos.UpdateBlockPos( min, max, origin, dir, t0, t1 ); + Vector3 intersect = origin + dir * t0; + pickedPos.SetAsValid( min, max, intersect ); return; } } @@ -124,14 +123,13 @@ namespace ClassicalSharp { public bool Valid = true; public CpeBlockFace BlockFace; - public void UpdateBlockPos( Vector3 p1, Vector3 p2, Vector3 origin, Vector3 dir, float t0, float t1 ) { - Min = Vector3.Min( p1, p2 ); - Max = Vector3.Max( p1, p2 ); + public void SetAsValid( Vector3 min, Vector3 max, Vector3 intersect ) { + Min = min; + Max = max; BlockPos = Vector3I.Truncate( Min ); Valid = true; Vector3I normal = Vector3I.Zero; - Vector3 intersect = origin + dir * t0; float dist = float.PositiveInfinity; TestAxis( intersect.X - Min.X, ref dist, -Vector3I.UnitX, ref normal, CpeBlockFace.XMin ); TestAxis( intersect.X - Max.X, ref dist, Vector3I.UnitX, ref normal, CpeBlockFace.XMax ); @@ -142,6 +140,12 @@ namespace ClassicalSharp { TranslatedPos = BlockPos + normal; } + public void SetAsInvalid() { + Valid = false; + BlockPos = TranslatedPos = Vector3I.MinusOne; + BlockFace = (CpeBlockFace)255; + } + void TestAxis( float dAxis, ref float dist, Vector3I nAxis, ref Vector3I normal, CpeBlockFace fAxis) { dAxis = Math.Abs( dAxis );