Exemple #1
0
    protected override void UseForPlayer(Player player, ItemsBean itemData, ItemUseTypeEnum useType)
    {
        //检测玩家前方是否有方块
        if (player.playerRay.RayToChunkBlock(out RaycastHit hit, out Vector3Int targetBlockPosition))
        {
            ChunkComponent chunkForHit = hit.collider.GetComponentInParent <ChunkComponent>();
            if (chunkForHit != null)
            {
                //获取位置和方向
                player.playerRay.GetHitPositionAndDirection(hit, out Vector3Int targetPosition, out Vector3Int closePosition, out BlockDirectionEnum direction);

                Vector3Int localPosition = targetPosition - chunkForHit.chunk.chunkData.positionForWorld;

                //放置位置
                Vector3Int upLocalPosition = localPosition + Vector3Int.up;

                //获取上方方块
                Block upBlock = chunkForHit.chunk.chunkData.GetBlockForLocal(upLocalPosition);

                //如果上方有方块 则无法放置
                if (upBlock != null && upBlock.blockType != BlockTypeEnum.None)
                {
                    return;
                }

                ItemsInfoBean itemsInfo = GetItemsInfo(itemData.itemId);
                CreatureHandler.Instance.CreateCreature(itemsInfo.type_id, targetPosition + Vector3Int.up);
            }
        }
    }
Exemple #2
0
    protected override void UseForPlayer(Player player, ItemsBean itemData, ItemUseTypeEnum useType)
    {
        //检测玩家前方是否有方块
        if (player.playerRay.RayToChunkBlock(out RaycastHit hit, out Vector3Int targetBlockPosition))
        {
            ChunkComponent chunkForHit = hit.collider.GetComponentInParent <ChunkComponent>();
            if (chunkForHit != null)
            {
                //获取位置和方向
                player.playerRay.GetHitPositionAndDirection(hit, out Vector3Int targetPosition, out Vector3Int closePosition, out BlockDirectionEnum direction);
                if (useType == ItemUseTypeEnum.Left)
                {
                    TargetBreak(itemData, targetPosition);
                }
                else
                {
                    Vector3Int localPosition = targetPosition - chunkForHit.chunk.chunkData.positionForWorld;
                    //获取原位置方块
                    Block tagetBlock = chunkForHit.chunk.chunkData.GetBlockForLocal(localPosition);

                    //如果不能锄地
                    if (tagetBlock.blockInfo.plough_state == 0)
                    {
                        return;
                    }

                    //获取上方方块
                    Block upBlock = chunkForHit.chunk.chunkData.GetBlockForLocal(localPosition + Vector3Int.up);

                    //如果上方有方块 则无法使用锄头
                    if (upBlock != null && upBlock.blockType != BlockTypeEnum.None)
                    {
                        return;
                    }
                    //扣除道具耐久
                    if (this is ItemBaseTool itemTool)
                    {
                        ItemsDetailsToolBean itemsDetailsTool = itemData.GetMetaData <ItemsDetailsToolBean>();
                        //如果没有耐久 不能锄地
                        if (itemsDetailsTool.life <= 0)
                        {
                            return;
                        }
                        itemsDetailsTool.AddLife(-1);
                        //保存数据
                        itemData.SetMetaData(itemsDetailsTool);
                        //回调
                        EventHandler.Instance.TriggerEvent(EventsInfo.ItemsBean_MetaChange, itemData);
                    }

                    BlockTypeEnum ploughBlockType = (BlockTypeEnum)tagetBlock.blockInfo.remark_int;
                    //替换为耕地方块
                    chunkForHit.chunk.SetBlockForLocal(localPosition, ploughBlockType, direction);

                    //播放粒子特效
                    BlockCptBreak.PlayBlockCptBreakEffect(ploughBlockType, targetPosition + new Vector3(0.5f, 0.5f, 0.5f));
                }
            }
        }
    }
        public void Update()
        {
            // for (int i = 0; i < chunkLoadQueue.Count; i++)
            if (chunkLoadQueue.Count > 0)
            {
                Chunk thisChunk = chunkLoadQueue.Dequeue();
                if (thisChunk != null)
                {
                    unloadedChunks -= 1;
                    thisChunk.Load();

                    // UnityEngine.Transform obj =
                    //  component.transform.Find("Chunk:" + thisChunk.getX() + "-" + thisChunk.getY() + "-" + thisChunk.getZ());
                    // ChunkComponent chunkComponent = ( obj == null ? null : obj.GetComponent<ChunkComponent>() );

                    ChunkComponent chunkComponent = component.chunkComponents[thisChunk.getX(), thisChunk.getY(), thisChunk.getZ()];

                    if (chunkComponent != null)
                    {
                        chunkComponent.chunk = thisChunk;
                    }

                    if (unloadedChunks == 0)
                    {
                        FinishLoad();
                    }
                }
            }
        }
        public void Render()
        {
            for (int x = 0; x < chunkSizeX; x++)
            {
                for (int z = 0; z < chunkSizeZ; z++)
                {
                    for (int y = 0; y < chunkSizeY; y++)
                    {
                        Chunk thisChunk = getChunk(x, y, z);
                        if (thisChunk == null)
                        {
                            continue;
                        }

                        ChunkComponent thisComponent = thisChunk.component;

                        if (thisComponent.mesh != null)
                        {
                            Vector3 position = (thisComponent.position.toVector3() * Constants.CHUNK_SIZE) + component.pivotPoint;

                            position  = component.transform.rotation * position;
                            position += component.transform.position;

                            Graphics.DrawMesh(thisComponent.mesh, Matrix4x4.TRS(position, component.transform.rotation, component.transform.lossyScale), GameMaster.Instance.voxelMaterials[0], 0);
                        }
                    }
                }
            }
        }
    protected override void UseForPlayer(Player player, ItemsBean itemData, ItemUseTypeEnum useType)
    {
        //检测玩家前方是否有方块
        if (player.playerRay.RayToChunkBlock(out RaycastHit hit, out Vector3Int targetBlockPosition))
        {
            ChunkComponent chunkForHit = hit.collider.GetComponentInParent <ChunkComponent>();
            if (chunkForHit != null)
            {
                //获取位置和方向
                player.playerRay.GetHitPositionAndDirection(hit, out Vector3Int targetPosition, out Vector3Int closePosition, out BlockDirectionEnum direction);

                Vector3Int localPosition = targetPosition - chunkForHit.chunk.chunkData.positionForWorld;
                //获取原位置方块
                Block tagetBlock = chunkForHit.chunk.chunkData.GetBlockForLocal(localPosition);

                //如果不能种地
                if (tagetBlock.blockInfo.plant_state == 0)
                {
                    return;
                }

                //种植位置
                Vector3Int upLocalPosition = localPosition + Vector3Int.up;
                //获取上方方块
                Block upBlock = chunkForHit.chunk.chunkData.GetBlockForLocal(upLocalPosition);

                //如果上方有方块 则无法种植
                if (upBlock != null && upBlock.blockType != BlockTypeEnum.None)
                {
                    return;
                }

                //种植的方块
                ItemsInfoBean itemsInfo      = GetItemsInfo(itemData.itemId);
                BlockTypeEnum plantBlockType = (BlockTypeEnum)itemsInfo.type_id;
                //初始化meta数据
                BlockMetaCrop blockCropData = new BlockMetaCrop();
                blockCropData.isStartGrow = false;
                blockCropData.growPro     = 0;
                string metaData = BlockBaseCrop.ToMetaData(blockCropData);
                //替换为种植
                chunkForHit.chunk.SetBlockForLocal(upLocalPosition, plantBlockType, BlockDirectionEnum.UpForward, metaData);

                //扣除道具
                UserDataBean userData = GameDataHandler.Instance.manager.GetUserData();
                userData.AddItems(itemData, -1);
                //刷新UI
                UIHandler.Instance.RefreshUI();
            }
        }
    }
    public bool TryBuildChunk()
    {
        if (initialized == true)
        {
            if (buildingChunks.Count > 0)
            {
                ChunkComponent thisComponent = buildingChunks.Dequeue();
                if (thisComponent != null)
                {
                    thisComponent.BuildRender(); return(true);
                }
            }
        }

        return(false);
    }
Exemple #7
0
    /// <summary>
    /// 使用 交互
    /// </summary>
    protected virtual void UseForInteractive(Player player)
    {
        //检测玩家前方是否有方块
        if (player.playerRay.RayToChunkBlock(out RaycastHit hit, out Vector3Int targetBlockPosition))
        {
            ChunkComponent chunkForHit = hit.collider.GetComponentInParent <ChunkComponent>();
            if (chunkForHit != null)
            {
                //获取位置和方向
                player.playerRay.GetHitPositionAndDirection(hit, out Vector3Int targetPosition, out Vector3Int closePosition, out BlockDirectionEnum direction);

                Vector3Int localPosition = targetPosition - chunkForHit.chunk.chunkData.positionForWorld;
                //获取原位置方块
                chunkForHit.chunk.chunkData.GetBlockForLocal(localPosition, out Block tagetBlock, out BlockDirectionEnum targetDirection);
                if (tagetBlock != null && tagetBlock.blockInfo.interactive_state == 1)
                {
                    tagetBlock.Interactive(player.gameObject, targetPosition, targetDirection);
                }
            }
        }
    }
Exemple #8
0
 protected virtual void UseForPlayer(Player player, ItemsBean itemsData, ItemUseTypeEnum itemUseType)
 {
     //检测玩家前方是否有方块
     if (player.playerRay.RayToChunkBlock(out RaycastHit hit, out Vector3Int targetBlockPosition))
     {
         ChunkComponent chunkForHit = hit.collider.GetComponentInParent <ChunkComponent>();
         if (chunkForHit != null)
         {
             //获取位置和方向
             player.playerRay.GetHitPositionAndDirection(hit, out Vector3Int targetPosition, out Vector3Int closePosition, out BlockDirectionEnum direction);
             //挖掘
             if (itemUseType == ItemUseTypeEnum.Left)
             {
                 TargetBreak(itemsData, targetPosition);
             }
             else
             {
                 TargetUse(itemsData, targetPosition);
             }
         }
     }
 }
Exemple #9
0
    /// <summary>
    /// 瞄准
    /// </summary>
    public virtual void UseForSightTarget(ItemsBean itemsData)
    {
        Player player = GameHandler.Instance.manager.player;

        if (player.playerRay.RayToChunkBlock(out RaycastHit hit, out Vector3Int targetBlockPosition))
        {
            ChunkComponent chunkForHit = hit.collider.GetComponentInParent <ChunkComponent>();
            if (chunkForHit != null && chunkForHit.chunk.isInit)
            {
                //获取位置和方向
                player.playerRay.GetHitPositionAndDirection(hit, out Vector3Int targetPosition, out Vector3Int closePosition, out BlockDirectionEnum direction);
                Vector3Int localPosition = targetPosition - chunkForHit.chunk.chunkData.positionForWorld;
                if (localPosition.x < 0 || localPosition.x > chunkForHit.chunk.chunkData.chunkWidth ||
                    localPosition.z < 0 || localPosition.z > chunkForHit.chunk.chunkData.chunkWidth ||
                    localPosition.y < 0 || localPosition.y > chunkForHit.chunk.chunkData.chunkHeight)
                {
                    return;
                }
                //获取原位置方块
                Block tagetBlock = chunkForHit.chunk.chunkData.GetBlockForLocal(localPosition);
                if (tagetBlock == null)
                {
                    return;
                }
                //展示目标位置
                GameHandler.Instance.manager.playerTargetBlock.Show(targetBlockPosition, tagetBlock, tagetBlock.blockInfo.interactive_state == 1);
            }
        }
        else
        {
            //展示目标位置
            if (GameHandler.Instance.manager.playerTargetBlock)
            {
                GameHandler.Instance.manager.playerTargetBlock.Hide();
            }
        }
    }
        public JsonAction Execute(HttpRequest httpRequest, JsonPacket jsonRequest, SessionComponent session)
        {
            Clear();

            // Connect
            NetworkChannel channel = new NetworkChannel(Connection);

            // Request
            JsonUploadRequestMessage jsonRequestMessage = JsonUploadRequestMessage.Parse(jsonRequest.Message);

            // Data
            string jsonId = jsonRequestMessage.Id;
            Entity entity = TransferMap.Get(jsonId);

            if (entity == null)
            {
                channel.SendNotFound();
                return(JsonAction.None);
            }

            JsonChunk      jsonChunk = jsonRequestMessage.Chunk;
            ChunkComponent transfer  = entity.Get <ChunkComponent>();

            if (jsonChunk != null)
            {
                JsonTransfer jsonTransfer = new JsonTransfer()
                {
                    Chunk = jsonChunk, Data = jsonRequest.Data
                };
                transfer.PushData(jsonTransfer);
            }

            if (!transfer.HasChunk())
            {
                channel.SendNotFound();
                return(JsonAction.None);
            }

            int delay = 0;

            jsonChunk = null;
            if (transfer.HasDataCapacity())
            {
                jsonChunk = transfer.PopChunk();
            }
            else
            {
                delay = (int)DemonTimeout.File.TotalMilliseconds;
            }

            // Response
            JsonUploadResponseMessage jsonResponseMessage = new JsonUploadResponseMessage(jsonId)
            {
                Chunk = jsonChunk, Delay = delay
            };
            JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage);

            HttpResponse httpResponse = new HttpResponse()
            {
                Data = session.Encrypt(jsonResponse)
            };

            channel.Send(httpResponse);
#if DEBUG
            jsonRequest.Data = null;
            Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
            return(JsonAction.Request);
        }
Exemple #11
0
        /// <summary>
        ///     Please see base class declaration for documentation.
        /// </summary>
        /// <param name="sendMessageParameters">-</param>
        /// <returns>-</returns>
        public MessagingResult Send(SendMessageParameters sendMessageParameters)
        {
            var encodedMessages = new List <string>();

            if (string.IsNullOrWhiteSpace(sendMessageParameters.Base64MessageContent))
            {
                throw new CouldNotSendEmptyMessageException("Sending empty messages does not make any sense.");
            }

            if (MessageCanBeChunked(sendMessageParameters.TechnicalMessageType))
            {
                if (MessageHasToBeChunked(sendMessageParameters.Base64MessageContent))
                {
                    var chunkContextId = Guid.NewGuid().ToString();
                    var totalSize      = Encoding.Unicode.GetByteCount(sendMessageParameters.Base64MessageContent);

                    var chunkedMessages = ChunkMessageContent(sendMessageParameters.Base64MessageContent,
                                                              sendMessageParameters.ChunkSize > 0
                            ? sendMessageParameters.ChunkSize
                            : ChunkSizeDefinition.MaximumSupported);

                    var current = 0;
                    foreach (var chunkedMessage in chunkedMessages)
                    {
                        var sendMessageParametersDuplicate = new SendChunkedMessageParameters
                        {
                            Recipients           = sendMessageParameters.Recipients,
                            TypeUrl              = sendMessageParameters.TypeUrl,
                            TechnicalMessageType = sendMessageParameters.TechnicalMessageType,
                            ApplicationMessageId = MessageIdService.ApplicationMessageId()
                        };
                        var chunkComponent = new ChunkComponent
                        {
                            Current   = current++,
                            Total     = chunkedMessage.Length,
                            ContextId = chunkContextId,
                            TotalSize = totalSize
                        };
                        sendMessageParametersDuplicate.ChunkInfo            = chunkComponent;
                        sendMessageParametersDuplicate.Base64MessageContent = chunkedMessage;
                        encodedMessages.Add(Encode(sendMessageParametersDuplicate).Content);
                    }
                }
                else
                {
                    encodedMessages = new List <string> {
                        Encode(sendMessageParameters).Content
                    };
                }
            }
            else
            {
                encodedMessages = new List <string> {
                    Encode(sendMessageParameters).Content
                };
            }

            var messagingParameters = sendMessageParameters.BuildMessagingParameter(encodedMessages);

            return(_messagingService.Send(messagingParameters));
        }
Exemple #12
0
        public JsonAction Execute(HttpRequest httpRequest, JsonPacket jsonRequest, SessionComponent session)
        {
            Clear();

            // Connect
            NetworkChannel channel = new NetworkChannel(Connection);

            // Request
            JsonAction jsonAction = JsonAction.None;
            JsonDownloadRequestMessage jsonRequestMessage = JsonDownloadRequestMessage.Parse(jsonRequest.Message);
            string jsonId = jsonRequestMessage.Id;

            if (jsonId != null)
            {
                // Data
                Entity entity = TransferMap.Get(jsonId);
                if (entity == null)
                {
                    channel.SendNotFound();
                    return(jsonAction);
                }

                ChunkComponent transfer = entity.Get <ChunkComponent>();
                if (transfer == null)
                {
                    channel.SendNotFound();
                    return(jsonAction);
                }

                JsonTransfer jsonTransfer = transfer.PopData();
                if (!transfer.Finished)
                {
                    jsonAction = JsonAction.Request;
                    entity.Update();
                }
                else
                {
                    TransferMap.Remove(jsonId);
                }

                string    jsonData  = null;
                JsonChunk jsonChunk = null;
                if (jsonTransfer != null)
                {
                    jsonData  = jsonTransfer.Data;
                    jsonChunk = jsonTransfer.Chunk;
                }

                // Response
                JsonDownloadResponseMessage jsonResponseMessage = new JsonDownloadResponseMessage(jsonId)
                {
                    Chunk = jsonChunk
                };
                JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage, jsonData);

                HttpResponse httpResponse = new HttpResponse()
                {
                    Data = session.Encrypt(jsonResponse)
                };
                channel.Send(httpResponse);
#if DEBUG
                jsonResponse.Data = null;
                Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
            }
            else
            {
                // Data
                Entity entity = session.Owner;
                SearchListComponent search     = entity.Get <SearchListComponent>();
                JsonClient          jsonClient = jsonRequestMessage.Client;

                // Request
                if (jsonClient == null && search.Empty)
                {
                    channel.SendNotFound();
                    return(jsonAction);
                }

                // Response
                jsonId = SecurityUtil.CreateKeyString();
                JsonDownloadResponseMessage jsonResponseMessage = new JsonDownloadResponseMessage(jsonId);
                JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage);

                HttpResponse httpResponse = new HttpResponse()
                {
                    Data = session.Encrypt(jsonResponse)
                };
                channel.Send(httpResponse);
#if DEBUG
                Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
                // Data
                entity = new Entity(jsonId);
                EntityIdleComponent idle = new EntityIdleComponent();
                entity.Add(idle);

                JsonChunk      jsonChunk = jsonRequestMessage.Chunk;
                ChunkComponent transfer  = new ChunkComponent(jsonChunk.Size, Options.ChunkSize, Options.MaxChunks);
                entity.Add(transfer);

                TransferMap.Add(entity);

                // Command
                string jsonData = jsonRequest.Data;
                jsonChunk = transfer.PopChunk();

                if (jsonClient == null)
                {
                    foreach (Entity e in search)
                    {
                        CommandState state = new CommandState()
                        {
                            Id = jsonId, Data = jsonData, Chunk = jsonChunk, Entity = e
                        };
                        Thread thread = new Thread(new ParameterizedThreadStart(ExecuteThread))
                        {
                            Priority = ThreadPriority.BelowNormal, IsBackground = true
                        };
                        thread.Start(state);
                    }
                }
                else
                {
                    Entity e = ClientMap.Get(jsonClient.Id);
                    if (e == null)
                    {
                        channel.SendNotFound();
                        return(jsonAction);
                    }

                    CommandState state = new CommandState()
                    {
                        Id = jsonId, Data = jsonData, Chunk = jsonChunk, Entity = e
                    };
                    Thread thread = new Thread(new ParameterizedThreadStart(ExecuteThread))
                    {
                        Priority = ThreadPriority.BelowNormal, IsBackground = true
                    };
                    thread.Start(state);
                }

                jsonAction = JsonAction.Request;
            }

            return(jsonAction);
        }
 public void Queue(ChunkComponent _component)
 {
     queue.Enqueue(_component);
 }
Exemple #14
0
    protected override void UseForPlayer(Player player, ItemsBean itemData, ItemUseTypeEnum useType)
    {
        //检测玩家前方是否有方块
        if (player.playerRay.RayToChunkBlock(out RaycastHit hit, out Vector3Int targetBlockPosition))
        {
            ChunkComponent chunkForHit = hit.collider.GetComponentInParent <ChunkComponent>();
            if (chunkForHit != null)
            {
                //获取位置和方向
                player.playerRay.GetHitPositionAndDirection(hit, out Vector3Int targetPosition, out Vector3Int closePosition, out BlockDirectionEnum direction);
                //如果上手没有物品 或者是左键点击 则挖掘
                if (itemData == null || itemData.itemId == 0 || useType == ItemUseTypeEnum.Left)
                {
                    TargetBreak(itemData, targetPosition);
                }
                //如果手上有物品 则使用
                else
                {
                    //获取目标方块
                    WorldCreateHandler.Instance.manager.GetBlockForWorldPosition(targetPosition, out Block targetBlock, out BlockDirectionEnum targetBlockDirection, out Chunk taragetChunk);
                    //首先获取靠近方块
                    WorldCreateHandler.Instance.manager.GetBlockForWorldPosition(closePosition, out Block closeBlock, out BlockDirectionEnum closeBlockDirection, out Chunk closeChunk);
                    //如果靠近得方块有区块
                    if (closeChunk != null)
                    {
                        //如果不是空方块 则不放置(液体则覆盖放置)
                        if (closeBlock != null && closeBlock.blockType != BlockTypeEnum.None && closeBlock.blockInfo.GetBlockShape() != BlockShapeEnum.Liquid)
                        {
                            return;
                        }
                        //如果重量为1 说明是草之类太轻的物体 则也不能放置
                        if (closeBlock != null && closeBlock.blockInfo.weight == 1)
                        {
                            return;
                        }
                        //获取物品信息
                        ItemsInfoBean itemsInfo = ItemsHandler.Instance.manager.GetItemsInfoById(itemData.itemId);
                        //获取方块信息
                        Block         useBlock  = BlockHandler.Instance.manager.GetRegisterBlock(itemsInfo.type_id);
                        BlockInfoBean blockInfo = useBlock.blockInfo;

                        BlockTypeEnum changeBlockType = blockInfo.GetBlockType();

                        //获取meta数据
                        string metaData = useBlock.ItemUseMetaData(closePosition, changeBlockType, direction, itemData.meta);
                        //使用方块
                        useBlock.ItemUse(
                            targetPosition, targetBlockDirection, targetBlock, taragetChunk,
                            closePosition, closeBlockDirection, closeBlock, closeChunk,
                            direction, metaData);

                        //扣除道具
                        UserDataBean userData = GameDataHandler.Instance.manager.GetUserData();
                        userData.AddItems(itemData, -1);
                        //刷新UI
                        UIHandler.Instance.RefreshUI();
                    }
                }
            }
        }
    }
    public void OnDrawGizmosSelected()
    {
        if (Application.platform != RuntimePlatform.WindowsEditor)
        {
            return;
        }

        Matrix4x4 rotationMatrix;

        if (Application.isPlaying == true && initialized == true)
        {
            for (int x = 0; x < sizeX; x++)
            {
                for (int z = 0; z < sizeZ; z++)
                {
                    for (int y = 0; y < sizeY; y++)
                    {
                        ChunkComponent thisComponent = chunkComponents[x, y, z];
                        Chunk          thisChunk     = thisComponent.chunk;

                        if (thisChunk != null)
                        {
                            if (thisChunk.isVoid() == false && thisChunk.isSolid() == false)
                            {
                                rotationMatrix = Matrix4x4.TRS(transform.TransformPoint(thisComponent.transform.localPosition), transform.rotation, transform.lossyScale);
                                Gizmos.matrix  = rotationMatrix;
                                Gizmos.color   = Color.green;
                                Gizmos.DrawWireCube((Vector3.one * (Constants.CHUNK_SIZE * 0.5f)), new Vector3(Constants.CHUNK_SIZE, Constants.CHUNK_SIZE, Constants.CHUNK_SIZE));
                            }
                            else if (thisChunk.isSolid() == true)
                            {
                                rotationMatrix = Matrix4x4.TRS(transform.TransformPoint(thisComponent.transform.localPosition), transform.rotation, transform.lossyScale);
                                Gizmos.matrix  = rotationMatrix;
                                Gizmos.color   = Color.blue;
                                Gizmos.DrawWireCube((Vector3.one * (Constants.CHUNK_SIZE * 0.5f)), new Vector3(Constants.CHUNK_SIZE, Constants.CHUNK_SIZE, Constants.CHUNK_SIZE));
                            }
                        }
                    }
                }
            }
        }
        else
        {
            pivotPoint  = new Vector3(sizeX * Constants.CHUNK_SIZE, sizeY * Constants.CHUNK_SIZE, sizeZ * Constants.CHUNK_SIZE) * 0.5f;
            pivotPoint += (Vector3.one * 0.5f);
            pivotPoint *= -1;

            for (int x = 0; x < sizeX; x++)
            {
                for (int z = 0; z < sizeZ; z++)
                {
                    for (int y = 0; y < sizeY; y++)
                    {
                        Vector3 chunkPos = new Vector3(x * Constants.CHUNK_SIZE, y * Constants.CHUNK_SIZE, z * Constants.CHUNK_SIZE);
                        chunkPos      += pivotPoint;
                        rotationMatrix = Matrix4x4.TRS(transform.TransformPoint(chunkPos), transform.rotation, transform.lossyScale);
                        Gizmos.matrix  = rotationMatrix;
                        Gizmos.color   = Color.red;
                        Gizmos.DrawWireCube((Vector3.one * (Constants.CHUNK_SIZE * 0.5f)), new Vector3(Constants.CHUNK_SIZE, Constants.CHUNK_SIZE, Constants.CHUNK_SIZE));
                    }
                }
            }
        }

        Gizmos.color   = Color.yellow;
        rotationMatrix = Matrix4x4.TRS(transform.TransformPoint(Vector3.zero), transform.rotation, transform.lossyScale);
        Gizmos.matrix  = rotationMatrix;
        Gizmos.DrawWireCube(-(Vector3.one * 0.5f),
                            new Vector3(Constants.CHUNK_SIZE * sizeX, Constants.CHUNK_SIZE * sizeY, Constants.CHUNK_SIZE * sizeZ)
                            );
    }
    public void Tick()
    {
        if (loaded == false && voxelObject.loaded == true)
        {
            loaded = true;
            Util.LoadVoxelObject(voxelObject, voxelObject.getName());

            TagCollisionRebuild();
        }

        if (physicsRebuildWaiting == false && buildingChunks.Count > 0)
        {
            physicsRebuildWaiting = true;
        }

        if (physicsRebuildWaiting == true && buildingChunks.Count == 0)
        {
            physicsRebuildWaiting = false;
            TagCollisionRebuild();
        }

        if (buildPhysics == true)
        {
            buildPhysics = false;
            RebuildCollision();
        }

        if (reload == true)
        {
            reload = false;
            TagForReload();
        }

        if (reinitialize == true)
        {
            reinitialize = false;
            Initialize(type);
        }
        else if (initialized == true)
        {
            // if (buildingChunks.Count > 0)
            // {
            //  ChunkComponent thisComponent = buildingChunks.Dequeue();
            //  if (thisComponent != null) { thisComponent.BuildChunk(); }

            //  // for (int i = 0; i < 4; i++)
            //  // {
            //  //  if (buildingChunks.Count > 0)
            //  //  {
            //  //      ChunkComponent thisComponent = buildingChunks.Dequeue();
            //  //      if (thisComponent != null) { thisComponent.BuildChunk(); }
            //  //  }
            //  // }
            // }

            for (int x = 0; x < sizeX; x++)
            {
                for (int z = 0; z < sizeZ; z++)
                {
                    for (int y = 0; y < sizeY; y++)
                    {
                        ChunkComponent thisComponent = chunkComponents[x, y, z];

                        if (thisComponent == null)
                        {
                            Debug.Log("Null component!"); return;
                        }
                        if (thisComponent.chunk == null)
                        {
                            Debug.Log("Null chunk!"); return;
                        }

                        if (thisComponent.chunk != null)
                        {
                            if (thisComponent.chunk.getCollisionState() != CollisionState.Built || thisComponent.chunk.getRenderState() != RenderState.Rendered)
                            {
                                ChunkLoader.instance.Queue(thisComponent);
                                buildingChunks.Enqueue(thisComponent);                                //thisComponent.BuildChunk();
                            }
                        }
                    }
                }
            }

            chunksWaitingForBuild = buildingChunks.Count;
        }
    }
    public VoxelComponent Initialize(VoxelObjectType _type = VoxelObjectType.GENERIC)
    {
        if (entity == Unity.Entities.Entity.Null)
        {
            CreateEntity();
            this.GetComponent <EntityTracker>().SetReceivedEntity(entity);
        }

        foreach (Transform child in transform)
        {
            Destroy(child.gameObject);
        }

        type = _type;
        switch (type)
        {
        case VoxelObjectType.GENERIC:
            voxelObject = new VoxelObject(sizeX, sizeY, sizeZ, this);
            break;

        case VoxelObjectType.ASTEROID:
            voxelObject = new Asteroid(sizeX, sizeY, sizeZ, this);
            break;

        case VoxelObjectType.PLANETOID:
            voxelObject = new Planetoid(sizeX, sizeY, sizeZ, this);
            break;
        }

        voxelObject.isStatic = isStatic;
        voxelObject.setName(gameObject.name);

        if (seedFromName == false)
        {
            voxelObject.setRandomSeed(voxelObject.getGUID().GetHashCode());
        }

        chunkObjects    = new GameObject[sizeX, sizeY, sizeZ];
        chunkComponents = new ChunkComponent[sizeX, sizeY, sizeZ];

        pivotPoint  = new Vector3(sizeX * Constants.CHUNK_SIZE, sizeY * Constants.CHUNK_SIZE, sizeZ * Constants.CHUNK_SIZE) * 0.5f;
        pivotPoint += (Vector3.one * 0.5f);
        pivotPoint *= -1;

        for (int x = 0; x < sizeX; x++)
        {
            for (int z = 0; z < sizeZ; z++)
            {
                for (int y = sizeY - 1; y >= 0; y--)
                {
                    ChunkComponent chunkComponent = new ChunkComponent();
                    chunkComponent.transform      = this.transform;
                    chunkComponent.position       = new Coord3D(x, y, z, voxelObject);
                    chunkComponent.voxelComponent = this;
                    chunkComponents[x, y, z]      = chunkComponent;

                    ChunkLoader.instance.Queue(chunkComponent);
                }
            }
        }

        reinitialize = false;
        initialized  = true;
        return(this);
    }