[Solved] Zones in an external object
Posted: 14 August 2020, 15:33
Inspired by the thread about making effective use of Modules to create classes and organize the project on the PHP side, I was wondering if there is an established way to apply similar OOP principles to the client side, as the js file can become a monolith as well.
I tried creating an object that would manage tokens and movement animations on the board based on Santorini's approach to the client side Board object. An object is defined in an external file and exported with the following lines:
./modules/scripts/ObjectManager.js
Then the js file is included in the tpl file:
./gamename_gamename.tpl
Then the object is constructed in setup
./gamename.js
This seems to be mostly working, except the implementation of Zone methods (and probably many other dependent methods) is broken because "this" now refers to the ObjectManager instead of the "game" object you're normally defining Zones in. For example, calling placeInZone on a zone created in ObjectManager produces the error: "TypeError: this.page.attachToNewParent is not a function"
I can add these lines to the object containing the zone's constructor, where this.game is a reference to "this" that was passed in to the ObjectManager constructor:
./modules/scripts/ObjectManager.js
This makes placeInZone "work", but I'm sure this isn't the right way to go about it. Is there a simpler and more comprehensive way to make sure that methods within BGA client side objects defined in an external file have the correct scope?
I tried creating an object that would manage tokens and movement animations on the board based on Santorini's approach to the client side Board object. An object is defined in an external file and exported with the following lines:
./modules/scripts/ObjectManager.js
Code: Select all
...
window.ObjectManager = ObjectManager;
export { ObjectManager };./gamename_gamename.tpl
Code: Select all
...
<script type="text/javascript">
const URL = dojoConfig.packages.reduce((r,p) => p.name == "bgagame" ? p.location : r, null);
document.write('<script src="' + URL + '/modules/scripts/ObjectManager.js" type="module"><\/script>');
..../gamename.js
Code: Select all
this.objects = new ObjectManager( this );I can add these lines to the object containing the zone's constructor, where this.game is a reference to "this" that was passed in to the ObjectManager constructor:
./modules/scripts/ObjectManager.js
Code: Select all
this.attachToNewParent = dojo.hitch( this.game, this.game.attachToNewParent );
this.transformSlideAnimTo3d = dojo.hitch( this.game, this.game.transformSlideAnimTo3d );