Piorjade

Love2D MP Client w/ colliders

Sep 11th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.94 KB | None | 0 0
  1. --[[
  2.  
  3.     This is a testgame to test out multiplayer features.
  4.     ~Piorjade
  5. ]]
  6.  
  7. local socket = require "socket"
  8.  
  9. local address, port = "localhost", 12345
  10. local DEBUG
  11.  
  12. local entity
  13. local updaterate = 0.03
  14. local SPEED = 100
  15.  
  16. local world = {}
  17. local t
  18.  
  19. local OBJECT_WIDTH, OBJECT_HEIGHT = 159, 159
  20.  
  21. local _print = print
  22.  
  23. function print(text)
  24.     if DEBUG then
  25.         _print(text)
  26.     end
  27. end
  28.  
  29. function love.load()
  30.     udp = socket.udp()
  31.     udp:settimeout(0)
  32.     udp:setpeername(address, port)
  33.     math.randomseed(os.time())
  34.     entity = tostring(math.random(800, 9999))
  35.     local dg = string.format("%s %s %d %d %s", entity, 'at', 320, 240, "Player")
  36.     udp:send(dg)
  37.  
  38.     t = 0
  39. end
  40.  
  41. function move(oldx, oldy, x, y)
  42.     for each, object in pairs(world) do
  43.         if each ~= entity and object.type == "metal_block" and ( (world[entity].x+x >= object.x and world[entity].x+x <= (object.x+OBJECT_WIDTH) ) or (world[entity].x+x+OBJECT_WIDTH >= object.x and world[entity].x+x+OBJECT_WIDTH <= (object.x+OBJECT_WIDTH) ) ) and ((world[entity].y+y >= object.y and world[entity].y+y <= (object.y+OBJECT_WIDTH)) or (world[entity].y+y+OBJECT_WIDTH >= object.y and world[entity].y+y+OBJECT_WIDTH <= (object.y+OBJECT_WIDTH))) then
  44.             print("Collision with : "..tostring(each).." type: "..tostring(object.type))
  45.            
  46.  
  47.             --Berechne wie viel von der Bewegung abgezogen werden muss, damit man so nah wie möglich an das objekt rankommt
  48.             local diffx, diffy
  49.             --Rechts
  50.             if x > 0 then
  51.                 diffx = (world[entity].x+x+OBJECT_WIDTH+1) - (object.x)
  52.             --links
  53.             elseif x < 0 then
  54.                 diffx = (object.x+OBJECT_WIDTH+1) - (world[entity].x+x)
  55.             else
  56.                 diffx = 0
  57.             end
  58.            
  59.             --unten
  60.             if y > 0 then
  61.                 diffy = (world[entity].y+y+OBJECT_WIDTH+1) - object.y
  62.             --oben
  63.             elseif y < 0 then
  64.                 diffy = (object.y+OBJECT_WIDTH+1) - (world[entity].y+y)
  65.             else
  66.                 diffy = 0
  67.             end
  68.  
  69.             print("Differenzen: diffx="..tostring(diffx).." y="..tostring(diffy))
  70.  
  71.             --Ausgleichen damit man nicht weiter zurückgeschoben wird als die bewegung ("Bounce" Effekt)
  72.             if x > 0 then
  73.                 if x - diffx < 0 then
  74.                     diffx = x
  75.                 end
  76.             elseif x < 0 then
  77.                 if x + diffx > 0 then
  78.                     diffx = -x
  79.                 end
  80.             end
  81.  
  82.             if y > 0 then
  83.                 if y - diffy < 0 then
  84.                     diffy = y
  85.                 end
  86.             elseif y < 0 then
  87.                 if y + diffy > 0 then
  88.                     diffy = -y
  89.                 end
  90.             end
  91.            
  92.            
  93.  
  94.             --Falls es schon so nah wie möglich ist, wird aus der rechnung automatisch 0
  95.            
  96.  
  97.             local newx, newy
  98.             if x > 0 then
  99.                 newx = x-diffx
  100.             else
  101.                 newx = x+diffx
  102.             end
  103.  
  104.             if y > 0 then
  105.                 newy = y-diffy
  106.             else
  107.                 newy = y+diffy
  108.             end
  109.             print("NEUE BEWEGUNG: x="..tostring(newx).." y="..tostring(newy))
  110.             return newx, newy
  111.            
  112.         end
  113.     end
  114.     return x, y
  115. end
  116.  
  117. function love.update(deltatime)
  118.     t = t+deltatime
  119.  
  120.     if t > updaterate then
  121.         local x, y = 0, 0
  122.         if love.keyboard.isDown("up") then x, y = move(x, y, x, y-(SPEED*t)) end
  123.         if love.keyboard.isDown("down") then x, y = move(x, y, x, y+(SPEED*t)) end
  124.         if love.keyboard.isDown("left") then x, y = move(x, y, x-(SPEED*t), y) end
  125.         if love.keyboard.isDown("right") then x, y = move(x, y, x+(SPEED*t), y) end
  126.         local dg = string.format("%s %s %f %f", entity, "move", x, y)
  127.         udp:send(dg)
  128.         local dg = string.format("%s %s $", entity, "update")
  129.         udp:send(dg)
  130.         t = t-updaterate
  131.     end
  132.  
  133.     repeat
  134.         data, msg = udp:receive()
  135.         if data then
  136.             ent, cmd, parms = data:match("^(%S*) (%S*) (.*)")
  137.             if cmd == "at" then
  138.                 local x, y, typ = parms:match("^(%-?[%d.e]*) (%-?[%d.e]*) (%S*)")
  139.                 assert(x and y and typ)
  140.                 x, y = tonumber(x), tonumber(y)
  141.                 world[ent] = {x=x, y=y, type = typ}
  142.             else
  143.                 print("unrecognized command: ", cmd)
  144.             end
  145.         elseif msg ~= "timeout" then
  146.             error("Network error: "..tostring(msg))
  147.         end
  148.     until not data
  149.  
  150. end
  151.  
  152. function love.draw()
  153.     for k, v in pairs(world) do
  154.         if v.type == "metal_block" then
  155.             love.graphics.rectangle("fill", v.x, v.y, OBJECT_WIDTH, OBJECT_WIDTH)
  156.         elseif v.type == "Player" then
  157.             love.graphics.rectangle("line", v.x, v.y, OBJECT_WIDTH, OBJECT_WIDTH)
  158.         end
  159.     end
  160. end
Advertisement
Add Comment
Please, Sign In to add comment