美素佳儿 litter wizard小游戏
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

game.web.js 11KB

5 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. var __reflect = (this && this.__reflect) || function (p, c, t) {
  2. p.__class__ = c, t ? t.push(c) : t = [c], p.__types__ = p.__types__ ? t.concat(p.__types__) : t;
  3. };
  4. var __extends = (this && this.__extends) || function (d, b) {
  5. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. //////////////////////////////////////////////////////////////////////////////////////
  10. //
  11. // Copyright (c) 2014-present, Egret Technology.
  12. // All rights reserved.
  13. // Redistribution and use in source and binary forms, with or without
  14. // modification, are permitted provided that the following conditions are met:
  15. //
  16. // * Redistributions of source code must retain the above copyright
  17. // notice, this list of conditions and the following disclaimer.
  18. // * Redistributions in binary form must reproduce the above copyright
  19. // notice, this list of conditions and the following disclaimer in the
  20. // documentation and/or other materials provided with the distribution.
  21. // * Neither the name of the Egret nor the
  22. // names of its contributors may be used to endorse or promote products
  23. // derived from this software without specific prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  26. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  28. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  31. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  34. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //////////////////////////////////////////////////////////////////////////////////////
  37. var egret;
  38. (function (egret) {
  39. var web;
  40. (function (web) {
  41. /**
  42. * @class egret.HTML5NetContext
  43. * @classdesc
  44. * @extends egret.NetContext
  45. * @private
  46. */
  47. var HTML5NetContext = (function (_super) {
  48. __extends(HTML5NetContext, _super);
  49. /**
  50. * @private
  51. */
  52. function HTML5NetContext() {
  53. return _super.call(this) || this;
  54. }
  55. /**
  56. * @private
  57. *
  58. * @param loader
  59. */
  60. HTML5NetContext.prototype.proceed = function (loader) {
  61. var self = this;
  62. if (loader.dataFormat == egret.URLLoaderDataFormat.TEXTURE) {
  63. this.loadTexture(loader);
  64. return;
  65. }
  66. if (loader.dataFormat == egret.URLLoaderDataFormat.SOUND) {
  67. this.loadSound(loader);
  68. return;
  69. }
  70. var request = loader._request;
  71. var virtualUrl = self.getVirtualUrl(egret.$getUrl(request));
  72. var httpLoader = new egret.HttpRequest();
  73. httpLoader.addEventListener(egret.Event.COMPLETE, onLoadComplete, this);
  74. httpLoader.addEventListener(egret.IOErrorEvent.IO_ERROR, onError, this);
  75. httpLoader.addEventListener(egret.ProgressEvent.PROGRESS, onPostProgress, this);
  76. httpLoader.open(virtualUrl, request.method);
  77. httpLoader.responseType = this.getResponseType(loader.dataFormat);
  78. if (request.method == egret.URLRequestMethod.GET || !request.data) {
  79. httpLoader.send();
  80. }
  81. else if (request.data instanceof egret.URLVariables) {
  82. httpLoader.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  83. var urlVars = request.data;
  84. httpLoader.send(urlVars.toString());
  85. }
  86. else {
  87. httpLoader.setRequestHeader("Content-Type", "multipart/form-data");
  88. httpLoader.send(request.data);
  89. }
  90. /*function onReadyStateChange() {
  91. if (xhr.readyState == 4) {// 4 = "loaded"
  92. if (xhr.status != loader._status) {
  93. loader._status = xhr.status;
  94. HTTPStatusEvent.dispatchHTTPStatusEvent(loader, xhr.status);
  95. }
  96. if (xhr.status >= 400 || xhr.status == 0) {//请求错误
  97. IOErrorEvent.dispatchIOErrorEvent(loader);
  98. }
  99. else {
  100. onLoadComplete();
  101. }
  102. }
  103. }*/
  104. function onPostProgress(event) {
  105. loader.dispatchEvent(event);
  106. }
  107. function onError(event) {
  108. removeListeners();
  109. loader.dispatchEvent(event);
  110. }
  111. function onLoadComplete() {
  112. removeListeners();
  113. switch (loader.dataFormat) {
  114. case egret.URLLoaderDataFormat.VARIABLES:
  115. loader.data = new egret.URLVariables(httpLoader.response);
  116. break;
  117. //case URLLoaderDataFormat.TEXT:
  118. // loader.data = httpLoader.response;
  119. // break;
  120. //case URLLoaderDataFormat.BINARY:
  121. // loader.data = httpLoader.response;
  122. // break;
  123. default:
  124. loader.data = httpLoader.response;
  125. break;
  126. }
  127. window.setTimeout(function () {
  128. egret.Event.dispatchEvent(loader, egret.Event.COMPLETE);
  129. }, 0);
  130. }
  131. function removeListeners() {
  132. httpLoader.removeEventListener(egret.Event.COMPLETE, onLoadComplete, self);
  133. httpLoader.removeEventListener(egret.IOErrorEvent.IO_ERROR, onError, self);
  134. httpLoader.removeEventListener(egret.ProgressEvent.PROGRESS, onPostProgress, self);
  135. }
  136. };
  137. /**
  138. * @private
  139. *
  140. * @param dataFormat
  141. */
  142. HTML5NetContext.prototype.getResponseType = function (dataFormat) {
  143. switch (dataFormat) {
  144. case egret.URLLoaderDataFormat.TEXT:
  145. case egret.URLLoaderDataFormat.VARIABLES:
  146. return egret.URLLoaderDataFormat.TEXT;
  147. case egret.URLLoaderDataFormat.BINARY:
  148. return "arraybuffer";
  149. default:
  150. return dataFormat;
  151. }
  152. };
  153. /**
  154. * @private
  155. *
  156. * @param loader
  157. */
  158. HTML5NetContext.prototype.loadSound = function (loader) {
  159. var self = this;
  160. var virtualUrl = this.getVirtualUrl(loader._request.url);
  161. var sound = new egret.Sound();
  162. sound.addEventListener(egret.Event.COMPLETE, onLoadComplete, self);
  163. sound.addEventListener(egret.IOErrorEvent.IO_ERROR, onError, self);
  164. sound.addEventListener(egret.ProgressEvent.PROGRESS, onPostProgress, self);
  165. sound.load(virtualUrl);
  166. function onPostProgress(event) {
  167. loader.dispatchEvent(event);
  168. }
  169. function onError(event) {
  170. removeListeners();
  171. loader.dispatchEvent(event);
  172. }
  173. function onLoadComplete(e) {
  174. removeListeners();
  175. loader.data = sound;
  176. window.setTimeout(function () {
  177. loader.dispatchEventWith(egret.Event.COMPLETE);
  178. }, 0);
  179. }
  180. function removeListeners() {
  181. sound.removeEventListener(egret.Event.COMPLETE, onLoadComplete, self);
  182. sound.removeEventListener(egret.IOErrorEvent.IO_ERROR, onError, self);
  183. sound.removeEventListener(egret.ProgressEvent.PROGRESS, onPostProgress, self);
  184. }
  185. };
  186. /**
  187. * @private
  188. *
  189. * @param loader
  190. */
  191. HTML5NetContext.prototype.loadTexture = function (loader) {
  192. var self = this;
  193. var virtualUrl = this.getVirtualUrl(loader._request.url);
  194. var imageLoader = new egret.ImageLoader();
  195. imageLoader.addEventListener(egret.Event.COMPLETE, onLoadComplete, self);
  196. imageLoader.addEventListener(egret.IOErrorEvent.IO_ERROR, onError, self);
  197. imageLoader.addEventListener(egret.ProgressEvent.PROGRESS, onPostProgress, self);
  198. imageLoader.load(virtualUrl);
  199. function onPostProgress(event) {
  200. loader.dispatchEvent(event);
  201. }
  202. function onError(event) {
  203. removeListeners();
  204. loader.dispatchEvent(event);
  205. }
  206. function onLoadComplete(e) {
  207. removeListeners();
  208. var bitmapData = imageLoader.data;
  209. bitmapData.source.setAttribute("bitmapSrc", virtualUrl);
  210. var texture = new egret.Texture();
  211. texture._setBitmapData(bitmapData);
  212. loader.data = texture;
  213. window.setTimeout(function () {
  214. loader.dispatchEventWith(egret.Event.COMPLETE);
  215. }, self);
  216. }
  217. function removeListeners() {
  218. imageLoader.removeEventListener(egret.Event.COMPLETE, onLoadComplete, self);
  219. imageLoader.removeEventListener(egret.IOErrorEvent.IO_ERROR, onError, self);
  220. imageLoader.removeEventListener(egret.ProgressEvent.PROGRESS, onPostProgress, self);
  221. }
  222. };
  223. /**
  224. * @private
  225. *
  226. * @returns
  227. */
  228. HTML5NetContext.prototype.getChangeList = function () {
  229. return [];
  230. };
  231. /**
  232. * @private
  233. * 获取虚拟url
  234. * @param url
  235. * @returns {string}
  236. */
  237. HTML5NetContext.prototype.getVirtualUrl = function (url) {
  238. return url;
  239. };
  240. HTML5NetContext.getNetContext = function () {
  241. if (HTML5NetContext._instance == null) {
  242. HTML5NetContext._instance = new HTML5NetContext();
  243. }
  244. return HTML5NetContext._instance;
  245. };
  246. return HTML5NetContext;
  247. }(egret.HashObject));
  248. web.HTML5NetContext = HTML5NetContext;
  249. __reflect(HTML5NetContext.prototype, "egret.web.HTML5NetContext", ["egret.NetContext"]);
  250. egret.NetContext = HTML5NetContext;
  251. })(web = egret.web || (egret.web = {}));
  252. })(egret || (egret = {}));