thomas-anderson-lib
Version:
A text adventure game library based on Thomas Anderson from The Matrix. Create your own Matrix-inspired text adventures with this easy-to-use library.
1 lines • 11.3 kB
JavaScript
import*as e from"react";var t={156:(e,t,o)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.TheMatrix=t.MatrixGame=void 0,t.createGame=function(){return new i};var n=o(549),r=o(439),a=o(795);Object.defineProperty(t,"MatrixGame",{enumerable:!0,get:function(){return a.MatrixGame}});var i=function(){function e(){var e=this;this.engine=new n.GameEngine("main"),r.gameRooms.forEach((function(t){return e.engine.addRoom(t)}))}return e.prototype.processCommand=function(e){return this.engine.processCommand(e)},e.prototype.getScore=function(){return this.engine.getScore()},e.prototype.isGameOver=function(){return this.engine.isGameOver()},e}();t.TheMatrix=i},439:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.gameRooms=void 0;t.gameRooms=[{id:"main",description:"You awake with a throbbing headache in the cold metal hull of the Nebuchadnezzar. \nYou see a COOKIE on the table. Next to it is a BOWL OF GRUEL.\nPossible exits are NORTH, SOUTH and MORPHEUS.",items:[{id:"cookie",name:"COOKIE",description:"A delicious chocolate chip cookie. Freshly baked.",canTake:!0,onTake:function(e){return e.score+=2,e.flags.cookieTaken=!0,"You take the cookie, and it tastes like a cookie. Delicious. Much better than the gruel."}},{id:"gruel",name:"BOWL OF GRUEL",description:"A bowl of mysterious looking goop. It has proteins, aminos, everything you need. Well, not everything.",canTake:!1,onTake:function(e){var t=e.flags.gruelAttempts||0;return 0===t?(e.flags.gruelAttempts=1,e.score+=1,"You can't take the bowl. It's bolted to the table in the galley of the Nebuchadnezzar."):1===t?(e.flags.gruelAttempts=2,e.score+=1,"The bowl remains fixed. Maybe there's a reason you can't take it yet."):(e.score-=1e3,e.gameOver=!0,"In your attempt to forcefully grab the bowl, you trigger the Nebuchadnezzar's defense mechanisms which alerts nearby sentries. Agents flood the room. Zion is lost. Game Over.")}}],exits:{north:"construct",south:"subway",morpheus:"morpheus"},onEnter:function(e){return e.flags.cookieTaken?"You're back in the main hull of the Nebuchadnezzar. The BOWL OF GRUEL sits untouched. The screens around you flicker with green code.\nPossible exits are NORTH, SOUTH and MORPHEUS.":""}},{id:"construct",description:"You enter the Construct. An infinite white space stretches before you. You see a STEAK floating in mid-air.\nPossible exits are SOUTH.",items:[{id:"steak",name:"STEAK",description:"A perfectly cooked steak. It smells delicious.",canTake:!0,onTake:function(e){return e.score-=1,e.gameOver=!0,"You have given in to your carnal desires. Ignorance is bliss. You've been disconnected from the Matrix. Game Over."}}],exits:{south:"main"}},{id:"subway",description:"You descend into an abandoned subway station. A KUNG FU BOOT DRIVE glows faintly in the darkness.\nPossible exits are NORTH.",items:[{id:"kung_fu_boot_drive",name:"KUNG FU BOOT DRIVE",description:"A small neural chip. Insert it into your boot and you will instantly know kung fu.",canTake:!0,onTake:function(e){return e.score+=2,e.flags.kungFuBootDriveTaken=!0,"You take the kung fu boot drive, and put it in your pocket for safekeeping."}}],exits:{north:"main"},onEnter:function(e){return e.flags.kungFuBootDriveTaken?"The abandoned subway station feels different now. The walls seem to ripple with code.\nPossible exits are NORTH.":""}},{id:"morpheus",description:"You meet Morpheus. He wears reflective sunglasses and a long leather coat. 'I've been waiting for you,' he says.\nPossible exits are BACK.",items:[],exits:{back:"main"}}]},483:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.parseCommand=function(e){var t=e.trim().split(/\s+/);if(0===t.length)return{verb:"help"};var r={verb:""},a=0;if(1===t.length&&["north","south","east","west","n","s","e","w"].includes(t[0].toLowerCase()))return{verb:"go",noun:t[0].toLowerCase()};if(!o.has(t[a].toLowerCase()))return r.verb="unknown",r;r.verb=t[a].toLowerCase(),a++;if(a<t.length){for(var i=[];a<t.length&&!n.has(t[a].toLowerCase());)i.push(t[a]),a++;i.length>0&&(r.noun=i.join(" "))}a<t.length&&n.has(t[a].toLowerCase())&&(r.preposition=t[a].toLowerCase(),++a<t.length&&(r.object=t.slice(a).join(" ")));return r};var o=new Set(["go","get","take","look","examine","use","talk","give","drop","inventory","help","quit","die","north","south","east","west","n","s","e","w"]),n=new Set(["to","at","with","in","on","under","behind"])},549:(e,t,o)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.GameEngine=void 0;var n=o(483),r=function(){function e(e){this.rooms=new Map,this.state={currentRoom:e,inventory:[],score:0,gameOver:!1,visitedRooms:new Set([e]),flags:{}}}return e.prototype.addRoom=function(e){this.rooms.set(e.id,e)},e.prototype.getCurrentRoom=function(){return this.rooms.get(this.state.currentRoom)},e.prototype.getInventory=function(){return this.state.inventory},e.prototype.getScore=function(){return this.state.score},e.prototype.isGameOver=function(){return this.state.gameOver},e.prototype.updateScore=function(e){this.state.score+=e},e.prototype.moveToRoom=function(e){var t=this.getCurrentRoom(),o=this.rooms.get(e);if(!o)return{message:"You cannot go there. The path does not exist in this simulation."};if(null==t?void 0:t.onExit){var n=t.onExit(this.state);if(n)return{message:n}}if(this.state.currentRoom=e,this.state.visitedRooms.add(e),o.onEnter){var r=o.onEnter(this.state);if(r)return{message:r}}return{message:o.description}},e.prototype.handleGo=function(e){var t=this.getCurrentRoom();if(!t)return{message:"ERROR: Unable to locate your position in the Matrix."};var o=t.exits[e];return o?this.moveToRoom(o):{message:"That exit has been blocked by the system."}},e.prototype.handleGive=function(e){var t=this.getCurrentRoom();return"morpheus"===(null==t?void 0:t.id)&&"kung fu boot drive"===e?{message:"You give the kung fu boot drive to Morpheus. He is pleased and offers to show you how deep the rabbit hole goes.",gameOver:!0}:{message:"You cannot give that item."}},e.prototype.handleTake=function(e){var t=this.getCurrentRoom();if(!t)return{message:"ERROR: Unable to locate your position in the Matrix."};var o=t.items.findIndex((function(t){return t.name.toLowerCase()===e.toLowerCase()}));if(-1===o)return{message:"That object does not exist in this simulation."};var n=t.items[o];return n.onTake?{message:n.onTake(this.state),gameOver:this.state.gameOver}:n.canTake?(t.items.splice(o,1),this.state.inventory.push(n),{message:"You take ".concat(n.name,".")}):{message:"This object is part of the Matrix. You cannot take it."}},e.prototype.handleLook=function(e){if(!e){var t=this.getCurrentRoom();return{message:(null==t?void 0:t.description)||"You see nothing unusual in the code."}}var o=this.getCurrentRoom();if(!o)return{message:"ERROR: Unable to locate your position in the Matrix."};var n=o.items.find((function(t){return t.name.toLowerCase()===e.toLowerCase()}))||this.state.inventory.find((function(t){return t.name.toLowerCase()===e.toLowerCase()}));return n?{message:n.description}:{message:"That object does not exist in this simulation."}},e.prototype.handleInventory=function(){if(0===this.state.inventory.length)return{message:"You are not carrying anything. You feel a rumbling in your stomach. Protein and amino acid gruel leaves much to be desired."};var e=this.state.inventory.map((function(e){return e.name})).join(", ");return{message:"You are carrying: ".concat(e)}},e.prototype.processCommand=function(e){var t=(0,n.parseCommand)(e);switch(t.verb){case"go":return this.handleGo(t.noun||"");case"take":return this.handleTake(t.noun||"");case"look":return this.handleLook(t.noun);case"give":return this.handleGive(t.noun||"");case"help":case"inventory":var o=this.handleInventory(),r=this.getCurrentRoom();return{message:"".concat((null==r?void 0:r.description)||"","\n\n").concat(o.message)};case"die":return this.state.gameOver=!0,this.state.score=-1e3,{message:"That wasn't very smart. The machines have taken over the Nebuchadnezzar. Your score: "+this.state.score,gameOver:!0};case"quit":return this.state.gameOver=!0,{message:"You have chosen to disconnect from the Matrix. Your score: "+this.state.score,gameOver:!0};case"unknown":return{message:"Command not recognized. There has been a glitch in the Matrix."};default:return{message:"That command is not part of this simulation."}}},e}();t.GameEngine=r},649:t=>{t.exports=e},795:function(e,t,o){var n,r=this&&this.__createBinding||(Object.create?function(e,t,o,n){void 0===n&&(n=o);var r=Object.getOwnPropertyDescriptor(t,o);r&&!("get"in r?!t.__esModule:r.writable||r.configurable)||(r={enumerable:!0,get:function(){return t[o]}}),Object.defineProperty(e,n,r)}:function(e,t,o,n){void 0===n&&(n=o),e[n]=t[o]}),a=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||(n=function(e){return n=Object.getOwnPropertyNames||function(e){var t=[];for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[t.length]=o);return t},n(e)},function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var o=n(e),i=0;i<o.length;i++)"default"!==o[i]&&r(t,e,o[i]);return a(t,e),t}),s=this&&this.__spreadArray||function(e,t,o){if(o||2===arguments.length)for(var n,r=0,a=t.length;r<a;r++)!n&&r in t||(n||(n=Array.prototype.slice.call(t,0,r)),n[r]=t[r]);return e.concat(n||Array.prototype.slice.call(t))};Object.defineProperty(t,"__esModule",{value:!0}),t.MatrixGame=void 0;var u=i(o(649)),c=o(156);t.MatrixGame=function(e){var t=e.onGameOver,o=e.className,n=(0,u.useState)((function(){return(0,c.createGame)()}))[0],r=(0,u.useState)(""),a=r[0],i=r[1],l=(0,u.useState)([]),m=l[0],h=l[1],d=(0,u.useState)(0),f=d[0],g=d[1],p=(0,u.useCallback)((function(){if(a.trim()){var e=n.processCommand(a);h((function(t){return s(s([],t,!0),[e],!1)})),i(""),g(n.getScore()),n.isGameOver()&&t&&t(n.getScore())}}),[n,a,t]);return u.default.createElement("div",{className:o},u.default.createElement("div",{className:"game-output",style:{height:"400px",overflowY:"auto",padding:"1rem",backgroundColor:"#000",color:"#0f0",fontFamily:"monospace",marginBottom:"1rem"}},m.map((function(e,t){return u.default.createElement("div",{key:t},u.default.createElement("div",null,e.message),e.options&&u.default.createElement("div",{style:{marginTop:"0.5rem",color:"#0f0"}},e.options.map((function(e,t){return u.default.createElement("div",{key:t},"• ",e)}))))}))),u.default.createElement("div",{style:{display:"flex",gap:"0.5rem"}},u.default.createElement("input",{type:"text",value:a,onChange:function(e){i(e.target.value)},onKeyPress:function(e){"Enter"===e.key&&p()},placeholder:"Enter your command...",style:{flex:1,padding:"0.5rem",backgroundColor:"#000",color:"#0f0",border:"1px solid #0f0",fontFamily:"monospace"}}),u.default.createElement("button",{onClick:p,style:{padding:"0.5rem 1rem",backgroundColor:"#0f0",color:"#000",border:"none",cursor:"pointer"}},"Send")),u.default.createElement("div",{style:{marginTop:"1rem",color:"#0f0"}},"Score: ",f))}}},o={};var n=function e(n){var r=o[n];if(void 0!==r)return r.exports;var a=o[n]={exports:{}};return t[n].call(a.exports,a,a.exports,e),a.exports}(156),r=n.MatrixGame,a=n.TheMatrix,i=n.__esModule,s=n.createGame;export{r as MatrixGame,a as TheMatrix,i as __esModule,s as createGame};