-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharea.lua
116 lines (100 loc) · 2.94 KB
/
area.lua
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
require 'geo'
require 'iso'
require 'math'
require 'color'
Tile = Entity:extend()
function Tile:init(prop)
Entity.init(self, under.extend({
id = 1,
}, prop or {}))
self.id = self.prop.id
end
Area = Object:extend()
function Area:init(prop)
self.prop = under.extend({
tilesize = 32 / math.sqrt(2),
areasize = 32,
}, prop or {})
-- Setup tiles
self.tiles = {}
for i=1, self.prop.areasize do
self.tiles[i] = {}
for j=1, self.prop.areasize do
-- Create tile in world space
self.tiles[i][j] = Tile {
x = (i - 1) * self.prop.tilesize,
y = (j - 1) * self.prop.tilesize,
w = self.prop.tilesize,
h = self.prop.tilesize,
}
end
end
-- Setup spritebatch
self.spritebatch = love.graphics.newSpriteBatch(assets.gfx.tileset, self.prop.areasize * self.prop.areasize)
self.texquads = build_quads(assets.gfx.tileset, 32, 32)
-- Setup entity list
self.entities = {}
end
function Area:update(dt)
under.map(self.entities, function(entity) entity:update(dt) end)
end
function Area:draw()
-- Clear last frame
self.spritebatch:clear()
-- Draw tiles
-- Prepraed transformed quads for each tile so iso transformation is correct
map2d(self.tiles, function(tile)
self.spritebatch:addq(self.texquads[tile.id], tile:getPreTransform())
end)
color.white()
love.graphics.draw(self.spritebatch)
-- Draw entities
under.map(self.entities, function(entity)
entity:draw()
end)
end
-- Get a tile from ortho world coords
function Area:tileAt(wx, wy)
for i=1, #self.tiles do
for j=1, #self.tiles[i] do
local tile = self.tiles[i][j]
if geo.contains(tile.x, tile.y, tile.x+tile.w, tile.y+tile.h, wx, wy) then
return tile
end
end
end
return nil
end
-- TEST methods
function Area:createWater(tile)
for i=1, #self.tiles do
for j=1, #self.tiles[i] do
if self.tiles[i][j] == tile then
for k=-1, 1 do
for l=-1, 1 do
if k == 0 and l == 0 then
self.tiles[i][j].id = 2
else
local k, l = i + k, j + l
if self.tiles[k] and self.tiles[k][l]
and self.tiles[k][l].id ~= 2 then self.tiles[k][l].id = 3 end
end
end
end
end
end
end
end
function Area:createVilliager(tile)
for i=1, #self.tiles do
for j=1, #self.tiles[i] do
if self.tiles[i][j] == tile then
local villiager = Villiager {
x = tile.x,
y = tile.y,
}
table.insert(self.entities, villiager)
end
end
end
end