1 /** 2 * Contains Prefabs and Prefab, manages creation and management of prefabs. 3 */ 4 module dash.core.prefabs; 5 import dash.core, dash.components, dash.utility; 6 7 mixin( registerComponents!() ); 8 9 /** 10 * Prefabs manages prefabs and allows access to them. 11 */ 12 final abstract class Prefabs 13 { 14 static: 15 public: 16 /// The AA of prefabs. 17 Prefab[string] prefabs; 18 19 /// Allows functions to be called on this like it were the AA. 20 alias prefabs this; 21 22 // Not sure what this is all about, but opIndex no longer forwards as of 2.066. 23 static if( __VERSION__ > 2065 ) 24 { 25 Prefab opIndex( string index ) 26 { 27 if( auto fab = index in prefabs ) 28 { 29 return *fab; 30 } 31 else 32 { 33 warningf( "Prefab %s not found.", index ); 34 return null; 35 } 36 } 37 38 Prefab opIndexAssign( Prefab newFab, string index ) 39 { 40 prefabs[ index ] = newFab; 41 return newFab; 42 } 43 } 44 45 /** 46 * Load and initialize all prefabs in FilePath.Resources.Prefabs. 47 */ 48 void initialize() 49 { 50 foreach( key; prefabs.keys ) 51 prefabs.remove( key ); 52 53 foreach( res; scanDirectory( Resources.Prefabs ) ) 54 { 55 foreach( fabDesc; res.deserializeMultiFile!( GameObject.Description )() ) 56 { 57 auto newFab = new Prefab( fabDesc, res ); 58 prefabs[ newFab.name ] = newFab; 59 prefabResources[ res ] ~= newFab; 60 } 61 } 62 } 63 64 /** 65 * Refreshes prefabs that are outdated. 66 */ 67 void refresh() 68 { 69 //TODO: Implement 70 } 71 72 private: 73 Prefab[][Resource] prefabResources; 74 } 75 76 /** 77 * A prefab that allows for quick object creation. 78 */ 79 final class Prefab : Asset 80 { 81 public: 82 /// The name of the prefab. 83 const(string) name() @property 84 { 85 return description.name; 86 } 87 /// The description to create objects from. 88 GameObject.Description description; 89 90 /// Creates a prefab from a description. 91 this( GameObject.Description desc, Resource filePath ) 92 { 93 description = desc; 94 super( filePath ); 95 } 96 97 /** 98 * Creates a GameObject instance from the prefab. 99 * 100 * Returns: 101 * The new GameObject from the Prefab. 102 */ 103 GameObject createInstance() const 104 { 105 return GameObject.create( description ); 106 } 107 }