From b3a37d44e9e4b820bd980f8b46be3d70bede70de Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sun, 18 May 2014 15:18:45 +0200 Subject: [PATCH] Fixes to preloader, fixes to drawing library, and changed around code structure for gemswap --- compiled/radium.js | 381 +++++++++++++++++---------------- gemswap/core.coffee | 68 +++--- gemswap/gemswap.js | 84 ++++---- radium/engine.coffee | 13 +- radium/engine.draw.coffee | 24 +-- radium/resource-manager.coffee | 25 ++- radium/scene.coffee | 2 +- 7 files changed, 315 insertions(+), 282 deletions(-) diff --git a/compiled/radium.js b/compiled/radium.js index 45f0f30..d1206a6 100644 --- a/compiled/radium.js +++ b/compiled/radium.js @@ -21,6 +21,8 @@ Engine = (function() { this.createSound = __bind(this.createSound, this); this.createObject = __bind(this.createObject, this); this.createScene = __bind(this.createScene, this); + this.switchInitialScene = __bind(this.switchInitialScene, this); + this.switchPreloadScene = __bind(this.switchPreloadScene, this); this.setPreloadScene = __bind(this.setPreloadScene, this); this.setInitialScene = __bind(this.setInitialScene, this); this.skipTimers = __bind(this.skipTimers, this); @@ -46,6 +48,8 @@ Engine = (function() { this.named_timers = {}; this.unnamed_timers = []; this.ease.engine = this; + this.draw.engine = this; + this.resource_manager.engine = this; } Engine.prototype.addCanvas = function(canvas, label) { @@ -78,7 +82,6 @@ Engine = (function() { }; Engine.prototype.start = function() { - this.initial_scene.addTargetSurface(this.canvases[""]); return this.loop(); }; @@ -178,6 +181,18 @@ Engine = (function() { return this.preload_scene = scene; }; + Engine.prototype.switchPreloadScene = function() { + return this.preload_scene.addTargetSurface(this.canvases[""]); + }; + + Engine.prototype.switchInitialScene = function() { + var _ref; + if ((_ref = this.preload_scene) != null) { + _ref.removeTargetSurface(this.canvases[""]); + } + return this.initial_scene.addTargetSurface(this.canvases[""]); + }; + Engine.prototype.createScene = function(name) { var scene; scene = new Scene(this, name); @@ -258,190 +273,168 @@ Engine = (function() { })(); Engine.prototype.draw = { - _startPath: (function(_this) { - return function(surface, options) { - var _ref; - surface = _this.getSurface(surface); - if ((_ref = !options._is_text) != null ? _ref : false) { - surface.beginPath(); - } - return surface; - }; - })(this), - _finishPath: (function(_this) { - return function(surface, options) { - var _ref, _ref1, _ref10, _ref11, _ref12, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9; - if ((_ref = options.stroke) != null ? _ref : true) { - surface.lineWidth = (_ref1 = (_ref2 = options.lineWidth) != null ? _ref2 : (_ref3 = options.pen) != null ? _ref3.lineWidth : void 0) != null ? _ref1 : 1; - surface.strokeStyle = (_ref4 = (_ref5 = options.lineColor) != null ? _ref5 : (_ref6 = options.pen) != null ? _ref6.lineColor : void 0) != null ? _ref4 : "black"; - if ((_ref7 = options._is_text) != null ? _ref7 : false) { - surface.strokeText(options.text, options.x, options.y); - } else { - surface.stroke(); - } - } - if ((_ref8 = options.fill) != null ? _ref8 : false) { - surface.fillStyle = (_ref9 = (_ref10 = options.fillColor) != null ? _ref10 : (_ref11 = options.pen) != null ? _ref11.fillColor : void 0) != null ? _ref9 : "white"; - if ((_ref12 = options._is_text) != null ? _ref12 : false) { - return surface.fillText(options.text, options.x, options.y); - } else { - return surface.fill(); - } - } - }; - })(this), - _getTextWidth: (function(_this) { - return function(surface, text, options) { - var width; - _this._applyTextContext(surface, options); - width = surface.measureText(text).width; - surface.restore(); - return width; - }; - })(this), - _applyTextContext: (function(_this) { - return function(surface, options) { - var font_family, font_size, font_style, font_weight, scale, _ref, _ref1, _ref2, _ref3, _ref4, _ref5; - font_family = (_ref = options.font) != null ? _ref : "sans-serif"; - font_size = (_ref1 = options.size) != null ? _ref1 : 16; - font_weight = (_ref2 = options.weight) != null ? _ref2 : "normal"; - font_style = (_ref3 = options.style) != null ? _ref3 : "normal"; - scale = (_ref4 = options.scale) != null ? _ref4 : 1; - surface.save(); - surface.font = "" + font_weight + " " + font_style + " " + font_size + "px '" + font_family + "'"; - surface.globalAlpha = (_ref5 = options.alpha) != null ? _ref5 : 1; - return surface.scale(scale, scale); - }; - })(this), - line: (function(_this) { - return function(x1, y1, x2, y2, options, surface) { - if (options == null) { - options = {}; - } - if (surface == null) { - surface = ""; - } - surface = _this._startPath(surface, options); - surface.moveTo(x1, y1); - surface.lineTo(x2, y2); - return _this._finishPath(surface, options); - }; - })(this), - rectangle: (function(_this) { - return function(x1, y1, x2, y2, options, surface) { - if (options == null) { - options = {}; - } - if (surface == null) { - surface = ""; - } - surface = _this._startPath(surface, options); - surface.rect(x1, y1, x2 - x1, y2 - y1); - return _this._finishPath(surface, options); - }; - })(this), - boxEllipse: (function(_this) { - return function(x1, y1, x2, y2, options, surface) { - var rx, ry, x, y; - if (options == null) { - options = {}; - } - if (surface == null) { - surface = ""; - } - x = (x1 + x2) / 2; - y = (y1 + y2) / 2; - rx = (x2 - x1) / 2; - ry = (y2 - y1) / 2; - return _this.radiusEllipse(x, y, rx, ry, options, surface); - }; - })(this), - radiusEllipse: (function(_this) { - return function(x, y, rx, ry, options, surface) { - var i, step, _i, _ref, _ref1; - if (options == null) { - options = {}; - } - if (surface == null) { - surface = ""; - } - surface = _this._startPath(surface, options); - step = (_ref = options.step) != null ? _ref : 0.1; - if (rx === ry) { - surface.arc(x, y, rx, 0, 2 * Math.PI, false); + _startPath: function(surface, options) { + var _ref; + surface = this.engine.getSurface(surface); + if ((_ref = !options._is_text) != null ? _ref : false) { + surface.beginPath(); + } + return surface; + }, + _finishPath: function(surface, options) { + var _ref, _ref1, _ref10, _ref11, _ref12, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9; + if ((_ref = options.stroke) != null ? _ref : true) { + surface.lineWidth = (_ref1 = (_ref2 = options.lineWidth) != null ? _ref2 : (_ref3 = options.pen) != null ? _ref3.lineWidth : void 0) != null ? _ref1 : 1; + surface.strokeStyle = (_ref4 = (_ref5 = options.lineColor) != null ? _ref5 : (_ref6 = options.pen) != null ? _ref6.lineColor : void 0) != null ? _ref4 : "black"; + if ((_ref7 = options._is_text) != null ? _ref7 : false) { + surface.strokeText(options.text, options.x, options.y); } else { - surface.moveTo(x + rx, y); - for (i = _i = 0, _ref1 = Math.PI * 2 + step; 0 <= _ref1 ? _i <= _ref1 : _i >= _ref1; i = 0 <= _ref1 ? ++_i : --_i) { - surface.lineTo(x + (Math.cos(i) * rx), y + (Math.sin(i) * ry)); - } - } - return _this._finishPath(surface, options); - }; - })(this), - boxPolygon: (function(_this) { - return function(x1, y1, x2, y2, sides, options, surface) { - if (options == null) { - options = {}; - } - if (surface == null) { - surface = ""; - } - return pass; - }; - })(this), - radiusPolygon: (function(_this) { - return function(x, y, r, sides, options, surface) { - if (options == null) { - options = {}; - } - if (surface == null) { - surface = ""; - } - return pass; - }; - })(this), - text: (function(_this) { - return function(x, y, text, options, surface) { - var text_width; - if (options == null) { - options = {}; - } - if (surface == null) { - surface = ""; - } - if (options.alignment == null) { - options.alignment = "left"; - } - if (options.scale == null) { - options.scale = 1; + surface.stroke(); } - options._is_text = true; - options.text = text; - options.y = y; - if (options.fill == null) { - options.fill = true; - } - if (options.fillColor == null) { - options.fillColor = "black"; + } + if ((_ref8 = options.fill) != null ? _ref8 : false) { + surface.fillStyle = (_ref9 = (_ref10 = options.fillColor) != null ? _ref10 : (_ref11 = options.pen) != null ? _ref11.fillColor : void 0) != null ? _ref9 : "white"; + if ((_ref12 = options._is_text) != null ? _ref12 : false) { + return surface.fillText(options.text, options.x, options.y); + } else { + return surface.fill(); } - if (options.stroke == null) { - options.stroke = false; + } + }, + _getTextWidth: function(surface, text, options) { + var width; + this._applyTextContext(surface, options); + width = surface.measureText(text).width; + surface.restore(); + return width; + }, + _applyTextContext: function(surface, options) { + var font_family, font_size, font_style, font_weight, scale, _ref, _ref1, _ref2, _ref3, _ref4, _ref5; + font_family = (_ref = options.font) != null ? _ref : "sans-serif"; + font_size = (_ref1 = options.size) != null ? _ref1 : 16; + font_weight = (_ref2 = options.weight) != null ? _ref2 : "normal"; + font_style = (_ref3 = options.style) != null ? _ref3 : "normal"; + scale = (_ref4 = options.scale) != null ? _ref4 : 1; + surface.save(); + surface.font = "" + font_weight + " " + font_style + " " + font_size + "px '" + font_family + "'"; + surface.globalAlpha = (_ref5 = options.alpha) != null ? _ref5 : 1; + return surface.scale(scale, scale); + }, + line: function(x1, y1, x2, y2, options, surface) { + if (options == null) { + options = {}; + } + if (surface == null) { + surface = ""; + } + surface = this._startPath(surface, options); + surface.moveTo(x1, y1); + surface.lineTo(x2, y2); + return this._finishPath(surface, options); + }, + rectangle: function(x1, y1, x2, y2, options, surface) { + if (options == null) { + options = {}; + } + if (surface == null) { + surface = ""; + } + surface = this._startPath(surface, options); + surface.rect(x1, y1, x2 - x1, y2 - y1); + return this._finishPath(surface, options); + }, + boxEllipse: function(x1, y1, x2, y2, options, surface) { + var rx, ry, x, y; + if (options == null) { + options = {}; + } + if (surface == null) { + surface = ""; + } + x = (x1 + x2) / 2; + y = (y1 + y2) / 2; + rx = (x2 - x1) / 2; + ry = (y2 - y1) / 2; + return this.radiusEllipse(x, y, rx, ry, options, surface); + }, + radiusEllipse: function(x, y, rx, ry, options, surface) { + var i, step, _i, _ref, _ref1; + if (options == null) { + options = {}; + } + if (surface == null) { + surface = ""; + } + surface = this._startPath(surface, options); + step = (_ref = options.step) != null ? _ref : 0.1; + if (rx === ry) { + surface.arc(x, y, rx, 0, 2 * Math.PI, false); + } else { + surface.moveTo(x + rx, y); + for (i = _i = 0, _ref1 = Math.PI * 2 + step; 0 <= _ref1 ? _i <= _ref1 : _i >= _ref1; i = 0 <= _ref1 ? ++_i : --_i) { + surface.lineTo(x + (Math.cos(i) * rx), y + (Math.sin(i) * ry)); } - if (alignment === "left") { - options.x = x; - } else { - text_width = _this._getTextWidth(text, options); - if (alignment === "center") { - options.x = x - ((text_width / 2) * scale * scale); - } else if (alignment === "right") { - options.x = x - text_width; - } + } + return this._finishPath(surface, options); + }, + boxPolygon: function(x1, y1, x2, y2, sides, options, surface) { + if (options == null) { + options = {}; + } + if (surface == null) { + surface = ""; + } + return pass; + }, + radiusPolygon: function(x, y, r, sides, options, surface) { + if (options == null) { + options = {}; + } + if (surface == null) { + surface = ""; + } + return pass; + }, + text: function(x, y, text, options, surface) { + var text_width; + if (options == null) { + options = {}; + } + if (surface == null) { + surface = ""; + } + if (options.alignment == null) { + options.alignment = "left"; + } + if (options.scale == null) { + options.scale = 1; + } + options._is_text = true; + options.text = text; + options.y = y; + if (options.fill == null) { + options.fill = true; + } + if (options.fillColor == null) { + options.fillColor = "black"; + } + if (options.stroke == null) { + options.stroke = false; + } + if (alignment === "left") { + options.x = x; + } else { + text_width = this._getTextWidth(text, options); + if (alignment === "center") { + options.x = x - ((text_width / 2) * scale * scale); + } else if (alignment === "right") { + options.x = x - text_width; } - _this._startPath(surface, options); - _this._finishPath(surface, options); - return surface.restore(); - }; - })(this) + } + this._startPath(surface, options); + this._finishPath(surface, options); + return surface.restore(); + } }; var Ease, @@ -1335,6 +1328,7 @@ ResourceManager = (function() { this.load = __bind(this.load, this); this.prepare = __bind(this.prepare, this); this.doPreload = __bind(this.doPreload, this); + this.handleComplete = __bind(this.handleComplete, this); this.handleFinishedFile = __bind(this.handleFinishedFile, this); this.updateProgress = __bind(this.updateProgress, this); this.getImage = __bind(this.getImage, this); @@ -1479,7 +1473,8 @@ ResourceManager = (function() { }; ResourceManager.prototype.updateProgress = function(event) { - return this.file_progress = event.progress; + console.log(event); + return this.file_progress = event.loaded / event.total; }; ResourceManager.prototype.handleFinishedFile = function(event) { @@ -1502,7 +1497,18 @@ ResourceManager = (function() { } }; - ResourceManager.prototype.doPreload = function(stage, progress_callback, finished_callback) { + ResourceManager.prototype.handleComplete = function(event) { + var stage, _ref, _ref1; + stage = this.current_stage; + this.finished_callback(); + if (stage === 1) { + return (_ref = this.engine) != null ? _ref.switchPreloadScene() : void 0; + } else if (stage === 2) { + return (_ref1 = this.engine) != null ? _ref1.switchInitialScene() : void 0; + } + }; + + ResourceManager.prototype.doPreload = function(stage, progress_callback) { var data_file, data_files, image, images, script, scripts, sound, sounds, _i, _j, _k, _l, _len, _len1, _len2, _len3; this.current_stage = stage; if (stage === 1) { @@ -1549,9 +1555,9 @@ ResourceManager = (function() { type: createjs.LoadQueue.JSON }); } - this.queue.on("progress", progress_callback); + this.queue.on("fileprogress", progress_callback); this.queue.on("fileload", this.handleFinishedFile); - this.queue.on("complete", finished_callback); + this.queue.on("complete", this.handleComplete); return this.queue.load(); }; @@ -1559,14 +1565,16 @@ ResourceManager = (function() { if (finished_callback == null) { finished_callback = (function() {}); } - return this.doPreload(1, (function() {}), finished_callback.bind(this)); + this.finished_callback = finished_callback.bind(this); + return this.doPreload(1, (function() {})); }; ResourceManager.prototype.load = function(finished_callback) { if (finished_callback == null) { finished_callback = (function() {}); } - return this.doPreload(2, this.updateProgress, finished_callback.bind(this)); + this.finished_callback = finished_callback.bind(this); + return this.doPreload(2, this.updateProgress); }; return ResourceManager; @@ -1590,6 +1598,7 @@ Scene = (function() { this.removeTargetSurface = __bind(this.removeTargetSurface, this); this.handleClick = __bind(this.handleClick, this); this.addTargetSurface = __bind(this.addTargetSurface, this); + this.callEvent = __bind(this.callEvent, this); this.reset = __bind(this.reset, this); this.reset(); } diff --git a/gemswap/core.coffee b/gemswap/core.coffee index d84cfe3..89164e7 100644 --- a/gemswap/core.coffee +++ b/gemswap/core.coffee @@ -23,20 +23,40 @@ $(-> "images/blue_shimmer.png" ]) - ### - manager.addSounds([ - "sfx/match.wav" - "sfx/swap.wav" - ]) - ### + ###manager.addSounds([ + "music/music.wav" + ])### + + # Set up the engine + engine.addCanvas($("#gamecanvas")); + engine.setPreloadScene(engine.createScene("loader")) + engine.setInitialScene(engine.createScene("main")) manager.prepare -> - # Set up the engine - engine.addCanvas($("#gamecanvas")); - - engine.setPreloadScene(engine.createScene("loader")) - engine.setInitialScene(engine.createScene("main")) + # Let's go! + engine.start() + # Scene configuration + engine.getScene("loader").onLoad = -> + # Loading screen + @createInstance("loader", 0, 0) + + engine.getScene("main").onLoad = -> + # Actual game initialization + @createInstance("cursor", 0, 0) + + for x in [10 .. 728] by 80 + for y in [10 .. 550] by 80 + @createInstance("diamond", x, y) + + # Loader object + loader = engine.createObject("loader") + + loader.onStep = -> true + loader.onDraw = -> + engine.draw.rectangle(0, 0, 800 * manager.file_progress, 64, {fillColor: "#CCCCCC", fill: true}) + engine.draw.rectangle(0, 64, 800 * manager.total_progress, 128, {fillColor: "#AAAAAA", fill: true}) + manager.load -> # Game asset initialization... engine.createSprites({ @@ -53,18 +73,14 @@ $(-> }) # Object definitions - loader = engine.createObject("loader") - - loader.onStep = -> true - loader.onDraw = -> - engine.draw.rectangle(0, 0, 800 * manager.file_progress, 64, {fillColor: "#CCCCCC"}) - engine.draw.rectangle(0, 64, 800 * manager.total_progress, 128, {fillColor: "#AAAAAA"}) - cursor = engine.createObject("cursor") cursor.sprite = engine.getSprite("cursor") cursor.onStep = -> $("#debug").html("Mouse coords: #{@scene.mouse_x} / #{@scene.mouse_y}
Frameskip: #{@engine.frameskip}") + + cursor.onDraw = -> + #engine.draw.rectangle(20, 20, 400, 400, {fillColor: "#FFFFFF", fill: true}) diamond = engine.createObject("diamond") diamond.sprite = engine.getSprite("diamond") @@ -96,20 +112,4 @@ $(-> cursor.x = @engine.ease.quadInOut(cursor.x, @x - 9, 8) cursor.y = @engine.ease.quadInOut(cursor.y, @y - 9, 8) - # Scene configuration - engine.getScene("loader").onLoad = -> - # Loading screen - @createInstance(loader, 0, 0) - - engine.getScene("main").onLoad = -> - # Actual game initialization - @createInstance(cursor, 0, 0) - - for x in [10 .. 728] by 80 - for y in [10 .. 550] by 80 - @createInstance(diamond, x, y) - - # Let's go! - engine.start() - ) diff --git a/gemswap/gemswap.js b/gemswap/gemswap.js index 074ff71..76fd70e 100644 --- a/gemswap/gemswap.js +++ b/gemswap/gemswap.js @@ -12,18 +12,51 @@ */ manager.addImages(["images/cursor.png", "images/diamond.png", "images/diamond_inverted.png", "images/diamond_shimmer.png", "images/yellow.png", "images/yellow_inverted.png", "images/yellow_shimmer.png", "images/blue.png", "images/blue_inverted.png", "images/blue_shimmer.png"]); - /* - manager.addSounds([ - "sfx/match.wav" - "sfx/swap.wav" + /*manager.addSounds([ + "music/music.wav" ]) */ + engine.addCanvas($("#gamecanvas")); + engine.setPreloadScene(engine.createScene("loader")); + engine.setInitialScene(engine.createScene("main")); return manager.prepare(function() { - engine.addCanvas($("#gamecanvas")); - engine.setPreloadScene(engine.createScene("loader")); - engine.setInitialScene(engine.createScene("main")); + var loader; + engine.start(); + engine.getScene("loader").onLoad = function() { + return this.createInstance("loader", 0, 0); + }; + engine.getScene("main").onLoad = function() { + var x, y, _i, _results; + this.createInstance("cursor", 0, 0); + _results = []; + for (x = _i = 10; _i <= 728; x = _i += 80) { + _results.push((function() { + var _j, _results1; + _results1 = []; + for (y = _j = 10; _j <= 550; y = _j += 80) { + _results1.push(this.createInstance("diamond", x, y)); + } + return _results1; + }).call(this)); + } + return _results; + }; + loader = engine.createObject("loader"); + loader.onStep = function() { + return true; + }; + loader.onDraw = function() { + engine.draw.rectangle(0, 0, 800 * manager.file_progress, 64, { + fillColor: "#CCCCCC", + fill: true + }); + return engine.draw.rectangle(0, 64, 800 * manager.total_progress, 128, { + fillColor: "#AAAAAA", + fill: true + }); + }; return manager.load(function() { - var cursor, diamond, loader; + var cursor, diamond; engine.createSprites({ "cursor": "images/cursor.png", "diamond": "images/diamond.png", @@ -36,23 +69,12 @@ "blue_inverted": "images/blue_inverted.png", "blue_shimmer": "images/blue_shimmer.png" }); - loader = engine.createObject("loader"); - loader.onStep = function() { - return true; - }; - loader.onDraw = function() { - engine.draw.rectangle(0, 0, 800 * manager.file_progress, 64, { - fillColor: "#CCCCCC" - }); - return engine.draw.rectangle(0, 64, 800 * manager.total_progress, 128, { - fillColor: "#AAAAAA" - }); - }; cursor = engine.createObject("cursor"); cursor.sprite = engine.getSprite("cursor"); cursor.onStep = function() { return $("#debug").html("Mouse coords: " + this.scene.mouse_x + " / " + this.scene.mouse_y + "
Frameskip: " + this.engine.frameskip); }; + cursor.onDraw = function() {}; diamond = engine.createObject("diamond"); diamond.sprite = engine.getSprite("diamond"); diamond.draw_sprite = false; @@ -73,32 +95,12 @@ alpha: this.shimmer_value }); }; - diamond.onMouseOver = function() { + return diamond.onMouseOver = function() { this.fade_value = this.engine.ease.quadInOut(0.3, 0.7, 10, this.engine.ease.bounceOut(0.7, 0.3, 65)); cursor = this.engine.getObject("cursor").getInstances()[0]; cursor.x = this.engine.ease.quadInOut(cursor.x, this.x - 9, 8); return cursor.y = this.engine.ease.quadInOut(cursor.y, this.y - 9, 8); }; - engine.getScene("loader").onLoad = function() { - return this.createInstance(loader, 0, 0); - }; - engine.getScene("main").onLoad = function() { - var x, y, _i, _results; - this.createInstance(cursor, 0, 0); - _results = []; - for (x = _i = 10; _i <= 728; x = _i += 80) { - _results.push((function() { - var _j, _results1; - _results1 = []; - for (y = _j = 10; _j <= 550; y = _j += 80) { - _results1.push(this.createInstance(diamond, x, y)); - } - return _results1; - }).call(this)); - } - return _results; - }; - return engine.start(); }); }); }); diff --git a/radium/engine.coffee b/radium/engine.coffee index 112f1de..da2f134 100644 --- a/radium/engine.coffee +++ b/radium/engine.coffee @@ -21,6 +21,9 @@ class Engine # FUTURE: Iterate over easing methods and create an engine-bound version # in the local engine object, overriding the prototype methods? @ease.engine = this + @draw.engine = this + + @resource_manager.engine = this addCanvas: (canvas, label = "") => @canvases[label] = util.unpackElement(canvas) @@ -43,7 +46,6 @@ class Engine canvas.style.height = "#{h}px" start: () => - @initial_scene.addTargetSurface(@canvases[""]) @loop() loop: () => @@ -52,7 +54,7 @@ class Engine iteration: () => # Calculation of next frame and frameskip collection check frame_interval = (1000 / @fps) - + current_frame = Date.now() next_frame = current_frame + frame_interval @@ -103,6 +105,13 @@ class Engine setPreloadScene: (scene) => @preload_scene = scene + switchPreloadScene: => + @preload_scene.addTargetSurface(@canvases[""]) + + switchInitialScene: => + @preload_scene?.removeTargetSurface(@canvases[""]) + @initial_scene.addTargetSurface(@canvases[""]) + createScene: (name) => scene = new Scene(this, name) @initial_scene ?= scene diff --git a/radium/engine.draw.coffee b/radium/engine.draw.coffee index 0f2a400..2ccbeb4 100644 --- a/radium/engine.draw.coffee +++ b/radium/engine.draw.coffee @@ -1,13 +1,13 @@ Engine::draw = - _startPath: (surface, options) => - surface = @getSurface(surface) + _startPath: (surface, options) -> + surface = @engine.getSurface(surface) if not options._is_text ? false surface.beginPath() return surface - _finishPath: (surface, options) => + _finishPath: (surface, options) -> if options.stroke ? true surface.lineWidth = options.lineWidth ? options.pen?.lineWidth ? 1 surface.strokeStyle = options.lineColor ? options.pen?.lineColor ? "black" @@ -25,13 +25,13 @@ Engine::draw = else surface.fill() - _getTextWidth: (surface, text, options) => + _getTextWidth: (surface, text, options) -> @_applyTextContext(surface, options) width = surface.measureText(text).width surface.restore() return width - _applyTextContext: (surface, options) => + _applyTextContext: (surface, options) -> font_family = options.font ? "sans-serif" font_size = options.size ? 16 font_weight = options.weight ? "normal" @@ -43,25 +43,25 @@ Engine::draw = surface.globalAlpha = options.alpha ? 1 surface.scale(scale, scale) - line: (x1, y1, x2, y2, options = {}, surface = "") => + line: (x1, y1, x2, y2, options = {}, surface = "") -> surface = @_startPath(surface, options) surface.moveTo(x1, y1) surface.lineTo(x2, y2) @_finishPath(surface, options) - rectangle: (x1, y1, x2, y2, options = {}, surface = "") => + rectangle: (x1, y1, x2, y2, options = {}, surface = "") -> surface = @_startPath(surface, options) surface.rect(x1, y1, x2 - x1, y2 - y1) @_finishPath(surface, options) - boxEllipse: (x1, y1, x2, y2, options = {}, surface = "") => + boxEllipse: (x1, y1, x2, y2, options = {}, surface = "") -> x = (x1 + x2) / 2; y = (y1 + y2) / 2; rx = (x2 - x1) / 2; ry = (y2 - y1) / 2; @radiusEllipse(x, y, rx, ry, options, surface) - radiusEllipse: (x, y, rx, ry, options = {}, surface = "") => + radiusEllipse: (x, y, rx, ry, options = {}, surface = "") -> surface = @_startPath(surface, options) step = options.step ? 0.1 @@ -76,13 +76,13 @@ Engine::draw = @_finishPath(surface, options) - boxPolygon: (x1, y1, x2, y2, sides, options = {}, surface = "") => + boxPolygon: (x1, y1, x2, y2, sides, options = {}, surface = "") -> pass # TODO - radiusPolygon: (x, y, r, sides, options = {}, surface = "") => + radiusPolygon: (x, y, r, sides, options = {}, surface = "") -> pass # TODO - text: (x, y, text, options = {}, surface = "") => + text: (x, y, text, options = {}, surface = "") -> # Defaults options.alignment ?= "left" options.scale ?= 1 diff --git a/radium/resource-manager.coffee b/radium/resource-manager.coffee index 5b5a6ce..0cecb12 100644 --- a/radium/resource-manager.coffee +++ b/radium/resource-manager.coffee @@ -66,7 +66,8 @@ class ResourceManager return @resource_objects.images[@joinPath(path)] updateProgress: (event) => - @file_progress = event.progress + console.log(event) + @file_progress = event.loaded / event.total handleFinishedFile: (event) => switch event.item.type @@ -78,8 +79,18 @@ class ResourceManager if @current_stage == 2 @files_loaded += 1 @total_progress = @files_loaded / @files_total + + handleComplete: (event) => + stage = @current_stage + + @finished_callback() + + if stage == 1 + @engine?.switchPreloadScene() + else if stage == 2 + @engine?.switchInitialScene() - doPreload: (stage, progress_callback, finished_callback) => + doPreload: (stage, progress_callback) => @current_stage = stage if stage == 1 @@ -111,17 +122,19 @@ class ResourceManager @files_total += 1 @queue.loadFile({src: data_file, type: createjs.LoadQueue.JSON}) - @queue.on("progress", progress_callback) + @queue.on("fileprogress", progress_callback) @queue.on("fileload", @handleFinishedFile) - @queue.on("complete", finished_callback) + @queue.on("complete", @handleComplete) @queue.load() prepare: (finished_callback = (->)) => # This performs a stage 1 preload, loading the initial assets required for displaying the preload screen. - @doPreload(1, (->), finished_callback.bind(this)) + @finished_callback = finished_callback.bind(this) + @doPreload(1, (->)) load: (finished_callback = (->)) => # This performs the stage 2 preload; it will load the actual game assets. - @doPreload(2, @updateProgress, finished_callback.bind(this)) + @finished_callback = finished_callback.bind(this) + @doPreload(2, @updateProgress) \ No newline at end of file diff --git a/radium/scene.coffee b/radium/scene.coffee index c3ac0e8..38f36ff 100644 --- a/radium/scene.coffee +++ b/radium/scene.coffee @@ -13,7 +13,7 @@ class Scene @last_width = 800 @last_height = 600 - callEvent: (name, data = {}) -> + callEvent: (name, data = {}) => event_map = load: @onLoad