rob-revisited/extensions/webrtc/doc/samples/OnlineSample.php

90 lines
3.4 KiB
PHP

<?php
namespace App\Extensions\Webrtc;
use JumPHP\Net\LobbyScene;
use JumPHP\Net\NetSession;
use JumPHP\Renderer\Renderer;
use JumPHP\Scene\AbstractScene;
use JumPHP\WebRtc\DataChannel;
use JumPHP\WebRtc\Peer;
use JumPHP\WebRtc\Signaling;
use JumPHP\WebRtc\WebRtc;
/**
* Sample of jumphp/webrtc: two players meet on the relay (LobbyScene, quick match), exchange their WebRTC signals through it (Signaling::overNet),
* then talk PEER TO PEER: the relay no longer carries the game's messages. Space sends your position; positions go unreliable + unordered (like UDP).
* Two windows or two browsers: JUMPHP_SCENE='App\Extensions\Webrtc\OnlineSample' bin/jumphp run <game> (bin/jumphp relay up first)
*/
final class OnlineSample extends AbstractScene
{
public const LABEL = 'Two players meet on the relay, then talk peer to peer (WebRTC data channel)';
private ?Peer $peer = null;
private ?DataChannel $pos = null;
private string $state = '';
private float $x = 160;
private float $other = -1;
public function __construct(private WebRtc $webrtc)
{
}
public function enter(array $params = []): void
{
$this->other = -1;
$this->x = 160;
$net = $params['net'] ?? null;
if (!$net instanceof NetSession) {
$this->state = 'press space to find a player';
return;
}
$this->state = 'signaling';
$net->removeAllListeners();
$other = (int)($params['peer'] ?? ($net->peers()[0] ?? 0));
$this->peer = $this->webrtc->peer();
Signaling::overNet($net, $this->peer, $other);
$this->peer->on('state', fn (string $s) => $this->state = $s);
$this->peer->on('error', fn (string $e) => $this->state = 'error: ' . $e);
$this->pos = $this->peer->channel('pos', ordered: false, retransmits: 0);
$this->pos->on('message', fn (string $m) => $this->other = (float)$m);
if (!empty($params['host'])) {
$this->peer->offer();
}
}
public function exit(): void
{
$this->webrtc->closeAll();
$this->peer = $this->pos = null;
}
public function update(float $dt): void
{
if ($this->actions()->isPressed('back')) {
$this->scenes()->switchTo((string)$this->engine->config->get('start_scene'));
return;
}
if ($this->peer === null) {
if ($this->actions()->isPressed('send')) {
$this->scenes()->switchTo(LobbyScene::class, ['next' => self::class, 'back' => (string)$this->engine->config->get('start_scene'), 'players' => 2, 'auto' => 'quick']);
}
return;
}
$this->x = max(8, min($this->width() - 8, $this->x + $this->actions()->axis('left', 'right') * 120 * $dt));
if ($this->pos?->isOpen()) {
$this->pos->send((string)round($this->x, 1));
}
}
public function render(Renderer $r): void
{
$r->clear([10, 12, 22]);
$this->font()->draw($r, 'WEBRTC ONLINE: ' . strtoupper($this->state), 8, 8, 1, [120, 200, 255]);
$r->fillRect($this->x - 6, 90, 12, 12, '#40d080');
if ($this->other >= 0) {
$r->fillRect($this->other - 6, 110, 12, 12, '#ff9040');
}
$this->font()->draw($r, 'LEFT / RIGHT: MOVE (SENT PEER TO PEER) ESC: BACK', $this->width() / 2, $this->height() - 14, 1, [140, 140, 170], 'center');
}
}