Skip to content

Commit d438c93

Browse files
authored
Merge pull request phaserjs#4985 from rexrainbow/datamanager-improvement
Add incData, toggleData methods
2 parents 91bfd7d + 307c2b1 commit d438c93

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

src/data/DataManager.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,72 @@ var DataManager = new Class({
279279
return this;
280280
},
281281

282+
/**
283+
* Increase a value for the given key. If the key doesn't already exist in the Data Manager then it is increased from 0.
284+
*
285+
* When the value is first set, a `setdata` event is emitted.
286+
*
287+
* @method Phaser.Data.DataManager#inc
288+
* @fires Phaser.Data.Events#SET_DATA
289+
* @fires Phaser.Data.Events#CHANGE_DATA
290+
* @fires Phaser.Data.Events#CHANGE_DATA_KEY
291+
* @since 3.23.0
292+
*
293+
* @param {(string|object)} key - The key to increase the value for.
294+
* @param {*} [data] - The value to increase for the given key.
295+
*
296+
* @return {Phaser.Data.DataManager} This DataManager object.
297+
*/
298+
inc: function (key, data)
299+
{
300+
if (this._frozen)
301+
{
302+
return this;
303+
}
304+
305+
if (data === undefined)
306+
{
307+
data = 1;
308+
}
309+
310+
var value = this.get(key);
311+
if (value === undefined)
312+
{
313+
value = 0;
314+
}
315+
316+
this.set(key, (value + data));
317+
318+
return this;
319+
},
320+
321+
/**
322+
* Toggle a boolean value for the given key. If the key doesn't already exist in the Data Manager then it is toggled from false.
323+
*
324+
* When the value is first set, a `setdata` event is emitted.
325+
*
326+
* @method Phaser.Data.DataManager#toggle
327+
* @fires Phaser.Data.Events#SET_DATA
328+
* @fires Phaser.Data.Events#CHANGE_DATA
329+
* @fires Phaser.Data.Events#CHANGE_DATA_KEY
330+
* @since 3.23.0
331+
*
332+
* @param {(string|object)} key - The key to toggle the value for.
333+
*
334+
* @return {Phaser.Data.DataManager} This DataManager object.
335+
*/
336+
toggle: function (key)
337+
{
338+
if (this._frozen)
339+
{
340+
return this;
341+
}
342+
343+
this.set(key, !this.get(key));
344+
345+
return this;
346+
},
347+
282348
/**
283349
* Internal value setter, called automatically by the `set` method.
284350
*

src/gameobjects/GameObject.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,65 @@ var GameObject = new Class({
330330
return this;
331331
},
332332

333+
/**
334+
* Increase a value for the given key within this Game Objects Data Manager. If the key doesn't already exist in the Data Manager then it is increased from 0.
335+
*
336+
* If the Game Object has not been enabled for data (via `setDataEnabled`) then it will be enabled
337+
* before setting the value.
338+
*
339+
* If the key doesn't already exist in the Data Manager then it is created.
340+
*
341+
* When the value is first set, a `setdata` event is emitted from this Game Object.
342+
*
343+
* @method Phaser.GameObjects.GameObject#incData
344+
* @since 3.23.0
345+
*
346+
* @param {(string|object)} key - The key to increase the value for.
347+
* @param {*} [data] - The value to increase for the given key.
348+
*
349+
* @return {this} This GameObject.
350+
*/
351+
incData: function (key, value)
352+
{
353+
if (!this.data)
354+
{
355+
this.data = new DataManager(this);
356+
}
357+
358+
this.data.inc(key, value);
359+
360+
return this;
361+
},
362+
363+
/**
364+
* Toggle a boolean value for the given key within this Game Objects Data Manager. If the key doesn't already exist in the Data Manager then it is toggled from false.
365+
*
366+
* If the Game Object has not been enabled for data (via `setDataEnabled`) then it will be enabled
367+
* before setting the value.
368+
*
369+
* If the key doesn't already exist in the Data Manager then it is created.
370+
*
371+
* When the value is first set, a `setdata` event is emitted from this Game Object.
372+
*
373+
* @method Phaser.GameObjects.GameObject#toggleData
374+
* @since 3.23.0
375+
*
376+
* @param {(string|object)} key - The key to toggle the value for.
377+
*
378+
* @return {this} This GameObject.
379+
*/
380+
toggleData: function (key)
381+
{
382+
if (!this.data)
383+
{
384+
this.data = new DataManager(this);
385+
}
386+
387+
this.data.toggle(key);
388+
389+
return this;
390+
},
391+
333392
/**
334393
* Retrieves the value for the given key in this Game Objects Data Manager, or undefined if it doesn't exist.
335394
*

0 commit comments

Comments
 (0)