-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
157 lines (148 loc) · 4.76 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
package.path = './mylib/?.lua;' .. package.path
local bump = require "lib/bump"
local Player = require "entities/player"
local DiggableDirt = require "entities/dirt"
local DiggableStone = require "entities/stone"
local DiggableGrassDirt = require "entities/grassdirt"
local DiggableScrapDirt = require "entities/scrapdirt"
local DiggableScrapStone = require "entities/scrapstone"
local Camera = require "util/camera"
local Map = require "util/map"
local MessageQueue = require "util/messagequeue"
local AnimationManager = require "util/animations"
local dbg = require "lib/debugger"
spriteTable = {}
messageQueue = MessageQueue()
local entities = {}
local world = bump.newWorld(64)
local drawBoundingBoxes = false
local cam
local currentMap
local player
local showTitleScreen = true
function entities:getByName(name)
for _, entity in ipairs(self) do
if entity.name == name then
return entity
end
end
return nil
end
local function loadSprites(directory, table)
local files = love.filesystem.getDirectoryItems(directory)
for _, filename in ipairs(files) do
if love.filesystem.getInfo(directory .. "/" .. filename).type == "file" then
table[filename] = love.graphics.newImage(directory .. "/" .. filename)
print("Loaded file " .. filename)
end
end
end
-- places entity into bump world at the requested x and y coordinates
-- and adds to the entities list.
function spawn(entity)
table.insert(entities, entity)
world:add(entity, entity.x, entity.y, entity.w, entity.h)
entity.world = world
end
local function spawnDiggableMap(map)
for _, layer in pairs(map.layers) do
local width = layer.width
local height = layer.height
local diggablesTable = {}
for i = 0, width do
diggablesTable[i] = {}
for j = 0, height do
diggablesTable[i][j] = 0
end
end
for i, v in ipairs(layer.data) do
local x = (i-1) % width
local y = math.floor((i-1)/width)
local newDiggable
if v ~= 0 then
if v == 1 then
newDiggable = DiggableDirt(x, y, diggablesTable)
end
if v == 2 then
newDiggable = DiggableStone(x, y, diggablesTable)
end
if v == 11 then
newDiggable = DiggableGrassDirt(x, y, diggablesTable)
end
if v == 12 then
newDiggable = DiggableScrapDirt(x, y, diggablesTable)
end
if v == 13 then
newDiggable = DiggableScrapStone(x, y, diggablesTable)
end
diggablesTable[x][y] = newDiggable
spawn(newDiggable)
end
end
end
end
function love.load()
love.window.setMode(800, 600, {resizable=false})
love.graphics.setDefaultFilter("nearest")
love.graphics.setBackgroundColor(135/255, 206/255, 235/255, 1)
loadSprites("assets/sprites", spriteTable)
AnimationManager:init(spriteTable)
local newPlayer = Player(1000,1150)
player = newPlayer
spawn(newPlayer)
cam = Camera(newPlayer)
local mapfile = require"maps/background"
currentMap = Map(mapfile)
local diggables = require"maps/diggablesmap"
spawnDiggableMap(diggables)
end
function love.keypressed(key)
showTitleScreen = false
if key == "d" then
if drawBoundingBoxes == false then
drawBoundingBoxes = true
else
drawBoundingBoxes = false
end
end
if key == "escape" then
love.event.quit()
end
print(key)
for _, entity in ipairs(entities) do
entity:handleKeyPress(key)
end
end
function love.update(dt)
if not showTitleScreen then
for _, entity in ipairs(entities) do
entity:update(dt)
end
for i, entity in ipairs(entities) do
if entity.alive == false then
table.remove(entities, i)
world:remove(entity)
end
end
messageQueue.dispatch()
end
end
local font = love.graphics.newFont(12)
function love.draw()
if showTitleScreen then
love.graphics.draw(spriteTable["title screen.png"], 0, 0)
else
local dt = love.timer.getDelta()
cam:update(dt)
love.graphics.scale(2,2)
currentMap:draw()
for _, entity in ipairs(entities) do
entity:draw(dt)
if drawBoundingBoxes == true then
love.graphics.rectangle("line", entity.x, entity.y, entity.w, entity.h)
end
end
local score = love.graphics.newText(font, "Scrap: " .. player.collectedScrap)
love.graphics.draw(score, cam.x -200, cam.y-150)
end
end