選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

714 行
16 KiB

  1. define([
  2. 'js/resources',
  3. 'js/system/events',
  4. 'js/misc/physics',
  5. 'js/rendering/effects',
  6. 'js/rendering/tileOpacity',
  7. 'js/rendering/particles',
  8. 'js/rendering/shaders/outline'
  9. ], function(
  10. resources,
  11. events,
  12. physics,
  13. effects,
  14. tileOpacity,
  15. particles,
  16. shaderOutline
  17. ) {
  18. var scale = 40;
  19. var scaleMult = 5;
  20. var pixi = PIXI;
  21. return {
  22. stage: null,
  23. layers: {
  24. objects: null,
  25. mobs: null,
  26. characters: null,
  27. attacks: null,
  28. effects: null,
  29. particles: null,
  30. tileSprites: null,
  31. hiders: null
  32. },
  33. chunkSize: 30,
  34. titleScreen: false,
  35. pad: {
  36. x: 10,
  37. y: 10
  38. },
  39. width: 0,
  40. height: 0,
  41. pos: {
  42. x: 0,
  43. y: 0
  44. },
  45. moveTo: null,
  46. moveSpeed: 0,
  47. moveSpeedMax: 1.50,
  48. moveSpeedInc: 0.5,
  49. moveSpeedFlatten: 16,
  50. zoneId: null,
  51. textures: {},
  52. textureCache: {},
  53. lastTick: null,
  54. init: function() {
  55. PIXI.GC_MODES.DEFAULT = PIXI.GC_MODES.AUTO;
  56. PIXI.SCALE_MODES.DEFAULT = PIXI.SCALE_MODES.NEAREST;
  57. events.on('onGetMap', this.onGetMap.bind(this));
  58. events.on('onDeath', this.onDeath.bind(this));
  59. events.on('onToggleFullscreen', this.toggleScreen.bind(this));
  60. this.width = $('body').width();
  61. this.height = $('body').height();
  62. this.pad.x = ~~((this.width / 2) / 32);
  63. this.pad.y = ~~((this.height / 2) / 32);
  64. this.renderer = pixi.autoDetectRenderer(this.width, this.height, {
  65. backgroundColor: 0x2d2136
  66. });
  67. window.onresize = this.onResize.bind(this);
  68. $(this.renderer.view)
  69. .appendTo('.canvasContainer');
  70. this.stage = new pixi.Container();
  71. var layers = this.layers;
  72. Object.keys(layers).forEach(function(l) {
  73. if (l == 'tileSprites') {
  74. layers[l] = new pixi.particles.ParticleContainer(2500);
  75. layers[l].layer = 'tiles';
  76. } else
  77. layers[l] = new pixi.Container();
  78. this.stage.addChild(layers[l])
  79. }, this);
  80. var spriteNames = ['sprites', 'tiles', 'mobs', 'bosses', 'bigObjects', 'objects', 'characters', 'attacks', 'auras', 'walls', 'ui', 'animChar', 'animMob', 'animBoss'];
  81. resources.spriteNames.forEach(function(s) {
  82. if (s.indexOf('.png') > -1)
  83. spriteNames.push(s);
  84. });
  85. spriteNames.forEach(function(t) {
  86. this.textures[t] = new pixi.BaseTexture(resources.sprites[t].image);
  87. this.textures[t].scaleMode = pixi.SCALE_MODES.NEAREST;
  88. }, this);
  89. particles.init({
  90. renderer: this.renderer,
  91. stage: this.layers.particles
  92. });
  93. },
  94. toggleScreen: function() {
  95. var screenMode = 0;
  96. var isFullscreen = (window.innerHeight == screen.height);
  97. if (isFullscreen)
  98. screenMode = 0;
  99. else
  100. screenMode = 1;
  101. if (screenMode == 0) {
  102. (document.cancelFullscreen || document.msCancelFullscreen || document.mozCancelFullscreen || document.webkitCancelFullScreen).call(document);
  103. return 'Windowed';
  104. } else if (screenMode == 1) {
  105. var el = $('body')[0];
  106. (el.requestFullscreen || el.msRequestFullscreen || el.mozRequestFullscreen || el.webkitRequestFullscreen).call(el);
  107. return 'Fullscreen';
  108. }
  109. },
  110. buildTitleScreen: function() {
  111. this.titleScreen = true;
  112. this.setPosition({
  113. x: 0,
  114. y: 0
  115. }, true);
  116. var w = Math.ceil(this.width / scale) + 1;
  117. var h = Math.ceil(this.height / scale) + 1;
  118. var container = this.layers.tileSprites;
  119. for (var i = 0; i < w; i++) {
  120. for (var j = 0; j < h; j++) {
  121. var tile = 5;
  122. if (Math.random() < 0.4)
  123. tile = 6;
  124. var tile = new pixi.Sprite(this.getTexture('sprites', tile));
  125. var alpha = Math.sin((i % 4) + Math.cos(j % 8));
  126. if (tile == 5)
  127. alpha /= 2;
  128. tile.alpha = alpha;
  129. tile.position.x = i * scale;
  130. tile.position.y = j * scale;
  131. tile.width = scale;
  132. tile.height = scale;
  133. if (Math.random() < 0.5) {
  134. tile.position.x += scale;
  135. tile.scale.x = -scaleMult;
  136. }
  137. container.addChild(tile);
  138. }
  139. }
  140. },
  141. onDeath: function(pos) {
  142. this.setPosition({
  143. x: (pos.x - (this.width / (scale * 2))) * scale,
  144. y: (pos.y - (this.height / (scale * 2))) * scale
  145. }, true);
  146. },
  147. onResize: function() {
  148. this.width = $('body').width();
  149. this.height = $('body').height();
  150. this.renderer.resize(this.width, this.height);
  151. if (window.player) {
  152. this.setPosition({
  153. x: (window.player.x - (this.width / (scale * 2))) * scale,
  154. y: (window.player.y - (this.height / (scale * 2))) * scale
  155. }, true);
  156. }
  157. if (this.titleScreen) {
  158. this.clean();
  159. this.buildTitleScreen();
  160. }
  161. events.emit('onResize');
  162. },
  163. getTexture: function(baseTex, cell, size) {
  164. size = size || 8;
  165. var name = baseTex + '_' + cell;
  166. var textureCache = this.textureCache;
  167. var cached = textureCache[name];
  168. if (!cached) {
  169. var y = ~~(cell / 8);
  170. var x = cell - (y * 8);
  171. cached = new pixi.Texture(this.textures[baseTex], new pixi.Rectangle(x * size, y * size, size, size));
  172. textureCache[name] = cached;
  173. }
  174. return cached;
  175. },
  176. clean: function() {
  177. var container = this.layers.tileSprites;
  178. this.stage.removeChild(container);
  179. this.layers.tileSprites = container = new pixi.particles.ParticleContainer(2500);
  180. container.layer = 'tiles';
  181. this.stage.addChild(container);
  182. this.stage.children.sort(function(a, b) {
  183. if (a.layer == 'tiles')
  184. return -1;
  185. else if (b.layer == 'tiles')
  186. return 1;
  187. else
  188. return 0;
  189. }, this);
  190. },
  191. onGetMapCustomization: function(msg) {
  192. if (!msg.collide) {
  193. var children = this.layers.tiles.children;
  194. var cLen = children.length;
  195. var x = msg.x * scale;
  196. var y = msg.y * scale;
  197. for (var i = cLen - 1; i >= 0; i--) {
  198. var c = children[i];
  199. var cx = c.x;
  200. if (c.scale.x < 0)
  201. cx -= scale;
  202. if ((cx == x) && (c.y == y)) {
  203. c.parent.removeChild(c);
  204. break;
  205. }
  206. }
  207. }
  208. var tile = new pixi.Sprite(this.getTexture('sprites', msg.tile))
  209. tile.alpha = tileOpacity.map(msg.tile);
  210. tile.position.x = msg.x * scale;
  211. tile.position.y = msg.y * scale;
  212. tile.width = scale;
  213. tile.height = scale;
  214. if (Math.random() < 0.5) {
  215. tile.position.x += scale;
  216. tile.scale.x = -scaleMult;
  217. }
  218. this.layers.tiles.addChild(tile);
  219. physics.collisionMap[msg.x][msg.y] = msg.collide;
  220. physics.graph.grid[msg.x][msg.y] = !msg.collide;
  221. },
  222. buildTile: function(c, i, j) {
  223. var alpha = tileOpacity.map(c);
  224. var canFlip = tileOpacity.canFlip(c);
  225. var tile = new pixi.Sprite(this.getTexture('sprites', c + (0 * 160)));
  226. tile.alpha = alpha;
  227. tile.position.x = i * 8;
  228. tile.position.y = j * 8;
  229. tile.width = 8;
  230. tile.height = 8;
  231. if (canFlip) {
  232. if (Math.random() < 0.5) {
  233. tile.position.x += 8;
  234. tile.scale.x = -1;
  235. }
  236. }
  237. return tile;
  238. },
  239. onGetMap: function(msg) {
  240. this.titleScreen = false;
  241. physics.init(msg.collisionMap);
  242. var map = msg.map;
  243. var w = this.w = map.length;
  244. var h = this.h = map[0].length;
  245. var hiddenWalls = msg.hiddenWalls;
  246. var hiddenTiles = msg.hiddenTiles;
  247. this.hiddenRooms = msg.hiddenRooms;
  248. this.hiddenRooms.forEach(function(h) {
  249. h.container = new pixi.Container();
  250. this.layers.hiders.addChild(h.container);
  251. this.buildRectangle({
  252. x: h.x * scale,
  253. y: h.y * scale,
  254. w: h.width * scale,
  255. h: h.height * scale,
  256. color: 0x2d2136,
  257. parent: h.container
  258. });
  259. for (var i = h.x; i < h.x + h.width; i++) {
  260. for (var j = h.y; j < h.y + h.height; j++) {
  261. var cell = hiddenTiles[i][j];
  262. if (cell != 0) {
  263. var tile = this.buildTile(cell - 1, i, j);
  264. tile.position.x *= scaleMult;
  265. tile.position.y *= scaleMult;
  266. tile.width = scale;
  267. tile.height = scale;
  268. h.container.addChild(tile);
  269. }
  270. cell = hiddenWalls[i][j];
  271. if (cell == 0)
  272. continue;
  273. var tile = this.buildTile(cell - 1, i, j);
  274. tile.position.x *= scaleMult;
  275. tile.position.y *= scaleMult;
  276. tile.width = scale;
  277. tile.height = scale;
  278. h.container.addChild(tile);
  279. }
  280. }
  281. }, this);
  282. var padding = msg.padding ? JSON.parse(msg.padding) : {};
  283. this.clean();
  284. var container = new pixi.particles.ParticleContainer(270000);
  285. var isPadX = false;
  286. var isPadY = false;
  287. var padX = 0;
  288. var padY = 0;
  289. if (!msg.padding) {
  290. padX = 0;
  291. padY = 0;
  292. }
  293. var chunkSize = this.chunkSize;
  294. for (var i = -padX; i < w + padX; i++) {
  295. if ((i < 0) || (i >= w))
  296. isPadX = true;
  297. else
  298. isPadX = false;
  299. for (var j = -padY; j < h + padY; j++) {
  300. if ((j < 0) || (j >= h))
  301. isPadY = true;
  302. else
  303. isPadY = false;
  304. var cell = null;
  305. cell = map[i][j];
  306. if (!cell)
  307. continue;
  308. if (!cell.split)
  309. cell += '';
  310. cell = cell.split(',');
  311. for (var k = 0; k < cell.length; k++) {
  312. var c = cell[k];
  313. if (c == 0)
  314. continue;
  315. c--;
  316. var tile = this.buildTile(c, i, j);
  317. container.addChild(tile);
  318. }
  319. }
  320. }
  321. var renderTexture = pixi.RenderTexture.create(w * 8, h * 8);
  322. this.renderer.render(container, renderTexture);
  323. var cw = w / this.chunkSize;
  324. var ch = h / this.chunkSize;
  325. for (var i = 0; i < cw; i++) {
  326. var tw = Math.min(this.chunkSize, w - (i * chunkSize));
  327. for (var j = 0; j < ch; j++) {
  328. var th = Math.min(this.chunkSize, h - (j * chunkSize));
  329. var texture = new pixi.Texture(renderTexture, new pixi.Rectangle(i * this.chunkSize * 8, j * this.chunkSize * 8, tw * 8, th * 8));
  330. var sprite = new pixi.Sprite(texture);
  331. sprite.position.x = i * this.chunkSize * scale;
  332. sprite.position.y = j * this.chunkSize * scale;
  333. sprite.width = tw * scale;
  334. sprite.height = th * scale;
  335. this.layers.tileSprites.addChild(sprite);
  336. }
  337. }
  338. this.stage.children.sort(function(a, b) {
  339. if (a.layer == 'tiles')
  340. return -1;
  341. else if (b.layer == 'tiles')
  342. return 1;
  343. else
  344. return 0;
  345. }, this);
  346. if (this.zoneId != null)
  347. events.emit('onRezone', this.zoneId);
  348. this.zoneId = msg.zoneId;
  349. msg.clientObjects.forEach(function(c) {
  350. c.zoneId = this.zoneId;
  351. events.emit('onGetObject', c);
  352. }, this);
  353. },
  354. setPosition: function(pos, instant) {
  355. pos.x += 16;
  356. pos.y += 16;
  357. this.hideHiders();
  358. if (instant) {
  359. this.moveTo = null;
  360. this.pos = pos;
  361. this.stage.x = -~~this.pos.x;
  362. this.stage.y = -~~this.pos.y;
  363. return;
  364. }
  365. this.moveTo = pos;
  366. },
  367. hideHiders: function() {
  368. var player = window.player;
  369. if (!player)
  370. return;
  371. var x = player.x;
  372. var y = player.y;
  373. var hiddenRooms = this.hiddenRooms;
  374. var hLen = hiddenRooms.length;
  375. for (var i = 0; i < hLen; i++) {
  376. var h = hiddenRooms[i];
  377. h.container.visible = (
  378. (x < h.x) ||
  379. (x >= h.x + h.width) ||
  380. (y < h.y) ||
  381. (y >= h.y + h.height)
  382. );
  383. }
  384. },
  385. update: function() {
  386. var time = +new Date;
  387. if (this.moveTo) {
  388. var deltaX = this.moveTo.x - this.pos.x;
  389. var deltaY = this.moveTo.y - this.pos.y;
  390. if ((deltaX != 0) || (deltaY != 0)) {
  391. var moveSpeed = this.moveSpeed;
  392. var distance = Math.max(Math.abs(deltaX), Math.abs(deltaY));
  393. var moveSpeedMax = this.moveSpeedMax;
  394. if (distance > 100)
  395. moveSpeedMax *= 1.75;
  396. if (this.moveSpeed < moveSpeedMax)
  397. this.moveSpeed += this.moveSpeedInc;
  398. var elapsed = time - this.lastTick;
  399. moveSpeed *= (elapsed / 16.67);
  400. if (moveSpeed > distance)
  401. moveSpeed = distance;
  402. deltaX = (deltaX / distance) * moveSpeed;
  403. deltaY = (deltaY / distance) * moveSpeed;
  404. this.pos.x = this.pos.x + (deltaX);
  405. this.pos.y = this.pos.y + (deltaY);
  406. } else {
  407. this.moveSpeed = 0;
  408. this.moveTo = null;
  409. }
  410. this.stage.x = -~~this.pos.x;
  411. this.stage.y = -~~this.pos.y;
  412. events.emit('onSceneMove');
  413. }
  414. this.lastTick = time;
  415. },
  416. buildContainer: function(obj) {
  417. var container = new pixi.Container;
  418. this.layers[obj.layerName || obj.sheetName].addChild(container);
  419. return container;
  420. },
  421. buildRectangle: function(obj) {
  422. var graphics = new pixi.Graphics;
  423. var alpha = obj.alpha;
  424. if (alpha != null)
  425. graphics.alpha = alpha;
  426. var fillAlpha = obj.fillAlpha;
  427. if (fillAlpha == null)
  428. fillAlpha = 1;
  429. graphics.beginFill(obj.color || '0x48edff', fillAlpha);
  430. if (obj.strokeColor)
  431. graphics.lineStyle(scaleMult, obj.strokeColor);
  432. graphics.moveTo(obj.x, obj.y);
  433. graphics.lineTo(obj.x + obj.w, obj.y);
  434. graphics.lineTo(obj.x + obj.w, obj.y + obj.h);
  435. graphics.lineTo(obj.x, obj.y + obj.h);
  436. graphics.lineTo(obj.x, obj.y);
  437. graphics.endFill();
  438. (obj.parent || this.layers[obj.layerName || obj.sheetName]).addChild(graphics);
  439. return graphics;
  440. },
  441. moveRectangle(obj) {
  442. var points = obj.sprite.graphicsData[0].shape.points;
  443. if (!points)
  444. return;
  445. obj.sprite.dirty = true;
  446. obj.sprite.clearDirty = true;
  447. points[0] = obj.x;
  448. points[1] = obj.y;
  449. points[2] = obj.x + obj.w;
  450. points[3] = obj.y;
  451. points[4] = obj.x + obj.w;
  452. points[5] = obj.y + obj.h;
  453. points[6] = obj.x;
  454. points[7] = obj.y + obj.h;
  455. points[8] = obj.x;
  456. points[9] = obj.y;
  457. },
  458. buildObject: function(obj) {
  459. var w = 8;
  460. var h = 8;
  461. if (obj.w) {
  462. w = obj.w / scaleMult;
  463. h = obj.h / scaleMult;
  464. }
  465. if (obj.sheetName == 'bosses') {
  466. obj.layerName = 'mobs';
  467. w = 24;
  468. h = 24;
  469. obj.w = w * scaleMult;
  470. obj.h = h * scaleMult;
  471. } else if (obj.sheetName == 'bigObjects') {
  472. obj.layerName = 'mobs';
  473. w = 24;
  474. h = 24;
  475. obj.w = w * scaleMult;
  476. obj.h = h * scaleMult;
  477. }
  478. var sprite = new pixi.Sprite(this.getTexture(obj.sheetName, obj.cell, w))
  479. sprite.x = obj.x * scale;
  480. sprite.y = obj.y * scale;
  481. sprite.width = obj.w || scale;
  482. sprite.height = obj.h || scale;
  483. if (obj.sheetName == 'bosses') {
  484. sprite.x -= scale;
  485. sprite.y -= (scale * 2);
  486. } else if (obj.sheetName == 'bigObjects') {
  487. sprite.x -= scale;
  488. sprite.y -= (scale * 2);
  489. sprite.alpha = 0.80;
  490. }
  491. if (obj.flipX) {
  492. sprite.scale.x *= -1;
  493. if ((obj.sheetName == 'bosses') || (obj.sheetName == 'bigObjects'))
  494. sprite.x += (scale * 2);
  495. else
  496. sprite.x += scale;
  497. }
  498. (this.layers[obj.layerName || obj.sheetName] || this.layers.objects).addChild(sprite);
  499. return sprite;
  500. },
  501. addFilter: function(sprite) {
  502. var thickness = 16;
  503. if (sprite.width > scale)
  504. thickness = 8;
  505. var filter = new shaderOutline(this.renderer.width, this.renderer.height, thickness, '0xffffff');
  506. if (!sprite.filters)
  507. sprite.filters = [filter];
  508. else
  509. sprite.filters.push();
  510. return filter;
  511. },
  512. removeFilter: function(sprite, filter) {
  513. if (!sprite.filters)
  514. return;
  515. sprite.filters = null;
  516. },
  517. buildText: function(obj) {
  518. var textSprite = new pixi.Text(obj.text, {
  519. fontFamily: 'bitty',
  520. fontSize: (obj.fontSize || 14),
  521. fill: obj.color || 0xF2F5F5,
  522. stroke: 0x2d2136,
  523. strokeThickness: 4,
  524. align: 'center'
  525. });
  526. textSprite.x = obj.x - (textSprite.width / 2);
  527. textSprite.y = obj.y;
  528. var parent = obj.parent || this.layers[obj.layerName]
  529. parent.addChild(textSprite);
  530. return textSprite;
  531. },
  532. buildEmitter: function(config) {
  533. return particles.buildEmitter(config);
  534. },
  535. destroyEmitter: function(emitter) {
  536. particles.destroyEmitter(emitter);
  537. },
  538. setSprite: function(obj) {
  539. var cell = obj.cell;
  540. var y = ~~(cell / 8);
  541. var x = cell - (y * 8);
  542. var baseTex = this.textures[obj.sheetName];
  543. obj.sprite.texture = this.getTexture(obj.sheetName, obj.cell, obj.sprite.width / scaleMult);
  544. },
  545. reorder: function(sprite) {
  546. var mobLayer = this.layers.mobs;
  547. var mobs = mobLayer.children;
  548. mobs.sort(function(a, b) {
  549. return (b.y - a.y);
  550. });
  551. },
  552. destroyObject: function(obj) {
  553. if (!obj.sprite.parent)
  554. return;
  555. obj.sprite.parent.removeChild(obj.sprite);
  556. },
  557. render: function() {
  558. if (!this.stage)
  559. return;
  560. effects.render();
  561. particles.update();
  562. this.renderer.render(this.stage);
  563. }
  564. };
  565. });