-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.lua
221 lines (180 loc) · 5.6 KB
/
main.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
love.filesystem.load("perlin.lua")()
love.filesystem.load("chunk.lua")()
love.filesystem.load("terrain.lua")()
love.filesystem.load("grapplinghook.lua")()
love.filesystem.load("player.lua")()
love.filesystem.load("collision.lua")()
love.filesystem.load("common.lua")()
love.filesystem.load("loadgraphics.lua")()
love.filesystem.load("entity.lua")()
love.filesystem.load("gameplay.lua")()
love.filesystem.load("inventory.lua")()
anim8 = require 'anim8/anim8'
love.filesystem.setIdentity("lovecraft")
view = {zoom = 32, x = 0, y = 0}
showPerlin = false
oldMouse = {x = 0, y = 0}
cursor = {x = 0, y = 0}
cursorFade = false
cursorAlpha = 255
inreach = true
mineBlock = {r = nil, c = nil}
mineProgress = 0
placeTime = 0
instamine = false
debug = false
hookRelease = false
showInventory = false
g = 40
function love.load()
generator = love.thread.newThread("generator.lua")
generator:start()
player = Player:new()
terrain = Terrain:new()
terrain:generateInitial()
player.x = 0.5
player.y = -60
while terrain:getBlock(math.floor(player.y), math.floor(player.x) + 1) == AIR do
player.y = player.y + 1
end
while terrain:getBlock(math.floor(player.y), math.floor(player.x) + 1) ~= AIR do
player.y = player.y - 1
end
view.x = player.x
view.y = player.y - player.height / 2
first = true
player.inventory:give(WOOD_SHOVEL) -- Just temporary, for testing
end
function love.update(dt)
if dt > 0.1 then dt = 0.1 end
local oldx = player.x
local oldy = player.y
if not first then
player:update(dt)
end
for i = 1, #terrain.entities do
local entity = terrain.entities[i]
if entity.falling then
entity.vy = entity.vy + g * dt
entity.y = entity.y + entity.vy * dt
end
end
checkCollisions(terrain, player)
if showInventory then handleInventoryInput(player)
else handleGameplayInput(player, terrain, dt)
end
view.x = view.x + (player.x - view.x) * 0.2
view.y = view.y + (player.y - player.height / 2 - view.y) * 0.2
local viewDist = pythag(view.x, view.y, player.x, player.y)
local maxViewDist = 0.35 * math.min(love.graphics.getWidth(), love.graphics.getHeight()) / view.zoom
if viewDist > maxViewDist then
view.x = player.x - (player.x - view.x) * (maxViewDist / viewDist)
view.y = player.y - (player.y - view.y) * (maxViewDist / viewDist)
end
first = false
placeTime = placeTime + dt
player.walk:update(dt)
player.hook:update(terrain, dt)
-- Generate new chunks
for r = math.floor((player.y - 80) / 32), math.floor((player.y + 80) / 32) do
for c = math.floor((player.x - 80) / 32), math.floor((player.x + 80) / 32) do
terrain:generate(r, c)
end
end
terrain:checkGenerator()
end
function love.draw()
local x, y = love.mouse.getPosition()
if showPerlin then terrain:drawPerlin(view)
else
terrain:draw(view)
end
love.graphics.setColor(255, 255, 255, 255)
player:draw(view)
love.graphics.setColor(0, 0, 0, cursorAlpha)
if inreach and not showInventory then
love.graphics.setLineWidth(view.zoom/32)
love.graphics.setLineStyle("rough")
love.graphics.rectangle("line", (math.ceil(cursor.x)-1-view.x)*view.zoom + love.graphics.getWidth()/2, (math.ceil(cursor.y)-1-view.y)*view.zoom+love.graphics.getHeight()/2, view.zoom, view.zoom)
end
if mineProgress > 0 and mineProgress <= 1 then
love.graphics.draw(breakImage[math.ceil(mineProgress * 8)], (mineBlock.c-1-view.x)*view.zoom + love.graphics.getWidth()/2, (mineBlock.r-1-view.y)*view.zoom+love.graphics.getHeight()/2, 0, view.zoom/16, view.zoom/16)
end
if showInventory then
player.inventory:draw()
else
player.inventory:drawHotbar()
end
if debug then
love.graphics.setColor(0, 0, 0, 255)
love.graphics.print(love.timer.getFPS() .. " fps", love.graphics.getWidth() - 150, 50)
end
end
function love.keypressed(k, u)
if showInventory then
if k == "escape" and player.inventory.pickedItem.id == nil then
showInventory = false
end
else
if k == "escape" then
local command_channel = love.thread.getChannel('generator_command')
command_channel:clear()
command_channel:push("quit")
generator:wait()
love.event.push("quit")
elseif k == "space" then
if player.hook.fired then
player.hook:reset()
hookRelease = true
end
end
end
if k == "p" then
--showPerlin = not showPerlin
elseif k == "f3" then
debug = not debug
instamine = debug
elseif k == "e" and player.inventory.pickedItem.id == nil then
showInventory = not showInventory
elseif k == "[" then
if view.zoom > 1 then view.zoom = view.zoom / 2 end
elseif k == "]" then
if view.zoom < 256 then view.zoom = view.zoom * 2 end
elseif k == "g" then
player.hook:fire(player.x, player.y - player.height/2, cursor.x - player.x, cursor.y - (player.y - player.height/2))
end
end
function love.keyreleased(k)
if k == "space" then
hookRelease = false
end
end
function love.mousepressed(x, y, button)
if showInventory then
player.inventory:handleInput(button)
else
if button == 3 then
player.hook:fire(player.x, player.y - player.height/2, cursor.x - player.x, cursor.y - (player.y - player.height/2))
end
end
end
function love.wheelmoved(x, y)
if not showInventory then
if y > 0 then
player.inventory:selectPrev()
elseif y < 0 then
player.inventory:selectNext()
end
end
end
function pythag(x1, y1, x2, y2)
if x2 == nil and y2 == nil then
x2 = 0
y2 = 0
end
return math.sqrt((x1-x2)^2 + (y1-y2)^2)
end
function love.quit()
--This will be printed to the console on quit
print("Thanks for playing. Please play again soon!")
end