|
- return function(Map)
- -- 创建地图菱形网格
- function Map:createGridNode()
- local vertexFormat = {};
- table.insert(vertexFormat , {size = 3 , usage = cc.VertexFormatUsage.POSITION});
- table.insert(vertexFormat , {size = 4 , usage = cc.VertexFormatUsage.COLOR});
- local vertices = ManualVertices:new(vertexFormat);
- vertices:setColor(cc.c4f(0,0,1,0.5));
- -- 画菱形
-
- -- 画横轴
- for x = 0, self.TileCountX do
- local begin_ = self:getPixelPosition(x,0);
- local end_ = self:getPixelPosition(x,self.TileCountY);
- vertices:drawLine(cc.vec3(begin_.x , 0 , begin_.y) , cc.vec3(end_.x , 0 , end_.y))
- end
- -- 画纵轴
- for y = 0, self.TileCountY do
- local begin_ = self:getPixelPosition(0,y);
- local end_ = self:getPixelPosition(self.TileCountX,y);
- vertices:drawLine(cc.vec3(begin_.x , 0 , begin_.y) , cc.vec3(end_.x , 0 , end_.y))
- end
-
- -- 画一个矩形
- vertices:drawLine(cc.vec3(0,0,0), cc.vec3(self.MapWidth,0,0));
- vertices:drawLine(cc.vec3(self.MapWidth,0,0), cc.vec3(self.MapWidth,0, -self.MapHeight));
- vertices:drawLine(cc.vec3(self.MapWidth,0, -self.MapHeight), cc.vec3(0,0,-self.MapHeight));
- vertices:drawLine(cc.vec3(0,0,0), cc.vec3(0,0,-self.MapHeight));
-
- local mesh = cc.Mesh:createMesh(vertexFormat , vertices:getVertexCount());
- mesh:setPrimitiveType(cc.PrimitiveType.LINES);
- mesh:setVertexData(vertices:getVertices() , 0 , vertices:getVertexCount());
- mesh:setBoundingBox(vertices:getBoundingBox());
-
- local model = cc.Model:create(mesh);
- local material = cc.Material:create("preload/shaders/ccDefault.material#ShaderPositionColor3D");
- material:setParameterAutoBinding("CC_MVPMatrix" , "WORLD_VIEW_PROJECTION_MATRIX");
- model:setMaterialPointer(material);
-
- local node = cc.Node3D:create();
- node:addComponent(model);
-
- return node;
- end
-
- -- 创建网格上的文本
- function Map:createTextNode()
- local node = cc.Node:create();
- -- 横轴
- for x = 0, self.TileCountX do
- -- 纵轴
- for y = 0, self.TileCountY do
- local pos = self:getPixelPosition(x,y);
- local debugLabel = cc.Text:createNode();
- debugLabel:setDefaults();
- local config = debugLabel:getFontConfig();
- config.fontSize = 12;
- debugLabel:setFontConfig(config);
-
- debugLabel:setAnchorPoint(cc.p(0.5,0.5));
- debugLabel:setTranslation(pos.x , 0 , pos.y);
- debugLabel:setEulerRotation(-90,0,0);
- debugLabel:setScale(0.03);
- -- TODO这里需要显示逻辑坐标就可以了
- debugLabel:setText(tostring(x) .. "," .. tostring(y));
- node:addChild(debugLabel);
- end
- end
- return node;
- end
-
- -- 创建障碍块和可见区域
- function Map:createObstacleNode()
- local node = cc.Node3D:create();
- -- 画障碍块
- local vertexFormat = {};
- table.insert(vertexFormat , {size = 3 , usage = cc.VertexFormatUsage.POSITION});
- table.insert(vertexFormat , {size = 4 , usage = cc.VertexFormatUsage.COLOR});
-
- local material = cc.Material:create("preload/shaders/ccDefault.material#ShaderPositionColor3D");
- material:setParameterAutoBinding("CC_MVPMatrix" , "WORLD_VIEW_PROJECTION_MATRIX");
- local mesh = cc.MeshBatch:create(vertexFormat , cc.PrimitiveType.TRIANGLES , material , false);
- node:addComponent(mesh);
-
- local function drawOb(vertices , x , y)
- local leftTop = self:getPixelPosition(x,y);
- local rightTop = self:getPixelPosition(x + 1,y);
- local leftBottom = self:getPixelPosition(x,y + 1);
- local rightBottom = self:getPixelPosition(x + 1,y + 1);
- vertices:drawRect(cc.vec3(leftTop.x , 0 , leftTop.y)
- , cc.vec3(rightTop.x , 0 , rightTop.y)
- , cc.vec3(leftBottom.x , 0 , leftBottom.y)
- , cc.vec3(rightBottom.x , 0 , rightBottom.y));
- end
-
- -- 更新障碍块的顶点信息
- local function updateObstacle()
- local vertices = ManualVertices:new(vertexFormat);
- local x , y;
- -- 画障碍块
- for y = 0 , self.TileCountY - 1 do
- for x = 0 , self.TileCountX - 1 do
- if self.obstacles:isBlock(x,y) then
- vertices:setColor(cc.c4f(1,1,0,1));
- else
- vertices:setColor(cc.c4f(0,0,0,0));
- end
- drawOb(vertices , x , y);
- end
- end
- mesh:start();
- mesh:add(vertices:getVertices() , vertices:getVertexCount());
- mesh:finish();
- end
-
- -- 更新一个障碍块
- local function updateOne(x , y , val)
- local vertices = ManualVertices:new(vertexFormat);
- if val ~= Map.ObstacleType.ObstacleNone then
- vertices:setColor(cc.c4f(1,1,0,1));
- else
- vertices:setColor(cc.c4f(0,0,0,0));
- end
- drawOb(vertices , x , y);
- mesh:updateVertex((self.TileCountX * y + x) * vertices:getVertexCount() , vertices:getVertexCount() , vertices:getVertices());
- end
-
- self.ObstacleChanged = updateOne;
-
- updateObstacle();
- return node;
- end
- end
|