CCActionGrid3D.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. cc.RAND_MAX = 0xffffff;
  23. cc.rand = function () {
  24. return Math.random() * cc.RAND_MAX;
  25. };
  26. /**
  27. * cc.Waves3D action
  28. * @class
  29. * @extends cc.Grid3DAction
  30. */
  31. cc.Waves3D = cc.Grid3DAction.extend(/** @lends cc.Waves3D# */{
  32. _waves:null,
  33. _amplitude:null,
  34. _amplitudeRate:null,
  35. ctor:function () {
  36. cc.GridAction.prototype.ctor.call(this);
  37. this._waves = 0;
  38. this._amplitude = 0;
  39. this._amplitudeRate = 0;
  40. },
  41. /**
  42. * get Amplitude
  43. * @return {Number}
  44. */
  45. getAmplitude:function () {
  46. return this._amplitude;
  47. },
  48. /**
  49. * set Amplitude
  50. * @param {Number} amplitude
  51. */
  52. setAmplitude:function (amplitude) {
  53. this._amplitude = amplitude;
  54. },
  55. /**
  56. * get Amplitude Rate
  57. * @return {Number}
  58. */
  59. getAmplitudeRate:function () {
  60. return this._amplitudeRate;
  61. },
  62. /**
  63. * set Amplitude Rate
  64. * @param {Number} amplitudeRate
  65. */
  66. setAmplitudeRate:function (amplitudeRate) {
  67. this._amplitudeRate = amplitudeRate;
  68. },
  69. /**
  70. * initializes an action with duration, grid size, waves and amplitude
  71. * @param {Number} duration
  72. * @param {cc.Size} gridSize
  73. * @param {Number} waves
  74. * @param {Number} amplitude
  75. * @return {Boolean}
  76. */
  77. initWithDuration:function (duration, gridSize, waves, amplitude) {
  78. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  79. this._waves = waves;
  80. this._amplitude = amplitude;
  81. this._amplitudeRate = 1.0;
  82. return true;
  83. }
  84. return false;
  85. },
  86. update:function (time) {
  87. var locGridSize = this._gridSize;
  88. var locAmplitude = this._amplitude, locPos = cc.p(0, 0);
  89. var locAmplitudeRate = this._amplitudeRate, locWaves = this._waves;
  90. for (var i = 0; i < locGridSize.width + 1; ++i) {
  91. for (var j = 0; j < locGridSize.height + 1; ++j) {
  92. locPos.x = i;
  93. locPos.y = j;
  94. var v = this.originalVertex(locPos);
  95. v.z += (Math.sin(Math.PI * time * locWaves * 2 + (v.y + v.x) * 0.01) * locAmplitude * locAmplitudeRate);
  96. //cc.log("v.z offset is" + (Math.sin(Math.PI * time * this._waves * 2 + (v.y + v.x) * 0.01) * this._amplitude * this._amplitudeRate));
  97. this.setVertex(locPos, v);
  98. }
  99. }
  100. }
  101. });
  102. /**
  103. * creates an action with duration, grid size, waves and amplitude
  104. * @param {Number} duration
  105. * @param {cc.Size} gridSize
  106. * @param {Number} waves
  107. * @param {Number} amplitude
  108. * @return {cc.Waves3D}
  109. */
  110. cc.Waves3D.create = function (duration, gridSize, waves, amplitude) {
  111. var action = new cc.Waves3D();
  112. action.initWithDuration(duration, gridSize, waves, amplitude);
  113. return action;
  114. };
  115. /**
  116. * cc.FlipX3D action
  117. * @class
  118. * @extends cc.Grid3DAction
  119. */
  120. cc.FlipX3D = cc.Grid3DAction.extend(/** @lends cc.Waves3D# */{
  121. /**
  122. * initializes the action with duration
  123. * @param {Number} duration
  124. * @return {Boolean}
  125. */
  126. initWithDuration:function (duration) {
  127. return cc.Grid3DAction.prototype.initWithDuration.call(this, duration, cc.size(1, 1));
  128. },
  129. /**
  130. * initializes the action with gridSize and duration
  131. * @param {cc.Size} gridSize
  132. * @param {Number} duration
  133. * @return {Boolean}
  134. */
  135. initWithSize:function (gridSize, duration) {
  136. if (gridSize.width != 1 || gridSize.height != 1) {
  137. // Grid size must be (1,1)
  138. cc.log("Grid size must be (1,1)");
  139. return false;
  140. }
  141. return cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize);
  142. },
  143. update:function (time) {
  144. var angle = Math.PI * time; // 180 degrees
  145. var mz = Math.sin(angle);
  146. angle = angle / 2.0; // x calculates degrees from 0 to 90
  147. var mx = Math.cos(angle);
  148. var diff = new cc.Vertex3F();
  149. var tempVer = cc.p(0, 0);
  150. tempVer.x = tempVer.y = 1;
  151. var v0 = this.originalVertex(tempVer);
  152. tempVer.x = tempVer.y = 0;
  153. var v1 = this.originalVertex(tempVer);
  154. var x0 = v0.x;
  155. var x1 = v1.x;
  156. var x;
  157. var a, b, c, d;
  158. if (x0 > x1) {
  159. // Normal Grid
  160. a = cc.p(0, 0);
  161. b = cc.p(0, 1);
  162. c = cc.p(1, 0);
  163. d = cc.p(1, 1);
  164. x = x0;
  165. } else {
  166. // Reversed Grid
  167. c = cc.p(0, 0);
  168. d = cc.p(0, 1);
  169. a = cc.p(1, 0);
  170. b = cc.p(1, 1);
  171. x = x1;
  172. }
  173. diff.x = ( x - x * mx );
  174. diff.z = Math.abs(parseFloat((x * mz) / 4.0));
  175. // bottom-left
  176. var v = this.originalVertex(a);
  177. v.x = diff.x;
  178. v.z += diff.z;
  179. this.setVertex(a, v);
  180. // upper-left
  181. v = this.originalVertex(b);
  182. v.x = diff.x;
  183. v.z += diff.z;
  184. this.setVertex(b, v);
  185. // bottom-right
  186. v = this.originalVertex(c);
  187. v.x -= diff.x;
  188. v.z -= diff.z;
  189. this.setVertex(c, v);
  190. // upper-right
  191. v = this.originalVertex(d);
  192. v.x -= diff.x;
  193. v.z -= diff.z;
  194. this.setVertex(d, v);
  195. }
  196. });
  197. /**
  198. * creates FlipX3D action with duration
  199. * @param {Number} duration
  200. * @return {cc.FlipX3D}
  201. */
  202. cc.FlipX3D.create = function (duration) {
  203. var action = new cc.FlipX3D();
  204. action.initWithDuration(duration);
  205. return action;
  206. };
  207. /**
  208. * cc.FlipY3D action
  209. * @class
  210. * @extends cc.FlipX3D
  211. */
  212. cc.FlipY3D = cc.FlipX3D.extend(/** @lends cc.FlipY3D# */{
  213. update:function (time) {
  214. var angle = Math.PI * time; // 180 degrees
  215. var mz = Math.sin(angle);
  216. angle = angle / 2.0; // x calculates degrees from 0 to 90
  217. var my = Math.cos(angle);
  218. var diff = new cc.Vertex3F();
  219. var tempP = cc.p(0, 0);
  220. tempP.x = tempP.y = 1;
  221. var v0 = this.originalVertex(tempP);
  222. tempP.x = tempP.y = 0;
  223. var v1 = this.originalVertex(tempP);
  224. var y0 = v0.y;
  225. var y1 = v1.y;
  226. var y;
  227. var a, b, c, d;
  228. if (y0 > y1) {
  229. // Normal Grid
  230. a = cc.p(0, 0);
  231. b = cc.p(0, 1);
  232. c = cc.p(1, 0);
  233. d = cc.p(1, 1);
  234. y = y0;
  235. } else {
  236. // Reversed Grid
  237. b = cc.p(0, 0);
  238. a = cc.p(0, 1);
  239. d = cc.p(1, 0);
  240. c = cc.p(1, 1);
  241. y = y1;
  242. }
  243. diff.y = y - y * my;
  244. diff.z = Math.abs(parseFloat(y * mz) / 4.0);
  245. // bottom-left
  246. var v = this.originalVertex(a);
  247. v.y = diff.y;
  248. v.z += diff.z;
  249. this.setVertex(a, v);
  250. // upper-left
  251. v = this.originalVertex(b);
  252. v.y -= diff.y;
  253. v.z -= diff.z;
  254. this.setVertex(b, v);
  255. // bottom-right
  256. v = this.originalVertex(c);
  257. v.y = diff.y;
  258. v.z += diff.z;
  259. this.setVertex(c, v);
  260. // upper-right
  261. v = this.originalVertex(d);
  262. v.y -= diff.y;
  263. v.z -= diff.z;
  264. this.setVertex(d, v);
  265. }
  266. });
  267. /**
  268. * creates the action with duration
  269. * @param {Number} duration
  270. * @return {cc.FlipY3D}
  271. */
  272. cc.FlipY3D.create = function (duration) {
  273. var action = new cc.FlipY3D();
  274. action.initWithDuration(duration);
  275. return action;
  276. };
  277. /**
  278. * cc.Lens3D action
  279. * @class
  280. * @extends cc.FlipX3D
  281. */
  282. cc.Lens3D = cc.Grid3DAction.extend(/** @lends cc.Lens3D# */{
  283. /* lens center position */
  284. _position:null,
  285. _radius:0,
  286. /** lens effect. Defaults to 0.7 - 0 means no effect, 1 is very strong effect */
  287. _lensEffect:0,
  288. /** lens is concave. (true = concave, false = convex) default is convex i.e. false */
  289. _concave:false,
  290. _dirty:false,
  291. ctor:function () {
  292. cc.GridAction.prototype.ctor.call(this);
  293. this._position = cc.p(0, 0);
  294. this._radius = 0;
  295. this._lensEffect = 0;
  296. this._concave = false;
  297. this._dirty = false;
  298. },
  299. /**
  300. * Get lens center position
  301. * @return {Number}
  302. */
  303. getLensEffect:function () {
  304. return this._lensEffect;
  305. },
  306. /**
  307. * Set lens center position
  308. * @param {Number} lensEffect
  309. */
  310. setLensEffect:function (lensEffect) {
  311. this._lensEffect = lensEffect;
  312. },
  313. /**
  314. * Set whether lens is concave
  315. * @param {Boolean} concave
  316. */
  317. setConcave:function (concave) {
  318. this._concave = concave;
  319. },
  320. /**
  321. * get Position
  322. * @return {cc.Point}
  323. */
  324. getPosition:function () {
  325. return cc.p(this._position);
  326. },
  327. /**
  328. * set Position
  329. * @param {cc.Point} position
  330. */
  331. setPosition:function (position) {
  332. if (!cc.pointEqualToPoint(position, this._position)) {
  333. this._position.x = position.x;
  334. this._position.y = position.y;
  335. this._dirty = true;
  336. }
  337. },
  338. /**
  339. * initializes the action with center position, radius, a grid size and duration
  340. * @param {Number} duration
  341. * @param {cc.Size} gridSize
  342. * @param {cc.Point} position
  343. * @param {Number} radius
  344. * @return {Boolean}
  345. */
  346. initWithDuration:function (duration, gridSize, position, radius) {
  347. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  348. this.setPosition(position);
  349. this._radius = radius;
  350. this._lensEffect = 0.7;
  351. this._dirty = true;
  352. return true;
  353. }
  354. return false;
  355. },
  356. update:function (time) {
  357. if (this._dirty) {
  358. var locGridSizeWidth = this._gridSize.width, locGridSizeHeight = this._gridSize.height;
  359. var locRadius = this._radius, locLensEffect = this._lensEffect;
  360. var locPos = cc.p(0, 0);
  361. var vect = cc.p(0, 0);
  362. var v, r, l, new_r, pre_log;
  363. for (var i = 0; i < locGridSizeWidth + 1; ++i) {
  364. for (var j = 0; j < locGridSizeHeight + 1; ++j) {
  365. locPos.x = i;
  366. locPos.y = j;
  367. v = this.originalVertex(locPos);
  368. vect.x = this._position.x - v.x;
  369. vect.y = this._position.y - v.y;
  370. r = cc.pLength(vect);
  371. if (r < locRadius) {
  372. r = locRadius - r;
  373. pre_log = r / locRadius;
  374. if (pre_log == 0)
  375. pre_log = 0.001;
  376. l = Math.log(pre_log) * locLensEffect;
  377. new_r = Math.exp(l) * locRadius;
  378. r = cc.pLength(vect);
  379. if (r > 0) {
  380. vect.x = vect.x / r;
  381. vect.y = vect.y / r;
  382. vect.x = vect.x * new_r;
  383. vect.y = vect.y * new_r;
  384. v.z += cc.pLength(vect) * locLensEffect;
  385. }
  386. }
  387. this.setVertex(locPos, v);
  388. }
  389. }
  390. this._dirty = false;
  391. }
  392. }
  393. });
  394. /**
  395. * creates the action with center position, radius, a grid size and duration
  396. * @param {Number} duration
  397. * @param {cc.Size} gridSize
  398. * @param {cc.Point} position
  399. * @param {Number} radius
  400. * @return {cc.Lens3D}
  401. */
  402. cc.Lens3D.create = function (duration, gridSize, position, radius) {
  403. var action = new cc.Lens3D();
  404. action.initWithDuration(duration, gridSize, position, radius);
  405. return action;
  406. };
  407. /**
  408. * cc.Ripple3D action
  409. * @class
  410. * @extends cc.Grid3DAction
  411. */
  412. cc.Ripple3D = cc.Grid3DAction.extend(/** @lends cc.Ripple3D# */{
  413. /* center position */
  414. _position:null,
  415. _radius:null,
  416. _waves:null,
  417. _amplitude:null,
  418. _amplitudeRate:null,
  419. ctor:function () {
  420. cc.GridAction.prototype.ctor.call(this);
  421. this._position = cc.p(0, 0);
  422. this._radius = 0;
  423. this._waves = 0;
  424. this._amplitude = 0;
  425. this._amplitudeRate = 0;
  426. },
  427. /**
  428. * get center position
  429. * @return {cc.Point}
  430. */
  431. getPosition:function () {
  432. return cc.p(this._position);
  433. },
  434. /**
  435. * set center position
  436. * @param {cc.Point} position
  437. */
  438. setPosition:function (position) {
  439. this._position.x = position.x;
  440. this._position.y = position.y;
  441. },
  442. /**
  443. * get Amplitude
  444. * @return {Number}
  445. */
  446. getAmplitude:function () {
  447. return this._amplitude;
  448. },
  449. /**
  450. * set Amplitude
  451. * @param {Number} amplitude
  452. */
  453. setAmplitude:function (amplitude) {
  454. this._amplitude = amplitude;
  455. },
  456. /**
  457. * get Amplitude rate
  458. * @return {*}
  459. */
  460. getAmplitudeRate:function () {
  461. return this._amplitudeRate;
  462. },
  463. /**
  464. * get amplitude rate
  465. * @param {Number} amplitudeRate
  466. */
  467. setAmplitudeRate:function (amplitudeRate) {
  468. this._amplitudeRate = amplitudeRate;
  469. },
  470. /**
  471. * initializes the action with radius, number of waves, amplitude, a grid size and duration
  472. * @param {Number} duration
  473. * @param {cc.Size} gridSize
  474. * @param {cc.Point} position
  475. * @param {Number} radius
  476. * @param {Number} waves
  477. * @param {Number} amplitude
  478. * @return {Boolean}
  479. */
  480. initWithDuration:function (duration, gridSize, position, radius, waves, amplitude) {
  481. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  482. this.setPosition(position);
  483. this._radius = radius;
  484. this._waves = waves;
  485. this._amplitude = amplitude;
  486. this._amplitudeRate = 1.0;
  487. return true;
  488. }
  489. return false;
  490. },
  491. update:function (time) {
  492. var locGridSizeWidth = this._gridSize.width, locGridSizeHeight = this._gridSize.height;
  493. var locPos = cc.p(0, 0), locRadius = this._radius;
  494. var locWaves = this._waves, locAmplitude = this._amplitude, locAmplitudeRate = this._amplitudeRate;
  495. var v, r, tempPos = cc.p(0, 0);
  496. for (var i = 0; i < (locGridSizeWidth + 1); ++i) {
  497. for (var j = 0; j < (locGridSizeHeight + 1); ++j) {
  498. locPos.x = i;
  499. locPos.y = j;
  500. v = this.originalVertex(locPos);
  501. tempPos.x = this._position.x - v.x;
  502. tempPos.y = this._position.y - v.y;
  503. r = cc.pLength(tempPos);
  504. if (r < locRadius) {
  505. r = locRadius - r;
  506. var rate = Math.pow(r / locRadius, 2);
  507. v.z += (Math.sin(time * Math.PI * locWaves * 2 + r * 0.1) * locAmplitude * locAmplitudeRate * rate);
  508. }
  509. this.setVertex(locPos, v);
  510. }
  511. }
  512. }
  513. });
  514. /**
  515. * creates the action with radius, number of waves, amplitude, a grid size and duration
  516. * @param {Number} duration
  517. * @param {cc.Size} gridSize
  518. * @param {cc.Point} position
  519. * @param {Number} radius
  520. * @param {Number} waves
  521. * @param {Number} amplitude
  522. * @return {cc.Ripple3D}
  523. */
  524. cc.Ripple3D.create = function (duration, gridSize, position, radius, waves, amplitude) {
  525. var action = new cc.Ripple3D();
  526. action.initWithDuration(duration, gridSize, position, radius, waves, amplitude);
  527. return action;
  528. };
  529. /**
  530. * cc.Shaky3D action
  531. * @class
  532. * @extends cc.Grid3DAction
  533. */
  534. cc.Shaky3D = cc.Grid3DAction.extend(/** @lends cc.Shaky3D# */{
  535. _randRange:null,
  536. _shakeZ:null,
  537. ctor:function () {
  538. cc.GridAction.prototype.ctor.call(this);
  539. this._randRange = 0;
  540. this._shakeZ = false;
  541. },
  542. /**
  543. * initializes the action with a range, shake Z vertices, a grid and duration
  544. * @param {Number} duration
  545. * @param {cc.Size} gridSize
  546. * @param {Number} range
  547. * @param {Boolean} shakeZ
  548. * @return {Boolean}
  549. */
  550. initWithDuration:function (duration, gridSize, range, shakeZ) {
  551. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  552. this._randRange = range;
  553. this._shakeZ = shakeZ;
  554. return true;
  555. }
  556. return false;
  557. },
  558. update:function (time) {
  559. var locGridSizeWidth = this._gridSize.width, locGridSizeHeight = this._gridSize.height;
  560. var locRandRange = this._randRange, locShakeZ = this._shakeZ, locP = cc.p(0, 0);
  561. var v;
  562. for (var i = 0; i < (locGridSizeWidth + 1); ++i) {
  563. for (var j = 0; j < (locGridSizeHeight + 1); ++j) {
  564. locP.x = i;
  565. locP.y = j;
  566. v = this.originalVertex(locP);
  567. v.x += (cc.rand() % (locRandRange * 2)) - locRandRange;
  568. v.y += (cc.rand() % (locRandRange * 2)) - locRandRange;
  569. if (locShakeZ)
  570. v.z += (cc.rand() % (locRandRange * 2)) - locRandRange;
  571. this.setVertex(locP, v);
  572. }
  573. }
  574. }
  575. });
  576. /**
  577. * creates the action with a range, shake Z vertices, a grid and duration
  578. * @param {Number} duration
  579. * @param {cc.Size} gridSize
  580. * @param {Number} range
  581. * @param {Boolean} shakeZ
  582. * @return {cc.Shaky3D}
  583. */
  584. cc.Shaky3D.create = function (duration, gridSize, range, shakeZ) {
  585. var action = new cc.Shaky3D();
  586. action.initWithDuration(duration, gridSize, range, shakeZ);
  587. return action;
  588. };
  589. /**
  590. * cc.Liquid action
  591. * @class
  592. * @extends cc.Grid3DAction
  593. */
  594. cc.Liquid = cc.Grid3DAction.extend(/** @lends cc.Liquid# */{
  595. _waves:null,
  596. _amplitude:null,
  597. _amplitudeRate:null,
  598. ctor:function () {
  599. cc.GridAction.prototype.ctor.call(this);
  600. this._waves = 0;
  601. this._amplitude = 0;
  602. this._amplitudeRate = 0;
  603. },
  604. /**
  605. * get amplitude
  606. * @return {Number}
  607. */
  608. getAmplitude:function () {
  609. return this._amplitude;
  610. },
  611. /**
  612. * set amplitude
  613. * @param {Number} amplitude
  614. */
  615. setAmplitude:function (amplitude) {
  616. this._amplitude = amplitude;
  617. },
  618. /**
  619. * get amplitude rate
  620. * @return {Number}
  621. */
  622. getAmplitudeRate:function () {
  623. return this._amplitudeRate;
  624. },
  625. /**
  626. * set amplitude rate
  627. * @param {Number} amplitudeRate
  628. */
  629. setAmplitudeRate:function (amplitudeRate) {
  630. this._amplitudeRate = amplitudeRate;
  631. },
  632. /**
  633. * initializes the action with amplitude, a grid and duration
  634. * @param {Number} duration
  635. * @param {cc.Size} gridSize
  636. * @param {Number} waves
  637. * @param {Number} amplitude
  638. * @return {Boolean}
  639. */
  640. initWithDuration:function (duration, gridSize, waves, amplitude) {
  641. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  642. this._waves = waves;
  643. this._amplitude = amplitude;
  644. this._amplitudeRate = 1.0;
  645. return true;
  646. }
  647. return false;
  648. },
  649. update:function (time) {
  650. var locSizeWidth = this._gridSize.width, locSizeHeight = this._gridSize.height, locPos = cc.p(0, 0);
  651. var locWaves = this._waves, locAmplitude = this._amplitude, locAmplitudeRate = this._amplitudeRate;
  652. var v;
  653. for (var i = 1; i < locSizeWidth; ++i) {
  654. for (var j = 1; j < locSizeHeight; ++j) {
  655. locPos.x = i;
  656. locPos.y = j;
  657. v = this.originalVertex(locPos);
  658. v.x = (v.x + (Math.sin(time * Math.PI * locWaves * 2 + v.x * .01) * locAmplitude * locAmplitudeRate));
  659. v.y = (v.y + (Math.sin(time * Math.PI * locWaves * 2 + v.y * .01) * locAmplitude * locAmplitudeRate));
  660. this.setVertex(locPos, v);
  661. }
  662. }
  663. }
  664. });
  665. /**
  666. * creates the action with amplitude, a grid and duration
  667. * @param {Number} duration
  668. * @param {cc.Size} gridSize
  669. * @param {Number} waves
  670. * @param {Number} amplitude
  671. * @return {cc.Liquid}
  672. */
  673. cc.Liquid.create = function (duration, gridSize, waves, amplitude) {
  674. var action = new cc.Liquid();
  675. action.initWithDuration(duration, gridSize, waves, amplitude);
  676. return action;
  677. };
  678. /**
  679. * cc.Waves action
  680. * @class
  681. * @extends cc.Grid3DAction
  682. */
  683. cc.Waves = cc.Grid3DAction.extend(/** @lends cc.Waves# */{
  684. _waves:null,
  685. _amplitude:null,
  686. _amplitudeRate:null,
  687. _vertical:null,
  688. _horizontal:null,
  689. ctor:function () {
  690. cc.GridAction.prototype.ctor.call(this);
  691. this._waves = 0;
  692. this._amplitude = 0;
  693. this._amplitudeRate = 0;
  694. this._vertical = false;
  695. this._horizontal = false;
  696. },
  697. /**
  698. * get amplitude
  699. * @return {Number}
  700. */
  701. getAmplitude:function () {
  702. return this._amplitude;
  703. },
  704. /**
  705. * set amplitude
  706. * @param {Number} amplitude
  707. */
  708. setAmplitude:function (amplitude) {
  709. this._amplitude = amplitude;
  710. },
  711. /**
  712. * get amplitude rate
  713. * @return {Number}
  714. */
  715. getAmplitudeRate:function () {
  716. return this._amplitudeRate;
  717. },
  718. /**
  719. * set amplitude rate
  720. * @param {Number} amplitudeRate
  721. */
  722. setAmplitudeRate:function (amplitudeRate) {
  723. this._amplitudeRate = amplitudeRate;
  724. },
  725. /**
  726. * initializes the action with amplitude, horizontal sin, vertical sin, a grid and duration
  727. * @param {Number} duration
  728. * @param {cc.Size} gridSize
  729. * @param {Number} waves
  730. * @param {Number} amplitude
  731. * @param {Boolean} horizontal
  732. * @param {Boolean} vertical
  733. * @return {Boolean}
  734. */
  735. initWithDuration:function (duration, gridSize, waves, amplitude, horizontal, vertical) {
  736. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  737. this._waves = waves;
  738. this._amplitude = amplitude;
  739. this._amplitudeRate = 1.0;
  740. this._horizontal = horizontal;
  741. this._vertical = vertical;
  742. return true;
  743. }
  744. return false;
  745. },
  746. update:function (time) {
  747. var locSizeWidth = this._gridSize.width, locSizeHeight = this._gridSize.height, locPos = cc.p(0, 0);
  748. var locVertical = this._vertical, locHorizontal = this._horizontal;
  749. var locWaves = this._waves, locAmplitude = this._amplitude, locAmplitudeRate = this._amplitudeRate;
  750. var v;
  751. for (var i = 0; i < locSizeWidth + 1; ++i) {
  752. for (var j = 0; j < locSizeHeight + 1; ++j) {
  753. locPos.x = i;
  754. locPos.y = j;
  755. v = this.originalVertex(locPos);
  756. if (locVertical)
  757. v.x = (v.x + (Math.sin(time * Math.PI * locWaves * 2 + v.y * .01) * locAmplitude * locAmplitudeRate));
  758. if (locHorizontal)
  759. v.y = (v.y + (Math.sin(time * Math.PI * locWaves * 2 + v.x * .01) * locAmplitude * locAmplitudeRate));
  760. this.setVertex(locPos, v);
  761. }
  762. }
  763. }
  764. });
  765. /**
  766. * initializes the action with amplitude, horizontal sin, vertical sin, a grid and duration
  767. * @param {Number} duration
  768. * @param {cc.Size} gridSize
  769. * @param {Number} waves
  770. * @param {Number} amplitude
  771. * @param {Boolean} horizontal
  772. * @param {Boolean} vertical
  773. * @return {cc.Waves}
  774. */
  775. cc.Waves.create = function (duration, gridSize, waves, amplitude, horizontal, vertical) {
  776. var action = new cc.Waves();
  777. action.initWithDuration(duration, gridSize, waves, amplitude, horizontal, vertical);
  778. return action;
  779. };
  780. /** @brief */
  781. /**
  782. * cc.Twirl action
  783. * @class
  784. * @extends cc.Grid3DAction
  785. */
  786. cc.Twirl = cc.Grid3DAction.extend({
  787. /* twirl center */
  788. _position:null,
  789. _twirls:null,
  790. _amplitude:null,
  791. _amplitudeRate:null,
  792. ctor:function () {
  793. cc.GridAction.prototype.ctor.call(this);
  794. this._position = cc.p(0, 0);
  795. this._twirls = 0;
  796. this._amplitude = 0;
  797. this._amplitudeRate = 0;
  798. },
  799. /**
  800. * get twirl center
  801. * @return {cc.Point}
  802. */
  803. getPosition:function () {
  804. return cc.p(this._position);
  805. },
  806. /**
  807. * set twirl center
  808. * @param {cc.Point} position
  809. */
  810. setPosition:function (position) {
  811. this._position.x = position.x;
  812. this._position.y = position.y;
  813. },
  814. /**
  815. * get amplitude
  816. * @return {Number}
  817. */
  818. getAmplitude:function () {
  819. return this._amplitude;
  820. },
  821. /**
  822. * set amplitude
  823. * @param {Number} amplitude
  824. */
  825. setAmplitude:function (amplitude) {
  826. this._amplitude = amplitude;
  827. },
  828. /**
  829. * get amplitude rate
  830. * @return {Number}
  831. */
  832. getAmplitudeRate:function () {
  833. return this._amplitudeRate;
  834. },
  835. /**
  836. * set amplitude rate
  837. * @param {Number} amplitudeRate
  838. */
  839. setAmplitudeRate:function (amplitudeRate) {
  840. this._amplitudeRate = amplitudeRate;
  841. },
  842. /** initializes the action with center position, number of twirls, amplitude, a grid size and duration */
  843. initWithDuration:function (duration, gridSize, position, twirls, amplitude) {
  844. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  845. this.setPosition(position);
  846. this._twirls = twirls;
  847. this._amplitude = amplitude;
  848. this._amplitudeRate = 1.0;
  849. return true;
  850. }
  851. return false;
  852. },
  853. update:function (time) {
  854. var c = this._position;
  855. var locSizeWidth = this._gridSize.width, locSizeHeight = this._gridSize.height, locPos = cc.p(0, 0);
  856. var amp = 0.1 * this._amplitude * this._amplitudeRate;
  857. var locTwirls = this._twirls;
  858. var v, a, dX, dY, avg = cc.p(0, 0);
  859. for (var i = 0; i < (locSizeWidth + 1); ++i) {
  860. for (var j = 0; j < (locSizeHeight + 1); ++j) {
  861. locPos.x = i;
  862. locPos.y = j;
  863. v = this.originalVertex(locPos);
  864. avg.x = i - (locSizeWidth / 2.0);
  865. avg.y = j - (locSizeHeight / 2.0);
  866. a = cc.pLength(avg) * Math.cos(Math.PI / 2.0 + time * Math.PI * locTwirls * 2) * amp;
  867. dX = Math.sin(a) * (v.y - c.y) + Math.cos(a) * (v.x - c.x);
  868. dY = Math.cos(a) * (v.y - c.y) - Math.sin(a) * (v.x - c.x);
  869. v.x = c.x + dX;
  870. v.y = c.y + dY;
  871. this.setVertex(locPos, v);
  872. }
  873. }
  874. }
  875. });
  876. /**
  877. * creates the action with center position, number of twirls, amplitude, a grid size and duration
  878. * @param {Number} duration
  879. * @param {cc.Size} gridSize
  880. * @param {cc.Point} position
  881. * @param {Number} twirls
  882. * @param {Number} amplitude
  883. * @return {cc.Twirl}
  884. */
  885. cc.Twirl.create = function (duration, gridSize, position, twirls, amplitude) {
  886. var action = new cc.Twirl();
  887. action.initWithDuration(duration, gridSize, position, twirls, amplitude);
  888. return action;
  889. };