Skip to content

Commit 733d056

Browse files
committed
Game Cache added and tests written and working.
Loader updated. Cache now emits events on add and remove.
1 parent 98a6d28 commit 733d056

9 files changed

Lines changed: 139 additions & 22 deletions

File tree

v3/src/boot/Game.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var CreateRenderer = require('./CreateRenderer');
1717
var StateManager = require('../state/StateManager');
1818
var TextureManager = require('../textures/TextureManager');
1919
var Data = require('../components/Data');
20+
var Cache = require('../cache/Cache');
2021

2122
var Game = function (config)
2223
{
@@ -43,7 +44,7 @@ var Game = function (config)
4344
/**
4445
* @property {Phaser.Cache} cache - Reference to the assets cache.
4546
*/
46-
// this.cache = new Cache();
47+
this.cache = new Cache();
4748

4849
/**
4950
* @property {Phaser.Data} registry - Game wide data store.

v3/src/cache/BaseCache.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
var CacheEntry = require('./CacheEntry');
2+
var Events = require('./events');
3+
var EventDispatcher = require('../events/EventDispatcher');
24

35
var BaseCache = function ()
46
{
57
this.entries = new Map();
8+
9+
this.events = new EventDispatcher();
610
};
711

812
BaseCache.prototype.constructor = BaseCache;
@@ -12,6 +16,8 @@ BaseCache.prototype = {
1216
add: function (key, data)
1317
{
1418
this.entries.set(key, data);
19+
20+
this.events.dispatch(new Events.CACHE_ADD_EVENT(this, key, data));
1521
},
1622

1723
has: function (key)
@@ -26,7 +32,14 @@ BaseCache.prototype = {
2632

2733
remove: function (key)
2834
{
29-
this.entries.delete(key);
35+
var entry = this.get(key);
36+
37+
if (entry)
38+
{
39+
this.entries.delete(key);
40+
41+
this.events.dispatch(new Events.CACHE_REMOVE_EVENT(this, key, entry.data));
42+
}
3043
},
3144

3245
destroy: function ()

v3/src/cache/Cache.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var BaseCache = require('./BaseCache');
2+
3+
var Cache = function ()
4+
{
5+
this.sound = new BaseCache();
6+
this.video = new BaseCache();
7+
this.text = new BaseCache();
8+
this.json = new BaseCache();
9+
this.xml = new BaseCache();
10+
this.physics = new BaseCache();
11+
this.tilemap = new BaseCache();
12+
this.binary = new BaseCache();
13+
this.bitmapFont = new BaseCache();
14+
this.shader = new BaseCache();
15+
16+
this.custom = {};
17+
};
18+
19+
Cache.prototype.constructor = Cache;
20+
21+
Cache.prototype = {
22+
23+
// Add your own custom Cache entry, available under Cache.custom.key
24+
addCustom: function (key)
25+
{
26+
if (!this.custom.hasOwnProperty(key))
27+
{
28+
this.custom[key] = new BaseCache();
29+
30+
return this.custom[key];
31+
}
32+
}
33+
34+
};
35+
36+
module.exports = Cache;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var Event = require('../../events/Event');
2+
3+
var CacheAddEvent = function (cache, key, data)
4+
{
5+
Event.call(this, 'CACHE_ADD_EVENT');
6+
7+
this.cache = cache;
8+
this.key = key;
9+
this.data = data;
10+
};
11+
12+
CacheAddEvent.prototype = Object.create(Event.prototype);
13+
CacheAddEvent.prototype.constructor = CacheAddEvent;
14+
15+
module.exports = CacheAddEvent;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var Event = require('../../events/Event');
2+
3+
var CacheRemoveEvent = function (cache, key, data)
4+
{
5+
Event.call(this, 'CACHE_ADD_EVENT');
6+
7+
this.cache = cache;
8+
this.key = key;
9+
this.data = data;
10+
};
11+
12+
CacheRemoveEvent.prototype = Object.create(Event.prototype);
13+
CacheRemoveEvent.prototype.constructor = CacheRemoveEvent;
14+
15+
module.exports = CacheRemoveEvent;

v3/src/cache/events/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
3+
CACHE_ADD_EVENT: require('./CacheAddEvent'),
4+
CACHE_REMOVE_EVENT: require('./CacheRemoveEvent')
5+
6+
};

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'ebcb3f40-e3cf-11e6-b634-8762f6b557d0'
2+
build: '4ae31710-e708-11e6-93bd-59b161302f59'
33
};
44
module.exports = CHECKSUM;

v3/src/state/Systems.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var Systems = function (state, config)
2626

2727
// CORE SYSTEMS / PROPERTIES
2828

29+
this.cache;
2930
this.textures;
3031

3132
// Reference to State specific managers (Factory, Tweens, Loader, Physics, etc)
@@ -58,6 +59,7 @@ Systems.prototype = {
5859

5960
this.settings = Settings(this.config, this.game.config);
6061

62+
this.cache = this.game.cache;
6163
this.textures = this.game.textures;
6264

6365
// State specific managers (Factory, Tweens, Loader, Physics, etc)
@@ -96,6 +98,7 @@ Systems.prototype = {
9698
// this.state.transform = this.camera.transform;
9799

98100
this.state.state = this.game.state;
101+
this.state.cache = this.game.cache;
99102
this.state.textures = this.game.textures;
100103
},
101104

v3/src/state/systems/Loader.js

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ Loader.prototype.processCallback = function ()
139139
}
140140

141141
// The global Texture Manager
142+
var cache = this.state.sys.cache;
142143
var textures = this.state.sys.textures;
143144

144145
// Process multiatlas groups first
@@ -189,30 +190,57 @@ Loader.prototype.processCallback = function ()
189190

190191
this.storage.each(function (file)
191192
{
192-
if (file.type === 'image')
193+
switch (file.type)
193194
{
194-
textures.addImage(file.key, file.data);
195-
}
196-
else if (file.type === 'atlasjson')
197-
{
198-
var fileA = file.fileA;
199-
var fileB = file.fileB;
195+
case 'image':
196+
textures.addImage(file.key, file.data);
197+
break;
200198

201-
if (fileA.type === 'image')
202-
{
203-
textures.addAtlas(fileA.key, fileA.data, fileB.data);
204-
}
205-
else
206-
{
207-
textures.addAtlas(fileB.key, fileB.data, fileA.data);
208-
}
209-
}
210-
else if (file.type === 'json')
211-
{
212-
// console.dir(file.data);
199+
case 'atlasjson':
200+
var fileA = file.fileA;
201+
var fileB = file.fileB;
202+
203+
if (fileA.type === 'image')
204+
{
205+
textures.addAtlas(fileA.key, fileA.data, fileB.data);
206+
}
207+
else
208+
{
209+
textures.addAtlas(fileB.key, fileB.data, fileA.data);
210+
}
211+
break;
212+
213+
case 'json':
214+
cache.json.add(file.key, file.data);
215+
break;
216+
217+
case 'xml':
218+
cache.xml.add(file.key, file.data);
219+
break;
220+
221+
case 'text':
222+
cache.text.add(file.key, file.data);
223+
break;
224+
225+
case 'binary':
226+
cache.binary.add(file.key, file.data);
227+
break;
228+
229+
case 'sound':
230+
cache.sound.add(file.key, file.data);
231+
break;
232+
233+
case 'glsl':
234+
cache.shader.add(file.key, file.data);
235+
break;
213236
}
214237
});
215238

239+
// this.video = new BaseCache();
240+
// this.physics = new BaseCache();
241+
// this.tilemap = new BaseCache();
242+
// this.bitmapFont = new BaseCache();
243+
216244
this.storage.clear();
217245
};
218246

0 commit comments

Comments
 (0)