/* * Game Develop JS Platform * Copyright 2013-2014 Florian Rival (Florian.Rival@gmail.com). All rights reserved. * This project is released under the GNU Lesser General Public License. */ /** * Tools related to objects, for events generated code. * @namespace gdjs.evtTools * @class object * @static * @private */ gdjs.evtTools.object = gdjs.evtTools.object || {}; /** * Do a test on two tables of objects so as to pick only the pair of objects for which the test is true. * If inverted == true, only the objects of the first table are filtered. * * Note that the func method is not called stricly for each pair: When considering a pair of objects, if * these objects have already been marked as picked, the func method won't be called again. * * Cost (Worst case, func being always false): * Cost(Setting property 'picked' of NbObjList1+NbObjList2 objects to false) * + Cost(functor)*NbObjList1*NbObjList2 * + Cost(Testing NbObjList1+NbObjList2 booleans) * + Cost(Removing NbObjList1+NbObjList2 objects from all the lists) * * Cost (Best case, func being always true): * Cost(Setting property 'picked' of NbObjList1+NbObjList2 objects to false) * + Cost(functor)*(NbObjList1+NbObjList2) * + Cost(Testing NbObjList1+NbObjList2 booleans) * * @method TwoListsTest */ gdjs.evtTools.object.TwoListsTest = function(func, objectsLists1, objectsLists2, inverted, extraParam) { var isTrue = false; var objects1Values = objectsLists1.values(); var objects2Values = objectsLists2.values(); for(var i = 0, leni = objects1Values.length;i= objects.length) id = objects.length-1; //Should never happen. var theChosenOne = objects[id]; objectsLists.get(theChosenOne.getName()).push(theChosenOne); } return true; }; /** * Do the work of creating a new object * @private */ gdjs.evtTools.object.doCreateObjectOnScene = function(runtimeScene, objectName, objectsLists, x, y, layer) { //Let's ask the RuntimeScene to create the object var obj = runtimeScene.createObject(objectName); if ( obj !== null ) { //Do some extra setup obj.setPosition(x,y); obj.setLayer(layer); //Let the new object be picked by next actions/conditions. if ( objectsLists.containsKey(objectName) ) { objectsLists.get(objectName).push(obj); } } }; /** * Allows events to create a new object on a scene. * @private */ gdjs.evtTools.object.createObjectOnScene = function(runtimeScene, objectsLists, x, y, layer) { gdjs.evtTools.object.doCreateObjectOnScene(runtimeScene, objectsLists.keys()[0], objectsLists, x, y, layer); }; /** * Allows events to create a new object on a scene. * @private */ gdjs.evtTools.object.createObjectFromGroupOnScene = function(runtimeScene, objectsLists, objectName, x, y, layer) { gdjs.evtTools.object.doCreateObjectOnScene(runtimeScene, objectName, objectsLists, x, y, layer); }; /** * Allows events to get the number of objects picked. * @private */ gdjs.evtTools.object.pickedObjectsCount = function(objectsLists) { var size = 0; var values = objectsLists.values(); for(var i = 0, len = values.length;i