WaitWhile

Namespace:

namespace Lenga\Engine\Core;

class WaitWhile

Methods

keepWaiting

public function keepWaiting()

Example

use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\WaitWhile;

class WaitForStop extends Behaviour
{
    private bool $isMoving = false;

    public function start(): void
    {
        $this->startCoroutine($this->waitUntilStopped());
    }

    public function update(): void
    {
        // Control movement
        if (Input::getKey(KeyCode::W)) {
            $this->isMoving = true;
        } else {
            $this->isMoving = false;
        }
    }

    private function waitUntilStopped(): \Generator
    {
        Debug::log('Waiting while moving...');

        // Wait while condition is true (wait until moving stops)
        yield new WaitWhile(function() {
            return $this->isMoving;
        });

        Debug::log('Object has stopped moving!');
    }
}