NL

← Dialogue

Tutorial — Dialogue

1. De extensie installeren

Voeg in je project de extensie toe aan je spel:

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

De engine vindt hem zelf bij het starten, niets te koppelen.

2. Talk to a shopkeeper: choices, a gold counter, text written letter by letter

Maak een scène in src/Extensions/ (of kopieer hem met extension:add) en lees de code: de commentaren zeggen wat je indrukt.

<?php

namespace App\Extensions\Dialogue;

use JumPHP\Dialogue\Dialogue;
use JumPHP\Dialogue\DialogueBox;
use JumPHP\Dialogue\DialogueRunner;
use JumPHP\Renderer\Renderer;
use JumPHP\Scene\AbstractScene;

/**
 * Sample of jumphp/dialogue: Enter talks to a shopkeeper (a dialogue written as a PHP array: the same shape as a .dialogue.json file); up / down
 * and Enter choose, a counter of gold changes with what you buy. JUMPHP_SCENE='App\Extensions\Dialogue\DialogueSample' bin/jumphp run <game>
 */
final class DialogueSample extends AbstractScene
{
    public const LABEL = 'Talk to a shopkeeper: choices, a gold counter, text written letter by letter';

    public function __construct(private DialogueRunner $runner, private DialogueBox $box)
    {
    }

    public function enter(array $params = []): void
    {
        $this->runner->end();
        $this->runner->clearListeners();
        $this->runner->variables()->reset();
        $this->runner->variables()->apply(['gold' => 20, 'potions' => 0]);
        $this->runner->add(Dialogue::fromArray('shop', ['start' => 'hello', 'nodes' => [
            'hello' => ['speaker' => 'Shopkeeper', 'text' => ['Welcome!', 'You have {gold} gold.'], 'next' => 'menu'],
            'menu' => ['speaker' => 'Shopkeeper', 'text' => 'What will it be?', 'choices' => [
                ['text' => 'A potion (8 gold)', 'if' => 'gold >= 8', 'set' => ['gold' => '-8', 'potions' => '+1'], 'next' => 'bought'],
                ['text' => 'I cannot afford anything', 'if' => 'gold < 8', 'next' => 'poor'],
                ['text' => 'Goodbye', 'next' => 'bye'],
            ]],
            'bought' => ['speaker' => 'Shopkeeper', 'text' => 'A potion! {gold} gold left, {potions} potion(s).', 'next' => 'menu'],
            'poor' => ['speaker' => 'Shopkeeper', 'text' => 'Come back with more gold.', 'end' => true],
            'bye' => ['speaker' => 'Shopkeeper', 'text' => 'Safe travels.', 'end' => true],
        ]]));
    }

    public function update(float $dt): void
    {
        $talking = $this->runner->isActive();
        $this->box->update($dt, $talking ? $this->actions() : null);
        if (!$talking && $this->actions()->isPressed('confirm')) {
            $this->runner->start('shop');
        }
    }

    public function render(Renderer $r): void
    {
        $r->clear([30, 40, 70]);
        $this->font()->draw($r, $this->runner->isActive() ? '' : 'PRESS ENTER TO TALK', $this->width() / 2, 40, 1, [220, 225, 255], 'center');
        $this->box->render($r, $this->font());
    }
}

Start hem:

JUMPHP_SCENE='App\Extensions\Dialogue\DialogueSample' bin/jumphp run <game>

3. A quest log: objectives with counters, completed when all are done

Maak een scène in src/Extensions/ (of kopieer hem met extension:add) en lees de code: de commentaren zeggen wat je indrukt.

<?php

namespace App\Extensions\Dialogue;

use JumPHP\Dialogue\QuestLog;
use JumPHP\Renderer\Renderer;
use JumPHP\Scene\AbstractScene;

/**
 * Sample of jumphp/dialogue: a quest log. Enter (confirm) picks up a gem, Up talks to the elder, Back starts over; when every objective is done the
 * quest is completed. The QuestLog keeps its state as plain values (toArray / restore): that is what a save file holds.
 * JUMPHP_SCENE='App\Extensions\Dialogue\QuestSample' bin/jumphp run <game>
 */
final class QuestSample extends AbstractScene
{
    public const LABEL = 'A quest log: objectives with counters, completed when all are done';

    private string $message = '';

    public function __construct(private QuestLog $quests)
    {
    }

    public function enter(array $params = []): void
    {
        $this->quests->restore(['status' => [], 'have' => []]);
        $this->quests->define('gems', ['title' => 'The lost gems', 'text' => 'The elder lost three gems in the cave.', 'objectives' => [
            ['id' => 'gems', 'text' => 'Collect the gems', 'count' => 3],
            ['id' => 'elder', 'text' => 'Tell the elder'],
        ]]);
        $this->quests->start('gems');
        $this->message = 'ENTER: PICK UP A GEM   UP: TALK TO THE ELDER   BACK: START OVER';
    }

    public function update(float $dt): void
    {
        if ($this->actions()->isPressed('back')) {
            $this->enter();
            return;
        }
        if ($this->quests->status('gems') !== 'active') {
            return;
        }
        if ($this->actions()->isPressed('confirm')) {
            $this->quests->progress('gems', 'gems');
        }
        if ($this->actions()->isPressed('up')) {
            $this->quests->progress('gems', 'elder');
        }
        $q = $this->quests->describe('gems');
        if (!array_filter($q['objectives'], fn ($o) => !$o['done'])) {
            $this->quests->complete('gems');
            $this->message = 'QUEST COMPLETED';
        }
    }

    public function render(Renderer $r): void
    {
        $r->clear([22, 20, 34]);
        $q = $this->quests->describe('gems');
        $font = $this->font();
        $font->draw($r, strtoupper($q['title']) . '  [' . strtoupper($q['status']) . ']', 12, 14, 2, '#ffd040');
        $font->draw($r, strtoupper($q['text']), 12, 36, 1, [200, 200, 220]);
        $y = 60;
        foreach ($q['objectives'] as $o) {
            $font->draw($r, ($o['done'] ? '[X] ' : '[ ] ') . strtoupper($o['text']) . ($o['count'] > 1 ? '  ' . $o['have'] . '/' . $o['count'] : ''), 20, $y, 1, $o['done'] ? '#40d060' : '#ffffff');
            $y += 14;
        }
        $font->draw($r, $this->message, 12, $this->height() - 14, 1, [160, 165, 200]);
    }
}

Start hem:

JUMPHP_SCENE='App\Extensions\Dialogue\QuestSample' bin/jumphp run <game>

4. Controleren

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

Dialogue Documentatie