-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtiledmaphandler.lua
More file actions
123 lines (100 loc) · 3.56 KB
/
tiledmaphandler.lua
File metadata and controls
123 lines (100 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
--
-- tiledmaphandler.lua
-- lua-astar
--
-- Created by Jay Roberts on 2011-01-12.
-- Copyright 2011 GloryFish.org. All rights reserved.
--
require 'middleclass'
TiledMapHandler = class('TiledMapHandler')
function TiledMapHandler:initialize()
self.tiles = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,1,1,1,1,1,0,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,1,0,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
}
end
function TiledMapHandler:getNode(location)
-- Here you make sure the requested node is valid (i.e. on the map, not blocked)
if location.x > #self.tiles[1] or location.y > #self.tiles then
-- print 'location is outside of map on right or bottom'
return nil
end
if location.x < 1 or location.y < 1 then
-- print 'location is outside of map on left or top'
return nil
end
if self.tiles[location.y][location.x] == 1 then
-- print(string.format('location is solid: (%i, %i)', location.x, location.y))
return nil
end
return Node(location, 1, location.y * #self.tiles[1] + location.x)
end
function TiledMapHandler:getAdjacentNodes(curnode, dest)
-- Given a node, return a table containing all adjacent nodes
-- The code here works for a 2d tile-based game but could be modified
-- for other types of node graphs
local result = {}
local cl = curnode.location
local dl = dest
local n = false
n = self:_handleNode(cl.x + 1, cl.y, curnode, dl.x, dl.y)
if n then
table.insert(result, n)
end
n = self:_handleNode(cl.x - 1, cl.y, curnode, dl.x, dl.y)
if n then
table.insert(result, n)
end
n = self:_handleNode(cl.x, cl.y + 1, curnode, dl.x, dl.y)
if n then
table.insert(result, n)
end
n = self:_handleNode(cl.x, cl.y - 1, curnode, dl.x, dl.y)
if n then
table.insert(result, n)
end
return result
end
function TiledMapHandler:locationsAreEqual(a, b)
return a.x == b.x and a.y == b.y
end
function TiledMapHandler:_handleNode(x, y, fromnode, destx, desty)
-- Fetch a Node for the given location and set its parameters
local loc = {
x = x,
y = y
}
local n = self:getNode(loc)
if n ~= nil then
local dx = math.max(x, destx) - math.min(x, destx)
local dy = math.max(y, desty) - math.min(y, desty)
local emCost = dx + dy
n.mCost = n.mCost + fromnode.mCost
n.score = n.mCost + emCost
n.parent = fromnode
return n
end
return nil
end