Skip to content

Commit

Permalink
Added events emmiter for Loader system
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-hutsul committed Jul 12, 2023
1 parent 1c5ebb7 commit 03d5fca
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build/tiny.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/tiny.mini.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "h5tiny",
"version": "2.2.2",
"version": "2.2.3",
"description": "Fast and tiny JavaScript library for HTML5 game and creatives developing.",
"main": "./build/tiny.js",
"files": [
Expand Down
32 changes: 19 additions & 13 deletions src/systems/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Tiny.Cache = {
};

Tiny.Loader = function (game) {
if (Tiny.EventEmitter) Tiny.EventEmitter.mixin(this);
game.cache = Tiny.Cache;

this.game = game;
Expand All @@ -25,18 +26,18 @@ Tiny.Loader.prototype = {
this.list.push({
src: source,
key: key,
type: "image"
type: 'image'
});
},

spritesheet: function (key, source, arg_1, arg_2, totalFrames, duration) {
var res = {
src: source,
key: key,
type: "spritesheet"
type: 'spritesheet'
};

if (typeof arg_1 == "number") {
if (typeof arg_1 == 'number') {
res.width = arg_1;
res.height = arg_2;
res.total = totalFrames;
Expand All @@ -53,15 +54,18 @@ Tiny.Loader.prototype = {
src: source,
key: key,
data: atlasData,
type: "atlas"
type: 'atlas'
});
},

start: function (callback) {
var game = this.game;
var list = this.list;
var _this = this;
var game = _this.game;
var list = _this.list;

if (list.length == 0) {
var total = list.length;

if (total == 0) {
callback.call(game);
return;
}
Expand All @@ -75,15 +79,17 @@ Tiny.Loader.prototype = {
if (loader) {
loader(resource, loaded);
} else {
console.warn("Cannot find loader for " + resource.type);
console.warn('Cannot find loader for ' + resource.type);
loaded();
}
}

function loaded(resource, data) {
_this.emit('progress', 1 - list.length / total);
if (list.length != 0) {
loadNext();
} else {
_this.emit('complete');
callback.call(game);
}
}
Expand All @@ -97,7 +103,7 @@ Tiny.Loader.atlas = function (resource, cb) {

Tiny.Loader.image(resource, function (resource, image) {
for (var i = 0; i < resource.data.length; i++) {
var uuid = key + "." + resource.data[i].name;
var uuid = key + '.' + resource.data[i].name;
var texture = new Tiny.Texture(image, resource.data[i]);
texture.key = key;

Expand All @@ -119,7 +125,7 @@ Tiny.Loader.spritesheet = function (resource, cb) {
lastFrame = frameData.length - 1;

for (var i = 0; i <= lastFrame; i++) {
uuid = key + "." + i;
uuid = key + '.' + i;

texture = new Tiny.Texture(image, {
name: i,
Expand Down Expand Up @@ -161,7 +167,7 @@ Tiny.Loader.spritesheet = function (resource, cb) {
lastFrame = total - 1;

for (var i = 0; i < total; i++) {
uuid = key + "." + i;
uuid = key + '.' + i;
texture = new Tiny.Texture(image, {
name: i,
x: x,
Expand Down Expand Up @@ -192,7 +198,7 @@ Tiny.Loader.image = function (resource, cb) {

const image = new Image();

image.addEventListener("load", function () {
image.addEventListener('load', function () {
Tiny.Cache.image[resource.key] = image;

cb(resource, image);
Expand All @@ -206,4 +212,4 @@ Tiny.Loader.image = function (resource, cb) {
image.src = resource.src;
};

Tiny.registerSystem("load", Tiny.Loader);
Tiny.registerSystem('load', Tiny.Loader);

0 comments on commit 03d5fca

Please sign in to comment.