-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
182 lines (147 loc) · 5.33 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
local argparse = require("lib.argparse")
local repl = require("lib.repl")
lume = require("lib.lume")
-- TODO: find a good spot for this
-- require "strict"
local pps = function(x)
return require("lib.serpent").block(x, {maxlevel=8,maxnum=64,
nocode=true,comment=false})
end
ppsl = function(x) return require("lib.serpent").line(x) end
pp = function(x) print(pps(x)) end
ppl = function(x) print(ppsl(x)) end
log = function(...) print(string.format(...)) end
lfg = require("lfg")
local GameClient = require("client")
local GameServer = require("server")
local GameUser = require("user")
local client, map, server, world
local is_user_bootstrapped = false
-- HACK: set global Kore table for use from repl
GKORE = {}
local function parse_args()
local parser = argparse("kore", "Kore - The Rise of Persephone")
parser:argument("dir", "App dir")
parser:flag("--server", "Host a server")
parser:flag("--no-client", "Do not connect to host?")
parser:flag("--no-user", "Are you not a user?")
parser:flag("--no-kur", "Scared of Kur?")
parser:flag("--headless", "Headless mode")
parser:option("--host", "Server host to connect to", nil)
parser:option("--port", "Server port to connect to", GameServer.PORT)
parser:option("--character", "What character to use; one of [Minotaur, Zombie, Skeleton, Goblin, Antlion", nil)
parser:option("--spell", "What spell to use; one of [Fireball, Lightning, Channel, Icicle", nil)
parser:option("--name", "Are you really a user?", string.format("FOO{%s}", lume.uuid()))
parser:option("--map", "Map file to use", "map_arena3.lua")
return parser:parse()
end
local user
function love.load()
log("LOADING KORE")
local pargs = parse_args()
log("GOT PARSED ARGS: %s", ppsl(pargs))
local host = (pargs.host or (pargs.server and "localhost") or
"kore.chewbranca.com")
assert(lfg.init({map_file=pargs.map}, pargs))
map = lfg.map
_G.lfg_map = lfg.map
world = lfg.world
local player_layer = lfg.map:addCustomLayer("KorePlayers", #lfg.map.layers + 1)
player_layer.players = {}
player_layer.update = function(self, dt)
for _i, p in ipairs(self.players) do p:update(dt) end
end
player_layer.draw = function(self)
for _i, p in ipairs(self.players) do p:draw() end
end
local pjt_layer = lfg.map:addCustomLayer("KoreProjectiles", #lfg.map.layers + 1)
pjt_layer.projectiles = {}
pjt_layer.update = function(self, dt)
for _i, pjt in ipairs(self.projectiles) do pjt:update(dt) end
end
pjt_layer.draw = function(self)
for _uuid, pjt in pairs(self.projectiles) do pjt:draw() end
end
if pargs.server then
repl.start()
server = GameServer({
port = pargs.port,
map = map,
world = world,
kur = not pargs.no_kur,
})
GKORE.server = server
end
if not pargs.no_client then
client = GameClient(host, pargs.port)
if not pargs.no_user then
local payload = {
character = pargs.character,
spell_name = pargs.spell,
name = pargs.name
}
user = GameUser(payload)
end
GKORE.client = client
GKORE.user = user
end
end
local connect_delay = 3.0
function love.update(dt)
if server then server:update(dt) end
if client then client:update(dt) end
if user then
if not is_user_bootstrapped then
-- FIXME: DIRTY HACKS
-- need a full {client,server}:update(dt) cycle before this works
-- otherwise the create_player message gets lost
-- TODO: fix this or make this not terrible
if connect_delay <= 0.0 then
is_user_bootstrapped = user:bootstrap(client)
else
connect_delay = connect_delay - dt
end
else
user:update(dt)
end
end
map:update(dt)
end
function love.draw()
if user and is_user_bootstrapped then
local px = user and user:screen_x() or 0
local py = user and user:screen_y() or 0
local tx = math.max(0, math.floor(px - love.graphics.getWidth() / 2))
local ty = math.max(0, math.floor(py - love.graphics.getHeight() / 2))
love.graphics.push()
do
love.graphics.setColor(255, 255, 255)
map:draw(-tx, -ty)
if (user and user.debug) then
love.graphics.setColor(255, 0, 0)
map:bump_draw(world, -tx, -ty, 1, 1)
love.graphics.setColor(255, 255, 255)
end
love.graphics.translate(-tx, -ty)
if (user and user.debug) then
love.graphics.points(math.floor(px), math.floor(py))
love.graphics.rectangle("line", user:screen_x() - user:ox(), user:screen_y() - user:oy(), 128, 128)
end
client:draw()
end
love.graphics.pop()
love.graphics.setColor(255, 255, 255)
user:draw()
-- TODO: remove need for client:draw hackery
--client:draw()
end
end
function love.mousepressed(...)
if user then user:mousepressed(...) end
end
function love.keypressed(...)
if user then user:keypressed(...) end
end
function love.textinput(...)
if user then user:textinput(...) end
end