コード例 #1
0
        private void Client_OnDraw2D(object sender, Draw2DEventArgs e)
        {
            e.SpriteBatch.Begin();

            e.SpriteBatch.Draw(Client.EmptyTexture,
                               new Rectangle(0, 0, Client.Viewport.Width, Client.Viewport.Height),
                               Color.CornflowerBlue);

            AnchoredText t = new AnchoredText(Client.Font, "Press Enter to Start",
                                              new Vector2(Client.Viewport.Width / 2, 36), TextAnchor.MiddleCenter);

            t.Draw(e.SpriteBatch);

            string[] lines = new string[] {
                "WASD - Movement            Q - Up",
                "R    - Respawn             E - Down",
                "F    - Render Distance     Space - Jump"
            };
            for (int i = 0; i < lines.Length; i++)
            {
                new AnchoredText(Client.Font, lines[i].PadRight(40),
                                 Client.WindowCenter + (Down * i),
                                 TextAnchor.MiddleCenter
                                 ).Draw(e.SpriteBatch);
            }

            e.SpriteBatch.End();
        }
コード例 #2
0
ファイル: HUD.cs プロジェクト: DeveloperJose/CSharp-Minecraft
        public void Draw2D(object sender, Draw2DEventArgs e)
        {
            frameCounter++;

            e.SpriteBatch.Begin();

            if (!Client.Paused)
            {
                // Crosshair
                e.SpriteBatch.Draw(Client.CrosshairTexture,
                                   Client.WindowCenter,                                                            // Center of screen
                                   null,                                                                           // Source rectangle
                                   Color.White,                                                                    // Color
                                   0f,                                                                             // Rotation
                                   new Vector2(Client.CrosshairTexture.Width, Client.CrosshairTexture.Height) / 2, // Image center
                                   1f,                                                                             // Scale
                                   SpriteEffects.None,
                                   0f                                                                              // Depth
                                   );
                e.SpriteBatch.Draw(Client.EmptyTexture, Client.WindowCenter, Color.Red);
                string fps = string.Format("FPS: {0}", frameRate);
                e.SpriteBatch.DrawString(Client.Font, fps, Vector2.Zero, Color.White);

                MouseState m          = Mouse.GetState();
                int        mouseX     = m.X;
                int        mouseY     = m.Y;
                Vector3    nearsource = new Vector3((float)mouseX, (float)mouseY, 0f);
                Vector3    farsource  = new Vector3((float)mouseX, (float)mouseY, 1f);

                Vector3 nearPoint = Client.Viewport.Unproject(nearsource,
                                                              Client.MainPlayer.Camera.ProjectionMatrix,
                                                              Client.MainPlayer.Camera.ViewMatrix,
                                                              Matrix.Identity);

                Vector3 farPoint = Client.Viewport.Unproject(farsource,
                                                             Client.MainPlayer.Camera.ProjectionMatrix,
                                                             Client.MainPlayer.Camera.ViewMatrix,
                                                             Matrix.Identity);

                Vector3 direction = farPoint - nearPoint;
                direction.Normalize();

                //Matrix rotationMatrix = Matrix.CreateRotationX(Client.MainPlayer.Camera.Rotation.X) *
                //                        Matrix.CreateRotationY(Client.MainPlayer.Camera.Rotation.Y);
                //distance = Vector3.Transform(distance, rotationMatrix);

                Ray r = new Ray(nearPoint.Center(), direction);
                foreach (Vector3I coord in GetCellsOnRay(r, 15))
                {
                    BlockID id        = Client.MainWorld[coord];
                    Vector3 renderPos = coord.ToRenderCoords();
                    Vector3 min       = new Vector3(renderPos.X - 1f, renderPos.Y - 1f, renderPos.Z - 1f);
                    Vector3 max       = new Vector3(renderPos.X + 1f, renderPos.Y + 1f, renderPos.Z + 1f);

                    float i = Vector3.Distance(Client.MainPlayer.Camera.Position, renderPos);
                    if (i < 2)
                    {
                        continue;
                    }
                    if (id != BlockID.None && id != BlockID.Air)
                    {
                        e.SpriteBatch.DrawString(Client.Font, "Looking: " + id + " - " + i, new Vector2(0, 80), Color.White);
                        BoundingBoxRenderer.Render(new BoundingBox(min, max),
                                                   Client.Device,
                                                   Client.MainPlayer.Camera.ViewMatrix,
                                                   Client.MainPlayer.Camera.ProjectionMatrix,
                                                   Color.Red);
                        Selected = coord;
                        break;
                    }
                    else
                    {
                        BoundingBoxRenderer.Render(new BoundingBox(min, max),
                                                   Client.Device,
                                                   Client.MainPlayer.Camera.ViewMatrix,
                                                   Client.MainPlayer.Camera.ProjectionMatrix,
                                                   Color.White);
                    }
                }

                if (Mouse.GetState().LeftButton == ButtonState.Pressed &&
                    Client.MainWorld[Selected] != BlockID.None &&
                    Client.MainWorld[Selected] != BlockID.Air)
                {
                    Client.MainWorld[Selected] = BlockID.Air;
                }
            }
            else
            {
                AnchoredText t = new AnchoredText(Client.Font, "PAUSED: " + Client.Version, Client.WindowCenter, TextAnchor.MiddleCenter);
                t.Draw(e.SpriteBatch);
            }
            e.SpriteBatch.End();
        }