Spaces:
Running on Zero
Running on Zero
Download playcanvas/storyManager.js from KwabsHug/GameConfigIdea: direct link, hf CLI and curl.
- Browser
- Download file 4.09 kB
-
https://huggingface.co/spaces/KwabsHug/GameConfigIdea/resolve/main/playcanvas/storyManager.js
- Command line
-
hf download hf://spaces/KwabsHug/GameConfigIdea/playcanvas/storyManager.js
-
curl -L -o storyManager.js https://huggingface.co/spaces/KwabsHug/GameConfigIdea/resolve/main/playcanvas/storyManager.js
4.09 kB
| // storyManager.js | |
| var StoryManager = pc.createScript('storyManager'); | |
| StoryManager.attributes.add('storyConfig', { | |
| type: 'asset', | |
| assetType: 'json', | |
| title: 'Story Config' | |
| }); | |
| // Initialize function | |
| StoryManager.prototype.initialize = function() { | |
| this.currentState = null; | |
| this.storyData = null; | |
| console.log('Initializing StoryManager'); | |
| if (this.storyConfig) { | |
| // Primary Path: Asset assigned via script attributes | |
| console.log('Loading storyConfig asset via script attributes:', this.storyConfig); | |
| this.app.assets.load(this.storyConfig); | |
| this.storyConfig.ready(function () { | |
| if (this.storyConfig.resource) { | |
| console.log('storyConfig.json loaded successfully via script attributes:', this.storyConfig.resource); | |
| this.storyData = this.storyConfig.resource; | |
| this.startStory('arrival'); // Starting state | |
| } else { | |
| console.error('Failed to load storyConfig.json via script attributes.'); | |
| } | |
| }.bind(this)); | |
| } else { | |
| // Fallback Path: Programmatically find and load 'storyConfig.json' | |
| console.warn('Story Config asset is not assigned via script attributes. Attempting programmatic loading.'); | |
| var assetName = 'storyConfig.json'; // Ensure this matches exactly with the asset name in PlayCanvas | |
| var asset = this.app.assets.find(assetName); | |
| if (asset) { | |
| console.log('Found asset programmatically:', assetName); | |
| if (!asset.loaded) { | |
| console.log('Asset not loaded. Initiating load...'); | |
| this.app.assets.load(asset); | |
| } else { | |
| console.log('Asset already loaded.'); | |
| } | |
| asset.ready(function () { | |
| if (asset.resource) { | |
| console.log('storyConfig.json loaded successfully programmatically:', asset.resource); | |
| this.storyData = asset.resource; | |
| this.startStory('arrival'); // Starting state | |
| } else { | |
| console.error('storyConfig.json resource is undefined after loading.'); | |
| } | |
| }.bind(this)); | |
| } else { | |
| console.error('storyConfig.json not found in assets programmatically.'); | |
| // Optionally, you can implement further fallback mechanisms or alert the user | |
| } | |
| } | |
| // Bind events regardless of asset loading method | |
| this.bindEvents(); | |
| }; | |
| // Function to start or transition to a new state | |
| StoryManager.prototype.startStory = function(stateKey) { | |
| console.log('Starting story state:', stateKey); | |
| if (!this.storyData) { | |
| console.error('Story data is not loaded yet.'); | |
| return; | |
| } | |
| var state = this.storyData['singingCompetition'][stateKey]; | |
| if (!state) { | |
| console.error('State not found in storyData:', stateKey); | |
| return; | |
| } | |
| this.currentState = stateKey; | |
| this.displayState(state); | |
| }; | |
| // Function to display the current state | |
| StoryManager.prototype.displayState = function(state) { | |
| console.log('Displaying state:', this.currentState); | |
| // Emit an event to update the UI | |
| this.app.fire('story:display', { | |
| description: state.description, | |
| choices: state.choices | |
| }); | |
| }; | |
| // Function to handle choices | |
| StoryManager.prototype.makeChoice = function(choice) { | |
| console.log('Making choice:', choice); | |
| var current = this.storyData['singingCompetition'][this.currentState]; | |
| var nextStateKey = current.transitions[choice]; | |
| if (nextStateKey) { | |
| this.startStory(nextStateKey); | |
| } else { | |
| console.error('Transition not found for choice:', choice); | |
| } | |
| }; | |
| // Listen for choice selections from UI | |
| StoryManager.prototype.onChoiceSelected = function(choice) { | |
| this.makeChoice(choice); | |
| }; | |
| // Bind the choice selection event | |
| StoryManager.prototype.bindEvents = function() { | |
| console.log('Binding UI choice selection event'); | |
| this.app.on('ui:choiceSelected', this.onChoiceSelected, this); | |
| }; |