Created
November 8, 2016 16:51
-
-
Save KinoAR/a8f47c9f4e85e04cc4b9938371bf7a05 to your computer and use it in GitHub Desktop.
RPGMakerMV pattern for observing changes to player inventory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//============================================================================= | |
// Observer Pattern | |
//============================================================================= | |
//This creates an observer in RPGMaker MV | |
//This is an example of checking whether an item exists on an observer | |
(function() { | |
var parameters = PluginManager.parameters("ObserverPattern"); | |
function Setup() { | |
'use strict'; | |
//Static class for monitoring items | |
class ItemObserver { | |
static startObserver() { | |
this._itemList = []; | |
this.update(); | |
} | |
static update() { | |
this.monitorUpdates(); | |
this.requestUpdate(); | |
} | |
static requestUpdate() { | |
requestAnimationFrame(this.update.bind(this)); | |
} | |
static monitorUpdates() { | |
if(!this.listMatch()) { | |
console.log("Item List Updated"); | |
this.itemList = $gameParty.allItems(); | |
} | |
} | |
static listMatch() { | |
if($gameParty !== null) { | |
let items = $gameParty.allItems(); | |
for(let i = 0; i < items.length; i++) { | |
if(this.itemList[i] === undefined || this.itemList[i] !== items[i]) | |
return false; | |
} | |
return true; | |
} | |
return true; | |
} | |
static set itemList(itemList) { | |
this._itemList = itemList; | |
} | |
static get itemList() { | |
return this._itemList; | |
} | |
} | |
//Start Observer Update Method | |
ItemObserver.startObserver(); | |
} | |
Setup(); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment