Exemple #1
0
        public bool GetVoxel(VoxelChunk checkFirst, Vector3 worldLocation, ref Voxel newReference)
        {
            while (true)
            {
                if (checkFirst != null)
                {
                    if (checkFirst.IsWorldLocationValid(worldLocation))
                    {
                        if (!checkFirst.GetVoxelAtWorldLocation(worldLocation, ref newReference))
                        {
                            return(false);
                        }

                        Vector3 grid = checkFirst.WorldToGrid(worldLocation);
                        newReference.Chunk        = checkFirst;
                        newReference.GridPosition = new Vector3((int)grid.X, (int)grid.Y, (int)grid.Z);

                        return(true);
                    }

                    checkFirst = null;
                    continue;
                }

                VoxelChunk chunk = GetVoxelChunkAtWorldLocation(worldLocation);

                if (chunk == null)
                {
                    return(false);
                }


                return(chunk.GetVoxelAtWorldLocation(worldLocation, ref newReference));
            }
        }
Exemple #2
0
        public override void Update(DwarfTime gameTime, ChunkManager chunks, Camera camera)
        {
            UpdateTimer.Update(gameTime);
            if (HasMoved && UpdateTimer.HasTriggered)
            {
                Body p = (Body)Parent;

                VoxelChunk chunk = chunks.ChunkData.GetVoxelChunkAtWorldLocation(p.GlobalTransform.Translation);

                if (chunk != null)
                {
                    Vector3 g = chunk.WorldToGrid(p.GlobalTransform.Translation + Vector3.Down * 0.25f);

                    int h = chunk.GetFilledVoxelGridHeightAt((int)g.X, (int)g.Y, (int)g.Z);

                    if (h != -1)
                    {
                        Vector3 pos = p.GlobalTransform.Translation;
                        pos.Y = h;
                        pos  += VertexNoise.GetNoiseVectorFromRepeatingTexture(pos + Vector3.Down * 0.25f);
                        float  scaleFactor = GlobalScale / (Math.Max((p.GlobalTransform.Translation.Y - h) * 0.25f, 1));
                        Matrix newTrans    = OriginalTransform;
                        newTrans            *= Matrix.CreateScale(scaleFactor);
                        newTrans.Translation = (pos - p.GlobalTransform.Translation) + new Vector3(0.0f, 0.1f, 0.0f);
                        Tint           = new Color(Tint.R, Tint.G, Tint.B, (int)(scaleFactor * 255));
                        LocalTransform = newTrans;
                    }
                }
                UpdateTimer.HasTriggered = false;
            }


            base.Update(gameTime, chunks, camera);
        }
Exemple #3
0
        public bool GetFirstVoxelUnder(Vector3 rayStart, ref Voxel under, bool considerWater = false)
        {
            VoxelChunk startChunk = GetVoxelChunkAtWorldLocation(rayStart);

            if (startChunk == null)
            {
                return(false);
            }

            Point3 point = new Point3(startChunk.WorldToGrid(rayStart));

            for (int y = point.Y; y >= 0; y--)
            {
                int index = startChunk.Data.IndexAt(point.X, y, point.Z);

                if (startChunk.Data.Types[index] != 0 || (considerWater && startChunk.Data.Water[index].WaterLevel > 0))
                {
                    under.Chunk        = startChunk;
                    under.GridPosition = new Vector3(point.X, y, point.Z);
                    return(true);
                }
            }

            return(false);
        }
Exemple #4
0
        public virtual void HandleCollisions(ChunkManager chunks, float dt)
        {
            if (CollideMode == CollisionMode.None)
            {
                return;
            }

            Voxel currentVoxel = new Voxel();
            bool  success      = chunks.ChunkData.GetVoxel(null, LocalTransform.Translation, ref currentVoxel);

            List <Voxel> vs = new List <Voxel>
            {
                currentVoxel
            };

            VoxelChunk chunk = chunks.ChunkData.GetVoxelChunkAtWorldLocation(LocalTransform.Translation);


            if (!success || currentVoxel == null || chunk == null)
            {
                return;
            }

            Vector3 grid = chunk.WorldToGrid(LocalTransform.Translation);

            List <Voxel> adjacencies = chunk.GetNeighborsEuclidean((int)grid.X, (int)grid.Y, (int)grid.Z);

            vs.AddRange(adjacencies);

            // TODO: Find a faster way to do this
            // Vector3 half = Vector3.One*0.5f;
            //vs.Sort((a, b) => (MathFunctions.L1(LocalTransform.Translation, a.Position + half).CompareTo(MathFunctions.L1(LocalTransform.Translation, b.Position + half))));
            int y = (int)Position.Y;

            foreach (Voxel v in vs)
            {
                if (v == null || v.IsEmpty)
                {
                    continue;
                }

                if (CollideMode == CollisionMode.UpDown && (int)v.GridPosition.Y == y)
                {
                    continue;
                }

                if (CollideMode == CollisionMode.Sides && (int)v.GridPosition.Y != y)
                {
                    continue;
                }

                BoundingBox voxAABB = v.GetBoundingBox();
                if (Collide(voxAABB))
                {
                    OnTerrainCollision(v);
                }
            }
        }
Exemple #5
0
        public bool CollidesWithChunks(ChunkManager chunks, Vector3 pos, bool applyForce)
        {
            BoundingBox box          = new BoundingBox(pos - new Vector3(0.5f, 0.5f, 0.5f), pos + new Vector3(0.5f, 0.5f, 0.5f));
            Voxel       currentVoxel = new Voxel();
            bool        success      = chunks.ChunkData.GetVoxel(null, pos, ref currentVoxel);

            List <Voxel> vs = new List <Voxel>
            {
                currentVoxel
            };

            VoxelChunk chunk = chunks.ChunkData.GetVoxelChunkAtWorldLocation(pos);


            if (!success || currentVoxel == null || chunk == null)
            {
                return(false);
            }

            Vector3 grid = chunk.WorldToGrid(pos);

            List <Voxel> adjacencies = chunk.GetNeighborsEuclidean((int)grid.X, (int)grid.Y, (int)grid.Z);

            vs.AddRange(adjacencies);

            bool gotCollision = false;

            foreach (Voxel v in vs)
            {
                if (v.IsEmpty || !v.IsVisible)
                {
                    continue;
                }

                BoundingBox voxAABB = v.GetBoundingBox();
                if (box.Intersects(voxAABB))
                {
                    gotCollision = true;
                    if (applyForce)
                    {
                        Collide(box, voxAABB);
                    }

                    else
                    {
                        return(true);
                    }
                }
            }

            return(gotCollision);
        }
Exemple #6
0
        public WaterCell GetWaterCellAtLocation(Vector3 worldLocation)
        {
            VoxelChunk chunkAtLocation = GetVoxelChunkAtWorldLocation(worldLocation);

            if (chunkAtLocation == null)
            {
                return(new WaterCell());
            }

            Vector3 gridPos = chunkAtLocation.WorldToGrid(worldLocation);

            return(chunkAtLocation.Data.Water[chunkAtLocation.Data.IndexAt((int)gridPos.X, (int)gridPos.Y, (int)gridPos.Z)]);
        }
Exemple #7
0
        public bool GetNeighbors(Vector3 worldPosition, List <Vector3> succ, List <Voxel> toReturn)
        {
            toReturn.Clear();
            VoxelChunk chunk = GetVoxelChunkAtWorldLocation(worldPosition);

            if (chunk == null)
            {
                return(false);
            }

            Vector3 grid = chunk.WorldToGrid(worldPosition);

            chunk.GetNeighborsSuccessors(succ, (int)grid.X, (int)grid.Y, (int)grid.Z, toReturn);
            return(true);
        }
Exemple #8
0
        public bool GetVoxel(VoxelChunk checkFirst, Vector3 worldLocation, ref Voxel newReference)
        {
            while(true)
            {
                if(checkFirst != null)
                {
                    if(checkFirst.IsWorldLocationValid(worldLocation))
                    {
                        if(!checkFirst.GetVoxelAtWorldLocation(worldLocation, ref newReference))
                        {
                            return false;
                        }

                        Vector3 grid = checkFirst.WorldToGrid(worldLocation);
                        newReference.Chunk = checkFirst;
                        newReference.GridPosition = new Vector3((int) grid.X, (int) grid.Y, (int) grid.Z);

                        return true;
                    }

                    checkFirst = null;
                    continue;
                }

                VoxelChunk chunk = GetVoxelChunkAtWorldLocation(worldLocation);

                if (chunk == null)
                {
                    return false;
                }

                return chunk.GetVoxelAtWorldLocation(worldLocation, ref newReference);
            }
        }
Exemple #9
0
        public void Update(DwarfTime gameTime, ChunkManager chunks, Camera camera)
        {
            Vector3 targetVelocity = TargetPosition - Body.GlobalTransform.Translation;

            if (targetVelocity.LengthSquared() > 0.0001f)
            {
                targetVelocity.Normalize();
                targetVelocity *= MaxVelocity;
            }

            Matrix m = Body.LocalTransform;

            m.Translation      += targetVelocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
            Body.LocalTransform = m;

            Body.HasMoved = true;

            switch (State)
            {
            case BalloonState.DeliveringGoods:
                VoxelChunk chunk = chunks.ChunkData.GetVoxelChunkAtWorldLocation(Body.GlobalTransform.Translation);

                if (chunk != null)
                {
                    Vector3 gridPos = chunk.WorldToGrid(Body.GlobalTransform.Translation);
                    float   height  = chunk.GetFilledVoxelGridHeightAt((int)gridPos.X, (int)gridPos.Y, (int)gridPos.Z) + chunk.Origin.Y;
                    TargetPosition = new Vector3(Body.GlobalTransform.Translation.X, height + 5, Body.GlobalTransform.Translation.Z);

                    Vector3 diff = Body.GlobalTransform.Translation - TargetPosition;

                    if (diff.LengthSquared() < 2)
                    {
                        State = BalloonState.Waiting;
                    }
                }
                else
                {
                    State = BalloonState.Leaving;
                }

                break;

            case BalloonState.Leaving:
                TargetPosition = Vector3.UnitY * 100 + Body.GlobalTransform.Translation;

                if (Body.GlobalTransform.Translation.Y > 300)
                {
                    Die();
                }

                break;

            case BalloonState.Waiting:
                TargetPosition = Body.GlobalTransform.Translation;

                if (!shipmentGiven)
                {
                    shipmentGiven = true;
                }
                else
                {
                    State = BalloonState.Leaving;
                }


                break;
            }
        }