-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
171 lines (138 loc) · 4.38 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
io.stdout:setvbuf('no') -- show debug output live in SublimeText console
local amazing = require 'amazing'
local Tile = amazing.Tile
local canvas = nil
local player = nil
local map = nil
local dx, dy = 0, 0
local step_delay = 0
local function oneIn(count)
return love.math.random(count) == 1
end
local spawn_table = amazing.RandomTable({
['g'] = 70, -- goblin
['o'] = 30, -- orc
['b'] = 15, -- bandit
['p'] = 5, -- ogre
['$'] = 3, -- gold
['?'] = 2, -- scroll
[']'] = 3, -- armor
['/'] = 3, -- dagger
['['] = 1, -- shield
})
local prefab_map = [[
########################################
# + # # ####
# % % # % # % # % ###
# # # ### ###
###+######### # % ##
# # # ### ## % ##
# % % # % # % # % # > % #
# # # ### ## % ##
########+#### # % ##
# # # ### ###
+@ # % # % # % ###
# # + # ####
########################################
]]
local function generatePrefab()
return amazing.builder.prefab({
['map'] = prefab_map,
['spawn_table'] = spawn_table,
})
end
local function generateRandom()
return amazing.builder.random({
['spawn_table'] = spawn_table,
})
end
local function generate()
local builder = oneIn(10) and generatePrefab() or generateRandom()
map, player, spawns = builder.build()
love.graphics.setFont(love.graphics.newFont(14))
love.window.setTitle('Amazing')
love.window.setMode(1280, 800, { ['highdpi'] = false })
canvas = love.graphics.newCanvas()
love.graphics.setCanvas(canvas)
for x, y, v in map.iter() do
local s = ' '
local c = { 1.0, 1.0, 1.0 }
if bit.band(v, Tile.WALL) ~= 0 then
s = '#'
c = { 0.0, 0.6, 0.0 }
elseif bit.band(v, Tile.DOOR) ~= 0 then
s = '+'
c = { 1.0, 0.0, 1.0 }
elseif bit.band(v, Tile.STAIR_DN) ~= 0 then
s = '>'
elseif bit.band(v, Tile.STAIR_UP) ~= 0 then
s = '<'
end
love.graphics.setColor(c)
love.graphics.print(s, x * 12, y * 12)
end
love.graphics.setCanvas()
keys_pressed = {}
print()
end
function love.load(args)
love.math.setRandomSeed(1)
generate()
end
function love.draw()
love.graphics.setColor(1.0, 1.0, 1.0)
love.graphics.draw(canvas)
for _, spawn in ipairs(spawns) do
love.graphics.setColor(0.0, 0.0, 0.0)
love.graphics.rectangle('fill', spawn.x * 12, spawn.y * 12, 15, 15)
love.graphics.setColor(0.0, 0.6, 0.6)
love.graphics.print(spawn.id, spawn.x * 12, spawn.y * 12)
end
if player then
love.graphics.setColor(0.0, 0.0, 0.0)
love.graphics.rectangle('fill', player.x * 12, player.y * 12, 15, 15)
love.graphics.setColor(1.0, 1.0, 1.0)
love.graphics.print('@', player.x * 12, player.y * 12)
end
end
function isBlocked(x, y)
return bit.band(map.get(x, y), Tile.WALL) == Tile.WALL
end
local function tryMove(x, y)
if isBlocked(x, y) then return false end
player.x = x
player.y = y
for idx, spawn in ipairs(spawns) do
if spawn.x == player.x and spawn.y == player.y then
table.remove(spawns, idx)
end
end
return true
end
function love.update(dt)
step_delay = step_delay + dt
dx, dy = 0, 0
if keys_pressed['left'] then dx = -1 end
if keys_pressed['right'] then dx = 1 end
if keys_pressed['up'] then dy = -1 end
if keys_pressed['down'] then dy = 1 end
if step_delay > 0.1 then
step_delay = step_delay - 0.1
if player then
tryMove(player.x + dx, player.y + dy)
end
end
if not player then return end
local tile = map.get(player.x, player.y)
if bit.band(tile, Tile.STAIR_DN) == Tile.STAIR_DN then
generate()
end
end
function love.keypressed(key, scancode)
if key == 'g' then generate() end
if key == 'q' then love.event.quit() end
keys_pressed[key] = true
end
function love.keyreleased(key, scancode)
keys_pressed[key] = nil
end