Old Pulse

 avatar
user_3493139
csharp
5 months ago
6.6 kB
2
Indexable
    public Dictionary<Vector2, Queue<Pulse>> _singlePulseQueue = new Dictionary< Vector2, Queue<Pulse> >(64);

    public void GUIPulseSquare( Pulse pulse )
    {
        _PulseInstanceHandler(pulse);
    }


    public List<Dictionary<Vector2, Queue<Pulse>>> _multiPulseQueue = new List<Dictionary< Vector2, Queue<Pulse> >>(32);
    int _UniqueMPID = 1;

    public async void GUIPulseSequentialGrouped( List<List<Pulse>> pulseGroups, float secondsDelayPerPulseGroup, float secondsDelayPerPulse )
    {
        foreach (var pulseGroup in pulseGroups)
        {
            _PulseSequentialInstanceHandler(pulseGroup, secondsDelayPerPulse);

            await Task.Delay((int)(secondsDelayPerPulseGroup * 1000));
        }
    }
    public void GUIPulseSequential( List<Pulse> pulses, float secondsDelayPerPulse )
    {
        _PulseSequentialInstanceHandler(pulses, secondsDelayPerPulse);
    }
    public async void _PulseSequentialInstanceHandler( List<Pulse> pulses, float instanceDelaySeconds )
    {
        foreach (var pulse in pulses)
        {
            _PulseInstanceHandler(pulse);

            await Task.Delay((int)(instanceDelaySeconds * 1000));
        }
    }

    public async void _PulseInstanceHandler( Pulse pulse )
    {
        if (!_singlePulseQueue.ContainsKey(pulse.position)) _singlePulseQueue[pulse.position] = new Queue<Pulse>();

        Queue<Pulse> queue = _singlePulseQueue[pulse.position];

        //if there is a pulse still running on this square, add self to queue causing running queue to break itself, once it is it's turn in line run.
        if (queue.Count != 0)
        {
            queue.Enqueue(pulse);
            if (pulse.cachedColour.a == 0) pulse.cachedColour = queue.Peek().cachedColour;

            if (pulse.resetColour.a == 0) pulse.resetColour = pulse.cachedColour;
            else
            {
                //board colour combination test
                pulse.resetColour = Color.Lerp(pulse.resetColour, pulse.cachedColour, 0.35f);
                //
            }
            pulse.colour = Color.Lerp( pulse.colour, pulse.cachedColour, 0.35f );

            if (queue.TryPeek(out var peek))
            {
                DebugInfo.pulsesWaiting += 1;
                while (queue.TryPeek(out var peekCheck))
                {
                    if (peekCheck.Equals(pulse)) { DebugInfo.pulsesWaiting -= 1; break; }
                    await Task.Delay(1);
                    if (peekCheck is null) { DebugInfo.pulsesWaiting -= 1; return; }
                }
            }

            if (queue.TryPeek(out var pulseOnSquare)) { if (pulseOnSquare.Equals(pulse)) PulseSquare(pulse, queue); }
            else if (pulse.forceStart) { queue.Enqueue(pulse); PulseSquare(pulse, queue); }
        }
        else
        {
            queue.Enqueue(pulse);
            if (pulse.cachedColour.a == 0) pulse.cachedColour = storage.board.squares[pulse.position].material.color;

            pulse.colour = Color.Lerp(pulse.colour, pulse.cachedColour, 0.35f);

            if (pulse.resetColour.a == 0) pulse.resetColour = pulse.cachedColour;
            else { pulse.resetColour = Color.Lerp(pulse.resetColour, pulse.cachedColour, 0.35f); } //bcc test

            PulseSquare(pulse, queue);
        }
    }


    async void PulseSquare( Pulse pulse, Queue<Pulse> queue )
    {
        DebugInfo.pulsesRunning += 1;
        for (int pulseIndex = 0; pulseIndex < pulse.howManyTimes; pulseIndex++)
        {
            var square = storage.board.squares[pulse.position].material;


            //smooth transition to pulse colour
            Color currentSquareColor = square.color;

            if (pulse.fadeInEnabled)
            {
                for (int tick = 0; tick <= (ticksPerSecond * 0.015); tick++)
                {
                    square.color = Color.Lerp( currentSquareColor, pulse.colour, tick / (float)(ticksPerSecond * 0.015) );

                    await Task.Delay((int)((1 / ticksPerSecond) * 1000));
                }
            }
            square.color = pulse.colour;


            await Task.Delay((int)(pulse.holdSeconds * 1000));
            bool aloneInQueue = true;

            for (int tick = 0; tick < (ticksPerSecond * pulse.lerpSeconds); tick++)
            {
                aloneInQueue = queue.Count == 1;

                //break early if another pulse has been started on this square
                if ((!aloneInQueue && pulse.breakMode == Pulse.BreakMode.BreakDuringTick) || pulse.__forceBreak__)
                {
                    DebugInfo.pulsesRunning -= 1;

                    storage.board.squares[pulse.position].material.color = pulse.cachedColour;

                    if (pulse.__clearQueueWhenBreak) { queue.Clear(); }
                    queue.TryDequeue(out var szx);
                    return;
                }

                square.color = Color.Lerp(pulse.colour, pulse.resetColour, tick / (ticksPerSecond * pulse.lerpSeconds));

                await Task.Delay((int)((1 / ticksPerSecond) * 1000));
            }
            if ((!aloneInQueue && pulse.breakMode == Pulse.BreakMode.BreakAfterCycle) || pulse.__forceBreak__)
            {
                DebugInfo.pulsesRunning -= 1;
                storage.board.squares[pulse.position].material.color = pulse.cachedColour;

                if (pulse.__clearQueueWhenBreak) { queue.Clear(); }
                queue.TryDequeue(out var s);
                return;
            }
        }

        if (pulse.__clearOtherQueuePulsesWhenCyclesEnd__) { queue.Clear(); queue.Enqueue(pulse); }

        DebugInfo.pulsesRunning -= 1;

        //runs after all pulses have completed and were not interrupted
        storage.board.squares[pulse.position].material.color = pulse.resetColour;

        //if square is to hold a different colour until another pulse is run on this square, wait for the other pulse to start on this square to let the new pulse grab this square's cached colour before clearing itself
        if (!pulse.resetColour.Equals(pulse.cachedColour) && !pulse.__forceEnd__)
        {
            while (queue.ToArray()[queue.Count - 1].Equals(pulse) && !pulse.__forceEnd__) { await Task.Delay(1); if (pulse.__forceEnd__) { break; } }
        }

        storage.board.squares[pulse.position].material.color = pulse.cachedColour;

        if (pulse.__clearQueueWhenEnd) { queue.Clear(); }
        queue.TryDequeue(out var sx);
        return;
    }
Editor is loading...
Leave a Comment