-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.lua
126 lines (106 loc) · 4.32 KB
/
game.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
117
118
119
120
121
122
123
124
125
126
local game = {}
function game:enter()
player.location = "Overworld"
createWorld()
end
function game:update(dt)
player:update(dt)
for i=#enemies, 1, -1 do
enemies[i]:update(dt)
if enemies[i].dead then
player.xp = player.xp + enemies[i].xp
enemies[i].rect.fixture:destroy()
table.remove(enemies, i)
end
end
for i=#hearts, 1, -1 do
hearts[i]:update(dt)
if hearts[i].rect.fixture:isDestroyed() then
table.remove(hearts, i)
end
end
cam:lookAt(player.rect.body:getX() + player.rect.width / 2, player.rect.body:getY() + player.rect.height / 2)
end
function game:draw()
cam:attach()
-- plains1:drawObjectLayer(plains1.layers["Walls"])
-- love.graphics.setColor(1, 0, 0)
-- for i,v in ipairs(walls) do
-- love.graphics.rectangle("line", v.body:getX(), v.body:getY(), v.width, v.height)
-- end
-- love.graphics.setColor(1, 1, 1)
local yTop = math.floor(lume.clamp((player.rect.body:getY() - 500) / (16 * 20), 1, #data.tileSet))
local yBottom = math.floor(lume.clamp((player.rect.body:getY() + 1000) / (16 * 20), 1, #data.tileSet))
local xLeft = math.floor(lume.clamp((player.rect.body:getX() - 500) / (16 * 20), 1, #data.tileSet))
local xRight = math.floor(lume.clamp((player.rect.body:getX() + 1000) / (16 * 20), 1, #data.tileSet))
-- print("yTop: " .. yTop)
for y=yTop, yBottom do
for x=xLeft, xRight do
local ox = (x - 1) * 16 * 20
local oy = (y - 1) * 16 * 20
if data.tileSet[y][x][1] == "1" then
maps["plains" .. data.tileSet[y][x][2]]:translate(ox, oy)
drawChunk1(maps["plains" .. data.tileSet[y][x][2]])
elseif data.tileSet[y][x][1] == "2" then
maps["lake" .. data.tileSet[y][x][2]]:translate(ox, oy)
drawChunk1(maps["lake" .. data.tileSet[y][x][2]])
elseif data.tileSet[y][x][1] == "3" then
maps["village" .. data.tileSet[y][x][2]]:translate(ox, oy)
drawChunk1(maps["village" .. data.tileSet[y][x][2]])
elseif data.tileSet[y][x][1] == "4" then
maps["shrine1"]:translate(ox, oy)
drawChunk1(maps["shrine1"])
elseif data.tileSet[y][x][1] == "5" then
maps["forest1"]:translate(ox, oy)
drawChunk1(maps["forest1"])
else
maps["plains1"]:translate(ox, oy)
drawChunk1(maps["plains1 "])
end
end
end
player:draw()
for i, enemy in ipairs(enemies) do
enemy:draw()
end
for i, heart in ipairs(hearts) do
heart:draw()
end
for y=yTop, yBottom do
for x=xLeft, xRight do
local ox = (x - 1) * 16 * 20
local oy = (y - 1) * 16 * 20
if data.tileSet[y][x][1] == "1" then
maps["plains" .. data.tileSet[y][x][2]]:translate(ox, oy)
drawChunk2(maps["plains" .. data.tileSet[y][x][2]])
elseif data.tileSet[y][x][1] == "2" then
maps["lake" .. data.tileSet[y][x][2]]:translate(ox, oy)
drawChunk2(maps["lake" .. data.tileSet[y][x][2]])
elseif data.tileSet[y][x][1] == "3" then
maps["village" .. data.tileSet[y][x][2]]:translate(ox, oy)
drawChunk2(maps["village" .. data.tileSet[y][x][2]])
elseif data.tileSet[y][x][1] == "4" then
maps["shrine1"]:translate(ox, oy)
drawChunk2(maps["shrine1"])
elseif data.tileSet[y][x][1] == "5" then
maps["forest1"]:translate(ox, oy)
drawChunk2(maps["forest1"])
else
maps["plains1"]:translate(ox, oy)
drawChunk2(maps["plains1 "])
end
end
end
cam:detach()
player:drawHearts(0, 0)
end
function game:keypressed(key)
if key == 'escape' then
saveGame()
gamestate.switch(menu)
end
end
function game:leave()
deleteWorld()
end
return game