1 /**************************************************************************** 2 Copyright (c) 2013 cocos2d-x.org 3 4 http://www.cocos2d-x.org 5 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 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 ****************************************************************************/ 24 25 cc.ComponentContainer = cc.Class.extend({ 26 _components:null, 27 _owner:null, 28 29 ctor:function(node){ 30 this._components = null; 31 this._owner = node; 32 }, 33 34 getComponent:function(name){ 35 cc.Assert(name != null, "Argument must be non-nil"); 36 name = name.trim(); 37 return this._components[name]; 38 }, 39 40 add:function(component){ 41 cc.Assert(component != null, "Argument must be non-nil"); 42 cc.Assert(component.getOwner() == null, "Component already added. It can't be added again"); 43 if(this._components == null){ 44 this._components = {}; 45 this._owner.scheduleUpdate(); 46 } 47 var oldComponent = this._components[component.getName()]; 48 if(oldComponent){ 49 cc.Assert(oldComponent == null, "Component already added. It can't be added again"); 50 return false; 51 } 52 component.setOwner(this._owner); 53 this._components[component.getName()] = component; 54 component.onEnter(); 55 return true; 56 }, 57 58 remove:function(name){ 59 cc.Assert(name != null, "Argument must be non-nil"); 60 if(!this._components) 61 return false; 62 var locComponents = this._components; 63 name = name.trim(); 64 var component = locComponents[name]; 65 if(component) 66 return false; 67 component.onExit(); 68 component.setOwner(null); 69 delete locComponents[name]; 70 return true; 71 }, 72 73 removeAll:function(){ 74 if(!this._components) 75 return; 76 77 var locComponents = this._components; 78 for(var selKey in locComponents){ 79 var selComponent = locComponents[selKey]; 80 selComponent.onExit(); 81 selComponent.setOwner(null); 82 delete locComponents[selKey]; 83 } 84 this._owner.unscheduleUpdate(); 85 this._components = null; 86 }, 87 88 _alloc:function(){ 89 this._components = {}; 90 }, 91 92 visit:function(delta){ 93 if(!this._components) 94 return; 95 96 var locComponents = this._components; 97 for(var selKey in locComponents){ 98 locComponents[selKey].update(delta); 99 } 100 }, 101 102 isEmpty:function(){ 103 if(!this._components) 104 return true; 105 106 for(var selkey in this._components){ 107 return false; 108 } 109 return true; 110 } 111 }); 112 113 114