Esempio n. 1
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="list"></param>
    /// <param name="lineMask"></param> from left - zero line - to right. 0x28 - 00101000 - masks 2th and 4th lines;
    /// <param name="count"></param> using if !<=0 insdeat amountDist
    public void GenerateBlock(LinkedList <IDescriptorWithID> list, byte lineMask, int count = 0, float lowBound = 0)
    {
        float tmp;

        _lowerBound = _lowerBound > lowBound ? _lowerBound : lowBound;

        foreach (var e in list)
        {
            CarDescriptor car;
            if (e is CarDescriptor)
            {
                car = (CarDescriptor)e;
            }
            else
            {
                continue;
            }

            tmp            = car.Position.y + Vehicle.LengthStatic(car.Size) + SS.MinCarGap;
            _lowerBound    = _lowerBound > tmp ? _lowerBound : tmp;
            tmp           += car.Velocity * SS.TimeWindow;
            _lowerBoundFtr = _lowerBoundFtr > tmp ? _lowerBoundFtr : tmp;
            for (int line = 0; line < SS.Road.LinesCount; line++)
            {
                if (IsLineMasked(line, lineMask))
                {
                    continue;
                }
                tmp = car.Position.y + SS.TopBound(SS.Road.LineNumFromPosX(car.Position.x), line, car.Size);
                _topBounds[line] = _topBounds[line] > tmp ? _topBounds[line] : tmp;
                tmp += car.Velocity * SS.TimeWindow;
                _topBoundsFtr[line] = _topBoundsFtr[line] > tmp ? _topBoundsFtr[line] : tmp;
            }
        }

        for (int i = 0; i < SS.Road.LinesCount; i++)
        {
            if (_topBounds[i] <= _lowerBound || _topBoundsFtr[i] <= _lowerBoundFtr)
            {
                lineMask |= MaskLine(i);
            }
        }

        int carCnt = count > 0 ? count : _carAmntDistr.Next();

        carCnt = carCnt <= FreeLines(SS.Road.LinesCount, lineMask)
                        ? carCnt : FreeLines(SS.Road.LinesCount, lineMask);

#if UNITY_EDITOR
        if (carCnt <= 0)
        {
            Debug.LogError("CAR CNT MINI GEN = 0");
            Debug.LogError(lineMask);
            Debug.LogError(_lowerBound);
            Debug.LogError(_lowerBoundFtr);
            for (int i = 0; i < _topBounds.Length; i++)
            {
                Debug.LogError(_topBounds[i]);
                Debug.LogError(_topBoundsFtr[i]);
            }

            foreach (var v in list)
            {
                Debug.LogError(v);
            }
        }
#endif

        list.Clear();

        int     lineNum;
        float   minVel, maxVel;
        Vector2 pos;
        for (int i = 0; i < carCnt; i++)
        {
            do
            {
                lineNum = UnityEngine.Random.Range(0, SS.Road.LinesCount);
            } while (IsLineMasked(lineNum, lineMask));
            lineMask |= MaskLine(lineNum);

            minVel = (_lowerBoundFtr - _topBounds[lineNum]) / SS.TimeWindow;
            minVel = minVel >= SS.MinCarSpeed ? minVel : SS.MinCarSpeed;
            maxVel = (_topBoundsFtr[lineNum] - _lowerBound) / SS.TimeWindow;
            maxVel = maxVel <= SS.MaxCarSpeed ? maxVel : SS.MaxCarSpeed;
            tmp    = UnityEngine.Random.Range(minVel, maxVel);
            pos.x  = SS.Road.LinePosXFromNum(lineNum);
            pos.y  = _lowerBound + (_topBounds[lineNum] - _lowerBound) * (1 - (tmp - minVel) / (maxVel - minVel));
            list.AddLast(new CarDescriptor(pos, tmp, _carSizeDistr.Next()));
        }

        HasMore = false;
    }
    public void GenerateBlock(LinkedList <IDescriptorWithID> list)
    {
        ++_i;

        if (_i > 1 && _i <= _count)
        {
            list.Clear();
            var lines       = _ss.Road.LinesCount;
            var actualCount = 0;
            for (var i = 0; i < lines; ++i)
            {
                if (_isActual[i])
                {
                    ++actualCount;
                }
            }
            var meteorCount = _metAmountDistr.Next();
            meteorCount = meteorCount < actualCount ? meteorCount : actualCount - 1;
            while (meteorCount > 0)
            {
                var i = Random.Range(0, lines);
                if (!_isActual[i])
                {
                    continue;
                }
                _isActual[i] = false;
                var time = Random.Range(_ss.TimeWindow * LowTimeBound, _ss.TimeWindow * TopTimeBound);
                var to   = _lastOnLine[i].Position;
                to.y += _lastOnLine[i].Velocity * time + Vehicle.LengthStatic(_lastOnLine[i].Size) / 2;
                var from = new Vector2(to.x, _launcher.position.z);
                list.AddLast(new MeteorDescriptor(from, to, time));
                --meteorCount;
            }

            float commonLowerBound = 0;
            for (var i = 0; i < lines; ++i)
            {
                var car   = _lastOnLine[i];
                var bound = car.Position.y + Vehicle.LengthStatic(car.Size) + (_isActual[i] ? _ss.MinCarGap : TinyCarGap);
                _lowerBounds[i] = bound;
                if (!_isActual[i])
                {
                    continue;
                }

                _isActual[i] = false;
                if (bound > commonLowerBound)
                {
                    commonLowerBound = bound;
                }
                for (var l = 0; l < lines; ++l)
                {
                    bound = _ss.TopBound(i, l, car.Size) + car.Position.y;
                    if (_topBounds[l] < bound)
                    {
                        _topBounds[l] = bound;
                    }
                }
            }
            for (var i = 0; i < lines; i++)
            {
                if (_lowerBounds[i] < commonLowerBound)
                {
                    _lowerBounds[i] = commonLowerBound;
                }
            }

            var carCount = 0;
            for (var i = 0; i < lines; i++)
            {
                if (_topBounds[i] > _lowerBounds[i])
                {
                    ++carCount;
                }
            }
            if (carCount > _carCountDistr.Next())
            {
                carCount = _carCountDistr.Current;
            }

            do
            {
                var i = Random.Range(0, lines);
                if (_lastOnLine[i].Position.y < _lowerBounds[i] &&
                    _lowerBounds[i] < _topBounds[i])
                {
                    _isActual[i] = true;
                    var pos = new Vector2(_ss.Road.LinePosXFromNum(i),
                                          Random.Range(_lowerBounds[i], _topBounds[i]));
                    _lastOnLine[i] = new CarDescriptor(pos, _ss.NormalCarSpeed, _carSizeDistr.Next());
                    list.AddLast(_lastOnLine[i]);
                    --carCount;
                }
            } while (carCount > 0);
        }
        else if (_i == 1)
        {
            var tmp1 = _ss.MinCarSpeed;
            var tmp2 = _ss.MaxCarSpeed;
            _ss.MinCarSpeed = _ss.NormalCarSpeed * .9f;
            _ss.MinCarSpeed = _ss.NormalCarSpeed * 1.1f;
            _blockGen.GenerateBlock(list);
            _ss.MinCarSpeed = tmp1;
            _ss.MaxCarSpeed = tmp2;
            foreach (CarDescriptor e in list)
            {
                var line = _ss.Road.LineNumFromPosX(e.Position.x);
                _lastOnLine[line] = e;
                _isActual[line]   = true;
            }
        }
    }