-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.lua
More file actions
81 lines (64 loc) · 2.45 KB
/
renderer.lua
File metadata and controls
81 lines (64 loc) · 2.45 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
local Renderer = {}
function Renderer.new(camera, grid, constants)
local renderer = {
camera = camera,
grid = grid,
colors = constants.colors
}
setmetatable(renderer, {__index = Renderer})
return renderer
end
function Renderer:worldToScreen(worldX, worldY)
return worldX + self.camera.x, worldY + self.camera.y
end
function Renderer:drawGrid()
love.graphics.setColor(self.colors.grid)
local w, h = love.graphics.getDimensions()
local cellSize = self.grid.cellSize
-- Calculate visible grid range
local startX = math.max(0, math.floor(-self.camera.x / cellSize))
local startY = math.max(0, math.floor(-self.camera.y / cellSize))
local endX = math.min(self.grid.width, math.ceil((-self.camera.x + w) / cellSize))
local endY = math.min(self.grid.height, math.ceil((-self.camera.y + h) / cellSize))
-- Grid bounds in screen space
local gridStartX = self.camera.x
local gridStartY = self.camera.y
local gridEndX = self.grid:getPixelWidth() + self.camera.x
local gridEndY = self.grid:getPixelHeight() + self.camera.y
-- Draw vertical lines
for x = startX, endX do
local screenX = x * cellSize + self.camera.x
love.graphics.line(screenX, math.max(0, gridStartY), screenX, math.min(h, gridEndY))
end
-- Draw horizontal lines
for y = startY, endY do
local screenY = y * cellSize + self.camera.y
love.graphics.line(math.max(0, gridStartX), screenY, math.min(w, gridEndX), screenY)
end
end
function Renderer:drawPath(path, pathIndex)
if #path == 0 then return end
love.graphics.setColor(self.colors.path)
for i = pathIndex, #path do
self:drawCellAtPosition(path[i].x, path[i].y)
end
end
function Renderer:drawCellAtPosition(cellX, cellY, color)
local worldX, worldY = self.grid:cellToWorld(cellX, cellY)
local screenX, screenY = self:worldToScreen(worldX, worldY)
if color then
love.graphics.setColor(color)
end
love.graphics.rectangle("fill", screenX, screenY, self.grid.cellSize, self.grid.cellSize)
end
function Renderer:drawUnit(unit)
if not unit then return end
love.graphics.setColor(self.colors.selectedUnit)
self:drawCellAtPosition(unit.x, unit.y)
end
function Renderer:drawTargetCell(cell)
if not cell then return end
love.graphics.setColor(self.colors.targetCell)
self:drawCellAtPosition(cell.x, cell.y)
end
return Renderer