1 /****************************************************************************
  2  Copyright (c) 2011-2012 cocos2d-x.org
  3  Copyright (c) 2013-2014 Chukong Technologies Inc.
  4 
  5  http://www.cocos2d-x.org
  6 
  7  Permission is hereby granted, free of charge, to any person obtaining a copy
  8  of this software and associated documentation files (the "Software"), to deal
  9  in the Software without restriction, including without limitation the rights
 10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  copies of the Software, and to permit persons to whom the Software is
 12  furnished to do so, subject to the following conditions:
 13 
 14  The above copyright notice and this permission notice shall be included in
 15  all copies or substantial portions of the Software.
 16 
 17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  THE SOFTWARE.
 24  ****************************************************************************/
 25 
 26 /**
 27  * The base class of trigger condition.
 28  * @class
 29  * @extends ccs.Class
 30  */
 31 ccs.BaseTriggerCondition = ccs.Class.extend(/** @lends ccs.BaseTriggerCondition# */{
 32     /**
 33      * Construction of ccs.BaseTriggerCondition
 34      */
 35     ctor:function(){
 36     },
 37 
 38     /**
 39      * initializes a BaseTriggerCondition class.
 40      * @returns {boolean}
 41      */
 42     init: function () {
 43         return true;
 44     },
 45 
 46     /**
 47      * Detects trigger condition
 48      * @returns {boolean}
 49      */
 50     detect: function () {
 51         return true;
 52     },
 53 
 54     /**
 55      * Serialize a BaseTriggerCondition object.
 56      * @param jsonVal
 57      */
 58     serialize: function (jsonVal) {
 59     },
 60 
 61     /**
 62      * Removes all condition
 63      */
 64     removeAll: function () {
 65     }
 66 });
 67 
 68 /**
 69  * The base class of trigger action
 70  * @class
 71  * @extends ccs.Class
 72  */
 73 ccs.BaseTriggerAction = ccs.Class.extend(/** @lends ccs.BaseTriggerAction# */{
 74     /**
 75      * Construction of ccs.BaseTriggerAction
 76      */
 77     ctor:function(){
 78     },
 79 
 80     /**
 81      * Initializes a BaseTriggerAction object.
 82      * @returns {boolean}
 83      */
 84     init: function () {
 85         return true;
 86     },
 87 
 88     /**
 89      * Sets the action to done.
 90      */
 91     done: function () {
 92     },
 93 
 94     /**
 95      * Serializes a ccs.BaseTriggerAction object.
 96      * @param jsonVal
 97      */
 98     serialize: function (jsonVal) {
 99     },
100 
101     /**
102      * Removes all actions.
103      */
104     removeAll: function () {
105     }
106 });
107 
108 /**
109  * The trigger object of Cocostudio.
110  * @class
111  * @extends ccs.Class
112  */
113 ccs.TriggerObj = ccs.Class.extend(/** @lends ccs.TriggerObj# */{
114     _cons: null,
115     _acts: null,
116     _id: 0,
117     _enable: true,
118     _vInt: null,
119 
120     ctor: function () {
121         this._id = 0;
122         this._enable = true;
123 
124         ccs.TriggerObj.prototype.init.call(this);
125     },
126 
127     /**
128      * Initializes a ccs.TriggerObj
129      * @returns {boolean}
130      */
131     init: function () {
132         this._cons = [];
133         this._acts = [];
134         this._vInt = [];
135         return true;
136     },
137 
138     /**
139      * Detects trigger's conditions.
140      * @returns {boolean}
141      */
142     detect: function () {
143         if (!this._enable || this._cons.length === 0) {
144             return true;
145         }
146         var ret = true;
147         var obj = null;
148         for (var i = 0; i < this._cons.length; i++) {
149             obj = this._cons[i];
150             if (obj && obj.detect)
151                 ret = ret && obj.detect();
152         }
153         return ret;
154     },
155 
156     /**
157      * Sets trigger's actions to done.
158      */
159     done: function () {
160         if (!this._enable || this._acts.length === 0)
161             return;
162         var obj;
163         for (var i = 0; i < this._acts.length; i++) {
164             obj = this._acts[i];
165             if (obj && obj.done)
166                 obj.done();
167         }
168     },
169 
170     /**
171      * Removes all condition and actions from ccs.TriggerObj.
172      */
173     removeAll: function () {
174         var obj = null;
175         for (var i = 0; i < this._cons.length; i++) {
176             obj = this._cons[i];
177             if (obj)
178                 obj.removeAll();
179         }
180         this._cons = [];
181         for (var i = 0; i < this._acts.length; i++) {
182             obj = this._acts[i];
183             if (obj)
184                 obj.removeAll();
185         }
186         this._acts = [];
187     },
188 
189     /**
190      * Serializes ccs.TriggerObj
191      * @param jsonVal
192      */
193     serialize: function (jsonVal) {
194         this._id = jsonVal["id"] || 0;
195         var conditions = jsonVal["conditions"] || [];
196         for (var i = 0; i < conditions.length; i++) {
197             var subDict = conditions[i];
198             var classname = subDict["classname"];
199             var con = ccs.objectFactory.createObject(classname);
200             if (!con) {
201                 cc.log("class named classname(" + classname + ") can not implement!");
202                 continue;
203             }
204 
205             con.serialize(subDict);
206             con.init();
207             this._cons.push(con);
208         }
209 
210         var actions = jsonVal["actions"] || [];
211         for (var i = 0; i < actions.length; i++) {
212             var subDict = actions[i];
213             var classname = subDict["classname"];
214             var act = ccs.objectFactory.createObject(classname);
215             if (!act) {
216                 cc.log("class named classname(" + classname + ") can not implement!");
217                 continue;
218             }
219 
220             act.serialize(subDict);
221             act.init();
222             this._acts.push(act);
223         }
224 
225         var events = jsonVal["events"] || [];
226         for (var i = 0; i < events.length; i++) {
227             var subDict = events[i];
228             var event = subDict["id"];
229             if (event < 0) {
230                 continue;
231             }
232             this._vInt.push(event);
233         }
234     },
235 
236     /**
237      * Returns the id of ccs.TriggerObj.
238      * @returns {number}
239      */
240     getId: function () {
241         return this._id;
242     },
243 
244     /**
245      * Sets enable value.
246      * @param {Boolean} enable
247      */
248     setEnable: function (enable) {
249         this._enable = enable;
250     },
251 
252     /**
253      * Returns the events of ccs.TriggerObj.
254      * @returns {null|Array}
255      */
256     getEvents: function () {
257         return this._vInt;
258     }
259 });
260 
261 ccs.TriggerObj.create = function () {
262     return new ccs.TriggerObj();
263 };