This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact.php
81 lines (65 loc) · 2.53 KB
/
react.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
require 'vendor/autoload.php';
use EdgeLaser\Game;
use EdgeLaser\LaserColor;
$loop = React\EventLoop\Factory::create();
$factory = new Datagram\Factory($loop);
$socket = $factory->createClient('127.0.0.1:4242')->then(function (Datagram\Socket $client) use ($loop) {
$game = new Game($client);
$game->setName('ReactGame');
$game->setResolution(500);
$game->setDefaultColor(LaserColor::LIME);
$client->on('message', function($message, $serverAddress, $client) use (&$game) {
$command = chr(unpack('C', substr($message,0,1))[1]);
switch ($command) {
case 'A':
// Ack
$game->id = unpack('C', substr($message,1))[1];
echo 'received gameid "' . $game->id . '" from ' . $serverAddress. PHP_EOL;
break;
case 'G':
// Go
$game->state = Game::STATE_RUNNING;
echo 'received "Go" from ' . $serverAddress. PHP_EOL;
break;
case 'I':
// Input
$key = unpack('v', substr($message,1))[1];
echo 'received key "' . $key . '" from ' . $serverAddress. PHP_EOL;
break;
case 'S':
// Stop
$game->state = Game::STATE_PAUSED;
echo 'received "Pause" from ' . $serverAddress. PHP_EOL;
break;
default:
echo 'received "' . $message . '" from ' . $serverAddress. PHP_EOL;
break;
}
});
$client->on('error', function($error, $client) {
echo 'error: ' . $error->getMessage() . PHP_EOL;
});
$client->on('close', function(Datagram\Socket $client) {
echo "Bye !" . PHP_EOL;
});
// Register ourselves
$client->send(pack('C', 0) . pack('Z*', 'H' . $game->name));
$coeff = 0;
// Main Game Loop
// 20FPS = 0.05s per frame
$loop->addPeriodicTimer(0.05, function() use ($client, &$game, &$coeff) {
if ($game->isRunning()) {
$coeff = $coeff > 499 ? 0 : $coeff+4;
$game
->addLine(250, 0, $coeff, 250, LaserColor::CYAN)
->addLine(250, 500, $coeff, 250, LaserColor::CYAN)
->addCircle(250, 250, $coeff, LaserColor::FUCHSIA)
->addRectangle(10, 10, $coeff, $coeff);
$client->send(pack('C', $game->id) . 'R'); // Refresh (Display the actual frame)
}
});
}, function($error) {
echo 'ERROR: ' . $error->getMessage() . PHP_EOL;
});
$loop->run();