コード例 #1
0
ファイル: Index2Tests.cs プロジェクト: marsat02/octoawesome
        public void Index2ShortestDistanceOnAxisTest()
        {
            // Ursprung Null, Ziel Null
            Assert.Equals(0, Index2.ShortestDistanceOnAxis(0, 0, 10));

            // Ursprung Null, Ziel positiv in Range
            Assert.Equals(1, Index2.ShortestDistanceOnAxis(0, 1, 10));
            Assert.Equals(2, Index2.ShortestDistanceOnAxis(0, 2, 10));
            Assert.Equals(3, Index2.ShortestDistanceOnAxis(0, 3, 10));
            Assert.Equals(4, Index2.ShortestDistanceOnAxis(0, 4, 10));
            Assert.Equals(5, Index2.ShortestDistanceOnAxis(0, 5, 10));
            Assert.Equals(-4, Index2.ShortestDistanceOnAxis(0, 6, 10));
            Assert.Equals(-3, Index2.ShortestDistanceOnAxis(0, 7, 10));
            Assert.Equals(-2, Index2.ShortestDistanceOnAxis(0, 8, 10));
            Assert.Equals(-1, Index2.ShortestDistanceOnAxis(0, 9, 10));
            Assert.Equals(0, Index2.ShortestDistanceOnAxis(0, 10, 10));

            // Ursprung Null, Ziel negativ in Range
            Assert.Equals(-1, Index2.ShortestDistanceOnAxis(0, -1, 10));
            Assert.Equals(-2, Index2.ShortestDistanceOnAxis(0, -2, 10));
            Assert.Equals(-3, Index2.ShortestDistanceOnAxis(0, -3, 10));
            Assert.Equals(-4, Index2.ShortestDistanceOnAxis(0, -4, 10));
            Assert.Equals(5, Index2.ShortestDistanceOnAxis(0, -5, 10));
            Assert.Equals(4, Index2.ShortestDistanceOnAxis(0, -6, 10));
            Assert.Equals(3, Index2.ShortestDistanceOnAxis(0, -7, 10));
            Assert.Equals(2, Index2.ShortestDistanceOnAxis(0, -8, 10));
            Assert.Equals(1, Index2.ShortestDistanceOnAxis(0, -9, 10));
            Assert.Equals(0, Index2.ShortestDistanceOnAxis(0, -10, 10));

            // Urspung null, Ziel out of size
            Assert.Equals(5, Index2.ShortestDistanceOnAxis(0, 15, 10));
            Assert.Equals(2, Index2.ShortestDistanceOnAxis(0, 32, 10));
            Assert.Equals(5, Index2.ShortestDistanceOnAxis(0, -15, 10));
            Assert.Equals(-4, Index2.ShortestDistanceOnAxis(0, -54, 10));

            // Ursprung positiv in Range
            Assert.Equals(-3, Index2.ShortestDistanceOnAxis(3, 0, 10));
            Assert.Equals(2, Index2.ShortestDistanceOnAxis(3, 5, 10));
            Assert.Equals(2, Index2.ShortestDistanceOnAxis(3, -5, 10));
            Assert.Equals(2, Index2.ShortestDistanceOnAxis(3, 15, 10));
            Assert.Equals(-1, Index2.ShortestDistanceOnAxis(3, 32, 10));
            Assert.Equals(2, Index2.ShortestDistanceOnAxis(3, -15, 10));
            Assert.Equals(3, Index2.ShortestDistanceOnAxis(3, -54, 10));

            // Ursprung negativ in Range
            Assert.Equals(3, Index2.ShortestDistanceOnAxis(-3, 0, 10));
            Assert.Equals(-2, Index2.ShortestDistanceOnAxis(-3, 5, 10));
            Assert.Equals(-2, Index2.ShortestDistanceOnAxis(-3, -5, 10));
            Assert.Equals(-2, Index2.ShortestDistanceOnAxis(-3, 15, 10));
            Assert.Equals(-5, Index2.ShortestDistanceOnAxis(-3, 32, 10));
            Assert.Equals(-2, Index2.ShortestDistanceOnAxis(-3, -15, 10));
            Assert.Equals(-1, Index2.ShortestDistanceOnAxis(-3, -54, 10));
        }
コード例 #2
0
        protected override void OnPreDraw(GameTime gameTime)
        {
            if (player.ActorHost == null)
            {
                return;
            }

            if (ControlTexture == null)
            {
                ControlTexture = new RenderTarget2D(Manager.GraphicsDevice, ActualClientArea.Width, ActualClientArea.Height, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);
            }

            float octoDaysPerEarthDay = 360f;
            float inclinationVariance = MathHelper.Pi / 3f;

            float playerPosX = ((float)player.ActorHost.Player.Position.GlobalPosition.X / (planet.Size.X * Chunk.CHUNKSIZE_X)) * MathHelper.TwoPi;
            float playerPosY = ((float)player.ActorHost.Player.Position.GlobalPosition.Y / (planet.Size.Y * Chunk.CHUNKSIZE_Y)) * MathHelper.TwoPi;

            TimeSpan diff = DateTime.UtcNow - new DateTime(1888, 8, 8);

            float inclination = ((float)Math.Sin(playerPosY) * inclinationVariance) + MathHelper.Pi / 6f;
            //Console.WriteLine("Stand: " + (MathHelper.Pi + playerPosX) + " Neigung: " + inclination);
            Matrix sunMovement =
                Matrix.CreateRotationX(inclination) *
                //Matrix.CreateRotationY((((float)gameTime.TotalGameTime.TotalMinutes * MathHelper.TwoPi) + playerPosX) * -1);
                Matrix.CreateRotationY((float)(MathHelper.TwoPi - ((diff.TotalDays * octoDaysPerEarthDay * MathHelper.TwoPi) % MathHelper.TwoPi)));

            Vector3 sunDirection = Vector3.Transform(new Vector3(0, 0, 1), sunMovement);

            simpleShader.Parameters["DiffuseColor"].SetValue(new Microsoft.Xna.Framework.Color(190, 190, 190).ToVector4());
            simpleShader.Parameters["DiffuseIntensity"].SetValue(0.6f);
            simpleShader.Parameters["DiffuseDirection"].SetValue(sunDirection);

            // Console.WriteLine(sunDirection);

            // Index3 chunkOffset = player.ActorHost.Position.ChunkIndex;
            Index3 chunkOffset = camera.CameraChunk;

            Microsoft.Xna.Framework.Color background =
                new Microsoft.Xna.Framework.Color(181, 224, 255);

            Manager.GraphicsDevice.SetRenderTarget(MiniMapTexture);
            Manager.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            Manager.GraphicsDevice.Clear(background);

            foreach (var renderer in chunkRenderer)
            {
                if (!renderer.ChunkPosition.HasValue)
                {
                    continue;
                }

                Index3 shift = chunkOffset.ShortestDistanceXY(
                    renderer.ChunkPosition.Value, new Index2(
                        planet.Size.X,
                        planet.Size.Y));

                BoundingBox chunkBox = new BoundingBox(
                    new Vector3(
                        shift.X * Chunk.CHUNKSIZE_X,
                        shift.Y * Chunk.CHUNKSIZE_Y,
                        shift.Z * Chunk.CHUNKSIZE_Z),
                    new Vector3(
                        (shift.X + 1) * Chunk.CHUNKSIZE_X,
                        (shift.Y + 1) * Chunk.CHUNKSIZE_Y,
                        (shift.Z + 1) * Chunk.CHUNKSIZE_Z));

                int range = 3;
                if (shift.X >= -range && shift.X <= range &&
                    shift.Y >= -range && shift.Y <= range)
                {
                    renderer.Draw(camera.MinimapView, miniMapProjectionMatrix, shift);
                }
            }

            Manager.GraphicsDevice.SetRenderTarget(ControlTexture);
            Manager.GraphicsDevice.Clear(background);

            Manager.GraphicsDevice.BlendState        = BlendState.AlphaBlend;
            Manager.GraphicsDevice.DepthStencilState = DepthStencilState.None;

            // Draw Sun
            // GraphicsDevice.RasterizerState = RasterizerState.CullNone;
            sunEffect.Texture = sunTexture;
            Matrix billboard = Matrix.Invert(camera.View);

            billboard.Translation = player.ActorHost.Position.LocalPosition + (sunDirection * -10);
            sunEffect.World       = billboard;
            sunEffect.View        = camera.View;
            sunEffect.Projection  = camera.Projection;
            sunEffect.CurrentTechnique.Passes[0].Apply();
            Manager.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, billboardVertices, 0, 2);

            Manager.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            foreach (var renderer in chunkRenderer)
            {
                if (!renderer.ChunkPosition.HasValue)
                {
                    continue;
                }

                Index3 shift = chunkOffset.ShortestDistanceXY(
                    renderer.ChunkPosition.Value, new Index2(
                        planet.Size.X,
                        planet.Size.Y));

                BoundingBox chunkBox = new BoundingBox(
                    new Vector3(
                        shift.X * Chunk.CHUNKSIZE_X,
                        shift.Y * Chunk.CHUNKSIZE_Y,
                        shift.Z * Chunk.CHUNKSIZE_Z),
                    new Vector3(
                        (shift.X + 1) * Chunk.CHUNKSIZE_X,
                        (shift.Y + 1) * Chunk.CHUNKSIZE_Y,
                        (shift.Z + 1) * Chunk.CHUNKSIZE_Z));

                if (camera.Frustum.Intersects(chunkBox))
                {
                    renderer.Draw(camera.View, camera.Projection, shift);
                }
            }

            if (player.SelectedBox.HasValue)
            {
                // Index3 offset = player.ActorHost.Position.ChunkIndex * Chunk.CHUNKSIZE;
                Index3 offset           = camera.CameraChunk * Chunk.CHUNKSIZE;
                Index3 planetSize       = planet.Size * Chunk.CHUNKSIZE;
                Index3 relativePosition = new Index3(
                    Index2.ShortestDistanceOnAxis(offset.X, player.SelectedBox.Value.X, planetSize.X),
                    Index2.ShortestDistanceOnAxis(offset.Y, player.SelectedBox.Value.Y, planetSize.Y),
                    player.SelectedBox.Value.Z - offset.Z);

                Vector3 selectedBoxPosition = new Vector3(
                    player.SelectedBox.Value.X - (chunkOffset.X * Chunk.CHUNKSIZE_X),
                    player.SelectedBox.Value.Y - (chunkOffset.Y * Chunk.CHUNKSIZE_Y),
                    player.SelectedBox.Value.Z - (chunkOffset.Z * Chunk.CHUNKSIZE_Z));
                // selectionEffect.World = Matrix.CreateTranslation(selectedBoxPosition);
                selectionEffect.World      = Matrix.CreateTranslation(relativePosition);
                selectionEffect.View       = camera.View;
                selectionEffect.Projection = camera.Projection;
                foreach (var pass in selectionEffect.CurrentTechnique.Passes)
                {
                    pass.Apply();
                    Manager.GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.LineList, selectionLines, 0, 8, selectionIndeces, 0, 12);
                }
            }

            Manager.GraphicsDevice.SetRenderTarget(null);
        }