예제 #1
0
    internal void LoadBlock(MapBlock block)
    {
        if (block.items.Count > 0)
        {
            loadedAnyItems = true;
        }
        foreach (var item in block.items)
        {
            removedItems.Remove(item.id);
            ItemModel placedItem;
            if (!sceneItems.ContainsKey(item.id))
            {
                if (itemCount.ContainsKey(item.pos) && itemCount[item.pos] >= GameSettings.Instance.rendering.maxItemsPerTile)
                {
                    continue;
                }

                placedItem          = InstantiateItem(item, transform);
                sceneItems[item.id] = placedItem;
            }
            else
            {
                placedItem = sceneItems[item.id];
            }

            if (itemCount.ContainsKey(item.pos) && itemCount[item.pos] >= GameSettings.Instance.rendering.maxItemsPerTile)
            {
                placedItem.gameObject.SetActive(false);
                continue;
            }
            int currentTileCount = 0;
            if (itemCount.ContainsKey(item.pos))
            {
                currentTileCount = itemCount[item.pos];
            }
            itemCount[item.pos] = currentTileCount + 1;
            //RaycastHit hitInfo;
            //Vector3 position = GameMap.DFtoUnityCoord(item.pos) + Stacker.SpiralHemisphere(currentTileCount);
            //if (Physics.Raycast(position + new Vector3(0, 2.9f, 0), Vector3.down, out hitInfo, 3, Physics.DefaultRaycastLayers, QueryTriggerInteraction.Collide))
            //    placedItem.transform.position = hitInfo.point;
            //else
            placedItem.transform.position = GameMap.DFtoUnityCoord(item.pos.x + item.subpos_x, item.pos.y + item.subpos_y, item.pos.z + item.subpos_z);
            if (item.projectile)
            {
                placedItem.transform.position += new Vector3(0, GameMap.tileHeight / 2, 0);
                if (item.velocity_x > 0 || item.velocity_y > 0 || item.velocity_z > 0)
                {
                    placedItem.transform.rotation = Quaternion.LookRotation(GameMap.DFtoUnityDirection(item.velocity_x, item.velocity_y, item.velocity_z), Vector3.up);
                }
            }
            else
            {
                placedItem.transform.position += (Stacker.SpiralHemisphere(currentTileCount) + new Vector3(0, GameMap.floorHeight, 0));
                if (item.velocity_x > 0 || item.velocity_y > 0 || item.velocity_z > 0)
                {
                    placedItem.transform.rotation = Quaternion.LookRotation(GameMap.DFtoUnityDirection(item.velocity_x, item.velocity_y, item.velocity_z), Vector3.up);
                }
            }
        }
    }
예제 #2
0
    void UpdateItem(Item item)
    {
        ItemModel placedItem;
        var       itemPos = GameMap.DFtoUnityCoord(item.pos.x + item.subpos_x, item.pos.y + item.subpos_y, item.pos.z + item.subpos_z);

        //Disable the item if it's too far from the camera.
        if ((itemPos - Camera.main.transform.position).sqrMagnitude > (GameSettings.Instance.rendering.itemDrawDistance * GameSettings.Instance.rendering.itemDrawDistance))
        {
            if (sceneItems.ContainsKey(item.id))
            {
                sceneItems[item.id].gameObject.SetActive(false);
            }
            return;
        }

        //Also disable if it's outside of the viewing AABB
        if (!((item.pos.z < (GameMap.Instance.firstPerson ? GameMap.Instance.PosZ + GameSettings.Instance.rendering.drawRangeUp : GameMap.Instance.PosZ)) &&
              (item.pos.z >= (GameMap.Instance.PosZ - GameSettings.Instance.rendering.drawRangeDown)) &&
              (item.pos.x / GameMap.blockSize > (GameMap.Instance.PosXBlock - GameSettings.Instance.rendering.drawRangeSide)) &&
              (item.pos.x / GameMap.blockSize < (GameMap.Instance.PosXBlock + GameSettings.Instance.rendering.drawRangeSide)) &&
              (item.pos.y / GameMap.blockSize > (GameMap.Instance.PosYBlock - GameSettings.Instance.rendering.drawRangeSide)) &&
              (item.pos.y / GameMap.blockSize < (GameMap.Instance.PosYBlock + GameSettings.Instance.rendering.drawRangeSide))))
        {
            if (sceneItems.ContainsKey(item.id))
            {
                sceneItems[item.id].gameObject.SetActive(false);
            }
            return;
        }

        //Limit the number of items per tile.
        if (itemCount.ContainsKey(item.pos) && itemCount[item.pos] >= GameSettings.Instance.rendering.maxItemsPerTile)
        {
            if (sceneItems.ContainsKey(item.id))
            {
                sceneItems[item.id].gameObject.SetActive(false);
            }
            return;
        }

        //Instantiate it if it's not already there.
        if (!sceneItems.ContainsKey(item.id))
        {
            placedItem          = InstantiateItem(item, transform);
            sceneItems[item.id] = placedItem;
        }
        else
        {
            placedItem = sceneItems[item.id];
            placedItem.gameObject.SetActive(true);

            //If there's no significant differences between the new item and the existing one, there's no need to update it.
            if (AreItemsEqual(placedItem.originalItem, item))
            {
                return;
            }
            else
            {
                placedItem.originalItem = item;
            }
        }

        int currentTileCount = 0;

        if (itemCount.ContainsKey(item.pos))
        {
            currentTileCount = itemCount[item.pos];
        }
        itemCount[item.pos] = currentTileCount + 1;
        //RaycastHit hitInfo;
        //Vector3 position = GameMap.DFtoUnityCoord(item.pos) + Stacker.SpiralHemisphere(currentTileCount);
        //if (Physics.Raycast(position + new Vector3(0, 2.9f, 0), Vector3.down, out hitInfo, 3, Physics.DefaultRaycastLayers, QueryTriggerInteraction.Collide))
        //    placedItem.transform.position = hitInfo.point;
        //else
        placedItem.transform.position = itemPos;
        if (item.projectile)
        {
            placedItem.transform.position += new Vector3(0, GameMap.tileHeight / 2, 0);
            if (item.velocity_x > 0 || item.velocity_y > 0 || item.velocity_z > 0)
            {
                placedItem.transform.rotation = Quaternion.LookRotation(GameMap.DFtoUnityDirection(item.velocity_x, item.velocity_y, item.velocity_z), Vector3.up);
            }
        }
        else
        {
            placedItem.transform.position += (Stacker.SpiralHemisphere(currentTileCount) + new Vector3(0, GameMap.floorHeight, 0));
            if (item.velocity_x > 0 || item.velocity_y > 0 || item.velocity_z > 0)
            {
                placedItem.transform.rotation = Quaternion.LookRotation(GameMap.DFtoUnityDirection(item.velocity_x, item.velocity_y, item.velocity_z), Vector3.up);
            }
        }
    }