summaryrefslogtreecommitdiff
path: root/client/main.lua
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-01-24 03:20:44 +0000
committerubq323 <ubq323@ubq323.website>2023-01-24 03:20:44 +0000
commitb5484fc37c1f93654f95f21483d52229a34e6330 (patch)
tree2ea5088bb3859f7afdef4b46c7027acf48b7d524 /client/main.lua
things
Diffstat (limited to 'client/main.lua')
-rw-r--r--client/main.lua122
1 files changed, 122 insertions, 0 deletions
diff --git a/client/main.lua b/client/main.lua
new file mode 100644
index 0000000..4375ed3
--- /dev/null
+++ b/client/main.lua
@@ -0,0 +1,122 @@
+local enet = require"enet"
+local words = require"common.words"
+
+
+local PLAYER_SIZE = 30
+
+local local_player = false
+
+-- {
+-- pos={100,100},
+-- color={love.math.colorFromBytes(0xdf,0x73,0xff)},
+-- }
+
+local host,peer
+
+local function draw_player(pl,me)
+ local hplsz = PLAYER_SIZE/2
+ love.graphics.setColor(pl.color)
+ love.graphics.rectangle("fill",pl.pos[1]-hplsz,pl.pos[2]-hplsz,PLAYER_SIZE,PLAYER_SIZE)
+ love.graphics.setColor(0,0,0)
+ love.graphics.print(tostring(pl.id),pl.pos[1],pl.pos[2])
+ if me then
+ love.graphics.setColor(1,1,1)
+ love.graphics.rectangle("line",pl.pos[1]-hplsz,pl.pos[2]-hplsz,PLAYER_SIZE,PLAYER_SIZE)
+ end
+end
+
+local remote_players = {}
+
+local function update_local_player(pl,dt)
+ local SPEED = 300 -- pixels/sec
+ local function kd(code)
+ if love.keyboard.isScancodeDown(code) then return 1 else return 0 end
+ end
+ local dx = kd"d"-kd"a"
+ local dy = kd"s"-kd"w"
+
+ if dx == 0 and dy == 0 then
+ pl.pos_dirty = false
+ return
+ end
+
+ if dx ~= 0 and dy ~= 0 then
+ dx = dx / math.sqrt(2)
+ dy = dy / math.sqrt(2)
+ end
+
+ pl.pos[1] = pl.pos[1] + SPEED * dt * dx
+ pl.pos[2] = pl.pos[2] + SPEED * dt * dy
+ pl.pos_dirty = true
+end
+
+local function sync_local_player(pl,peer)
+ -- send updated info about local player to server
+ if pl.pos_dirty then
+ peer:send(words.join("ppos",pl.pos[1],pl.pos[2]))
+ end
+end
+
+function love.update(dt)
+ if local_player then
+ update_local_player(local_player,dt)
+ sync_local_player(local_player,peer)
+ end
+ repeat
+ local ev = host:service()
+ if ev and ev.type == "receive" then
+ local w = words.split(ev.data)
+ local op = w[1]
+ if op == "join" then
+ local id,x,y,r,g,b = unpack(w,2)
+ -- blegh
+ id=tonumber(id)
+ x=tonumber(x)
+ y=tonumber(y)
+ r=tonumber(r)
+ g=tonumber(g)
+ b=tonumber(b)
+ remote_players[id] = {pos={x,y},color={r,g,b},id=id}
+ elseif op == "leave" then
+ local id = tonumber(w[2])
+ remote_players[id]=nil
+ elseif op == "move" then
+ local id,x,y = unpack(w,2)
+ id=tonumber(id)
+ x=tonumber(x)
+ y=tonumber(y)
+ assert(remote_players[id],"wheeze")
+ remote_players[id].pos[1] = x
+ remote_players[id].pos[2] = y
+ elseif op == "you" then
+ local id,x,y,r,g,b = unpack(w,2)
+ id=tonumber(id)
+ x=tonumber(x)
+ y=tonumber(y)
+ r=tonumber(r)
+ g=tonumber(g)
+ b=tonumber(b)
+ local_player = {pos={x,y},color={r,g,b},id=id}
+ end
+ end
+ until not ev
+end
+
+function love.draw()
+ if local_player then
+ draw_player(local_player,true)
+ end
+ for _,pl in pairs(remote_players) do
+ draw_player(pl)
+ end
+end
+
+function love.load()
+ host = enet.host_create()
+ peer = host:connect("localhost:8473")
+end
+
+function love.quit()
+ peer:disconnect()
+ host:flush()
+end