PHPackages                             velkuns/game-text-engine - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. velkuns/game-text-engine

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

velkuns/game-text-engine
========================

This is a PHP Game Text Engine to create games based on texts, with choices, items, and more.

0.5.0(5mo ago)214[7 issues](https://github.com/velkuns/game-text-engine/issues)MITPHPPHP 8.3.\*||8.4.\*||8.5.\*CI passing

Since Oct 28Pushed 5mo agoCompare

[ Source](https://github.com/velkuns/game-text-engine)[ Packagist](https://packagist.org/packages/velkuns/game-text-engine)[ RSS](/packages/velkuns-game-text-engine/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (8)Versions (7)Used By (0)

Game Text Engine
================

[](#game-text-engine)

[![Current version](https://camo.githubusercontent.com/893de892a334d0392f432ab296f69f0dce70746d5dc3510d24121a74984f5b40/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f76656c6b756e732f67616d652d746578742d656e67696e652e7376673f6c6f676f3d636f6d706f736572)](https://packagist.org/packages/velkuns/game-text-engine)[![Supported PHP version](https://camo.githubusercontent.com/0c1418d817d120e42137fa892159a874d4f87ea46f278904c1d3971fe3ff8e71/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6f676f3d706870266c6162656c3d504850266d6573736167653d382e332532302d253230382e3526636f6c6f723d373737626234)](https://packagist.org/packages/velkuns/game-text-engine)[![CI](https://github.com/velkuns/game-text-engine/workflows/CI/badge.svg)](https://github.com/velkuns/game-text-engine/workflows/CI/badge.svg)[![Quality Gate Status](https://camo.githubusercontent.com/259a73b08c39b661343cbd2ecf36a105c11e63cc2bd7f81557ea4e214e2c96aa/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d76656c6b756e735f67616d652d746578742d656e67696e65266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/dashboard?id=velkuns_game-text-engine)[![Coverage](https://camo.githubusercontent.com/f75adb22cd4c173cb37438c1ac5c037fed1364f558b7712682031688b00e1a77/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d76656c6b756e735f67616d652d746578742d656e67696e65266d65747269633d636f766572616765)](https://sonarcloud.io/dashboard?id=velkuns_game-text-engine)

Why?
----

[](#why)

This is a PHP Game Text Engine to create games based on texts, with choices, items, and more.

Installation
------------

[](#installation)

If you wish to install it in your project, require it via composer:

```
composer require velkuns/game-text-engine
```

API Documentation
-----------------

[](#api-documentation)

### Game Object

[](#game-object)

```
declare(strict_types=1);

namespace Application;

use Velkuns\GameTextEngine\Api\ItemsApi;
use Velkuns\GameTextEngine\Api\PlayerApi;
use Velunns\GameTextEngine\Api\GameApi;
use Velkuns\GameTextEngine\Core\Factory\AttributeFactory;
use Velkuns\GameTextEngine\Core\Loader\JsonLoader;

//~ Factories
$modifierFactory  = new ModifierFactory();
$itemFactory      = new ItemFactory($modifierFactory);
$conditionFactory = new ConditionsFactory(new ConditionParser(), new ConditionElementResolver(), new ConditionValidator());
$graphFactory     = new GraphFactory($conditionFactory);
$attributeFactory = new AttributeFactory();
$entityFactory    = new EntityFactory(
    $attributeFactory,
    new TraitFactory($modifierFactory, $conditionFactory),
    $itemFactory
);

$items   = new ItemsApi($itemFactory);
$gameApi = new GameApi(
    new JsonLoader(),
    new StoryApi($graphFactory),
    $items,
    new BestiaryApi($entityFactory, $items),
    new AttributesApi($attributeFactory),
    new PlayerApi($entityFactory, $items),
    new CombatApi(new Randomizer(new Mt19937())),
);

//~ Load json data (can be from files or strings if came from database)
$storyData            = $game->loader->fromFile($dataDir . '/stories/test.json');
$itemsData            = $game->loader->fromFile($dataDir . '/items.json');
$bestiaryData         = $game->loader->fromFile($dataDir . '/bestiary.json');
$attributesRulesData  = $game->loader->fromFile($dataDir . '/rules/rules_attributes.json');
$traitsRulesData      = $game->loader->fromFile($dataDir . '/rules/rules_traits.json');
$alterationsRulesData = $game->loader->fromFile($dataDir . '/rules/rules_alterations.json');
$combatsRulesData     = $game->loader->fromFile($dataDir . '/rules/rules_combat.json');
$playerRulesData      = $game->loader->fromFile($dataDir . '/rules/rules_player.json');
$playerData           = $game->loader->fromFile($dataDir . '/templates/player.json');

//~ Load data into the game api
$gameApi->load(
    $storyData,
    $itemsData,
    $bestiaryData,
    $traitsRulesData,
    $alterationsRulesData,
    $combatsRulesData,
    $playerRulesData,
    $playerData
);

//~ Access to the other apis
$gameApi->storyApi->[...];
$gameApi->bestiaryApi->[...];
$gameApi->itemsApi->[...];
$gameApi->attributesApi->[...];
$gameApi->traitsApi->[...];
$gameApi->alterationssApi->[...];
$gameApi->playerApi->[...];

//~ Dumping apis into json data

/**
 * @phpstan-return array{
 *     story: string,
 *     items: string,
 *     bestiary: string,
 *     attributesRules: string,
 *     traitsRules: string,
 *     alterationsRules: string,
 *     combatRules: string,
 *     playerRules: string,
 *     playerData: string,
 * } $data Array of json data, to save in files or database
 */
$data = $gameApi->dump(/* true */); // true to pretty json output

//~ Exporting story graph into DOT data
$gameApi->exporter->toFile($gameApi->storyApi->graph, [...]);      // export story graph to file
$string = $gameApi->exporter->toString($gameApi->storyApi->graph); // export story graph to string

//~ Game API read
$source = '1';
$target = '2';
$gameApi->read($source, $target);
```

### Loader (to load data from files / strings):

[](#loader-to-load-data-from-files--strings)

```
