trinLayer.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. function TrinLayer() {
  2. TrinLayer.superclass.constructor.apply(this);
  3. this.autoReviveChildren = false;
  4. this.children = null;
  5. this.numChildren = 0;
  6. this.sortIndex = "";
  7. this.sortOrder = this.ASCEDING;
  8. }
  9. extend(TrinLayer, TrinBasic);
  10. TrinLayer.prototype.ASCEDNING = -1;
  11. TrinLayer.prototype.DESCENDING = 1;
  12. TrinLayer.prototype.DEPTH_ID = 0;
  13. TrinLayer.prototype.destroy = function() {
  14. this.kill();
  15. if (this.children !== null) {
  16. var entity;
  17. var i = 0;
  18. while (i < this.numChildren) {
  19. entity = this.children[i++];
  20. if (entity !== null) {
  21. entity.destroy();
  22. }
  23. }
  24. this.children = [];
  25. this.numChildren = 0;
  26. this.sortIndex = this.ASCEDING;
  27. if (this.parent !== null) {
  28. this.parent.remove(this);
  29. }
  30. }
  31. TrinLayer.superclass.destroy.apply(this);
  32. };
  33. TrinLayer.prototype.kill = function() {
  34. if (this.children !== null) {
  35. var entity;
  36. var i = 0;
  37. while (i < this.numChildren) {
  38. entity = this.children[i++];
  39. if (entity !== null && entity.exists) {
  40. entity.kill();
  41. }
  42. }
  43. }
  44. TrinLayer.superclass.kill.apply(this);
  45. };
  46. TrinLayer.prototype.revive = function() {
  47. TrinLayer.superclass.revive.apply(this);
  48. if (this.autoReviveChildren && this.children !== null) {
  49. var entity;
  50. var i = 0;
  51. while (i < this.numChildren) {
  52. entity = this.children[i++];
  53. if (entity !== null && !entity.exists) {
  54. entity.revive();
  55. }
  56. }
  57. }
  58. };
  59. TrinLayer.prototype.update = function() {
  60. TrinLayer.superclass.update.apply(this);
  61. this.updateChildren();
  62. };
  63. TrinLayer.prototype.draw = function(context) {
  64. this.drawChildren(context);
  65. TrinLayer.superclass.draw.apply(this, [context]);
  66. };
  67. TrinLayer.prototype.sort = function(index, order) {
  68. if (index === undefined) {
  69. index = "y";
  70. }
  71. if (order === undefined) {
  72. order = this.ASCEDING;
  73. }
  74. if (this.children !== null) {
  75. this.sortIndex = index;
  76. this.sortOrder = order;
  77. this.children.sort(this.sortHandler);
  78. }
  79. };
  80. TrinLayer.prototype.add = function(entity) {
  81. if (this.children === null) {
  82. this.children = [];
  83. }
  84. if (this.children.indexOf(entity) > -1) {
  85. return entity;
  86. }
  87. if (entity.parent !== null) {
  88. entity.parent.remove(entity);
  89. }
  90. entity.parent = this;
  91. var i = 0;
  92. var n = this.children.length;
  93. while (i < n) {
  94. if (this.children[i] === null) {
  95. this.children[i] = entity;
  96. return entity;
  97. }
  98. i++;
  99. }
  100. this.children[n] = entity;
  101. this.numChildren++;
  102. return entity;
  103. };
  104. TrinLayer.prototype.remove = function(entity, splice) {
  105. if (this.children === null) {
  106. return entity;
  107. }
  108. var i = this.children.indexOf(entity);
  109. if (i < 0 || i >= this.children.length) {
  110. return entity;
  111. }
  112. this.children[i] = null;
  113. entity.parent = null;
  114. if (splice) {
  115. this.children.splice(i, 1);
  116. this.numChildren--;
  117. }
  118. return entity;
  119. };
  120. TrinLayer.prototype.contains = function(entity) {
  121. if (this.children === null) {
  122. return false;
  123. }
  124. return (this.children.indexOf(entity) >= 0) ? true : false;
  125. };
  126. TrinLayer.prototype.recycle = function(_class) {
  127. var entity = this.getAvailable(_class);
  128. if (entity !== null) {
  129. return entity;
  130. }
  131. if (_class === undefined || _class === null) {
  132. return null;
  133. }
  134. entity = new _class();
  135. return (entity instanceof TrinBasic) ? this.add(entity) : null;
  136. };
  137. TrinLayer.prototype.replace = function(oldEntity, newEntity) {
  138. if (this.children === null) {
  139. return newEntity;
  140. }
  141. var i = this.children.indexOf(oldEntity);
  142. if (i >= 0 && i < this.children.length) {
  143. if (newEntity.parent !== null && newEntity.parent !== this) {
  144. newEntity.parent.remove(newEntity);
  145. newEntity.parent = this;
  146. }
  147. this.children[i] = newEntity;
  148. oldEntity.parent = null;
  149. }
  150. return newEntity;
  151. };
  152. TrinLayer.prototype.swap = function(entityA, entityB) {
  153. if (this.children === null) {
  154. return;
  155. }
  156. var iA = this.children.indexOf(entityA);
  157. var iB = this.children.indexOf(entityB);
  158. if (iA >= 0 && iA < this.children.length && iB >= 0 && iB < this.children.length)
  159. {
  160. this.children[iA] = entityB;
  161. this.children[iB] = entityA;
  162. }
  163. };
  164. TrinLayer.prototype.removeAll = function(destroy) {
  165. if (destroy === undefined) {
  166. destroy = true;
  167. }
  168. if (this.children === null) {
  169. return;
  170. }
  171. var entity;
  172. var i = 0;
  173. while (i < this.numChildren) {
  174. entity = this.children[i++];
  175. if (entity !== null) {
  176. if (destroy) {
  177. entity.destroy();
  178. }
  179. entity.parent = null;
  180. }
  181. this.children[i] = null;
  182. }
  183. this.numChildren = 0;
  184. };
  185. TrinLayer.prototype.getAvailable = function(_class) {
  186. if (this.children === null) {
  187. return null;
  188. }
  189. var entity;
  190. var i = 0;
  191. while (i < this.numChildren) {
  192. entity = this.children[i++];
  193. if (entity !== null && !entity.exists && ((_class === undefined) || (entity instanceof _class))) {
  194. return entity;
  195. }
  196. }
  197. return null;
  198. };
  199. TrinLayer.prototype.getExtant = function(_class) {
  200. if (this.children === null) {
  201. return null;
  202. }
  203. var entity;
  204. var i = 0;
  205. while (i < this.numChildren)
  206. {
  207. entity = this.children[i++];
  208. if (entity !== null && entity.exists && ((_class === null) || (entity instanceof _class))) {
  209. return entity;
  210. }
  211. }
  212. return null;
  213. };
  214. TrinLayer.prototype.getAlive = function(_class) {
  215. if (this.children === null) {
  216. return null;
  217. }
  218. var entity;
  219. var i = 0;
  220. while (i < this.numChildren) {
  221. entity = this.children[i++];
  222. if (entity !== null && entity.exists && entity.alive &&
  223. ((_class === null) || (entity instanceof _class))) {
  224. return entity;
  225. }
  226. }
  227. return null;
  228. };
  229. TrinLayer.prototype.getDead = function(_class) {
  230. if (this.children === null) {
  231. return null;
  232. }
  233. var entity;
  234. var i = 0;
  235. while (i < this.numChildren) {
  236. entity = this.children[i++];
  237. if (entity !== null && !entity.alive &&
  238. ((_class === null) || (entity instanceof _class))) {
  239. return entity;
  240. }
  241. }
  242. return null;
  243. };
  244. TrinLayer.prototype.setAll = function(variableName, value, recurse) {
  245. var entity;
  246. var i = 0;
  247. while (i < this.numChildren) {
  248. entity = this.children[i++];
  249. if (entity !== null) {
  250. if (recurse && entity instanceof TrinLayer) {
  251. entity.setAll(variableName, value, recurse);
  252. } else {
  253. entity[variableName] = value;
  254. }
  255. }
  256. }
  257. };
  258. TrinLayer.prototype.callAll = function(functionName, args, recurse) {
  259. var entity;
  260. var i = 0;
  261. while (i < this.numChildren) {
  262. entity = this.children[i++];
  263. if (entity !== null) {
  264. if (recurse && entity instanceof TrinLayer) {
  265. entity.callAll(functionName, args, recurse);
  266. } else if (typeof entity[functionName] === "function") {
  267. entity[functionName](args);
  268. }
  269. }
  270. }
  271. };
  272. TrinLayer.prototype.updateChildren = function() {
  273. if (this.children !== null) {
  274. var entity;
  275. var i = 0;
  276. while (i < this.numChildren) {
  277. entity = this.children[i++];
  278. if (entity !== null && entity.exists) {
  279. if (entity.active) {
  280. entity.update();
  281. }
  282. }
  283. }
  284. }
  285. };
  286. TrinLayer.prototype.drawChildren = function(context) {
  287. if (this.children !== null) {
  288. var entity;
  289. var i = 0;
  290. while (i < this.numChildren) {
  291. entity = this.children[i++];
  292. if (entity !== null && entity.exists && entity.visible) {
  293. entity.draw(context);
  294. }
  295. }
  296. }
  297. };
  298. TrinLayer.prototype.sortLayer = function(entity1, entity2) {
  299. if (entity1 === null) {
  300. return this.sortOrder;
  301. } else if (entity2 === null) {
  302. return -this.sortOrder;
  303. }
  304. if (entity1[this.sortIndex] < entity2[this.sortIndex]) {
  305. return this.sortOrder;
  306. } else if (entity1[this.sortIndex] > entity2[this.sortIndex]) {
  307. return -this.sortOrder;
  308. }
  309. return 0;
  310. };