CCActionGrid.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies 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. * @param {Number} duration
  27. * @param {cc.Size} gridSize
  28. */
  29. cc.GridAction = cc.ActionInterval.extend(/** @lends cc.GridAction# */{
  30. _gridSize:null,
  31. /**
  32. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  33. * @param {Number} duration
  34. * @param {cc.Size} gridSize
  35. */
  36. ctor:function(duration, gridSize){
  37. cc._checkWebGLRenderMode();
  38. cc.ActionInterval.prototype.ctor.call(this);
  39. this._gridSize = cc.size(0,0);
  40. gridSize && this.initWithDuration(duration, gridSize);
  41. },
  42. /**
  43. * to copy object with deep copy.
  44. * returns a clone of action.
  45. *
  46. * @return {cc.Action}
  47. */
  48. clone:function(){
  49. var action = new cc.GridAction();
  50. var locGridSize = this._gridSize;
  51. action.initWithDuration(this._duration, cc.size(locGridSize.width, locGridSize.height));
  52. return action;
  53. },
  54. /**
  55. * called before the action start. It will also set the target.
  56. *
  57. * @param {cc.Node} target
  58. */
  59. startWithTarget:function (target) {
  60. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  61. var newGrid = this.getGrid();
  62. var t = this.target;
  63. var targetGrid = t.grid;
  64. if (targetGrid && targetGrid.getReuseGrid() > 0) {
  65. var locGridSize = targetGrid.getGridSize();
  66. if (targetGrid.isActive() && (locGridSize.width == this._gridSize.width) && (locGridSize.height == this._gridSize.height))
  67. targetGrid.reuse();
  68. } else {
  69. if (targetGrid && targetGrid.isActive())
  70. targetGrid.setActive(false);
  71. t.grid = newGrid;
  72. t.grid.setActive(true);
  73. }
  74. },
  75. /**
  76. * Create a cc.ReverseTime action. Opposite with the original motion trajectory.
  77. * @return {cc.ReverseTime}
  78. */
  79. reverse:function () {
  80. return new cc.ReverseTime(this);
  81. },
  82. /**
  83. * Initializes the action with size and duration.
  84. * @param {Number} duration
  85. * @param {cc.Size} gridSize
  86. * @return {Boolean}
  87. */
  88. initWithDuration:function (duration, gridSize) {
  89. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  90. this._gridSize.width = gridSize.width;
  91. this._gridSize.height = gridSize.height;
  92. return true;
  93. }
  94. return false;
  95. },
  96. /**
  97. * Returns the grid.
  98. * @return {cc.GridBase}
  99. */
  100. getGrid:function () {
  101. // Abstract class needs implementation
  102. cc.log("cc.GridAction.getGrid(): it should be overridden in subclass.");
  103. }
  104. });
  105. /**
  106. * creates the action with size and duration
  107. * @function
  108. * @param {Number} duration
  109. * @param {cc.Size} gridSize
  110. * @return {cc.GridAction}
  111. */
  112. cc.gridAction = function (duration, gridSize) {
  113. return new cc.GridAction(duration, gridSize);
  114. };
  115. /**
  116. * Please use cc.gridAction instead. <br />
  117. * Creates the action with size and duration.
  118. * @param {Number} duration
  119. * @param {cc.Size} gridSize
  120. * @return {cc.GridAction}
  121. * @static
  122. * @deprecated since v3.0 <br /> Please use cc.gridAction instead.
  123. */
  124. cc.GridAction.create = cc.gridAction;
  125. /**
  126. * Base class for cc.Grid3D actions. <br/>
  127. * Grid3D actions can modify a non-tiled grid.
  128. * @class
  129. * @extends cc.GridAction
  130. */
  131. cc.Grid3DAction = cc.GridAction.extend(/** @lends cc.Grid3DAction# */{
  132. /**
  133. * returns the grid
  134. * @return {cc.Grid3D}
  135. */
  136. getGrid:function () {
  137. return new cc.Grid3D(this._gridSize);
  138. },
  139. /**
  140. * returns the vertex than belongs to certain position in the grid
  141. * @param {cc.Point} position
  142. * @return {cc.Vertex3F}
  143. */
  144. vertex:function (position) {
  145. return this.target.grid.vertex(position);
  146. },
  147. /**
  148. * returns the non-transformed vertex than belongs to certain position in the grid
  149. * @param {cc.Point} position
  150. * @return {cc.Vertex3F}
  151. */
  152. originalVertex:function (position) {
  153. return this.target.grid.originalVertex(position);
  154. },
  155. /**
  156. * sets a new vertex to a certain position of the grid
  157. * @param {cc.Point} position
  158. * @param {cc.Vertex3F} vertex
  159. */
  160. setVertex:function (position, vertex) {
  161. this.target.grid.setVertex(position, vertex);
  162. }
  163. });
  164. /**
  165. * creates the action with size and duration
  166. * @function
  167. * @param {Number} duration
  168. * @param {cc.Size} gridSize
  169. * @return {cc.Grid3DAction}
  170. */
  171. cc.grid3DAction = function (duration, gridSize) {
  172. return new cc.Grid3DAction(duration, gridSize);
  173. };
  174. /**
  175. * Please use cc.grid3DAction instead. <br />
  176. * creates the action with size and duration. <br />
  177. * @param {Number} duration
  178. * @param {cc.Size} gridSize
  179. * @return {cc.Grid3DAction}
  180. * @static
  181. * @deprecated since v3.0 <br /> Please use cc.grid3DAction instead.
  182. */
  183. cc.Grid3DAction.create = cc.grid3DAction;
  184. /**
  185. * Base class for cc.TiledGrid3D actions.
  186. * @class
  187. * @extends cc.GridAction
  188. */
  189. cc.TiledGrid3DAction = cc.GridAction.extend(/** @lends cc.TiledGrid3DAction# */{
  190. /**
  191. * returns the tile that belongs to a certain position of the grid
  192. * @param {cc.Point} position
  193. * @return {cc.Quad3}
  194. */
  195. tile:function (position) {
  196. return this.target.grid.tile(position);
  197. },
  198. /**
  199. * returns the non-transformed tile that belongs to a certain position of the grid
  200. * @param {cc.Point} position
  201. * @return {cc.Quad3}
  202. */
  203. originalTile:function (position) {
  204. return this.target.grid.originalTile(position);
  205. },
  206. /**
  207. * sets a new tile to a certain position of the grid
  208. * @param {cc.Point} position
  209. * @param {cc.Quad3} coords
  210. */
  211. setTile:function (position, coords) {
  212. this.target.grid.setTile(position, coords);
  213. },
  214. /**
  215. * returns the grid
  216. * @return {cc.TiledGrid3D}
  217. */
  218. getGrid:function () {
  219. return new cc.TiledGrid3D(this._gridSize);
  220. }
  221. });
  222. /**
  223. * Creates the action with duration and grid size
  224. * @function
  225. * @param {Number} duration
  226. * @param {cc.Size} gridSize
  227. * @return {cc.TiledGrid3DAction}
  228. */
  229. cc.tiledGrid3DAction = function (duration, gridSize) {
  230. return new cc.TiledGrid3DAction(duration, gridSize);
  231. };
  232. /**
  233. * Please use cc.tiledGrid3DAction instead
  234. * Creates the action with duration and grid size
  235. * @param {Number} duration
  236. * @param {cc.Size} gridSize
  237. * @return {cc.TiledGrid3DAction}
  238. * @static
  239. * @deprecated since v3.0 <br /> Please use cc.tiledGrid3DAction instead.
  240. */
  241. cc.TiledGrid3DAction.create = cc.tiledGrid3DAction;
  242. /**
  243. * <p>
  244. * cc.StopGrid action. <br/>
  245. * @warning Don't call this action if another grid action is active. <br/>
  246. * Call if you want to remove the the grid effect. Example: <br/>
  247. * cc.sequence(Lens.action(...), cc.stopGrid(...), null); <br/>
  248. * </p>
  249. * @class
  250. * @extends cc.ActionInstant
  251. */
  252. cc.StopGrid = cc.ActionInstant.extend(/** @lends cc.StopGrid# */{
  253. /**
  254. * called before the action start. It will also set the target.
  255. *
  256. * @param {cc.Node} target
  257. */
  258. startWithTarget:function (target) {
  259. cc.ActionInstant.prototype.startWithTarget.call(this, target);
  260. var grid = this.target.grid;
  261. if (grid && grid.isActive())
  262. grid.setActive(false);
  263. }
  264. });
  265. /**
  266. * Allocates and initializes the action
  267. * @function
  268. * @return {cc.StopGrid}
  269. */
  270. cc.stopGrid = function () {
  271. return new cc.StopGrid();
  272. };
  273. /**
  274. * Please use cc.stopGrid instead
  275. * Allocates and initializes the action
  276. * @return {cc.StopGrid}
  277. * @static
  278. * @deprecated since v3.0 <br /> Please use cc.stopGrid instead.
  279. */
  280. cc.StopGrid.create = cc.stopGrid;
  281. /**
  282. * cc.ReuseGrid action
  283. * @class
  284. * @extends cc.ActionInstant
  285. * @param {Number} times
  286. */
  287. cc.ReuseGrid = cc.ActionInstant.extend(/** @lends cc.ReuseGrid# */{
  288. _times:null,
  289. /**
  290. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  291. * @param {Number} times
  292. */
  293. ctor: function(times) {
  294. cc.ActionInstant.prototype.ctor.call(this);
  295. times !== undefined && this.initWithTimes(times);
  296. },
  297. /**
  298. * initializes an action with the number of times that the current grid will be reused
  299. * @param {Number} times
  300. * @return {Boolean}
  301. */
  302. initWithTimes:function (times) {
  303. this._times = times;
  304. return true;
  305. },
  306. /**
  307. * called before the action start. It will also set the target.
  308. *
  309. * @param {cc.Node} target
  310. */
  311. startWithTarget:function (target) {
  312. cc.ActionInstant.prototype.startWithTarget.call(this, target);
  313. if (this.target.grid && this.target.grid.isActive())
  314. this.target.grid.setReuseGrid(this.target.grid.getReuseGrid() + this._times);
  315. }
  316. });
  317. /**
  318. * creates an action with the number of times that the current grid will be reused
  319. * @function
  320. * @param {Number} times
  321. * @return {cc.ReuseGrid}
  322. */
  323. cc.reuseGrid = function (times) {
  324. return new cc.ReuseGrid(times);
  325. };
  326. /**
  327. * Please use cc.reuseGrid instead
  328. * creates an action with the number of times that the current grid will be reused
  329. * @param {Number} times
  330. * @return {cc.ReuseGrid}
  331. * @static
  332. * @deprecated since v3.0 <br /> Please use cc.reuseGrid instead.
  333. */
  334. cc.ReuseGrid.create = cc.reuseGrid;