示例#1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                this.Exit();
            }

            var kState = Keyboard.GetState();

            if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape))
            {
                this.Exit();
            }

            if (TimeSinceLogicUpdate.TotalMilliseconds < 40)
            {
                TimeSinceLogicUpdate += gameTime.ElapsedGameTime;

                var down = kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Down);
                var up   = kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Up);

                var left  = kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left);
                var right = kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right);

                if (down)
                {
                    if (combinedState.HasFlag(combinedKeyState.vUp))
                    {
                        combinedState &= ~combinedKeyState.vUp;
                    }
                    else
                    {
                        combinedState |= combinedKeyState.vDown;
                    }
                }

                if (up)
                {
                    if (combinedState.HasFlag(combinedKeyState.vDown))
                    {
                        combinedState &= ~combinedKeyState.vDown;
                    }
                    else
                    {
                        combinedState |= combinedKeyState.vUp;
                    }
                }

                if (left)
                {
                    if (combinedState.HasFlag(combinedKeyState.vRight))
                    {
                        combinedState &= ~combinedKeyState.vRight;
                    }
                    else
                    {
                        combinedState |= combinedKeyState.vLeft;
                    }
                }

                if (right)
                {
                    if (combinedState.HasFlag(combinedKeyState.vLeft))
                    {
                        combinedState &= ~combinedKeyState.vLeft;
                    }
                    else
                    {
                        combinedState |= combinedKeyState.vRight;
                    }
                }
            }
            else
            {
                if (Map != null)
                {
                    var deltaX = 7;
                    var deltaY = 7;

                    TacticalClass.NudgeStatus moveStatus = TacticalClass.NudgeStatus.E_EDGE;

                    if (combinedState.HasFlag(combinedKeyState.vUp))
                    {
                        moveStatus = Tactical.NudgeY(-deltaY);
                    }
                    else if (combinedState.HasFlag(combinedKeyState.vDown))
                    {
                        moveStatus = Tactical.NudgeY(+deltaY);
                    }

                    if (combinedState.HasFlag(combinedKeyState.vLeft))
                    {
                        moveStatus = Tactical.NudgeX(-deltaX);
                    }
                    else if (combinedState.HasFlag(combinedKeyState.vRight))
                    {
                        moveStatus = Tactical.NudgeX(+deltaX);
                    }

                    bool MapMoved = moveStatus != TacticalClass.NudgeStatus.E_EDGE;

                    if (MapTexture == null || MapMoved)
                    {
                        MapTexture = Map.GetTexture(GraphicsDevice);
                    }

                    //if (MapTexture != null && kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.S)) {
                    //    var outdir = Directory.GetCurrentDirectory();
                    //    var outfile = Path.Combine(outdir, "scr.png");
                    //    using (FileStream s = File.OpenWrite(outfile)) {
                    //        MapTexture.SaveAsPng(s, MapTexture.Width, MapTexture.Height);
                    //    }
                    //}
                }

                combinedState        = 0;
                TimeSinceLogicUpdate = new TimeSpan(0);
            }

            //if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Space)) {
            //    offX = defOX;
            //    offY = defOY;
            //    rotation = defRotation;
            //    scale = 1f;
            //}

            //if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Up)) {
            //    offY -= (int)scale;
            //} else if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Down)) {
            //    offY += (int)scale;
            //}

            //if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left)) {
            //    offX -= (int)scale;
            //} else if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right)) {
            //    offX += (int)scale;
            //}

            //var rot = MathHelper.PiOver4 / 4;

            //if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Q)) {
            //    rotation.X += rot;
            //} else if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.A)) {
            //    rotation.X -= rot;
            //}

            //if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.W)) {
            //    rotation.Y += rot;
            //} else if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.S)) {
            //    rotation.Y -= rot;
            //}

            //if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.E)) {
            //    rotation.Z += rot;
            //} else if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.D)) {
            //    rotation.Z -= rot;
            //}

            //if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Multiply)) {
            //    scale *= 1.01f;
            //} else if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Divide)) {
            //    scale /= 1.01f;
            //}

            //if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.L)) {
            //    effect.LightingEnabled = !effect.LightingEnabled;
            //}

            //if (LoadedVoxels.Count > 0) {
            //    var fcount = (int)(LoadedVoxels[0].MotLib.Header.FrameCount - 1);
            //    if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Z)) {
            //        if (VoxelFrame < fcount) {
            //            ++VoxelFrame;
            //        } else {
            //            VoxelFrame = 0;
            //        }
            //        VoxelChanged = true;
            //    } else if (kState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.X)) {
            //        if (VoxelFrame > 0) {
            //            --VoxelFrame;
            //        } else {
            //            VoxelFrame = fcount;
            //        }
            //        VoxelChanged = true;
            //    }
            //}


            //// TODO: Add your update logic here

            var pos = Mouse.GetState();

            MousePos.X = pos.X;
            MousePos.Y = pos.Y;

            if (MouseFrameChanged)
            {
                MouseTextures.GetTexture((uint)MouseFrame, ref MouseTexture);
            }

            //if (VoxelChanged) {
            //    VoxelChanged = false;
            //    if (MousePalette != null) {
            //        var combinedVertices = new List<VXL.VertexPositionColorNormal>();
            //        var combinedIndices = new List<int>();

            //        foreach (var V in LoadedVoxels) {
            //            var Vertices = new List<VXL.VertexPositionColorNormal>();
            //            var Indices = new List<int>();

            //            V.Voxel.GetVertices(MousePalette, VoxelFrame, Vertices, Indices);

            //            var indexShift = combinedVertices.Count;

            //            combinedVertices.AddRange(Vertices);

            //            combinedIndices.AddRange(Indices.Select(ix => ix + indexShift));
            //            //  break;
            //        }

            //        VoxelContent = combinedVertices.ToArray();
            //        VoxelIndices = combinedIndices.ToArray();

            //        //Console.WriteLine("Loaded {0} vertices and {1} indices", VoxelContent.Length, VoxelIndices.Length);

            //        if (vertexBuffer != null) {
            //            vertexBuffer.Dispose();
            //        }

            //        // Initialize the vertex buffer, allocating memory for each vertex.
            //        vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, VXL.VertexPositionColorNormal.VertexDeclaration, VoxelContent.Length, BufferUsage.WriteOnly);

            //        // Set the vertex buffer data to the array of vertices.
            //        vertexBuffer.SetData<VXL.VertexPositionColorNormal>(VoxelContent);

            //        indexBuffer = new IndexBuffer(graphics.GraphicsDevice, typeof(int), VoxelIndices.Length, BufferUsage.WriteOnly);

            //        indexBuffer.SetData(VoxelIndices);

            //    }
            //}

            base.Update(gameTime);
        }