Ejemplo n.º 1
0
        /// <summary>
        /// Update item damage and handle synchorization.
        /// </summary>
        /// <param name="item"></param>
        /// <param name="health"></param>
        protected void OnItemDamaged(HarvestableTIPoolItem item, int damage)
        {
            if (!item.isCollider)
            {
                return;
            }

            BaseUNNetworkData data    = null;
            BaseUNNetworkData current = null;

            for (int i = 0; i < bufferedData.Count; i++)
            {
                current = bufferedData[i];

                if (current.treeInstanceID == item.uid && current.terrainID == item.terrain.name)
                {
                    current.health -= damage;
                    data            = current;

                    break;
                }
            }

            if (data == null)
            {
                data = UNNetworkData <T1> .Pack <T1, T2>(item.terrain, item.uid, item.health, PacketType.HealthUpdate);

                bufferedData.Add(data as T2);
            }

            SendEvent(data as T2);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Called when an harvestable item instance has been created
        /// </summary>
        /// <param name="instance">the created instance</param>
        protected void OnHarvestableTreeInstancePooled(HarvestableTIPoolItem instance)
        {
            BaseUNNetworkData data;

            for (int i = 0; i < bufferedData.Count; i++)
            {
                data = bufferedData[i];

                if (instance.uid == data.treeInstanceID && instance.terrain.name == data.terrainID && instance.isCollider)
                {
                    instance.health = data.health;
                }
            }
        }
Ejemplo n.º 3
0
        public ChunkObject(int _instanceID, TreeInstance treeInstance, Vector3 terrainSize, TerrainData tData, Vector3 terrainPosition)
        {
            this.treeInstance   = treeInstance;
            this.originalHeight = treeInstance.heightScale;

            worldPosition = treeInstance.position.LocalToWorld(terrainSize, terrainPosition);

            this.depthPosition = new Vector2(worldPosition.x, worldPosition.z);

            this.prototypeID = treeInstance.prototypeIndex;

            this.instanceID = _instanceID;

            ThreadManager.instance.RunOnUnityThread(new ThreadTask <ChunkObject, TerrainData>((ChunkObject chunkObject, TerrainData terrainData) =>
            {
                GameObject prefab = terrainData.treePrototypes[chunkObject.prototypeID].prefab;
                HarvestableTIPoolItem harvestableComponent = prefab.GetComponent <HarvestableTIPoolItem>();

                if (harvestableComponent != null)
                {
                    chunkObject.harvestableComponent = harvestableComponent;
                }
            }, this, tData));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Pack the data and create a data instance
        /// </summary>
        public static T2 Pack <T1, T2>(Terrain terrain, int treeInstanceID, int health, PacketType type) where T2 : UNNetworkData <T1>
        {
            T2 instance = System.Activator.CreateInstance <T2>();

            instance.terrainID      = terrain.name;
            instance.treeInstanceID = treeInstanceID;
            instance.eventType      = type;

            UNTerrain UNTerrain = terrain.GetComponent <UNTerrain>();

            if (instance.minHealth == -1 && instance.maxHealth == -1 && UNTerrain != null)
            {
                TreeInstance          treeInstance         = UNTerrain.terrain.terrainData.GetTreeInstance(treeInstanceID);
                HarvestableTIPoolItem harvestableComponent = UNTerrain.terrain.terrainData.treePrototypes[treeInstance.prototypeIndex].prefab.GetComponent <HarvestableTIPoolItem>();

                if (harvestableComponent != null)
                {
                    instance.minHealth = harvestableComponent.minHealth;
                    instance.maxHealth = harvestableComponent.maxHealth;
                }
                else
                {
                    harvestableComponent = UNTerrain.Pool.TryGetType <HarvestableTIPoolItem>();

                    if (harvestableComponent != null)
                    {
                        instance.minHealth = harvestableComponent.minHealth;
                        instance.maxHealth = harvestableComponent.maxHealth;
                    }
                }
            }

            instance.health = health;

            return(instance);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Unpack the data
        /// </summary>
        public override void UnPack()
        {
            base.UnPack();

            var       terrains = UNTerrain.terrains;
            Terrain   terrain;
            UNTerrain unTerrainComponent;

            for (int i = 0; i < terrains.Count; i++)
            {
                unTerrainComponent = terrains[i];
                terrain            = unTerrainComponent.terrain;

                if (terrain.name == terrainID)
                {
                    if (eventType == PacketType.HealthUpdate)
                    {
                        BaseUNNetworkData data = null;

                        if (!bufferedData.Contains(this))
                        {
                            bufferedData.Add(this);
                            data = this;
                        }
                        else
                        {
                            for (int b = 0; b < bufferedData.Count; b++)
                            {
                                if (this.Equals(bufferedData[b]))
                                {
                                    data = bufferedData[b];
                                    break;
                                }
                            }
                        }

                        if (data != null)
                        {
                            if (data.minHealth == -1 && data.maxHealth == -1)
                            {
                                TreeInstance          treeInstance         = terrain.terrainData.GetTreeInstance(data.treeInstanceID);
                                HarvestableTIPoolItem harvestableComponent = terrain.terrainData.treePrototypes[treeInstance.prototypeIndex].prefab.GetComponent <HarvestableTIPoolItem>();

                                if (harvestableComponent != null)
                                {
                                    data.minHealth = harvestableComponent.minHealth;
                                    data.maxHealth = harvestableComponent.maxHealth;
                                }
                            }

                            data.health = health;
                        }
                    }

                    break;
                }
                else if (i == terrains.Count - 1)
                {
                    BaseUNNetworkData.WaitingForStreamData.Add(this);
                }
            }
        }