CCActionGrid.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /****************************************************************************
  2. Copyright (c) 2010-2013 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. /**
  23. * Base class for Grid actions
  24. * @class
  25. * @extends cc.ActionInterval
  26. */
  27. cc.GridAction = cc.ActionInterval.extend(/** @lends cc.GridAction# */{
  28. _gridSize:null,
  29. ctor:function(){
  30. cc.ActionInterval.prototype.ctor.call(this);
  31. this._gridSize = cc.size(0,0);
  32. },
  33. clone:function(){
  34. var action = new cc.GridAction();
  35. var locGridSize = this._gridSize;
  36. action.initWithDuration(this._duration, cc.size(locGridSize.width, locGridSize.height));
  37. return action;
  38. },
  39. startWithTarget:function (target) {
  40. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  41. var newGrid = this.getGrid();
  42. var t = this._target;
  43. var targetGrid = t.getGrid();
  44. if (targetGrid && targetGrid.getReuseGrid() > 0) {
  45. var locGridSize = targetGrid.getGridSize();
  46. if (targetGrid.isActive() && (locGridSize.width == this._gridSize.width) && (locGridSize.height == this._gridSize.height))
  47. targetGrid.reuse();
  48. } else {
  49. if (targetGrid && targetGrid.isActive())
  50. targetGrid.setActive(false);
  51. t.setGrid(newGrid);
  52. t.getGrid().setActive(true);
  53. }
  54. },
  55. reverse:function () {
  56. return cc.ReverseTime.create(this);
  57. },
  58. /**
  59. * initializes the action with size and duration
  60. * @param {Number} duration
  61. * @param {cc.Size} gridSize
  62. * @return {Boolean}
  63. */
  64. initWithDuration:function (duration, gridSize) {
  65. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  66. this._gridSize.width = gridSize.width;
  67. this._gridSize.height = gridSize.height;
  68. return true;
  69. }
  70. return false;
  71. },
  72. /**
  73. * returns the grid
  74. * @return {cc.GridBase}
  75. */
  76. getGrid:function () {
  77. // Abstract class needs implementation
  78. cc.log("cc.GridAction.getGrid(): it should be overridden in subclass.");
  79. }
  80. });
  81. /**
  82. * creates the action with size and duration
  83. * @param {Number} duration
  84. * @param {cc.Size} gridSize
  85. * @return {cc.GridAction}
  86. */
  87. cc.GridAction.create = function (duration, gridSize) {
  88. var action = new cc.GridAction();
  89. action.initWithDuration(duration, gridSize);
  90. return action;
  91. };
  92. /**
  93. * Base class for cc.Grid3D actions. <br/>
  94. * Grid3D actions can modify a non-tiled grid.
  95. * @class
  96. * @extends cc.GridAction
  97. */
  98. cc.Grid3DAction = cc.GridAction.extend(/** @lends cc.GridAction# */{
  99. /**
  100. * returns the grid
  101. * @return {cc.GridBase}
  102. */
  103. getGrid:function () {
  104. return cc.Grid3D.create(this._gridSize);
  105. },
  106. /**
  107. * returns the vertex than belongs to certain position in the grid
  108. * @param {cc.Point} position
  109. * @return {cc.Vertex3F}
  110. */
  111. vertex:function (position) {
  112. return this._target.getGrid().vertex(position);
  113. },
  114. /**
  115. * returns the non-transformed vertex than belongs to certain position in the grid
  116. * @param {cc.Point} position
  117. * @return {*}
  118. */
  119. originalVertex:function (position) {
  120. return this._target.getGrid().originalVertex(position);
  121. },
  122. /**
  123. * sets a new vertex to a certain position of the grid
  124. * @param {cc.Point} position
  125. * @param {cc.Vertex3F} vertex
  126. */
  127. setVertex:function (position, vertex) {
  128. this._target.getGrid().setVertex(position, vertex);
  129. }
  130. });
  131. /**
  132. * creates the action with size and duration
  133. * @param {Number} duration
  134. * @param {cc.Size} gridSize
  135. * @return {cc.Grid3DAction}
  136. */
  137. cc.Grid3DAction.create = function (duration,gridSize) {
  138. var action = new cc.Grid3DAction();
  139. action.initWithDuration(duration,gridSize);
  140. return action;
  141. };
  142. /**
  143. * Base class for cc.TiledGrid3D actions
  144. * @class
  145. * @extends cc.GridAction
  146. */
  147. cc.TiledGrid3DAction = cc.GridAction.extend(/** @lends cc.TiledGrid3DAction# */{
  148. /**
  149. * returns the tile that belongs to a certain position of the grid
  150. * @param {cc.Point} position
  151. * @return {cc.Quad3}
  152. */
  153. tile:function (position) {
  154. return this._target.getGrid().tile(position);
  155. },
  156. /**
  157. * returns the non-transformed tile that belongs to a certain position of the grid
  158. * @param {cc.Point} position
  159. * @return {cc.Quad3}
  160. */
  161. originalTile:function (position) {
  162. return this._target.getGrid().originalTile(position);
  163. },
  164. /**
  165. * sets a new tile to a certain position of the grid
  166. * @param {cc.Point} position
  167. * @param {cc.Quad3} coords
  168. */
  169. setTile:function (position, coords) {
  170. this._target.getGrid().setTile(position, coords);
  171. },
  172. /**
  173. * returns the grid
  174. * @return {cc.GridBase}
  175. */
  176. getGrid:function () {
  177. return cc.TiledGrid3D.create(this._gridSize);
  178. }
  179. });
  180. /**
  181. * creates the action with size and duration
  182. * @param {Number} duration
  183. * @param {cc.Size} gridSize
  184. * @return {cc.TiledGrid3DAction}
  185. */
  186. cc.TiledGrid3DAction.create = function (duration, gridSize) {
  187. var ret = new cc.TiledGrid3DAction();
  188. ret.initWithDuration(duration, gridSize);
  189. return ret;
  190. };
  191. /**
  192. * <p>
  193. * cc.StopGrid action. <br/>
  194. * @warning Don't call this action if another grid action is active. <br/>
  195. * Call if you want to remove the the grid effect. Example: <br/>
  196. * cc.Sequence.create(Lens.action(...), cc.StopGrid.create(...), null); <br/>
  197. * </p>
  198. * @class
  199. * @extends cc.ActionInstant
  200. */
  201. cc.StopGrid = cc.ActionInstant.extend(/** @lends cc.StopGrid# */{
  202. startWithTarget:function (target) {
  203. cc.ActionInstant.prototype.startWithTarget.call(this, target);
  204. var grid = this._target.getGrid();
  205. if (grid && grid.isActive())
  206. grid.setActive(false);
  207. }
  208. });
  209. /**
  210. * Allocates and initializes the action
  211. * @return {cc.StopGrid}
  212. */
  213. cc.StopGrid.create = function () {
  214. return new cc.StopGrid();
  215. };
  216. /**
  217. * cc.ReuseGrid action
  218. * @class
  219. * @extends cc.ActionInstant
  220. */
  221. cc.ReuseGrid = cc.ActionInstant.extend(/** @lends cc.ReuseGrid# */{
  222. _times:null,
  223. /**
  224. * initializes an action with the number of times that the current grid will be reused
  225. * @param {Number} times
  226. * @return {Boolean}
  227. */
  228. initWithTimes:function (times) {
  229. this._times = times;
  230. return true;
  231. },
  232. startWithTarget:function (target) {
  233. cc.ActionInstant.prototype.startWithTarget.call(this, target);
  234. if (this._target.getGrid() && this._target.getGrid().isActive())
  235. this._target.getGrid().setReuseGrid(this._target.getGrid().getReuseGrid() + this._times);
  236. }
  237. });
  238. /**
  239. * creates an action with the number of times that the current grid will be reused
  240. * @param {Number} times
  241. * @return {cc.ReuseGrid}
  242. */
  243. cc.ReuseGrid.create = function (times) {
  244. return new cc.ReuseGrid();
  245. };