I’ve been told that this is one of the ways to add a mesh for every enemy I have spawned in a game, if you need context, link to the original question is:
http://www.twolivesleft.com/Codea/Talk/discussion/4061/multiple-drawings#Item_6
Otherwise, is there anyway to apply a sprite to a mesh? If not, how can I apply this to a small engine?
This is a tough one. Several things need to be taken into acct. How many rectangles? Will they overlap and need z-sorting?
I use 4 atlas maps (make in any paint program) One for characters, tile map, detail objects, and fx. I have one mesh for each layer and animate the recangles in the mesh. So far no slow down. I only draw about 400 rectangles. I’m sure I can get this past 1000, but the rectangles are only 66px wide and tall. Use tables for each layer and either flag the individual rectangle them to be drawn off the screen. I would move away from sprites after you have total control over them and use meshs. But it depends on how many enemes you want on the screen at once.
can do the same, when you apply a mesh () to a cube for example
is the most common way of applying images to mesh () is only one example.
self._C = mesh()
self._C.vertices = vertCube
self._C.texture = "img"
self._C.texCoords = cubetexCoords.
self._C:setColors(255,255,255,255)
This is an older version of code that creates mesh objects with texture.
The image size is 660 and the tiles are 66px high and wide, or 1/10th of the total image size. just change the nominator of the fraction to chage the position. Read up on setRectTex on how the cordinates work. There are plenty of tutorials around.
world[r][c] is the list for the imported map from tiled(super awesome) map editor
grid[r][c] is a list for the astar and los routines. The other variables are self named, but this should give you some ideas
for r = 1 ,mapSizeX do
grid[r] = {}
for c = 1,mapSizeY do
cnt = cnt + 1 --the mesh rectangle count or rectangle id
local pos =vec2(r,c)
-- tType = world[r][c]
-- print("world[r][c] "..world[r][c].." pos.x "..pos.x.."r:"..r.." c:"..c)
grid[r][c] = mapData(cnt,pos,world[r][c],0,0) --print(grid[r][c
end
end
--mapData(cnt,pos,math.random(2))
-- set up GameMap tiles
cnt = 0--mapDisplayX*mapDisplayY
myMesh = mesh()
myMesh.texture = img -- the reference to the atlas image
for r = 1, mapDisplayX do
gameMap[r] = {}
for c = 1, mapDisplayY do
cnt = cnt + 1
meshNewRect(myMesh, (r-mapOffsetX)*tileSize-(tileSize/2),(c-mapOffsetY)*tileSize-(tileSize/2), tileSize,tileSize)
-- print(to_string(grid[r][c].tileType))
if grid[r][c].tileType == 1 then
myMesh:setRectTex(cnt,1/10,9/10,66/660,66/660)
elseif grid[r][c].tileType == 4 then
-- print("4")
myMesh:setRectTex(cnt,1/10,9/10,66/660,66/660)
end
gameMap[r][c]= cnt
end
--- cnt = cnt + 1
-- print("x:"..tX.." y:"..tY.." cnt"..cnt)
-- meshNewRect(myMesh, (tX-mapOffsetX)*tileSize-(tileSize/2),(tY-mapOffsetY)*tileSize-(tileSize/2), tileSize,tileSize)
-- myMesh:setRectTex(cnt,4/10,9/10,66/660,66/660)
end
print("cout "..cnt)
pos = vec2(2,2)
cnt = cnt + 1
if createPlayer == 0 then
print("cnt "..cnt)
player = Player(cnt,pos,5,10,1)
table.insert(objectGrid,player)
createPlayer = 1
print("createPlayer "..createPlayer)
meshNewRect(myMesh, (pos.x-mapOffsetX)*tileSize-(tileSize/2),(pos.y-mapOffsetY)*tileSize-(tileSize/2), tileSize,tileSize)
myMesh:setRectTex(cnt,1/10,9/10,66/660,66/660)
print("createPlayer2 "..createPlayer)
cnt = cnt + 1
pos1 = vec2(8,8)
playerZ = Player(cnt,pos1,2,4,1)
table.insert(objectGrid,playerZ)
meshNewRect(myMesh, (pos.x-mapOffsetX)*tileSize-(tileSize/2),(pos.y-mapOffsetY)*tileSize-(tileSize/2), tileSize,tileSize)
myMesh:setRectTex(cnt,2/10,9/10,66/660,66/660)
end
end