rvilbrandt/gamebook

PHP 游戏书解析器

0.0.2 2016-05-20 08:38 UTC

This package is not auto-updated.

Last update: 2024-09-26 00:34:54 UTC


README

Build Status Latest Stable Version License

游戏书解析器

由Ronald Vilbrandt编写的游戏书文件PHP解析器

关于

这个小巧但相当不错的项目是为了实现我从青少年时期就有的梦想,那时我读了Steve Jackson或Ian Livingstone的奇幻游戏书。后来我也读了所谓的“单人冒险”的《Das Schwarze Auge》(黑眼 / Arkania 宇宙)分支,它们使用了与前面提到的早期游戏书相同的技巧。在90年代,当我用C64的BASIC玩耍时,我创建了一些简单的文字冒险游戏,有多个选择选项。它们并不真正好或长,但嘿——那是一回事。😄现在在2016年,大约20年后,我想实现我童年的梦想,并利用我的技能和一些时间来创建一个自创游戏书格式的解析器的工作版本。

经典游戏书的样本

游戏书有编号的文本块(我称之为“场景”)。你从第一个块开始阅读,在结尾处你有多个选项来继续阅读。有两种结束书籍的方式。第一种是找到正确的路径到达幸福的结局,另一种是找到许多死胡同之一,以糟糕的结局结束(在大多数情况下你会痛苦地死去 😄)。

欢迎来到这个伟大的冒险。你站在三扇门的前面。两扇已经打开,最后一扇是锁着的,需要一把钥匙才能打开。

你想做什么?

穿过第一扇打开的门。继续阅读在XXX

直接穿过第二扇门。继续阅读在XXX

在阅读场景时,你通常会找到一些可以携带的物品,或者你必须通过掷骰子与怪物战斗。场景中的某些选项只能在携带了物品或做出了之前的决定(例如,是否与某人交谈)后才能访问。

更多详情请见https://en.wikipedia.org/wiki/Gamebook

什么是游戏书解析器?

我的游戏书解析器读取带有场景、选项和条件的游戏书文本文件,并将它们解析为PHP对象。你可以用它来创建HTML页面或CLI应用程序,让读者玩游戏。

我的游戏书格式的特点

  • 带有多个继续选项的场景
  • 带有多个文本块的场景,这些文本块可以在满足条件时可选地显示(参见条件)
  • 在满足条件时可选地显示的选项(参见条件)
  • 条件可以是“状态”,可以是相等、小于、大于或不存在的。此外,还可以检查库存中是否有物品

我的游戏书解析器的特点

  • 解析当前场景,比较条件,处理库存和状态更改,并返回易于访问的解析对象
  • 作为MVC环境(如charme)的模型
  • 使用简单的JSON对象创建PHP对象,以便于面向对象地访问(附带JSON文件读取器)
  • 使用会话写入器处理游戏状态(附带PHP会话支持)
  • 组件(如JSON文件读取器或会话处理器)可以立即替换为自定义组件

未来目标

  • 具有属性(力量、生命值、技能等)的角色处理
  • 具有属性(如力量、生命值、技能等)的怪物/敌人
  • 基于属性和虚拟“骰子滚动”进行战斗

演示

已经包含了一个小演示,该演示以HTML格式输出解析后的场景和库存。 在此尝试此演示(基于下面的示例游戏书)。

我的游戏书格式结构

JSON示例

主要

{
    "title": "Yet another fairytale",
    "author": "Ronald Vilbrandt",
    "version": "0.4",
    "startScene": "beginning",
    "scenes": [],
    "items": []
}

简单的场景(有死胡同)

{
    "id": "door_1_death",
    "contents": [
        {
            "text": "You fall into a deep hole to death. Boy, that escalated quickly."
        }
    ]
}

简单的场景(有继续前进的选项)

{
    "id": "door_2",
    "contents": [
        {
            "text": "You're standing in an empty room with nothing but a key in the center of the room."
        }
    ],
    "options": [
        {
            "text": "Take the key.",
            "jumpTo": "door_2_key_taken"
        },
        {
            "text": "Leave the key alone and return to the previous room.",
            "jumpTo": "beginning"
        }
    ]

}

高级场景(向库存中添加物品)

{
    "id": "door_2_key_taken",
    "contents": [
        {
            "text": "You've put the key into your pocket."
        }
    ],
    "options": [
        {
            "text": "Leave the room.",
            "jumpTo": "beginning"
        }
    ],
    "inventory": {
        "add": [
            "key"
        ]
    }
}

高级场景(设置状态)

{
    "id": "door_3",
    "contents": [
        {
            "text": "Congratulation! You've found the final room and exit.\nThank you, stranger! But our Princess is in another castle!"
        }
    ],
    "inventory": {
        "remove": [
            "key"
        ]
    },
    "states": {
        "door_3_opened": 1
    }

}

复杂场景(根据状态和库存查询以显示或隐藏选项)

{
    "id": "beginning",
    "contents": [
        {
            "text": "Welcome to this great adventure. You're standing in front of three doors. Two are already opened, the last one is locked and needs a key to be opened."
        }
    ],
    "options": [
        {
            "text": "Walk through the first open door.",
            "jumpTo": "door_1_death"
        }, 
        {
            "text": "Go straight through door number two.",
            "jumpTo": "door_2",
            "conditions": {
                "inventory": {
                    "hasNot": [
                        "key"
                    ]
                }
            }
        }, 
        {
            "text": "Open the locked door with the key.",
            "jumpTo": "door_3",
            "conditions": {
                "inventory": {
                    "has": [
                        "key"
                    ]
                }
            }
        },
        {
            "text": "Go through the door you've openend with the key previously.",
            "jumpTo": "door_3",
            "conditions": {
                "states": {
                    "eq": {
                        "door_3_opened": 1
                    }
                }
            }
        }
    ]
}

物品列表

{
    "id": "potion",
    "name": "Magic Potion",
    "description": "One more magic potion. It will heal my aching wounds."
}