Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- This is a testgame to test out multiplayer features.
- ~Piorjade
- ]]
- local socket = require "socket"
- local address, port = "localhost", 12345
- local DEBUG
- local entity
- local updaterate = 0.03
- local SPEED = 100
- local world = {}
- local t
- local OBJECT_WIDTH, OBJECT_HEIGHT = 159, 159
- local _print = print
- function print(text)
- if DEBUG then
- _print(text)
- end
- end
- function love.load()
- udp = socket.udp()
- udp:settimeout(0)
- udp:setpeername(address, port)
- math.randomseed(os.time())
- entity = tostring(math.random(800, 9999))
- local dg = string.format("%s %s %d %d %s", entity, 'at', 320, 240, "Player")
- udp:send(dg)
- t = 0
- end
- function move(oldx, oldy, x, y)
- for each, object in pairs(world) do
- 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
- print("Collision with : "..tostring(each).." type: "..tostring(object.type))
- --Berechne wie viel von der Bewegung abgezogen werden muss, damit man so nah wie möglich an das objekt rankommt
- local diffx, diffy
- --Rechts
- if x > 0 then
- diffx = (world[entity].x+x+OBJECT_WIDTH+1) - (object.x)
- --links
- elseif x < 0 then
- diffx = (object.x+OBJECT_WIDTH+1) - (world[entity].x+x)
- else
- diffx = 0
- end
- --unten
- if y > 0 then
- diffy = (world[entity].y+y+OBJECT_WIDTH+1) - object.y
- --oben
- elseif y < 0 then
- diffy = (object.y+OBJECT_WIDTH+1) - (world[entity].y+y)
- else
- diffy = 0
- end
- print("Differenzen: diffx="..tostring(diffx).." y="..tostring(diffy))
- --Ausgleichen damit man nicht weiter zurückgeschoben wird als die bewegung ("Bounce" Effekt)
- if x > 0 then
- if x - diffx < 0 then
- diffx = x
- end
- elseif x < 0 then
- if x + diffx > 0 then
- diffx = -x
- end
- end
- if y > 0 then
- if y - diffy < 0 then
- diffy = y
- end
- elseif y < 0 then
- if y + diffy > 0 then
- diffy = -y
- end
- end
- --Falls es schon so nah wie möglich ist, wird aus der rechnung automatisch 0
- local newx, newy
- if x > 0 then
- newx = x-diffx
- else
- newx = x+diffx
- end
- if y > 0 then
- newy = y-diffy
- else
- newy = y+diffy
- end
- print("NEUE BEWEGUNG: x="..tostring(newx).." y="..tostring(newy))
- return newx, newy
- end
- end
- return x, y
- end
- function love.update(deltatime)
- t = t+deltatime
- if t > updaterate then
- local x, y = 0, 0
- if love.keyboard.isDown("up") then x, y = move(x, y, x, y-(SPEED*t)) end
- if love.keyboard.isDown("down") then x, y = move(x, y, x, y+(SPEED*t)) end
- if love.keyboard.isDown("left") then x, y = move(x, y, x-(SPEED*t), y) end
- if love.keyboard.isDown("right") then x, y = move(x, y, x+(SPEED*t), y) end
- local dg = string.format("%s %s %f %f", entity, "move", x, y)
- udp:send(dg)
- local dg = string.format("%s %s $", entity, "update")
- udp:send(dg)
- t = t-updaterate
- end
- repeat
- data, msg = udp:receive()
- if data then
- ent, cmd, parms = data:match("^(%S*) (%S*) (.*)")
- if cmd == "at" then
- local x, y, typ = parms:match("^(%-?[%d.e]*) (%-?[%d.e]*) (%S*)")
- assert(x and y and typ)
- x, y = tonumber(x), tonumber(y)
- world[ent] = {x=x, y=y, type = typ}
- else
- print("unrecognized command: ", cmd)
- end
- elseif msg ~= "timeout" then
- error("Network error: "..tostring(msg))
- end
- until not data
- end
- function love.draw()
- for k, v in pairs(world) do
- if v.type == "metal_block" then
- love.graphics.rectangle("fill", v.x, v.y, OBJECT_WIDTH, OBJECT_WIDTH)
- elseif v.type == "Player" then
- love.graphics.rectangle("line", v.x, v.y, OBJECT_WIDTH, OBJECT_WIDTH)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment