IEnumerator Simulate()
    {
        while (true)
        {
            if (!enabled)
            {
                yield return(null);

                continue;
            }

            for (int i = 0; i < activeFire.Count; i++)
            {
                IFlamable        fire      = activeFire[i];
                List <IFlamable> fireCache = fire.GetCacheList();
                if (fireCache.Count == 0)
                {
                    continue;
                }
                Vector2 position = fire.GetPosition();
                ArrangeRect(ref fireRect, ref position, ref windVector);
                bool atLeastOneHeating = false;
                for (int j = 0; j < fireCache.Count; j++)
                {
                    IFlamable  flammable  = fireCache[j];
                    FlameState flameState = flammable.GetFlameState();

                    //keep out invalid
                    if (flammable == null || flameState == FlameState.BURNED_OUT || flameState == FlameState.BURNING)
                    {
                        continue;
                    }

                    atLeastOneHeating = true;
                    if (flammable.GetFlameState() != FlameState.HEATING)
                    {
                        flammable.SetFlameState(FlameState.HEATING);
                    }

                    flammable.AddTemperature(Random.value + 0.5f);
                }
                if (!atLeastOneHeating)
                {
                    fireCache.Clear();
                }
            }
            yield return(null);
        }
    }
示例#2
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
        {
            Ray        inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(inputRay, out hit))
            {
                TerrainData td = activeTerrain.terrainData;
                switch (actionMode)
                {
                case ModeAction.ADD:
                    Vector3 hitPoint = hit.point;
                    activeTerrain.AddTreeInstance(CreateTreeInstance(hitPoint.x / td.size.x, hitPoint.z / td.size.z));
                    break;

                case ModeAction.REMOVE:
                    IDeletable tree = hit.collider.gameObject.GetComponent <IDeletable>();
                    if (tree != null && hit.collider.gameObject.tag == "Tree")
                    {
                        tree.Delete();
                    }
                    break;

                case ModeAction.TOGGLE:
                    if (hit.collider.gameObject.tag == "Tree")
                    {
                        //Fire fire = hit.collider.GetComponentInChildren<Fire>();
                        IFlamable flammable = hit.collider.GetComponent <IFlamable>();
                        if (flammable == null)
                        {
                            break;
                        }
                        if (flammable.GetFlameState() == FlameState.BURNING)
                        {
                            flammable.SetFlameState(FlameState.NONE);
                        }
                        else
                        {
                            flammable.SetFlameState(FlameState.BURNING);
                        }
                        break;
                    }
                    if (hit.collider.gameObject.tag == "Fire")
                    {
                        IDeletable firepit = hit.collider.gameObject.GetComponent <IDeletable>();
                        if (firepit != null)
                        {
                            firepit.Delete();
                        }
                        break;
                    }
                    Instantiate(firepitPrefab, hit.point, Quaternion.identity, activeTerrain.transform);
                    break;

                default:
                    break;
                }
            }
        }
    }