private IEnumerator RecalculateFiresCache(Vector3 windVector)
 {
     for (int i = 0; i < activeFire.Count; i++)
     {
         IFlamable fire = activeFire[i];
         RecalculateFireCache(fire, windVector);
     }
     yield return(null);
 }
 public void AddTree(IFlamable tree)
 {
     if (fireCacheRecalculateCoroutine != null)
     {
         StopCoroutine(fireCacheRecalculateCoroutine);
         fireCacheRecalculateCoroutine = null;
     }
     quadTree.Insert(tree);
     fireCacheRecalculateCoroutine = StartCoroutine(RecalculateFiresCache(windVector));
 }
    //on trigger, cast the colliding gameobject to IFlamable
    //Fire their 'take damage'
    private void OnTriggerEnter(Collider other)
    {
        //We need to see if the thing we collided with has IFlamable
        IFlamable flamable = other.GetComponent <IFlamable>();

        if (flamable != null) //If the above cast fails, it'll be null
        {
            //We know that anything that implements IFlamable can run TakeFireDamage();
            flamable.TakeFireDamage();
        }
    }
    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);
        }
    }
    private void RecalculateFireCache(IFlamable fire, Vector3 windVector)
    {
        Vector2 position = fire.GetPosition();
        Rect    r        = new Rect(0, 0, FIRESPREAD_SIZE_RAD * 2, FIRESPREAD_SIZE_RAD * 2);

        ArrangeRect(ref r, ref position, ref windVector);
        Vector2          fireRectCenter = r.center;
        List <IFlamable> tmp            = fire.GetCacheList();

        tmp.Clear();
        quadTree.Retrieve(ref tmp, ref r);
        //make circle from rect
        tmp.RemoveAll(x =>
        {
            return((x.GetPosition() - fireRectCenter).sqrMagnitude > FIRESPREAD_RAD_SQR);
        });
    }
 public void RemoveFire(IFlamable position)
 {
     activeFire.Remove(position);
 }
 public void AddFire(IFlamable fire)
 {
     activeFire.Add(fire);
     RecalculateFireCache(fire, windVector);
 }
 public void RemoveTree(IFlamable tree)
 {
     quadTree.Remove(tree);
 }
Example #9
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;
                }
            }
        }
    }