教程 — Effects
1. 安装扩展
在项目中把扩展添加到游戏:
bin/jumphp extension:add <game> jumphp/effects
引擎启动时会自动发现它,无需接线。
2. Each easing curve moves a dot from side to side, under a vignette
在 src/Extensions/ 中创建场景(或用 extension:add 复制),阅读下面的代码:注释说明了按什么键。
<?php
namespace App\Extensions\Effects;
use JumPHP\Effects\Ease;
use JumPHP\Effects\Vignette;
use JumPHP\Renderer\Renderer;
use JumPHP\Scene\AbstractScene;
/**
* Sample of jumphp/effects: every easing curve moves a dot over the same two seconds, under a vignette (the dark corners of the screen).
* JUMPHP_SCENE='App\Extensions\Effects\EasingSample' bin/jumphp run <game>
*/
final class EasingSample extends AbstractScene
{
public const LABEL = 'Each easing curve moves a dot from side to side, under a vignette';
private float $time = 0;
public function enter(array $params = []): void
{
$this->time = 0;
}
public function update(float $dt): void
{
$this->time += $dt;
}
public function render(Renderer $r): void
{
$r->clear([20, 22, 40]);
$names = Ease::NAMES;
$rowHeight = ($this->height() - 16) / max(1, count($names));
$t = fmod($this->time, 2.4) / 2.0; // 0 -> 1 in two seconds, then it waits a little
$t = min(1.0, $t);
foreach ($names as $i => $name) {
$y = 8 + $i * $rowHeight + $rowHeight / 2;
$r->drawLine(60, (int)$y, $this->width() - 12, (int)$y, [60, 64, 96]);
$x = Ease::lerp(60, $this->width() - 12, Ease::get($name)($t));
$r->fillCircle((int)$x, (int)$y, 3, '#ffd040');
$this->font()->draw($r, strtoupper($name), 4, (int)$y - 3, 1, [200, 205, 235]);
}
Vignette::render($r, 0.6);
}
}
运行:
JUMPHP_SCENE='App\Extensions\Effects\EasingSample' bin/jumphp run <game>
3. Enter, Space or a click flashes the screen
在 src/Extensions/ 中创建场景(或用 extension:add 复制),阅读下面的代码:注释说明了按什么键。
<?php
namespace App\Extensions\Effects;
use JumPHP\Renderer\Renderer;
use JumPHP\Scene\AbstractScene;
use JumPHP\Effects\Flash;
/**
* Sample of the extension (extension:add copies it into the game, src/Extensions/<Name>/): Enter, Space or a click flashes the screen.
* JUMPHP_SCENE='App\Extensions\Effects\FlashSample' bin/jumphp run <game>
*/
final class FlashSample extends AbstractScene
{
/** what the IDE writes by its Play button */
public const LABEL = 'Enter, Space or a click flashes the screen';
public function __construct(private Flash $flash)
{
}
public function enter(array $params = []): void
{
$this->flash->flash('#ff4040');
}
public function update(float $dt): void
{
if ($this->actions()->isPressed('confirm') || $this->engine->input->getMouse()->isPressed(1)) {
$this->flash->flash('#40a0ff');
}
$this->flash->update($dt);
}
public function render(Renderer $r): void
{
$r->clear([16, 14, 28]);
$this->font()->draw($r, 'PRESS ENTER', $this->width() / 2, $this->height() / 2 - 4, 1, [200, 205, 235], 'center');
$this->flash->render($r);
}
}
运行:
JUMPHP_SCENE='App\Extensions\Effects\FlashSample' bin/jumphp run <game>
4. Enter cycles the transitions: fade, wipes, iris, blinds, dissolve
在 src/Extensions/ 中创建场景(或用 extension:add 复制),阅读下面的代码:注释说明了按什么键。
<?php
namespace App\Extensions\Effects;
use JumPHP\Effects\Transition;
use JumPHP\Renderer\Renderer;
use JumPHP\Scene\AbstractScene;
/**
* Sample of jumphp/effects: Enter, Space or a click covers the screen with the next transition (fade, wipes, iris, blinds, dissolve), the
* colour changes under the cover, then it is revealed the same way. JUMPHP_SCENE='App\Extensions\Effects\TransitionSample' bin/jumphp run <game>
*/
final class TransitionSample extends AbstractScene
{
public const LABEL = 'Enter cycles the transitions: fade, wipes, iris, blinds, dissolve';
private const COLORS = [[28, 60, 110], [110, 40, 40], [30, 90, 50]];
private int $color = 0;
private int $type = 0;
private string $name = '';
public function __construct(private Transition $transition)
{
}
public function enter(array $params = []): void
{
$this->color = 0;
$this->type = 0;
$this->name = '';
$this->transition->seed(7);
$this->transition->reveal('fade', 0.01);
$this->transition->update(1);
}
public function update(float $dt): void
{
if (($this->actions()->isPressed('confirm') || $this->engine->input->getMouse()->isPressed(1)) && !$this->transition->isActive()) {
$type = $this->name = Transition::TYPES[$this->type++ % count(Transition::TYPES)];
$this->transition->cover($type, 0.45, '#000000', function () use ($type): void {
$this->color = ($this->color + 1) % count(self::COLORS);
$this->transition->reveal($type, 0.45, '#000000');
});
}
$this->transition->update($dt);
}
public function render(Renderer $r): void
{
$r->clear(self::COLORS[$this->color]);
$this->font()->draw($r, strtoupper($this->name ?: 'PRESS ENTER'), $this->width() / 2, $this->height() / 2 - 8, 2, [255, 255, 255], 'center');
$this->transition->render($r);
}
}
运行:
JUMPHP_SCENE='App\Extensions\Effects\TransitionSample' bin/jumphp run <game>
5. 检查
bin/jumphp test <game>
bin/jumphp shot <game>