-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
147 lines (133 loc) · 3.9 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
bump = require("lib/bump/bump")
camera = require("lib/hump/camera")(102 + 64 * 2, 170 - 16, 2)
cindy = require("lib/cindy/cindy")
b2c = cindy.byte2channel
class = require("lib/30log/30log")
screen = require("lib/shack/shack")
tick = require("lib/tick/tick")
utf8 = require("utf8")
debug = os.getenv("DEBUG")
cmap = os.getenv("MAP")
player = require("player")
map = require("map")
gamestate = 1
worklist = {
player
}
currentconsole = ""
function love.load()
if map.map == 5 then
love.event.quit()
end
tick.rate = 1 / 60
love.graphics.setDefaultFilter("linear", "nearest")
world = bump.newWorld()
map:load()
for _, item in pairs(worklist) do
item:load()
end
camera.x, camera.y = player.loc.x, player.loc.y - 100
end
function love.update(dt)
dt = math.min(1 / 60, dt)
if player.cameramode == 1 then
camera:lockPosition(player.loc.x + 150, player.loc.y, player:camera(7, "x"))-- snap camera: player:camera(7)
elseif player.cameramode == 2 then
camera:lockPosition(player.loc.x - 150, player.loc.y, player:camera(7, "x"))
elseif player.cameramode == 3 then
camera:lockPosition(player.loc.x, player.loc.y + 150, player:camera(7, "y"))
elseif player.cameramode == 4 then
camera:lockPosition(player.loc.x, player.loc.y - 90, player:camera(7, "y"))
end
for _, item in pairs(worklist) do
item:update(dt)
end
screen:update(dt)
if player.console then
love.keyboard.setTextInput(true)
else
love.keyboard.setTextInput(false)
end
end
function love.keypressed(key)
if player.console then
if key == "backspace" then
-- get the byte offset to the last UTF-8 character in the string.
local byteoffset = utf8.offset(currentconsole, -1)
if byteoffset then
-- remove the last UTF-8 character.
-- string.sub operates on bytes rather than UTF-8 characters, so we couldn't do string.sub(text, 1, -2).
currentconsole = string.sub(currentconsole, 1, byteoffset - 1)
end
elseif key == "return" then
loadstring(currentconsole)()
currentconsole = 0
player.console = false
end
end
end
function love.textinput(t)
currentconsole = currentconsole .. t
end
function love.draw()
if player.console == true then
love.graphics.setColor(0,0,0,0.5)
love.graphics.rectangle("fill", 0, 0, 500, 20)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.print(currentconsole, 5, 5)
end
if debug then
local x, y = love.mouse.getPosition()
local camerax, cameray = camera:cameraCoords(love.mouse.getPosition())
love.graphics.print(
x ..
" " ..
y ..
"\n" ..
player.vel.x ..
" " ..
player.loc.y ..
"\n" ..
love.timer.getFPS() ..
"\n" ..
love.timer.getAverageDelta() ..
"\n" ..
camerax .. " " .. cameray
)
end
if player.gamestate == 1 then
screen:apply()
camera:attach()
love.graphics.setBackgroundColor(b2c(135), b2c(206), b2c(235))
love.graphics.setColor(1, 1, 1, 1)
map:draw()
for _, item in pairs(worklist) do
item:draw()
end
camera:detach()
elseif player.gamestate == 2 then
love.graphics.print("you died", love.graphics.getWidth() / 2, love.graphics.getHeight() / 2, 0, 5, 5)
if losetime == nil then
losetime = love.timer.getTime()
elseif love.timer.getTime() - losetime >= 1 then
player.gamestate = 1
player.loc = map.spawn:clone()
world:update(player, player.loc.x, player.loc.y)
losetime = nil
end
elseif player.gamestate == 4 then
love.graphics.setColor(1, 0, 0)
love.graphics.print("YOU WIN", love.graphics.getWidth() / 2, love.graphics.getHeight() / 2, 0, 5, 5)
if wintime == nil then
wintime = love.timer.getTime()
elseif love.timer.getTime() - wintime >= 3 then
map.map = map.map + 1
love.load()
player.gamestate = 1
wintime = nil
end
end
end
math.sign = function(n)
return (n < 0) and -1 or ((n > 0) and 1 or 0)
end