mapgen.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var MapGen_1 = function(map, game) {
  2. //map.texCodes[128+ (128*256)] = 1;
  3. // make some blocks
  4. for(var x = 098; x <= 137; x+= 13) {
  5. for(var y = 095; y <= 137; y+= 13) {
  6. genblock(pt(x,y));
  7. }
  8. }
  9. var n1 = game.aiPaths.addNode(pt(119,126));
  10. var n2 = game.aiPaths.addNode(pt(129,126));
  11. var n3 = game.aiPaths.addNode(pt(129,116));
  12. var n4 = game.aiPaths.addNode(pt(119,116));
  13. game.aiPaths.addEdge(n1, n2, true);
  14. game.aiPaths.addEdge(n2, n3, true);
  15. game.aiPaths.addEdge(n3, n4, true);
  16. game.aiPaths.addEdge(n4, n1, true);
  17. renderToTexmap();
  18. function genblock(target) {
  19. var house = game.spawn(Entities.susieshouse);
  20. game.setComp(house, 'position', {x: target.x, y: target.y });
  21. map.fillRectLay(target.x - 6, target.y - 6, target.x + 6, target.y + 6, 'road');
  22. map.fillRectLay(target.x - 5, target.y - 5, target.x + 5, target.y + 5, 'sidewalk');
  23. map.fillRectLay(target.x - 4, target.y - 4, target.x + 4, target.y + 4, 'grass');
  24. // map.andLayAt(121, 127, 'sidewalk');
  25. map.orLayAt(target.x - 5, target.y + 6, 'sidewalk');
  26. map.orLayAt(target.x + 5, target.y + 6, 'sidewalk');
  27. map.orLayAt(target.x - 6, target.y + 5, 'sidewalk');
  28. map.orLayAt(target.x + 6, target.y + 5, 'sidewalk');
  29. map.orLayAt(target.x - 5, target.y - 6, 'sidewalk');
  30. map.orLayAt(target.x + 5, target.y - 6, 'sidewalk');
  31. map.orLayAt(target.x - 6, target.y - 5, 'sidewalk');
  32. map.orLayAt(target.x + 6, target.y - 5, 'sidewalk');
  33. }
  34. function above(x,y) {
  35. if(y >= map.size.y - 1) return -1;
  36. return x + (y+1)* map.size.x;
  37. }
  38. function below(x,y) {
  39. if(y <= 0) return -1;
  40. return x + (y-1)* map.size.x;
  41. }
  42. function right(x,y) {
  43. if(x >= map.size.x - 1) return -1;
  44. return (x+1) + y * map.size.x;
  45. }
  46. function left(x,y) {
  47. if(x <= 0) return -1;
  48. return (x-1) + y * map.size.x;
  49. }
  50. function isAbove(x, y, t) {
  51. if(y >= map.size.y - 1) return 0;
  52. return map.layout[x + (y+1)* map.size.x] & t ? 1 : 0;
  53. }
  54. function isBelow(x, y, t) {
  55. if(y <= 0) return 0;
  56. return map.layout[x + (y-1)* map.size.x] & t ? 1 : 0;
  57. }
  58. function isRight(x, y, t) {
  59. if(x >= map.size.x - 1) return 0;
  60. return (map.layout[(x+1) + y * map.size.x]|0) & (t|0) ? 1 : 0;
  61. }
  62. function isLeft(x, y, t) {
  63. if(x <= 0) return 0;
  64. return map.layout[(x-1) + y * map.size.x] & t ? 1 : 0;
  65. }
  66. function getAt(x, y) {
  67. return map.layout[x + y * map.size.x];
  68. }
  69. function resolveTile(x, y) {
  70. var t = getAt(x, y);
  71. var tinfo = map.texLayMapping[map.layoutCodes_inv[t]];
  72. // return out on simple textures
  73. if(typeof tinfo == 'string') {
  74. return map.texImgMapping_inv[tinfo];// the tex code for the string name
  75. }
  76. // fancy directional tiles like sidewalks
  77. if(tinfo.type == 'directional') {
  78. var N = isAbove(x, y, t),
  79. S = isBelow(x, y, t),
  80. E = isRight(x, y, t),
  81. W = isLeft(x, y, t);
  82. var suffix = N + '' + S + '' + E + '' + W;
  83. return map.texImgMapping_inv[tinfo.prefix + '' + suffix];
  84. }
  85. }
  86. function renderToTexmap() {
  87. for(var y = 0; y < map.size.y; y++) {
  88. for(var x = 0; x < map.size.x; x++) {
  89. map.texCodes[x + y * map.size.x] = resolveTile(x, y);
  90. }
  91. }
  92. }
  93. }