terrainMap.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 TerrainMap::onAdd(%this)
  23. {
  24. %tileSize = 1.4;
  25. %this.mapSize = 75;
  26. %center = (%tileSize / 2) - ((%this.mapSize * %tileSize) / 2);
  27. %this.BatchCulling = true;
  28. %this.BatchLayout = "rect";
  29. %this.BatchIsolated = false;
  30. %this.BatchSortMode = "Batch";
  31. %this.DefaultSpriteSize = %tileSize;
  32. %this.DefaultSpriteStride = %tileSize;
  33. %this.Position = %center SPC %center;
  34. %this.BodyType = static;
  35. %this.Enabled = true;
  36. //Create the tile
  37. for(%y = 0; %y < %this.mapSize; %y++)
  38. {
  39. for(%x = 0; %x < %this.mapSize; %x++)
  40. {
  41. %this.addSprite(%x SPC %y);
  42. %this.setSpriteImage("ToyAssets:Blank");
  43. }
  44. }
  45. SandboxScene.add(%this);
  46. %this.colorMode = "Earth";
  47. }
  48. function TerrainMap::generate(%this, %generator, %octaveCount, %persistence)
  49. {
  50. for(%y = 0; %y < %this.mapSize; %y++)
  51. {
  52. for(%x = 0; %x < %this.mapSize; %x++)
  53. {
  54. %this.selectSprite(%x SPC %y);
  55. %this.setSpriteBlendColor(%this.getColor(%x, %y, %generator, %octaveCount, %persistence));
  56. }
  57. }
  58. }
  59. function TerrainMap::getColor(%this, %x, %y, %generator, %octaveCount, %persistence)
  60. {
  61. %zoomFactor = 0.1;
  62. %value = %generator.getComplexNoise(%x * %zoomFactor, %y * %zoomFactor, %octaveCount, %persistence);
  63. return %this.call("get" @ %this.colorMode @ "Color", %value);
  64. }
  65. function TerrainMap::getMoonColor(%this, %value)
  66. {
  67. return %value SPC %value SPC %value SPC "1";
  68. }
  69. function TerrainMap::getEarthColor(%this, %value)
  70. {
  71. if(%value > 0.7)
  72. {
  73. return (%value + 0.2) SPC (%value + 0.2) SPC (%value + 0.2) SPC "1";
  74. }
  75. else if(%value > 0.6)
  76. {
  77. %value = 0.4 + (%value / 10);
  78. return %value SPC %value SPC (%value + 0.05) SPC "1";
  79. }
  80. else if(%value > 0.5)
  81. {
  82. return (%value / 5) SPC (%value * 1.2) SPC (%value / 5) SPC "1";
  83. }
  84. else if(%value > 0.48)
  85. {
  86. return "1" SPC "0.9" SPC %value SPC "1";
  87. }
  88. else
  89. {
  90. %factor = %value / 0.48;
  91. %red = 0.1 * %factor;
  92. %green = 0.1 + (0.2 * %factor);
  93. %blue = 0.4 + (0.5 * %factor);
  94. return %red SPC %green SPC %blue SPC "1";
  95. }
  96. }
  97. function TerrainMap::getMarsColor(%this, %value)
  98. {
  99. return %value SPC (%value / 4) SPC "0" SPC "1";
  100. }
  101. function TerrainMap::getPlanetXColor(%this, %value)
  102. {
  103. if(%value > 0.45)
  104. {
  105. %factor = ((%value - 0.45) / 0.55);
  106. %red = 0.15 + (0.29 * %factor);
  107. %green = 0.1 - (0.05 * (1 - %factor));
  108. %blue = 0.23 + (0.45 * %factor);
  109. return %red SPC %green SPC %blue SPC "1";
  110. }
  111. else
  112. {
  113. %factor = %value / 0.45;
  114. %red = 0.1 * %factor;
  115. %green = 0.2 + (0.3 * %factor);
  116. %blue = 0.05 + (0.1 * %factor);
  117. return %red SPC %green SPC %blue SPC "1";
  118. }
  119. }