2
0
Эх сурвалжийг харах

Fixed Entity.GetComponent, Entity.NumComponents so they use real 'T' type not just T.Type ComponentType.

Mark Sibly 7 жил өмнө
parent
commit
3b53d0dc01

+ 1 - 1
modules/mojo3d/scene/components/animator.monkey2

@@ -5,7 +5,7 @@ Class Entity Extension
 	
 	
 	Property Animator:Animator()
 	Property Animator:Animator()
 		
 		
-		Return Cast<Animator>( GetComponent( Animator.Type ) )
+		Return GetComponent<Animator>()
 	End
 	End
 	
 	
 End
 End

+ 1 - 1
modules/mojo3d/scene/components/collider.monkey2

@@ -14,7 +14,7 @@ Class Entity Extension
 	
 	
 	Property Collider:Collider()
 	Property Collider:Collider()
 		
 		
-		Return Cast<Collider>( GetComponent( Collider.Type ) )
+		Return GetComponent<Collider>()
 	End
 	End
 	
 	
 End
 End

+ 1 - 1
modules/mojo3d/scene/components/rigidbody.monkey2

@@ -39,7 +39,7 @@ Class Entity Extension
 	
 	
 	Property RigidBody:RigidBody()
 	Property RigidBody:RigidBody()
 		
 		
-		Return Cast<RigidBody>( GetComponent( RigidBody.Type ) )
+		Return GetComponent<RigidBody>()
 	End
 	End
 	
 	
 End
 End

+ 21 - 19
modules/mojo3d/scene/entity.monkey2

@@ -410,41 +410,43 @@ Class Entity Extends DynamicObject
 
 
 	#rem monkeydoc Gets the number of components of a given type attached to the entity.
 	#rem monkeydoc Gets the number of components of a given type attached to the entity.
 	#end
 	#end
-	Method NumComponents:Int( type:ComponentType )
+	Method NumComponents<T>:Int() Where T Extends Component
 		
 		
 		Local n:=0
 		Local n:=0
 		For Local c:=Eachin _components
 		For Local c:=Eachin _components
-			If c.Type=type n+=1
+			If Cast<T>( c ) n+=1
 		Next
 		Next
-		Return n
-	End
-
-	Method NumComponents<T>:Int() Where T Extends Component
 		
 		
-		Return NumComponents( T.Type )
+		Return n
 	End
 	End
-
+	
 	#rem monkeydoc Gets a component of a given type attached to the entity.
 	#rem monkeydoc Gets a component of a given type attached to the entity.
 	
 	
-	If there is not exactly one component of the given type attached to the entity, null is returned.
+	If there is more than one component of the given type attached, the first is returned.
 
 
 	#end	
 	#end	
-	Method GetComponent:Component( type:ComponentType )
-
-		Local t:Component
-				
+	Method GetComponent<T>:T() Where T Extends Component
+		
 		For Local c:=Eachin _components
 		For Local c:=Eachin _components
-			If c.Type<>type Continue
-			If t Return Null
-			t=c
+			Local t:=Cast<T>( c )
+			If t Return t
 		Next
 		Next
 		
 		
-		Return t
+		Return Null
 	End
 	End
 	
 	
-	Method GetComponent<T>:T() Where T Extends Component
+	Method GetComponents<T>:T[]() Where T Extends Component
+		
+		Local cs:=New Component[NumComponents<T>()],i:=0
+		
+		For Local c:=Eachin _components
+			Local t:=Cast<T>( c ) 
+			If Not t Continue
+			cs[i]=t
+			i+=1
+		Next
 		
 		
-		Return Cast<T>( GetComponent( T.Type ) )
+		Return cs
 	End
 	End
 	
 	
 	#rem monkeydoc Attaches a component to the entity.
 	#rem monkeydoc Attaches a component to the entity.