TR

← Effects

Eğitim — Effects

1. Eklentiyi kurun

Projenizde eklentiyi oyununuza ekleyin:

bin/jumphp extension:add <game> jumphp/effects

Motor başlangıçta kendisi bulur, bağlanacak bir şey yok.

2. Each easing curve moves a dot from side to side, under a vignette

src/Extensions/ içinde bir sahne oluşturun (veya extension:add ile kopyalayın) ve kodu okuyun: yorumlar neye basılacağını söyler.

<?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);
    }
}

Çalıştırın:

JUMPHP_SCENE='App\Extensions\Effects\EasingSample' bin/jumphp run <game>

3. Enter, Space or a click flashes the screen

src/Extensions/ içinde bir sahne oluşturun (veya extension:add ile kopyalayın) ve kodu okuyun: yorumlar neye basılacağını söyler.

<?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);
    }
}

Çalıştırın:

JUMPHP_SCENE='App\Extensions\Effects\FlashSample' bin/jumphp run <game>

4. Enter cycles the transitions: fade, wipes, iris, blinds, dissolve

src/Extensions/ içinde bir sahne oluşturun (veya extension:add ile kopyalayın) ve kodu okuyun: yorumlar neye basılacağını söyler.

<?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);
    }
}

Çalıştırın:

JUMPHP_SCENE='App\Extensions\Effects\TransitionSample' bin/jumphp run <game>

5. Doğrula

bin/jumphp test <game>
bin/jumphp shot <game>

Effects Dokümantasyon