Example #1
0
 public VoxelHitInfo(IVoxel voxel, Vector3 hitPosition, Vector3 center, DirectionXYZ normal)
 {
     VoxelInfo       = voxel;
     HitPosition     = hitPosition;
     MeshCenter      = center;
     NormalDirection = normal;
 }
Example #2
0
        public bool CanRemove(IVoxel getInternalVoxel)
        {
            if (townVoxels.Count == 1)
            {
                return(false);
            }
            var setWithout = new HashSet <IVoxel>(townVoxels.Where(v => v != getInternalVoxel));
            var removedSet = new HashSet <IVoxel>(townVoxels.Where(v => v != getInternalVoxel));

            // Note i think this is simplye the VisitOnce function in the Utilities dll on my work pc
            var queue = new Queue <IVoxel>();

            queue.Enqueue(removedSet.First());
            while (queue.Count > 0)
            {
                var curr = queue.Dequeue();
                if (!removedSet.Contains(curr))
                {
                    continue;
                }
                removedSet.Remove(curr);
                curr.Get4Connected().Intersect(setWithout).ForEach(queue.Enqueue);
            }
            return(removedSet.Count == 0); // If the removedset is empty then all voxels are still conected when given voxel is removed.
        }
 public override void OnCreated(IVoxel handle)
 {
     producers[handle] = new SimpleWorkerProducer()
     {
         ProvidedWorkersAmount = 10
     };
 }
Example #4
0
        public void DestroyVoxelContents(IVoxel voxel)
        {
            if (voxel.Data != null && voxel.Data.Type != null)
            {
                voxel.Data.Type.OnDestroyed(voxel);
            }

            //Destroy contents, but height is not considered contents so keep that

            //note: put gameplay-related changes here
            var prevHeight = 0f;

            if (voxel.Data != null)
            {
                prevHeight = voxel.Data.Height;
            }
            ((GameVoxel)voxel).Data = new ObservableVoxelData(() =>
            {
                if (voxel.Data == null)
                {
                    return;                     // Ignore changes in the observablevoxeldata constructor
                }
                NotifyVoxelChanged(voxel);
            }, generator);
            voxel.Data.Height = prevHeight;
        }
Example #5
0
        public void CreateVoxelContents(IVoxel voxel, IGameVoxelType type)
        {
            voxel.Data.Type = type;

            type.OnCreated(voxel); // Maybe register this too as an event
            created.OnNext(voxel);
        }
Example #6
0
 /// <summary>
 /// Determines whether [contains] [the specified item].
 /// </summary>
 /// <param name="item">The item.</param>
 /// <returns><c>true</c> if [contains] [the specified item]; otherwise, <c>false</c>.</returns>
 internal bool Contains(IVoxel item)
 {
     if (item == null)
     {
         return(false);
     }
     return(Contains(item.ID));
 }
Example #7
0
 public override void OnCreated(IVoxel handle)
 {
     handle.SetPart(new Ore()
     {
         ResourceCount = 1
     });
     base.OnCreated(handle);
 }
Example #8
0
    private void CheckRaycastHit()
    {
        var rayPosition = CachedTransform.position + Vector3.forward * RAY_LENGTH;

        if (VoxelUtility.VoxelRayCast(CachedTransform.position, transform.TransformDirection(Vector3.forward), out var voxel, RAY_LENGTH))
        {
            rayPosition      = voxel.HitPosition;
            _currentHitVoxel = voxel.VoxelInfo;
        }
 public virtual void OnDestroyed(IVoxel handle)
 {
     foreach (var addon in addons.Values)
     {
         var inst = addon.CreateAddon(handle);
         addon.Instances[handle] = inst;
         inst.OnDestroyed(handle);
     }
 }
Example #10
0
        public bool Equals(IVoxel other)
        {
            if (ReferenceEquals(null, other))
            {
                return(false);
            }
            var isColorVoxel = other is ColorVoxel;

            return(isColorVoxel && Equals((ColorVoxel)other));
        }
Example #11
0
        /// <summary>
        /// Compares two voxels for equality.
        /// </summary>
        /// <param name="IVoxel">A voxel to compare with.</param>
        /// <returns>True if both match; False otherwise.</returns>
        public Boolean Equals(IVoxel <T> IVoxel)
        {
            if ((Object)IVoxel == null)
            {
                return(false);
            }

            return(X.Equals(IVoxel.X) &&
                   Y.Equals(IVoxel.Y) &&
                   Z.Equals(IVoxel.Z));
        }
Example #12
0
    public void SetVoxel(Pos pos, IVoxel voxel)
    {
        Pair <Pos, Pos> chunkPos = Chunk.GetPosInChunk(pos);
        Chunk           c        = GetChunk(chunkPos.val1);

        if (c == null)
        {
            return;
        }
        c.SetVoxel(chunkPos.val2, voxel);
    }
Example #13
0
 public void SetTownCenter(IVoxel center)
 {
     if (TownCenter != null)
     {
         throw new InvalidOperationException("Changing town center is not supported");
     }
     TownCenter = center;
     townVoxels.Add(center);
     ((IVoxelHandle)center).MarkChanged();
     ((IVoxelHandle)center).Get8Connected().ForEach(v => v.MarkChanged());
 }
Example #14
0
        public IMesh GetMesh(IVoxel gameVoxel)
        {
            if (gameVoxel.Data.Type == null)
            {
                return(null);
            }

            var handle = gameVoxel;

            return(gameVoxel.Data.Type.GetMesh((IVoxelHandle)handle));
        }
Example #15
0
        public void RemoveVoxel(IVoxel voxel)
        {
            if (!CanRemove(voxel))
            {
                throw new InvalidOperationException();
            }
            townVoxels.Remove(voxel);

            ((IVoxelHandle)voxel).MarkChanged();
            ((IVoxelHandle)voxel).Get8Connected().ForEach(v => v.MarkChanged());
        }
Example #16
0
        public void AddVoxel(IVoxel voxel)
        {
            if (!CanAddVoxel(voxel))
            {
                throw new InvalidOperationException("Invalid voxel to add");
            }
            townVoxels.Add(voxel);

            ((IVoxelHandle)voxel).MarkChanged();
            ((IVoxelHandle)voxel).Get8Connected().ForEach(v => v.MarkChanged());
        }
Example #17
0
 public static void MoveToWarehouse(IVoxel voxel, ItemType[] items)
 {
     foreach (var i in items)
     {
         var warehouse = GetConnectedWarehouses(voxel).FirstOrDefault(v => v.Data.Inventory.AvailableSlots > 0);
         if (warehouse == null)
         {
             return;
         }
         warehouse.Data.Inventory.AddNewItems(i, 1);
     }
 }
Example #18
0
 public void SetVoxel(Pos inPos, IVoxel voxel)
 {
     if (!InChunk(inPos))
     {
         return;
     }
     if (empty && voxel != null)
     {
         empty = false;
     }
     voxels[inPos.x, inPos.y, inPos.z] = new VoxelInstance(voxel, this, inPos, new VoxelData());
 }
        /// <summary>
        /// Return order: (1,0) (0,1) (-1,0) (0,-1)
        /// </summary>
        /// <returns></returns>
        public static IEnumerable <IVoxel> Get4Connected(this IVoxel center)
        {
            var ret = new IVoxel[4];

            ret[0] = center.GetRelative(new Point2(1, 0));
            ret[1] = center.GetRelative(new Point2(0, 1));
            ret[2] = center.GetRelative(new Point2(-1, 0));
            ret[3] = center.GetRelative(new Point2(0, -1));
            return(ret);
            //MHG>: removed this, this will cause problems in any case when using the return order
            //      return ret.Where(s => s != null);
        }
Example #20
0
 /// <summary>
 /// Returns true when given voxel borders to the town (so not in the town)
 /// </summary>
 public bool IsAtBorder(IVoxel voxel)
 {
     if (townVoxels.Contains(voxel))
     {
         return(false);
     }
     if (!townVoxels.Overlaps(voxel.Get4Connected()))
     {
         return(false);
     }
     return(true);
 }
        public static void Push(IVoxel voxel, ItemType item)
        {
            if (!CanPush(voxel, item))
            {
                throw new InvalidOperationException();
            }

            voxel.Data.Inventory.AddNewItems(item, 1);

            var roads = GetRoads(voxel).Where(v => v.Data.Type.CanAcceptItemType((IVoxelHandle)v, item));

            ((RoadType)roads.First().Data.Type).DeliverItemClosest((IVoxelHandle)roads.First(), (IVoxelHandle)voxel, item);
        }
Example #22
0
        /// <summary>
        /// Checks if the given voxel is located
        /// within this cube.
        /// </summary>
        /// <param name="IVoxel">A voxel of type T.</param>
        /// <returns>True if the voxel is located within this cube; False otherwise.</returns>
        public Boolean Contains(IVoxel <T> IVoxel)
        {
            #region Initial Checks

            if (IVoxel == null)
            {
                throw new ArgumentNullException("The given voxel must not be null!");
            }

            #endregion

            return(Contains(IVoxel.X, IVoxel.Y, IVoxel.Z));
        }
Example #23
0
 private Vertex GetOrMakeVoxelVertex(IVoxel baseVoxel, Dictionary <long, Vertex> voxelDictionary)
 {
     lock (voxelDictionary)
     {
         if (voxelDictionary.ContainsKey(baseVoxel.ID))
         {
             return(voxelDictionary[baseVoxel.ID]);
         }
         var newVertex = new Vertex(baseVoxel.BottomCoordinate);
         voxelDictionary.Add(baseVoxel.ID, newVertex);
         return(newVertex);
     }
 }
Example #24
0
        /// <summary>
        /// Creates a new town for given towncenter
        /// Returns existing town if already found
        /// </summary>
        public Town CreateTown(IVoxel center)
        {
            var existing = towns.FirstOrDefault(t => t.TownCenter == center);

            if (existing != null)
            {
                return(existing);
            }
            var ret = new Town(this, datastoreRecord.CreateRecord());

            towns.Add(ret);
            ret.SetTownCenter(center);
            return(ret);
        }
        public bool HasAddon <T>(IVoxel handle) where T : VoxelInstanceAddon
        {
            if (!addons.ContainsKey(typeof(T)))
            {
                return(false);
            }

            // This makes has addon return false when the addon instance has not yet been initialized
            if (!addons[typeof(T)].Instances.ContainsKey(handle))
            {
                return(false);
            }
            return(true);
        }
        public static IEnumerable <IVoxel> GetRange(this IVoxel center, int radius)
        {
            for (int x = -radius; x <= +radius; x++)
            {
                for (int y = -radius; y <= +radius; y++)
                {
                    var v = center.GetRelative(new Point2(x, y));

                    if (v != null)
                    {
                        yield return(v);
                    }
                }
            }
        }
Example #27
0
        public void Simulate()
        {
            if (UserInputDisabled)
            {
                return;
            }
            toolSelectionMenu.ProcessUserInput();

            /*if (TW.Graphics.Mouse.RelativeScrollWheel < 0 || TW.Graphics.Keyboard.IsKeyPressed(Key.UpArrow))
             *  inputHandler.OnPreviousTool();
             * if (TW.Graphics.Mouse.RelativeScrollWheel > 0 || TW.Graphics.Keyboard.IsKeyPressed(Key.DownArrow))
             *  inputHandler.OnNextTool();*/

            if (TW.Graphics.Keyboard.IsKeyPressed(Key.O))
            {
                inputHandler.OnSave();
            }

            if (trySimulateUIControls())
            {
                return;
            }

            var target = GetTargetedVoxel();

            if (target == null)
            {
                return;
            }
            if (target != previousTarget)
            {
                inputHandler.OnTargetChanged(target);
            }
            previousTarget = target;

            if (TW.Graphics.Mouse.LeftMouseJustPressed)
            {
                inputHandler.OnLeftClick(target);
            }
            if (TW.Graphics.Mouse.RightMouseJustPressed)
            {
                inputHandler.OnRightClick(target);
            }
            foreach (var k in TW.Graphics.Keyboard.PressedKeys.Where(k => TW.Graphics.Keyboard.IsKeyPressed(k)))
            {
                inputHandler.OnKeyPressed(target, k);
            }
        }
Example #28
0
 public static void TakeFromWarehouse(IVoxel voxel, ItemType[] input)
 {
     if (!CanTakeFromWarehouse(voxel, input))
     {
         throw new InvalidOperationException("Can't take items");
     }
     foreach (var i in input)
     {
         var warehouse = GetConnectedWarehouses(voxel).FirstOrDefault(v => v.Data.Inventory.Items.Contains(i));
         if (warehouse == null)
         {
             throw new InvalidOperationException("Algorithm error.");
         }
         warehouse.Data.Inventory.DestroyItems(i, 1);
     }
 }
        public IMesh GetMesh(IVoxel gameVoxel)
        {
            var baseMesh = provider.GetMesh(gameVoxel);
            var town     = townCenterService.GetTownForVoxel(gameVoxel);

            if (town == null)
            {
                return(baseMesh);
            }

            var builder = new MeshBuilder();

            builder.AddMesh(baseMesh, Matrix.Identity);
            builder.AddMesh(this.builder.CreateMesh(p => town == townCenterService.GetTownForVoxel(gameVoxel.GetRelative(p))), Matrix.Identity);
            return(builder.CreateMesh());
        }
 public bool IsMineableVoxel(IVoxel voxel)
 {
     if (voxel == null)
     {
         return(false);
     }
     if (!voxel.HasPart <Ore>())
     {
         return(false);
     }
     if (voxel.GetPart <Ore>().IsDepleted())
     {
         return(false);
     }
     return(true);
 }