Exemplo n.º 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)
        {
            InputDing.PreUpdate();

            if (InputDing.CurKey.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (InputDing.KeyDownUp(Keys.OemPlus) || InputDing.KeyDownUp(Keys.Add))
            {
                speedFactor *= 2;
                numbertje    = (numbertje - 1f) / 2f + 1f;

                if (speedFactor <= 0)
                {
                    numbertje   = 0;
                    speedFactor = 1;
                }
            }

            if (InputDing.KeyDownUp(Keys.OemMinus))
            {
                if (speedFactor >= 2)
                {
                    speedFactor /= 2;
                    numbertje    = (numbertje - 1f) * 2f + 1f;
                }
            }



            //Line of sight stuff
            //Should happen when player runs out of range

            if (InputDing.KeyDownUp(Keys.C))
            {
                if (chaseCamera == false)
                {
                    fromAboveCamera = false;
                    followCamera    = false;
                }
                chaseCamera = !chaseCamera;
            }

            if (InputDing.KeyDownUp(Keys.B))
            {
                chaseCameraShowDebugBlocks = !chaseCameraShowDebugBlocks;
            }

            if (chaseCamera || chaseCameraShowDebugBlocks)
            {
                if (curChaseCameraPoint == null)
                {
                    curChaseCameraPoint = determiner.GetNextLosObject();
                }

                if (curChaseCameraPoint != null)
                {
                    var curmazepoint = GetPosAtThisNumerMazePoint(numbertje);
                    var curposnumber = currentPath.IndexOf(curmazepoint);

                    while (true)
                    {
                        if (!curChaseCameraPoint.LosPoints.Any(t => currentPath.Skip(curposnumber).Any(z => t.X == z.X && t.Y == z.Y)))
                        {
                            curChaseCameraPoint = determiner.GetNextLosObject();
                            if (curChaseCameraPoint == null)
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }


                if (InputDing.KeyDownUp(Keys.Enter))
                {
                    curChaseCameraPoint = determiner.GetNextLosObject();
                }
            }

            if (chaseCamera && curChaseCameraPoint != null)
            {
                camera.cameraPosition = new Vector3(curChaseCameraPoint.CameraPoint.X * 10.0f, 7.5f, curChaseCameraPoint.CameraPoint.Y * 10.0f);

                var playerPos = GetPosAtThisNumer(numbertje);


                var newRot = (float)Math.Atan2(playerPos.Y - curChaseCameraPoint.CameraPoint.Y, playerPos.X - curChaseCameraPoint.CameraPoint.X) * -1f - (MathHelper.Pi / 2.0f);

                camera.updownRot    = 0;
                camera.leftrightRot = newRot;
            }



            if (InputDing.KeyDownUp(Keys.T))
            {
                if (!fromAboveCamera)
                {
                    chaseCamera  = false;
                    followCamera = false;
                    drawRoof     = false;

                    camera.leftrightRot = 0.15f;
                    camera.updownRot    = -0.72f;
                }
                fromAboveCamera = !fromAboveCamera;
            }


            if (fromAboveCamera)
            {
                //float prenumbertje = numbertje - (float)gameTime.ElapsedGameTime.TotalSeconds;

                //var pre = GetPosAtThisNumer(prenumbertje);
                var now = GetPosAtThisNumer(numbertje);

                camera.cameraPosition = new Vector3((now.X + 1.9f) * 10.0f, (7.0f) * 10.0f, (now.Y + 7.0f) * 10.0f);
            }



            if (InputDing.KeyDownUp(Keys.O))
            {
                if (!followCamera)
                {
                    fromAboveCamera = false;
                    chaseCamera     = false;
                }
                followCamera = !followCamera;
            }

            if (followCamera)
            {
                var pospos       = GetPosAtThisNumer(numbertje);
                var posposbefore = GetPosAtThisNumer(numbertje - (0.5f / (float)speedFactor));
                var posposnext   = GetPosAtThisNumer(Math.Max(numbertje + (0.5f / (float)speedFactor), 1.1f));
                var pospos3d     = new Vector3(pospos.X * 10.0f, 7.5f, pospos.Y * 10.0f);

                camera.cameraPosition = pospos3d;

                camera.updownRot = 0;

                var oldRot = camera.leftrightRot;
                var newRot = (float)Math.Atan2(posposnext.Y - posposbefore.Y, posposnext.X - posposbefore.X) * -1f - (MathHelper.Pi / 2.0f);

                //camera.leftrightRot = (9.0f * oldRot + 1.0f * newRot) / 10.0f;
                camera.leftrightRot = newRot;
            }



            //Reset when done
            if ((numbertje * speedFactor) > pathCount + speedFactor)
            {
                numbertje = 0;
                GenerateMaze();
            }


            camera.Update(gameTime);


            if (InputDing.KeyDownUp(Keys.Up))
            {
                numbertje      = 0;
                curMazeWidth  *= 2;
                curMazeHeight *= 2;
                GenerateMaze();
            }

            if (InputDing.KeyDownUp(Keys.Down))
            {
                if (curMazeWidth > 4 && curMazeHeight > 4)
                {
                    numbertje      = 0;
                    curMazeWidth  /= 2;
                    curMazeHeight /= 2;
                    if (curMazeWidth < 1)
                    {
                        curMazeWidth = 1;
                    }
                    if (curMazeHeight < 1)
                    {
                        curMazeHeight = 1;
                    }
                    GenerateMaze();
                }
            }

            if (InputDing.CurKey.IsKeyDown(Keys.D0))
            {
                GenerateMaze();
            }

            if (InputDing.KeyDownUp(Keys.H))
            {
                drawRoof = !drawRoof;
            }

            if (InputDing.KeyDownUp(Keys.L))
            {
                lighting = !lighting;
            }

            if (InputDing.KeyDownUp(Keys.P))
            {
                drawPath = !drawPath;
            }

            numbertje += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (InputDing.CurKey.IsKeyDown(Keys.G))
            {
                numbertje = 0;
            }

            if (InputDing.KeyDownUp(Keys.R))
            {
                numbertje = 0;
                GenerateMaze();
            }

            InputDing.AfterUpdate();
            base.Update(gameTime);
        }
Exemplo n.º 2
0
        public void GenerateMaze()
        {
            if (indexBuffer != null)
            {
                indexBuffer.Dispose();
            }
            if (vertexBuffer != null)
            {
                vertexBuffer.Dispose();
            }


            Algorithm alg;
            int       randomnumber = curMazeWidth < 2048 ? random.Next(3) : random.Next(2);

            if (randomnumber == 0)
            {
                alg = new AlgorithmBacktrack();
            }
            else if (randomnumber == 1)
            {
                alg = new AlgorithmDivision();
            }
            else
            {
                alg = new AlgorithmKruskal();
            }

            lastAlgorithm = alg.GetType().Name;

            currentMaze = alg.Generate(curMazeWidth, curMazeHeight, InnerMapType.BitArreintjeFast, null);
            var walls = currentMaze.GenerateListOfMazeWalls();

            currentPath = PathFinderDepthFirst.GoFind(currentMaze.InnerMap, null);


            determiner          = new LineOfSightDeterminer(currentMaze.InnerMap, currentPath);
            curChaseCameraPoint = null;

            VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[walls.Count * 8];
            int[] indices = new int[walls.Count * 12];

            int curVertice = 0;
            int curIndice  = 0;



            foreach (var wall in walls)
            {
                //int factorHeight = 10;
                //int factorWidth = 10;

                WallModel model = new WallModel(wall);

                model.GoGenerateVertices(vertices, indices, ref curVertice, ref curIndice);
            }

            wallsCount = walls.Count;

            vertexBuffer = new VertexBuffer(GraphicsDevice, VertexPositionNormalTexture.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
            indexBuffer  = new IndexBuffer(GraphicsDevice, IndexElementSize.ThirtyTwoBits, indices.Length, BufferUsage.WriteOnly);

            vertexBuffer.SetData(vertices);
            indexBuffer.SetData(indices);

            GeneratePath(currentPath);
        }