Piorjade

Love2D MP Server w/ colliders

Sep 11th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.11 KB | None | 0 0
  1. --[[
  2.  
  3.     Server for the testgame
  4.  
  5. ]]
  6.  
  7. local socket = require "socket"
  8. local udp = socket.udp()
  9. local OBJECT_WIDTH = 159
  10. print(tostring(socket))
  11. udp:settimeout(0)
  12. udp:setsockname("*", 12345)
  13. local world = {
  14.     [1] = {
  15.         type = "metal_block",
  16.         x = 0,
  17.         y = 0
  18.     },
  19.     [2] = {
  20.         type = "metal_block",
  21.         x = OBJECT_WIDTH,
  22.         y = 0
  23.     },
  24.     [3] = {
  25.         type = "metal_block",
  26.         x = OBJECT_WIDTH*2,
  27.         y = 0,
  28.     },
  29.     [4] = {
  30.         type = "metal_block",
  31.         x = OBJECT_WIDTH*2,
  32.         y = OBJECT_WIDTH*3
  33.     }
  34. }
  35. local data, msg_or_ip, port_or_nil
  36. local entity, cmd, parms
  37.  
  38. local running = true
  39. print("Beginning server loop.")
  40. while running do
  41.     data, msg_or_ip, port_or_nil = udp:receivefrom()
  42.     if data then
  43.         entity, cmd, parms = data:match("^(%S*) (%S*) (.*)")
  44.         if cmd == "move" then
  45.             local x, y = parms:match("^(%-?[%d.e]*) (%-?[%d.e]*)$")
  46.             assert(x and y)
  47.             x, y = tonumber(x), tonumber(y)
  48.             local ent = world[entity] or {x=0, y=0}
  49.             world[entity] = {x=ent.x+x, y=ent.y+y, type=ent.type}
  50.         elseif cmd == "at" then
  51.             local x, y, typ = parms:match("^(%-?[%d.e]*) (%-?[%d.e]*) (%S*)")
  52.             print("parms: "..parms.." Typ: "..typ)
  53.             assert(x and y and typ)
  54.             x, y = tonumber(x), tonumber(y)
  55.             world[entity] = {x=x, y=y, type=typ}
  56.             world[entity].type = tostring(typ)
  57.             print(tostring(entity).." "..tostring(world[entity].type))
  58.         elseif cmd == "update" then
  59.             for k, v in pairs(world) do
  60.                 print("ITEM: "..tostring(k).." "..tostring(v.type))
  61.                 udp:sendto(string.format("%s %s %d %d %s", k, 'at', v.x, v.y, v.type), msg_or_ip,  port_or_nil)
  62.             end
  63.         elseif cmd == "quit" then
  64.             running = false
  65.         else
  66.             print("Unrecognized command: ", cmd)
  67.         end
  68.     elseif msg_or_ip ~= "timeout" then
  69.         error("Unknown network error: "..tostring(msg))
  70.     end
  71.  
  72.     socket.sleep(0.001)
  73. end
  74. print("Thank you.")
Advertisement
Add Comment
Please, Sign In to add comment