GameObjectManager.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. function execGameObjects()
  23. {
  24. //find all GameObjectAssets
  25. %assetQuery = new AssetQuery();
  26. if(!AssetDatabase.findAssetType(%assetQuery, "GameObjectAsset"))
  27. return; //if we didn't find ANY, just exit
  28. %count = %assetQuery.getCount();
  29. for(%i=0; %i < %count; %i++)
  30. {
  31. %assetId = %assetQuery.getAsset(%i);
  32. %gameObjectAsset = AssetDatabase.acquireAsset(%assetId);
  33. if(isFile(%gameObjectAsset.scriptFilePath))
  34. exec(%gameObjectAsset.scriptFilePath);
  35. }
  36. }
  37. function findGameObject(%name)
  38. {
  39. //find all GameObjectAssets
  40. %assetQuery = new AssetQuery();
  41. if(!AssetDatabase.findAssetType(%assetQuery, "GameObjectAsset"))
  42. return 0; //if we didn't find ANY, just exit
  43. %count = %assetQuery.getCount();
  44. for(%i=0; %i < %count; %i++)
  45. {
  46. %assetId = %assetQuery.getAsset(%i);
  47. %gameObjectAsset = AssetDatabase.acquireAsset(%assetId);
  48. if(%gameObjectAsset.gameObjectName $= %name)
  49. {
  50. if(isFile(%gameObjectAsset.TAMLFilePath))
  51. {
  52. return %gameObjectAsset;
  53. }
  54. }
  55. }
  56. return 0;
  57. }
  58. function spawnGameObject(%name, %addToMissionGroup)
  59. {
  60. if(%addToMissionGroup $= "")
  61. %addToMissionGroup = true;
  62. %gameObjectAsset = findGameObject(%name);
  63. if(isObject(%gameObjectAsset))
  64. {
  65. %newSGOObject = TamlRead(%gameObjectAsset.TAMLFilePath);
  66. if(%addToMissionGroup == true) //save instance when saving level
  67. MissionGroup.add(%newSGOObject);
  68. else // clear instance on level exit
  69. MissionCleanup.add(%newSGOObject);
  70. return %newSGOObject;
  71. }
  72. return 0;
  73. }
  74. function saveGameObject(%name, %tamlPath, %scriptPath)
  75. {
  76. %gameObjectAsset = findGameObject(%name);
  77. //find if it already exists. If it does, we'll update it, if it does not, we'll make a new asset
  78. if(isObject(%gameObjectAsset))
  79. {
  80. %assetID = %gameObjectAsset.getAssetId();
  81. %gameObjectAsset.TAMLFilePath = %tamlPath;
  82. %gameObjectAsset.scriptFilePath = %scriptPath;
  83. TAMLWrite(%gameObjectAsset, AssetDatabase.getAssetFilePath(%assetID));
  84. AssetDatabase.refreshAsset(%assetID);
  85. }
  86. else
  87. {
  88. //Doesn't exist, so make a new one
  89. %gameObjectAsset = new GameObjectAsset()
  90. {
  91. assetName = %name @ "Asset";
  92. gameObjectName = %name;
  93. TAMLFilePath = %tamlPath;
  94. scriptFilePath = %scriptPath;
  95. };
  96. //Save it alongside the taml file
  97. %path = filePath(%tamlPath);
  98. TAMLWrite(%gameObjectAsset, %path @ "/" @ %name @ ".asset.taml");
  99. AssetDatabase.refreshAllAssets(true);
  100. }
  101. }