コード例 #1
0
 public void DrawMovingBlockSet(Camera camera, MovingBlockSet movingBlockSet)
 {
     if (m_vertices.Count <= 20000 && camera.ViewFrustum.Intersection(movingBlockSet.BoundingBox(extendToFillCells: false)))
     {
         GenerateGeometry(movingBlockSet);
         int      count = m_vertices.Count;
         ushort[] array = movingBlockSet.Indices.Array;
         _ = movingBlockSet.Indices.Count;
         Vector3         vector = movingBlockSet.Position + movingBlockSet.GeometryOffset;
         TerrainVertex[] array2 = movingBlockSet.Vertices.Array;
         int             count2 = movingBlockSet.Vertices.Count;
         for (int i = 0; i < count2; i++)
         {
             TerrainVertex item = array2[i];
             item.X += vector.X;
             item.Y += vector.Y;
             item.Z += vector.Z;
             m_vertices.Add(item);
         }
         for (int j = 0; j < movingBlockSet.Indices.Count; j++)
         {
             m_indices.Add((ushort)(array[j] + count));
         }
     }
 }
コード例 #2
0
        public void TerrainCollision(MovingBlockSet movingBlockSet)
        {
            Point3 point = default(Point3);

            point.X = (int)MathUtils.Floor((float)movingBlockSet.Box.Left + movingBlockSet.Position.X);
            point.Y = (int)MathUtils.Floor((float)movingBlockSet.Box.Top + movingBlockSet.Position.Y);
            point.Z = (int)MathUtils.Floor((float)movingBlockSet.Box.Near + movingBlockSet.Position.Z);
            Point3 point2 = default(Point3);

            point2.X = (int)MathUtils.Ceiling((float)movingBlockSet.Box.Right + movingBlockSet.Position.X);
            point2.Y = (int)MathUtils.Ceiling((float)movingBlockSet.Box.Bottom + movingBlockSet.Position.Y);
            point2.Z = (int)MathUtils.Ceiling((float)movingBlockSet.Box.Far + movingBlockSet.Position.Z);
            for (int i = point.X; i < point2.X; i++)
            {
                for (int j = point.Z; j < point2.Z; j++)
                {
                    for (int k = point.Y; k < point2.Y; k++)
                    {
                        if (Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValue(i, k, j)) != 0)
                        {
                            this.CollidedWithTerrain?.Invoke(movingBlockSet, new Point3(i, k, j));
                        }
                    }
                }
            }
        }
コード例 #3
0
        public MovingBlocksRaycastResult?Raycast(Vector3 start, Vector3 end, bool extendToFillCells)
        {
            Ray3        ray         = new Ray3(start, Vector3.Normalize(end - start));
            BoundingBox boundingBox = new BoundingBox(Vector3.Min(start, end), Vector3.Max(start, end));

            m_result.Clear();
            FindMovingBlocks(boundingBox, extendToFillCells, m_result);
            float          num            = float.MaxValue;
            MovingBlockSet movingBlockSet = null;

            foreach (MovingBlockSet item in m_result)
            {
                BoundingBox box  = item.BoundingBox(extendToFillCells);
                float?      num2 = ray.Intersection(box);
                if (num2.HasValue && num2.Value < num)
                {
                    num            = num2.Value;
                    movingBlockSet = item;
                }
            }
            if (movingBlockSet != null)
            {
                MovingBlocksRaycastResult value = default(MovingBlocksRaycastResult);
                value.Ray            = ray;
                value.Distance       = num;
                value.MovingBlockSet = movingBlockSet;
                return(value);
            }
            return(null);
        }
コード例 #4
0
        public IMovingBlockSet AddMovingBlockSet(Vector3 position, Vector3 targetPosition, float speed, float acceleration, float drag, Vector2 smoothness, IEnumerable <MovingBlock> blocks, string id, object tag, bool testCollision)
        {
            MovingBlockSet movingBlockSet = new MovingBlockSet
            {
                Position       = position,
                StartPosition  = position,
                TargetPosition = targetPosition,
                Speed          = speed,
                Acceleration   = acceleration,
                Drag           = drag,
                Smoothness     = smoothness,
                Id             = id,
                Tag            = tag,
                Blocks         = blocks.ToList()
            };

            movingBlockSet.UpdateBox();
            if (testCollision)
            {
                MovingBlocksCollision(movingBlockSet);
                if (movingBlockSet.Stop)
                {
                    return(null);
                }
            }
            if (m_canGenerateGeometry)
            {
                GenerateGeometry(movingBlockSet);
            }
            m_movingBlockSets.Add(movingBlockSet);
            return(movingBlockSet);
        }
コード例 #5
0
        public void RemoveMovingBlockSet(IMovingBlockSet movingBlockSet)
        {
            MovingBlockSet movingBlockSet2 = (MovingBlockSet)movingBlockSet;

            if (m_movingBlockSets.Remove(movingBlockSet2))
            {
                m_removing.Add(movingBlockSet2);
                movingBlockSet2.RemainCounter = 4;
            }
        }
コード例 #6
0
        public void Draw(Camera camera, int drawOrder)
        {
            m_vertices.Clear();
            m_indices.Clear();
            foreach (MovingBlockSet movingBlockSet2 in m_movingBlockSets)
            {
                DrawMovingBlockSet(camera, movingBlockSet2);
            }
            int num = 0;

            while (num < m_removing.Count)
            {
                MovingBlockSet movingBlockSet = m_removing[num];
                if (movingBlockSet.RemainCounter-- > 0)
                {
                    DrawMovingBlockSet(camera, movingBlockSet);
                    num++;
                }
                else
                {
                    m_removing.RemoveAt(num);
                }
            }
            if (m_vertices.Count > 0)
            {
                Vector3 viewPosition = camera.ViewPosition;
                Vector3 v            = new Vector3(MathUtils.Floor(viewPosition.X), 0f, MathUtils.Floor(viewPosition.Z));
                Matrix  value        = Matrix.CreateTranslation(v - viewPosition) * camera.ViewMatrix.OrientationMatrix * camera.ProjectionMatrix;
                Display.BlendState        = BlendState.Opaque;
                Display.DepthStencilState = DepthStencilState.Default;
                Display.RasterizerState   = RasterizerState.CullCounterClockwiseScissor;
                m_shader.GetParameter("u_origin").SetValue(v.XZ);
                m_shader.GetParameter("u_viewProjectionMatrix").SetValue(value);
                m_shader.GetParameter("u_viewPosition").SetValue(camera.ViewPosition);
                m_shader.GetParameter("u_texture").SetValue(m_subsystemAnimatedTextures.AnimatedBlocksTexture);
                m_shader.GetParameter("u_samplerState").SetValue(SamplerState.PointClamp);
                m_shader.GetParameter("u_fogColor").SetValue(new Vector3(m_subsystemSky.ViewFogColor));
                m_shader.GetParameter("u_fogStartInvLength").SetValue(new Vector2(m_subsystemSky.ViewFogRange.X, 1f / (m_subsystemSky.ViewFogRange.Y - m_subsystemSky.ViewFogRange.X)));
                Display.DrawUserIndexed(PrimitiveType.TriangleList, m_shader, TerrainVertex.VertexDeclaration, m_vertices.Array, 0, m_vertices.Count, m_indices.Array, 0, m_indices.Count);
            }
            if (DebugDrawMovingBlocks)
            {
                DebugDraw();
            }
        }
コード例 #7
0
        public void MovingBlocksCollision(MovingBlockSet movingBlockSet)
        {
            BoundingBox boundingBox = movingBlockSet.BoundingBox(extendToFillCells: true);

            m_result.Clear();
            FindMovingBlocks(boundingBox, extendToFillCells: true, m_result);
            int num = 0;

            while (true)
            {
                if (num < m_result.Count)
                {
                    if (m_result.Array[num] != movingBlockSet)
                    {
                        break;
                    }
                    num++;
                    continue;
                }
                return;
            }
            movingBlockSet.Stop = true;
        }
コード例 #8
0
        public void GenerateGeometry(MovingBlockSet movingBlockSet)
        {
            Point3 point = default(Point3);

            point.X = ((movingBlockSet.CurrentVelocity.X > 0f) ? ((int)MathUtils.Floor(movingBlockSet.Position.X)) : (point.X = (int)MathUtils.Ceiling(movingBlockSet.Position.X)));
            point.Y = ((movingBlockSet.CurrentVelocity.Y > 0f) ? ((int)MathUtils.Floor(movingBlockSet.Position.Y)) : (point.Y = (int)MathUtils.Ceiling(movingBlockSet.Position.Y)));
            point.Z = ((movingBlockSet.CurrentVelocity.Z > 0f) ? ((int)MathUtils.Floor(movingBlockSet.Position.Z)) : (point.Z = (int)MathUtils.Ceiling(movingBlockSet.Position.Z)));
            if (!(point != movingBlockSet.GeometryGenerationPosition))
            {
                return;
            }
            Point3 p      = new Point3(movingBlockSet.Box.Left, movingBlockSet.Box.Top, movingBlockSet.Box.Near);
            Point3 point2 = new Point3(movingBlockSet.Box.Width, movingBlockSet.Box.Height, movingBlockSet.Box.Depth);

            point2.Y = MathUtils.Min(point2.Y, 254);
            if (m_blockGeometryGenerator == null)
            {
                int x = 2;
                x = (int)MathUtils.NextPowerOf2((uint)x);
                m_blockGeometryGenerator = new BlockGeometryGenerator(new Terrain(), m_subsystemTerrain, null, base.Project.FindSubsystem <SubsystemFurnitureBlockBehavior>(throwOnError: true), null, base.Project.FindSubsystem <SubsystemPalette>(throwOnError: true));
                for (int i = 0; i < x; i++)
                {
                    for (int j = 0; j < x; j++)
                    {
                        m_blockGeometryGenerator.Terrain.AllocateChunk(i, j);
                    }
                }
            }
            Terrain terrain = m_subsystemTerrain.Terrain;

            for (int k = 0; k < point2.X + 2; k++)
            {
                for (int l = 0; l < point2.Z + 2; l++)
                {
                    int x2         = k + p.X + point.X - 1;
                    int z          = l + p.Z + point.Z - 1;
                    int shaftValue = terrain.GetShaftValue(x2, z);
                    m_blockGeometryGenerator.Terrain.SetTemperature(k, l, Terrain.ExtractTemperature(shaftValue));
                    m_blockGeometryGenerator.Terrain.SetHumidity(k, l, Terrain.ExtractHumidity(shaftValue));
                    for (int m = 0; m < point2.Y + 2; m++)
                    {
                        int y     = m + p.Y + point.Y - 1;
                        int light = Terrain.ExtractLight(terrain.GetCellValue(x2, y, z));
                        m_blockGeometryGenerator.Terrain.SetCellValueFast(k, m, l, Terrain.MakeBlockValue(0, light, 0));
                    }
                }
            }
            m_blockGeometryGenerator.Terrain.SeasonTemperature = terrain.SeasonTemperature;
            m_blockGeometryGenerator.Terrain.SeasonHumidity    = terrain.SeasonHumidity;
            foreach (MovingBlock block in movingBlockSet.Blocks)
            {
                int x3    = block.Offset.X - p.X + 1;
                int y2    = block.Offset.Y - p.Y + 1;
                int z2    = block.Offset.Z - p.Z + 1;
                int value = Terrain.ReplaceLight(light: m_blockGeometryGenerator.Terrain.GetCellLightFast(x3, y2, z2), value: block.Value);
                m_blockGeometryGenerator.Terrain.SetCellValueFast(x3, y2, z2, value);
            }
            m_blockGeometryGenerator.ResetCache();
            movingBlockSet.Vertices.Clear();
            movingBlockSet.Indices.Clear();
            for (int n = 1; n < point2.X + 1; n++)
            {
                for (int num = 1; num < point2.Y + 1; num++)
                {
                    for (int num2 = 1; num2 < point2.Z + 1; num2++)
                    {
                        int cellValueFast = m_blockGeometryGenerator.Terrain.GetCellValueFast(n, num, num2);
                        int num3          = Terrain.ExtractContents(cellValueFast);
                        if (num3 != 0)
                        {
                            BlocksManager.Blocks[num3].GenerateTerrainVertices(m_blockGeometryGenerator, movingBlockSet.Geometry, cellValueFast, n, num, num2);
                        }
                    }
                }
            }
            movingBlockSet.GeometryOffset             = new Vector3(p) - new Vector3(1f);
            movingBlockSet.GeometryGenerationPosition = point;
        }