Panc, here you go:
–==============================================================
– Jumper setup
–==============================================================
local Grid = require (“jumper.grid”)
local Pathfinder = require (“jumper.pathfinder”)
local Node = require (“jumper.core.node”)
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 )
local map = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,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HOUSE.png”, 480, 320)
–==============================================================
– variables and forward references
–==============================================================
– most commonly used screen coordinates
– thanks to crawlSpaceLib for initial set
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local screenLeft = display.screenOriginX
local screenWidth = display.contentWidth - screenLeft * 2
local screenRight = screenLeft + screenWidth
local screenTop = display.screenOriginY
local screenHeight = display.contentHeight - screenTop * 2
local screenBottom = screenTop + screenHeight
local drawer
local mapStartPos = {}
local mapEndPos = {}
local tankPresent = false
local mode = “ORTHOGONAL”
local numSquaresChosen = {}
–==============================================================
– utilty functions
–==============================================================
– pass in pixel coords and get back the grid coords
– as a table: {x=n, y=n}
local function gridXYFromPixelXY(x,y)
local pos = {}
pos.x = math.floor((x)/32) + 1
if pos.x < 1 then pos.x = 1 end
pos.y = math.floor((y)/32) + 1
if pos.y < 1 then pos.y = 1 end
--print(“pos.x/y”, pos.x, pos.y, x, y)
return pos
end
– pass in grid coords and get back pixel pos for
– center of that square as a table: {x=n, y=n}
local function pixelXYFromGridXY(x,y)
local pos = {}
pos.x = math.floor((x)*32) -16
pos.y = math.floor((y)*32) -16
return pos
end
–==============================================================
– move the PLR from point to point, following the
– path returned from jumper.
–==============================================================
local function followPath()
PLR.idx = 2
PLR.myPath = {}
for node, count in PLR.pathNodes do
local xPos, yPos = node:getPos()
print((‘Step: %d - x: %d, y: %d’):format(count, xPos, yPos))
PLR.myPath[#PLR.myPath+1] = {x=xPos, y=yPos}
end
if #PLR.myPath > 1 then
local function nextStep(obj)
if PLR.idx < #PLR.myPath+1 then
local pos = pixelXYFromGridXY(PLR.myPath[PLR.idx].x,PLR.myPath[PLR.idx].y)
transition.to(PLR, {time=400, x=pos.x, y=pos.y, onComplete=nextStep})
if pos.x > PLR.x then
print(“moving right”)
character:setSequence(“walkright”)
character:play()
elseif
pos.x < PLR.x then
print(“moving left”)
elseif
pos.y > PLR.y then
print(“moving down”)
character:setSequence(“walkdown”)
character:play()
elseif
pos.y < PLR.y then
print(“moving up”)
end–>>v
mapStartPos = PLR.myPath[PLR.idx]–this is vital for current location being the start position
end
PLR.idx = PLR.idx + 1
end
nextStep()
end
end
–==============================================================
– the jumper code that finds the best path from
– startPos to endPos.
–==============================================================
local function goMapping(startPos, endPos)
local sx,sy = startPos.x, startPos.y
local ex,ey = endPos.x, endPos.y
local walkable = 0
local pather = Pathfinder(grid, ‘JPS’, walkable) – “DIJKSTRA”,“JPS”,“THETASTAR”,“BFS”,“DFS”,“ASTAR”
pather:setMode(mode) – ‘ORTHOGONAL’ ‘DIAGONAL’
local path = pather:getPath(sx,sy, ex,ey)
if path then
if mode == “DIAGONAL” then
path:fill()
end
PLR.pathNodes = path:nodes()
followPath()
print((‘Path found! Length: %.2f’):format(path:getLength()))
else
print((‘Path from [%d,%d] to [%d,%d] was : not found!’):format(sx,sy,ex,ey))
end
end
–==============================================================
– make the tile squares. add tap event to
– each so we can see the grid coords for
– that square.
–==============================================================
local function grassTouched(event)
local gridSquare = event.target
mapEndPos = gridXYFromPixelXY(event.target.x, event.target.y)
if tankPresent then
goMapping(mapStartPos, mapEndPos)
end
print("{x=" … gridSquare.gridPos.x … “, y=” … gridSquare.gridPos.y … “}”)
end
– create a rounded rectangle on the screen.
local function doSquare(x,y,c,s)
local sz = s or 32
local radius = 8
if sz == 32 then radius = 1 end
local square = display.newRoundedRect(0, 0, sz, sz, radius)
square.strokeWidth = 1
square:setStrokeColor(172, 172, 172)
square:setFillColor(c[1], c[2], c[3])
square.alpha = .50
square.x = (x * 32) - 16
square.y = (y * 32) - 16
square.gridPos = {x=col, y=row}
square.xyPos = {x=square.x, y=square.y}
square:addEventListener(“tap”, grassTouched)
return square
end
local function drawMap()
local colors = { {0, 0, 0}, {0, 5, 0} }
for x = 1, #map[1] do
for y = 1, #map do
local color = colors[map[y][x]+1] or {67, 149, 0}
gridSquare = doSquare(x,y,color, 32)
gridSquare.gridPos = {x=x, y=y}
end
end
end
local function makeDrawer()
local sheetData = { width=23, height=38, numFrames=6, sheetContentWidth=69, sheetContentHeight=78 }
local mySheet = graphics.newImageSheet( “Characters/Sam_walking_dwn-right.png”, sheetData )
local sequenceData = {
{ name = “walkdown”, start=1, count=3, loopCount = 1, time=900 },
{ name = “walkright”, start=4, count=6, loopCount = 1, time=900 }
}
character = display.newSprite( mySheet, sequenceData )
character:setSequence(“walkdown”)
PLR = character
local pos = gridXYFromPixelXY(PLR.x, PLR.y)
local pixX = pos.x * 32 - 16
local pixY = pos.y * 32 - 16
PLR.x = pixX
PLR.y = pixY
PLR.gridPos = pos – save the grid X/Y location
mapStartPos = pos
tankPresent = true
map[pos.y][pos.x] = 1
return true
end
drawMap()
makeDrawer()