Example #1
0
 private void button1_Click(object sender, EventArgs e)
 {
     World.Type        = (MapType)cboType.SelectedIndex;
     World.Theme       = (Theme)cboTheme.SelectedIndex;
     World.DisplayName = txtDisplay.Text;
     World.CodeName    = txtCode.Text;
     LoadSave.Save(World);
     this.Close();
 }
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            font = Content.Load <SpriteFont>("hudfont");

            gameWorld  = new World(50, 50, 1, true);
            gameCamera = new Camera(GraphicsDevice, GraphicsDevice.Viewport);
            cursor     = new Cursor();

            this.EnsurePrefabs();
            List <string> pfs = new List <String>(Directory.EnumerateFiles(Path.Combine(Content.RootDirectory, "prefabs/")));

            LoadSave.LoadAnim(ref spawnSprites, Path.Combine(Content.RootDirectory, "spawns.vxs"));

            foreach (string fn in pfs)
            {
                Prefabs.Add(LoadSave.LoadPrefab(fn));
            }

            drawEffect = new BasicEffect(GraphicsDevice)
            {
                World              = gameCamera.worldMatrix,
                View               = gameCamera.viewMatrix,
                Projection         = gameCamera.projectionMatrix,
                VertexColorEnabled = true,
                //LightingEnabled = true // turn on the lighting subsystem.
            };

            cursorEffect = new BasicEffect(GraphicsDevice)
            {
                World              = gameCamera.worldMatrix,
                View               = gameCamera.viewMatrix,
                Projection         = gameCamera.projectionMatrix,
                VertexColorEnabled = true
            };

            saveForm = new SaveForm(gameWorld);

            GC.Collect();
        }
        /// <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)
        {
            if (!this.IsActive)
            {
                return;
            }
            if (saveForm != null && saveForm.Visible)
            {
                return;
            }
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            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) || cks.IsKeyDown(Keys.W))
            {
                moveVector += new Vector3(0, 1, 0);
            }
            if (cks.IsKeyDown(Keys.Down) || cks.IsKeyDown(Keys.S))
            {
                moveVector += new Vector3(0, -1, 0);
            }
            if (cks.IsKeyDown(Keys.Right) || cks.IsKeyDown(Keys.D))
            {
                moveVector += new Vector3(-1, 0, 0);
            }
            if (cks.IsKeyDown(Keys.Left) || cks.IsKeyDown(Keys.A))
            {
                moveVector += new Vector3(1, 0, 0);
            }
            gameCamera.AddToPosition(moveVector);
            gameCamera.UpdateViewMatrix();
            //gameCamera.Rotate(mousedelta.X, mousedelta.Y);

            if (cks.IsKeyDown(Keys.Z) && !lks.IsKeyDown(Keys.Z))
            {
                gameCamera.Zoom = !gameCamera.Zoom;
            }

            if (cks.IsKeyDown(Keys.PageUp) && !lks.IsKeyDown(Keys.PageUp))
            {
                if (cursor.Mode == CursorMode.LandScape)
                {
                    cursor.Height++;
                }
                if (cursor.Mode == CursorMode.Prefab)
                {
                    cursor.destructable++;
                }
            }
            if (cks.IsKeyDown(Keys.PageDown) && !lks.IsKeyDown(Keys.PageDown))
            {
                if (cursor.Mode == CursorMode.LandScape)
                {
                    cursor.Height--;
                }
                if (cursor.Mode == CursorMode.Prefab)
                {
                    cursor.destructable--;
                }
            }
            if (cks.IsKeyDown(Keys.Tab) && !lks.IsKeyDown(Keys.Tab))
            {
                cursor.Mode++;
            }

            if (cks.IsKeyDown(Keys.F2) && !lks.IsKeyDown(Keys.F2))
            {
                saveForm = new SaveForm(gameWorld);
                saveForm.Show(); //LoadSave.Save(gameWorld);
            }
            if (cks.IsKeyDown(Keys.F5) && !lks.IsKeyDown(Keys.F5))
            {
                LoadSave.Load(ref gameWorld);
            }

            if (cks.IsKeyDown(Keys.F8) && !lks.IsKeyDown(Keys.F8))
            {
                cursor.MirrorMode = !cursor.MirrorMode;
            }

            if (cks.IsKeyDown(Keys.D1))
            {
                cursor.Shape = CursorShape.Circle; cursor.UpdateMesh();
            }
            if (cks.IsKeyDown(Keys.D2))
            {
                cursor.Shape = CursorShape.Square; cursor.UpdateMesh();
            }

            if (cks.IsKeyDown(Keys.Back) && cursor.Mode == CursorMode.Prefab)
            {
                cursor.CutPrefabSapce(gameWorld, Prefabs[selectedPrefab]);
            }

            if (cks.IsKeyDown(Keys.F12) && !lks.IsKeyDown(Keys.F12))
            {
                if (cks.IsKeyDown(Keys.LeftShift))
                {
                    if (gameWorld.Y_CHUNKS > 5)
                    {
                        gameWorld.Y_CHUNKS--;
                    }
                }
                else
                {
                    if (gameWorld.Y_CHUNKS < 50)
                    {
                        gameWorld.Y_CHUNKS++;
                    }
                }
            }
            if (cks.IsKeyDown(Keys.F11) && !lks.IsKeyDown(Keys.F11))
            {
                if (cks.IsKeyDown(Keys.LeftShift))
                {
                    if (gameWorld.X_CHUNKS > 5)
                    {
                        gameWorld.X_CHUNKS--;
                    }
                }
                else if (cks.IsKeyDown(Keys.LeftControl))
                {
                    if (gameWorld.X_CHUNKS < 50)
                    {
                        gameWorld.X_CHUNKS++;
                        gameWorld.PadLeft();
                    }
                }
                else
                {
                    if (gameWorld.X_CHUNKS < 50)
                    {
                        gameWorld.X_CHUNKS++;
                    }
                }
            }

            if (cks.IsKeyDown(Keys.F10) && !lks.IsKeyDown(Keys.F10))
            {
                cursor.Theme++;
                if ((int)cursor.Theme > Enum.GetValues(typeof(Theme)).Length - 1)
                {
                    cursor.Theme = 0;
                }
                gameWorld.Theme = cursor.Theme;
                if (cks.IsKeyDown(Keys.LeftShift))
                {
                    gameWorld.SwitchTheme(cursor.Theme);
                }
            }
            if (cks.IsKeyDown(Keys.F9) && !lks.IsKeyDown(Keys.F9))
            {
                cursor.Theme--;
                if (cursor.Theme < 0)
                {
                    cursor.Theme = (Theme)Enum.GetValues(typeof(Theme)).Length - 1;
                }
                gameWorld.Theme = cursor.Theme;
                if (cks.IsKeyDown(Keys.LeftShift))
                {
                    gameWorld.SwitchTheme(cursor.Theme);
                }
            }

            if (wheelDelta != 0)
            {
                if (cursor.Mode != CursorMode.Prefab && cursor.Mode != CursorMode.Spawn)
                {
                    if (wheelDelta > 0)
                    {
                        if (cks.IsKeyDown(Keys.LeftShift))
                        {
                            cursor.Height++;
                        }

                        else
                        {
                            cursor.Size += 2;
                        }
                        cursor.UpdateMesh();
                    }
                    else
                    {
                        if (cks.IsKeyDown(Keys.LeftShift))
                        {
                            cursor.Height--;
                        }
                        else
                        {
                            cursor.Size -= 2;
                        }
                        cursor.UpdateMesh();
                    }
                }
                else if (cursor.Mode == CursorMode.Prefab)
                {
                    if (wheelDelta > 0)
                    {
                        if (cks.IsKeyDown(Keys.LeftShift))
                        {
                            cursor.Height++;
                        }
                        else
                        {
                            selectedPrefab++;
                        }
                        if (selectedPrefab >= Prefabs.Count)
                        {
                            selectedPrefab = 0;
                        }
                    }
                    else
                    {
                        if (cks.IsKeyDown(Keys.LeftShift))
                        {
                            cursor.Height--;
                        }
                        else
                        {
                            selectedPrefab--;
                        }
                        if (selectedPrefab < 0)
                        {
                            selectedPrefab = Prefabs.Count - 1;
                        }
                    }
                }
                else if (cursor.Mode == CursorMode.Spawn)
                {
                    if (wheelDelta > 0)
                    {
                        if (cks.IsKeyDown(Keys.LeftShift))
                        {
                            cursor.Height++;
                        }
                        else
                        {
                            selectedSpawn++;
                        }
                        if (selectedSpawn >= spawnSprites.AnimChunks.Count)
                        {
                            selectedSpawn = 0;
                        }
                    }
                    else
                    {
                        if (cks.IsKeyDown(Keys.LeftShift))
                        {
                            cursor.Height--;
                        }
                        else
                        {
                            selectedSpawn--;
                        }
                        if (selectedSpawn < 0)
                        {
                            selectedSpawn = spawnSprites.AnimChunks.Count - 1;
                        }
                    }
                }
            }

            if (cms.RightButton == ButtonState.Pressed && lms.RightButton != ButtonState.Pressed)
            {
                if (cursor.Mode == CursorMode.Prefab)
                {
                    Prefabs[selectedPrefab].Rotate();
                }
                if (cursor.Mode == CursorMode.Spawn)
                {
                    spawnRot++;
                    if (spawnRot > 3)
                    {
                        spawnRot = 0;
                    }
                    //for(int i=0;i<spawnSprites.AnimChunks.Count;i++)
                    //  spawnSprites.AnimChunks[i].Rotate();
                }
            }

            if (cms.LeftButton == ButtonState.Pressed)
            {
                if (brushTime == 0)
                {
                    cursor.PerformAction(gameWorld, Prefabs[selectedPrefab], selectedSpawn, spawnRot);
                }

                brushTime += gameTime.ElapsedGameTime.TotalMilliseconds;
                if (cursor.Mode != CursorMode.Spawn && brushTime > 50)
                {
                    brushTime = 0;
                }
            }
            if (cms.LeftButton == ButtonState.Released)
            {
                brushTime = 0;
            }

            Viewport vp = GraphicsDevice.Viewport;
            //  Note the order of the parameters! Projection first.
            Vector3 pos1    = vp.Unproject(new Vector3(mousePos.X, mousePos.Y, 0), gameCamera.projectionMatrix, gameCamera.viewMatrix, gameCamera.worldMatrix);
            Vector3 pos2    = vp.Unproject(new Vector3(mousePos.X, mousePos.Y, 1), gameCamera.projectionMatrix, gameCamera.viewMatrix, gameCamera.worldMatrix);
            Ray     pickRay = new Ray(pos1, Vector3.Normalize(pos2 - pos1));
            int     wpx     = 0;
            int     wpy     = 0;
            int     wpz     = 0;

            foreach (Chunk c in gameWorld.Chunks)
            {
                if (!c.Visible)
                {
                    continue;
                }
                if (!pickRay.Intersects(c.boundingSphere).HasValue)
                {
                    continue;
                }
                for (int y = 0; y < Chunk.Y_SIZE; y++)
                {
                    for (int x = 0; x < Chunk.X_SIZE; x++)
                    {
                        //for (int z = Chunk.Z_SIZE - 1; z >= 0; z--)
                        //{
                        //if (c.Voxels[x, y, z].Active == false) continue;

                        int z = Chunk.Z_SIZE - 1;

                        Vector3     worldOffset = new Vector3(c.worldX * (Chunk.X_SIZE * Voxel.SIZE), c.worldY * (Chunk.Y_SIZE * Voxel.SIZE), c.worldZ * (Chunk.Z_SIZE * Voxel.SIZE)) + ((new Vector3(x, y, z) * Voxel.SIZE));
                        BoundingBox box         = new BoundingBox(worldOffset + new Vector3(-Voxel.HALF_SIZE, -Voxel.HALF_SIZE, -Voxel.HALF_SIZE), worldOffset + new Vector3(Voxel.HALF_SIZE, Voxel.HALF_SIZE, Voxel.HALF_SIZE));
                        if (pickRay.Intersects(box).HasValue)
                        {
                            wpx = (c.worldX * Chunk.X_SIZE) + x;
                            wpy = (c.worldY * Chunk.Y_SIZE) + y;
                            wpz = (c.worldZ * Chunk.Z_SIZE) + z;
                            for (int zz = Chunk.Z_SIZE - 1; zz >= 0; zz--)
                            {
                                if (!gameWorld.GetVoxel(wpx, wpy, zz).Active || gameWorld.GetVoxel(wpx, wpy, zz).Type != VoxelType.Ground)
                                {
                                    wpz = zz; break;
                                }
                            }
                            //if (cursor.Mode == CursorMode.Prefab || cursor.Mode == CursorMode.Spawn)
                            wpz             = (Chunk.Z_SIZE) - cursor.Height;
                            cursor.Position = new Vector3(wpx, wpy, wpz);
                            //gameWorld.SetVoxelActive(wpx, wpy, wpz, false);
                            break;
                        }
                    }
                }
                //}
            }

            lks = cks;
            lms = cms;

            drawEffect.View  = gameCamera.viewMatrix;
            drawEffect.World = gameCamera.worldMatrix;

            cursorEffect.World = Matrix.CreateTranslation(cursor.Position * Voxel.SIZE) * gameCamera.worldMatrix;

            gameWorld.Update(gameTime, gameCamera);
            cursor.Update(gameTime);

            base.Update(gameTime);
        }