コード例 #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 == ButtonState.Pressed)
            {
                this.Exit();
            }

            AnimChunk currentChunk = sprite.AnimChunks[sprite.CurrentFrame];

            KeyboardState cks        = Keyboard.GetState();
            MouseState    cms        = Mouse.GetState();
            Vector2       mousePos   = new Vector2(cms.X, cms.Y);
            Vector2       mousedelta = mousePos - new Vector2(lms.X, lms.Y);
            int           wheelDelta = cms.ScrollWheelValue - lms.ScrollWheelValue;

            Vector3 moveVector = new Vector3(0, 0, 0);

            if (cks.IsKeyDown(Keys.Up) && !lks.IsKeyDown(Keys.Up))
            {
                moveVector += new Vector3(0, -1, 0);
            }
            if (cks.IsKeyDown(Keys.Down) && !lks.IsKeyDown(Keys.Down))
            {
                moveVector += new Vector3(0, 1, 0);
            }
            if (cks.IsKeyDown(Keys.Right) && !lks.IsKeyDown(Keys.Right))
            {
                moveVector += new Vector3(1, 0, 0);
            }
            if (cks.IsKeyDown(Keys.Left) && !lks.IsKeyDown(Keys.Left))
            {
                moveVector += new Vector3(-1, 0, 0);
            }
            if (cks.IsKeyDown(Keys.PageDown) && !lks.IsKeyDown(Keys.PageDown))
            {
                moveVector += new Vector3(0, 0, -1);
            }
            if (cks.IsKeyDown(Keys.PageUp) && !lks.IsKeyDown(Keys.PageUp))
            {
                moveVector += new Vector3(0, 0, 1);
            }
            if (cks.IsKeyDown(Keys.LeftShift))
            {
                cursor.ChangeSize(moveVector, currentChunk.X_SIZE);
            }
            if (!cks.IsKeyDown(Keys.LeftShift))
            {
                cursor.Position += moveVector;
            }

            cursor.Position = Vector3.Clamp(cursor.Position, Vector3.Zero, (Vector3.One * (currentChunk.X_SIZE)) - (cursor.Size));

            if (cks.IsKeyDown(Keys.Space) && !lks.IsKeyDown(Keys.Space))
            {
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                {
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                    {
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                        {
                            currentChunk.SetVoxel(x, y, z, true, selectedColor);
                        }
                    }
                }
                currentChunk.UpdateMesh();
            }

            if (cks.IsKeyDown(Keys.Tab) && !lks.IsKeyDown(Keys.Tab))
            {
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                {
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                    {
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                        {
                            currentChunk.SetVoxel(x, y, z, false, currentChunk.Voxels[x, y, z].Color);
                        }
                    }
                }
                currentChunk.UpdateMesh();
            }

            if (cks.IsKeyDown(Keys.LeftControl) && (cks.IsKeyDown(Keys.C) && !lks.IsKeyDown(Keys.C)))
            {
                ClearClipboard();
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                {
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                    {
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                        {
                            clipBoard.SetVoxel(x - (int)cursor.Position.X, y - (int)cursor.Position.Y, z - (int)cursor.Position.Z, currentChunk.Voxels[x, y, z].Active, currentChunk.Voxels[x, y, z].Color);
                        }
                    }
                }
            }
            if (cks.IsKeyDown(Keys.LeftControl) && (cks.IsKeyDown(Keys.X) && !lks.IsKeyDown(Keys.X)))
            {
                ClearClipboard();
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                {
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                    {
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                        {
                            clipBoard.SetVoxel(x - (int)cursor.Position.X, y - (int)cursor.Position.Y, z - (int)cursor.Position.Z, currentChunk.Voxels[x, y, z].Active, currentChunk.Voxels[x, y, z].Color);
                        }
                    }
                }

                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                {
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                    {
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                        {
                            currentChunk.SetVoxel(x, y, z, false, currentChunk.Voxels[x, y, z].Color);
                        }
                    }
                }
                currentChunk.UpdateMesh();
            }

            if (cks.IsKeyDown(Keys.LeftControl) && (cks.IsKeyDown(Keys.V) && !lks.IsKeyDown(Keys.V)))
            {
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                {
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                    {
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                        {
                            currentChunk.SetVoxel(x, y, z, clipBoard.Voxels[x - (int)cursor.Position.X, y - (int)cursor.Position.Y, z - (int)cursor.Position.Z].Active, clipBoard.Voxels[x - (int)cursor.Position.X, y - (int)cursor.Position.Y, z - (int)cursor.Position.Z].Color);
                        }
                    }
                }
                currentChunk.UpdateMesh();
            }

            if (cks.IsKeyDown(Keys.NumPad4) && !lks.IsKeyDown(Keys.NumPad4))
            {
                sprite.CurrentFrame--;
                if (sprite.CurrentFrame < 0)
                {
                    sprite.CurrentFrame = sprite.AnimChunks.Count - 1;
                }
            }
            if (cks.IsKeyDown(Keys.NumPad6) && !lks.IsKeyDown(Keys.NumPad6))
            {
                sprite.CurrentFrame++;
                if (sprite.CurrentFrame >= sprite.AnimChunks.Count)
                {
                    sprite.CurrentFrame = 0;
                }
            }

            if (cks.IsKeyDown(Keys.NumPad5) && !lks.IsKeyDown(Keys.NumPad5))
            {
                playingAnim = !playingAnim;
                animTime    = 0;
            }

            if (cks.IsKeyDown(Keys.Insert) && !lks.IsKeyDown(Keys.Insert))
            {
                sprite.InsertFrame();
            }
            if (cks.IsKeyDown(Keys.Home) && !lks.IsKeyDown(Keys.Home))
            {
                sprite.CopyFrame();
            }
            if (cks.IsKeyDown(Keys.End) && !lks.IsKeyDown(Keys.End))
            {
                sprite.AddFrame(true);
            }
            if (cks.IsKeyDown(Keys.Delete) && !lks.IsKeyDown(Keys.Delete))
            {
                sprite.DeleteFrame();
            }

            if (cks.IsKeyDown(Keys.F2) && !lks.IsKeyDown(Keys.F2))
            {
                LoadSave.Save(sprite, ref swatches);
            }
            if (cks.IsKeyDown(Keys.F5) && !lks.IsKeyDown(Keys.F5))
            {
                LoadSave.Load(ref sprite, GraphicsDevice, ref swatches);
            }

            if (cks.IsKeyDown(Keys.F8) && !lks.IsKeyDown(Keys.F8))
            {
                TileSheetConverter.Load(ref sprite, GraphicsDevice);
            }


            if (cks.IsKeyDown(Keys.D1))
            {
                selectedSwatch = 0; selectedColor = swatches[0];
            }
            if (cks.IsKeyDown(Keys.D2))
            {
                selectedSwatch = 1; selectedColor = swatches[1];
            }
            if (cks.IsKeyDown(Keys.D3))
            {
                selectedSwatch = 2; selectedColor = swatches[2];
            }
            if (cks.IsKeyDown(Keys.D4))
            {
                selectedSwatch = 3; selectedColor = swatches[3];
            }
            if (cks.IsKeyDown(Keys.D5))
            {
                selectedSwatch = 4; selectedColor = swatches[4];
            }
            if (cks.IsKeyDown(Keys.D6))
            {
                selectedSwatch = 5; selectedColor = swatches[5];
            }
            if (cks.IsKeyDown(Keys.D7))
            {
                selectedSwatch = 6; selectedColor = swatches[6];
            }
            if (cks.IsKeyDown(Keys.D8))
            {
                selectedSwatch = 7; selectedColor = swatches[7];
            }
            if (cks.IsKeyDown(Keys.D9))
            {
                selectedSwatch = 8; selectedColor = swatches[8];
            }
            if (cks.IsKeyDown(Keys.D0))
            {
                selectedSwatch = 9; selectedColor = swatches[9];
            }

            if (cks.IsKeyDown(Keys.F12) && !lks.IsKeyDown(Keys.F12))
            {
                if (sprite.X_SIZE < 32)
                {
                    sprite = new VoxelSprite(sprite.X_SIZE + 1, sprite.Y_SIZE + 1, sprite.Z_SIZE + 1, GraphicsDevice);
                }
            }
            if (cks.IsKeyDown(Keys.F11) && !lks.IsKeyDown(Keys.F11))
            {
                if (sprite.X_SIZE > 3)
                {
                    sprite = new VoxelSprite(sprite.X_SIZE - 1, sprite.Y_SIZE - 1, sprite.Z_SIZE - 1, GraphicsDevice);
                }
            }

            if (wheelDelta != 0)
            {
                if (wheelDelta > 0)
                {
                    viewZoom += 1f;
                }
                else
                {
                    viewZoom -= 1f;
                }
            }


            if (cms.LeftButton == ButtonState.Pressed)
            {
                Point mp = Helper.VtoP(mousePos);

                if (viewRect.Contains(new Point((int)mousePos.X, (int)mousePos.Y)))
                {
                    viewPitch -= mousedelta.Y * 0.01f;
                    viewYaw   += mousedelta.X * 0.01f;
                }

                Color prevColor = selectedColor;
                if (redRect.Contains(mp))
                {
                    selectedColor.R          = (byte)MathHelper.Clamp(((256f / 400f) * ((mp.X - (float)redRect.Left))), 0f, 255f);
                    swatches[selectedSwatch] = selectedColor;
                    if (cks.IsKeyDown(Keys.LeftShift))
                    {
                        sprite.ReplaceColor(prevColor, selectedColor);
                    }
                }
                if (greenRect.Contains(mp))
                {
                    selectedColor.G          = (byte)MathHelper.Clamp(((256f / 400f) * ((mp.X - (float)greenRect.Left))), 0f, 255f);
                    swatches[selectedSwatch] = selectedColor;
                    if (cks.IsKeyDown(Keys.LeftShift))
                    {
                        sprite.ReplaceColor(prevColor, selectedColor);
                    }
                }
                if (blueRect.Contains(mp))
                {
                    selectedColor.B          = (byte)MathHelper.Clamp(((256f / 400f) * ((mp.X - (float)blueRect.Left))), 0f, 255f);
                    swatches[selectedSwatch] = selectedColor;
                    if (cks.IsKeyDown(Keys.LeftShift))
                    {
                        sprite.ReplaceColor(prevColor, selectedColor);
                    }
                }

                if (lms.LeftButton != ButtonState.Pressed)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        if (swatchRects[i].Contains(mp))
                        {
                            selectedColor  = swatches[i];
                            selectedSwatch = i;
                        }
                    }

                    if (prevFrameRect.Contains(mp))
                    {
                        sprite.CurrentFrame--;
                        if (sprite.CurrentFrame < 0)
                        {
                            sprite.CurrentFrame = sprite.AnimChunks.Count - 1;
                        }
                    }
                    if (nextFrameRect.Contains(mp))
                    {
                        sprite.CurrentFrame++;
                        if (sprite.CurrentFrame >= sprite.AnimChunks.Count)
                        {
                            sprite.CurrentFrame = 0;
                        }
                    }
                }
            }

            cursor.Update(gameTime, selectedColor);

            lks = cks;
            lms = cms;

            if (playingAnim)
            {
                animTime += gameTime.ElapsedGameTime.TotalMilliseconds;
                if (animTime > 100)
                {
                    animTime = 0;
                    sprite.CurrentFrame++;
                    if (sprite.CurrentFrame > sprite.AnimChunks.Count - 1)
                    {
                        sprite.CurrentFrame = 0;
                    }
                }
            }

            drawEffect.World   = worldMatrix * Matrix.CreateRotationY(viewYaw) * Matrix.CreateRotationX(viewPitch);
            drawEffect.View    = Matrix.CreateLookAt(new Vector3(0, 0, viewZoom), new Vector3(0, 0, 0), Vector3.Up);
            cursorEffect.World = Matrix.CreateTranslation((-((Vector3.One * Voxel.SIZE) * (currentChunk.X_SIZE / 2f)) + (cursor.Position * Voxel.SIZE))) * drawEffect.World;
            cursorEffect.View  = drawEffect.View;

            base.Update(gameTime);
        }