5 Use this with parameters matrix,noOfTiles,tilesPath,realTileSize,displayBuffer
10 o
.mapHeight
= #o
.matrix
11 o
.mapWidth
= #o
.matrix
[1]
16 --TODO verschieben nach graphics
17 function Map
:loadTiles()
20 for i = 0, (self.noOfTiles-1) do
21 quads[i+1] = love.graphics.newQuad((((i+1)*self.realTileSize)), 0, self.realTileSize, self.realTileSize, self.realTileSize*self.noOfTiles, self.realTileSize)
25 quads
[1] = love
.graphics
.newQuad(1,1,32,32,102,34)
26 quads
[2] = love
.graphics
.newQuad(35,1,32,32,102,34)
27 quads
[3] = love
.graphics
.newQuad(69,32,32,32,102,34)
29 local image
= love
.graphics
.newImage( self
.tilesPath
)
30 image
:setFilter("nearest","linear")
31 image
:setWrap("repeat","repeat")
32 self
.spriteBatch
= love
.graphics
.newSpriteBatch(image
, 10000 )
33 for x
= 1, self
.mapWidth
do
34 for y
= 1, self
.mapHeight
do
35 self
.spriteBatch
:addq(quads
[(self
.matrix
[x
][y
])+1],((x
-1)*self
.realTileSize
),((y
-1)*self
.realTileSize
),0,1,1,0,0)
41 it's necessary to recalculate the tile sizes for each cam
42 TODO: really used?? check this
44 function Map
:adjustMapProperties(camWidth
,camHeight
,zoom
)
45 self
.tileSize
= math
.floor(self
.realTileSize
/zoom
)
46 self
.displayWidth
= math
.ceil(camWidth
/self
.tileSize
)
47 self
.displayHeight
= math
.ceil(camHeight
/self
.tileSize
)
52 --the walls are lines between these points
53 --a line can be straight top-down ( | ) or left-right ( - ), 90° ( 0,2:1,1 / ) or 45° ( 0,0:2,1 )
55 --map generation algorithm
56 function Map
:generateAdjacentFields(width
,height
)
57 local startx
= math
.random(width
)
58 local starty
= math
.random(height
)
60 --helper for watching mapsize
63 --set up the map grid 100*100 in size
67 map_grid
[i
][j
] = 1 --set every cell of the map to wall
71 --mark start point as room ( 0 )
72 map_grid
[startx
][starty
] = 0
76 map_grid
[startx
-1][starty
] = 2
77 map_grid
[startx
][starty
-1] = 2
78 map_grid
[startx
][starty
+1] = 2
79 map_grid
[startx
+1][starty
] = 2
82 --active cells are acted on to continue the path
83 local active_cells
= {{startx
,starty
}}
86 cell_set
= #active_cells
89 --representation of the adaject cells
93 current_cell
= { active_cells
[i
][1],active_cells
[i
][2] } --select x and y coords
97 for j
= 1,8 do --mark random adjacent cells as active
98 if math
.random(30) < 12 then
99 if j
== 1 or j
== 2 or j
== 3 then
101 elseif j
== 4 or j
== 5 then
107 if j
== 1 or j
== 4 or j
== 6 then
109 elseif j
== 2 or j
== 7 then
114 --mark cell as active and room if not already a room cell and if not out ouf map borders
115 new_x
= current_cell
[1]+temp_x
--x coord
116 new_y
= current_cell
[2]+temp_y
--y coord
117 if (new_x
< 98 and new_x
> 1 and new_y
< 98 and new_y
> 1) then --obey map borders
118 if (map_grid
[ new_x
][ new_y
] == 2 or map_grid
[ new_x
][ new_y
] == 1) then --only do something if this cell is not already a room
119 table.insert(active_cells
, {new_x
,new_y
} ) --insert to the active cell list
120 map_grid
[new_x
][new_y
] = 0 --mark active cell as room
122 for k
= 1,4 do --mark borders
125 elseif k
== 2 or k
== 3 then
133 elseif k
== 1 or k
== 4 then
139 border_x
= new_x
+temp_x2
--x coord
140 border_y
= new_y
+temp_y2
--y coord
142 if map_grid
[ border_x
][ border_y
] == 1 then
143 map_grid
[ border_x
][ border_y
] = 2 --mark as border if its still wall
147 room_count
= room_count
+1
154 for i
= 1,cell_set
do
155 table.remove(active_cells
,i
)
157 if cell_set
== 0 then
163 if room_count
< 1500 then
172 --TODO kick this function?? is not used
173 function generate_borders()
178 if map_grid
[ii
][jj
] == 2 then
186 -- there are some possibilities for selecting the tile for the border depending on the sorrounding
192 -- these blocks can be wall, border or space, so there are 3^8 combinations
194 -- orientation of border
201 -- 22,5° / 1. part 16
202 -- 22,5° / 2. part 17
203 -- 22,5° \ 1. part 18
204 -- 22,5° \ 2. part 19
205 -- 22,5° bottom-left,top-right 1. part 20
206 -- 22,5° bottom-left,top-right 2. part 21
207 -- 22,5* top-left, bottom-right 1. part 22
208 -- 22,5* top-left, bottom-right 2. part 23
210 --function select_tile(x,y)
216 Generates spawn points in the water
218 function Map
:generateSpawnPoints(width
,height
)
220 randx
= math
.random(width
)
221 randy
= math
.random(height
)
222 until map_grid
[randx
][randy
] == 0
223 startcoords
= {randx
*self
.realTileSize
,randy
*self
.realTileSize
}
227 function Map
:generateMap()
229 math
.randomseed( seed
) --pseudo-random, good enough for us
231 while Map
:generateAdjacentFields(100,100) == false do