spawnAreaBehavior.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 SpawnAreaBehavior::initialize(%this, %object, %count, %spawnTime, %spawnVariance, %autoSpawn, %spawnLocation)
  23. {
  24. %this.object = %object;
  25. %this.count = %count;
  26. %this.spawnTime = %spawnTime;
  27. %this.spawnVariance = %spawnVariance;
  28. %this.autoSpawn = %autoSpawn;
  29. %this.spawnLocation = %spawnLocation;
  30. }
  31. //-----------------------------------------------------------------------------
  32. function SpawnAreaBehavior::onBehaviorAdd(%this, %scenegraph)
  33. {
  34. %this.spawnCount = 0;
  35. %this.schedule(%this.spawnStart * 1000, "spawn");
  36. }
  37. //-----------------------------------------------------------------------------
  38. function SpawnAreaBehavior::spawn(%this)
  39. {
  40. if (!isObject(%this.object) || !%this.owner.enabled)
  41. return;
  42. %clone = %this.object.clone();
  43. SandboxScene.add(%clone);
  44. %clone.setEnabled(1);
  45. %xPos = 0;
  46. %yPos = 0;
  47. %spawnLocation = %this.spawnLocation;
  48. %edges = "Top" TAB "Bottom" TAB "Left" TAB "Right";
  49. if (%spawnLocation $= "Edges")
  50. %spawnLocation = getField(%edges, getRandom(0, 3));
  51. switch$ (%spawnLocation)
  52. {
  53. case "Area":
  54. %xPos = getRandom(getWord(%this.owner.getAreaMin(), 0), getWord(%this.owner.getAreaMax(), 0));
  55. %yPos = getRandom(getWord(%this.owner.getAreaMin(), 1), getWord(%this.owner.getAreaMax(), 1));
  56. case "Center":
  57. %xPos = %this.owner.position.x;
  58. %yPos = %this.owner.position.y;
  59. case "Top":
  60. %xPos = getRandom(getWord(%this.owner.getAreaMin(), 0), getWord(%this.owner.getAreaMax(), 0));
  61. %yPos = getWord(%this.owner.getAreaMin(), 1);
  62. case "Bottom":
  63. %xPos = getRandom(getWord(%this.owner.getAreaMin(), 0), getWord(%this.owner.getAreaMax(), 0));
  64. %yPos = getWord(%this.owner.getAreaMax(), 1);
  65. case "Left":
  66. %xPos = getWord(%this.owner.getAreaMin(), 0);
  67. %yPos = getRandom(getWord(%this.owner.getAreaMin(), 1), getWord(%this.owner.getAreaMax(), 1));
  68. case "Right":
  69. %xPos = getWord(%this.owner.getAreaMax(), 0);
  70. %yPos = getRandom(getWord(%this.owner.getAreaMin(), 1), getWord(%this.owner.getAreaMax(), 1));
  71. }
  72. %clone.position = %xPos SPC %yPos;
  73. %this.spawnCount++;
  74. if (%this.spawnCount < %this.count || %this.count == -1)
  75. {
  76. %minTime = (%this.spawnTime - %this.spawnVariance) * 1000;
  77. %maxTime = (%this.spawnTime + %this.spawnVariance) * 1000;
  78. %spawnTime = getRandom(%minTime, %maxTime);
  79. if( %spawnTime < 55 )
  80. %spawnTime = 55;
  81. %this.schedule(%spawnTime, "spawn");
  82. }
  83. }