You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.6 KiB

  1. return function(Map)
  2. -- 创建地图菱形网格
  3. function Map:createGridNode()
  4. local vertexFormat = {};
  5. table.insert(vertexFormat , {size = 3 , usage = cc.VertexFormatUsage.POSITION});
  6. table.insert(vertexFormat , {size = 4 , usage = cc.VertexFormatUsage.COLOR});
  7. local vertices = ManualVertices:new(vertexFormat);
  8. vertices:setColor(cc.c4f(0,0,1,0.5));
  9. -- 画菱形
  10. -- 画横轴
  11. for x = 0, self.TileCountX do
  12. local begin_ = self:getPixelPosition(x,0);
  13. local end_ = self:getPixelPosition(x,self.TileCountY);
  14. vertices:drawLine(cc.vec3(begin_.x , 0 , begin_.y) , cc.vec3(end_.x , 0 , end_.y))
  15. end
  16. -- 画纵轴
  17. for y = 0, self.TileCountY do
  18. local begin_ = self:getPixelPosition(0,y);
  19. local end_ = self:getPixelPosition(self.TileCountX,y);
  20. vertices:drawLine(cc.vec3(begin_.x , 0 , begin_.y) , cc.vec3(end_.x , 0 , end_.y))
  21. end
  22. -- 画一个矩形
  23. vertices:drawLine(cc.vec3(0,0,0), cc.vec3(self.MapWidth,0,0));
  24. vertices:drawLine(cc.vec3(self.MapWidth,0,0), cc.vec3(self.MapWidth,0, -self.MapHeight));
  25. vertices:drawLine(cc.vec3(self.MapWidth,0, -self.MapHeight), cc.vec3(0,0,-self.MapHeight));
  26. vertices:drawLine(cc.vec3(0,0,0), cc.vec3(0,0,-self.MapHeight));
  27. local mesh = cc.Mesh:createMesh(vertexFormat , vertices:getVertexCount());
  28. mesh:setPrimitiveType(cc.PrimitiveType.LINES);
  29. mesh:setVertexData(vertices:getVertices() , 0 , vertices:getVertexCount());
  30. mesh:setBoundingBox(vertices:getBoundingBox());
  31. local model = cc.Model:create(mesh);
  32. local material = cc.Material:create("preload/shaders/ccDefault.material#ShaderPositionColor3D");
  33. material:setParameterAutoBinding("CC_MVPMatrix" , "WORLD_VIEW_PROJECTION_MATRIX");
  34. model:setMaterialPointer(material);
  35. local node = cc.Node3D:create();
  36. node:addComponent(model);
  37. return node;
  38. end
  39. -- 创建网格上的文本
  40. function Map:createTextNode()
  41. local node = cc.Node:create();
  42. -- 横轴
  43. for x = 0, self.TileCountX do
  44. -- 纵轴
  45. for y = 0, self.TileCountY do
  46. local pos = self:getPixelPosition(x,y);
  47. local debugLabel = cc.Text:createNode();
  48. debugLabel:setDefaults();
  49. local config = debugLabel:getFontConfig();
  50. config.fontSize = 12;
  51. debugLabel:setFontConfig(config);
  52. debugLabel:setAnchorPoint(cc.p(0.5,0.5));
  53. debugLabel:setTranslation(pos.x , 0 , pos.y);
  54. debugLabel:setEulerRotation(-90,0,0);
  55. debugLabel:setScale(0.03);
  56. -- TODO这里需要显示逻辑坐标就可以了
  57. debugLabel:setText(tostring(x) .. "," .. tostring(y));
  58. node:addChild(debugLabel);
  59. end
  60. end
  61. return node;
  62. end
  63. -- 创建障碍块和可见区域
  64. function Map:createObstacleNode()
  65. local node = cc.Node3D:create();
  66. -- 画障碍块
  67. local vertexFormat = {};
  68. table.insert(vertexFormat , {size = 3 , usage = cc.VertexFormatUsage.POSITION});
  69. table.insert(vertexFormat , {size = 4 , usage = cc.VertexFormatUsage.COLOR});
  70. local material = cc.Material:create("preload/shaders/ccDefault.material#ShaderPositionColor3D");
  71. material:setParameterAutoBinding("CC_MVPMatrix" , "WORLD_VIEW_PROJECTION_MATRIX");
  72. local mesh = cc.MeshBatch:create(vertexFormat , cc.PrimitiveType.TRIANGLES , material , false);
  73. node:addComponent(mesh);
  74. local function drawOb(vertices , x , y)
  75. local leftTop = self:getPixelPosition(x,y);
  76. local rightTop = self:getPixelPosition(x + 1,y);
  77. local leftBottom = self:getPixelPosition(x,y + 1);
  78. local rightBottom = self:getPixelPosition(x + 1,y + 1);
  79. vertices:drawRect(cc.vec3(leftTop.x , 0 , leftTop.y)
  80. , cc.vec3(rightTop.x , 0 , rightTop.y)
  81. , cc.vec3(leftBottom.x , 0 , leftBottom.y)
  82. , cc.vec3(rightBottom.x , 0 , rightBottom.y));
  83. end
  84. -- 更新障碍块的顶点信息
  85. local function updateObstacle()
  86. local vertices = ManualVertices:new(vertexFormat);
  87. local x , y;
  88. -- 画障碍块
  89. for y = 0 , self.TileCountY - 1 do
  90. for x = 0 , self.TileCountX - 1 do
  91. if self.obstacles:isBlock(x,y) then
  92. vertices:setColor(cc.c4f(1,1,0,1));
  93. else
  94. vertices:setColor(cc.c4f(0,0,0,0));
  95. end
  96. drawOb(vertices , x , y);
  97. end
  98. end
  99. mesh:start();
  100. mesh:add(vertices:getVertices() , vertices:getVertexCount());
  101. mesh:finish();
  102. end
  103. -- 更新一个障碍块
  104. local function updateOne(x , y , val)
  105. local vertices = ManualVertices:new(vertexFormat);
  106. if val ~= Map.ObstacleType.ObstacleNone then
  107. vertices:setColor(cc.c4f(1,1,0,1));
  108. else
  109. vertices:setColor(cc.c4f(0,0,0,0));
  110. end
  111. drawOb(vertices , x , y);
  112. mesh:updateVertex((self.TileCountX * y + x) * vertices:getVertexCount() , vertices:getVertexCount() , vertices:getVertices());
  113. end
  114. self.ObstacleChanged = updateOne;
  115. updateObstacle();
  116. return node;
  117. end
  118. end