Advertisement
birne006

Untitled

Jun 13th, 2013 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.60 KB | None | 0 0
  1. -- Gamestate game
  2.  
  3. function game:init() -- run once
  4.     playerTypes = {
  5.         runner = {},
  6.         hunter = {}
  7.     }
  8. end
  9.  
  10. function game:enter(previous, playerTable, ownID) -- speak: load
  11.     participants = {}
  12.     playerID = ownID
  13.  
  14.     function collide(dt, shape_one, shape_two, mtv_x, mtv_y) -- to-do: player collsion via network => update network player nicht lokal!
  15.         --print('colliding:', shape_one, shape_two)
  16.         if shape_one.data ~= nil then -- resolve player collision
  17.             shape_one:move(mtv_x, mtv_y)
  18.             shape_one.data.x = shape_one.data.x + mtv_x
  19.             shape_one.data.y = shape_one.data.y + mtv_y
  20.         end
  21.         if shape_two.data ~= nil then -- resolve player player collision
  22.             shape_two:move(-mtv_x, -mtv_y)
  23.             shape_two.data.x = shape_two.data.x - mtv_x
  24.             shape_two.data.y = shape_two.data.y - mtv_y
  25.             if shape_one.data.t == playerTypes.runner and shape_two.data.t == playerTypes.hunter
  26.                     or shape_one.data.t == playerTypes.hunter and shape_two.data.t == playerTypes.runner then
  27.                 print("Hunter wins!")
  28.             end
  29.         end
  30.     end
  31.  
  32.     function addRunner(id, x, y)
  33.         local runner = HC:addRectangle(x, y, playerDefault.collisionWidth, playerDefault.collisionHeight)
  34.         HC.addToGroup("player", runner)
  35.         if id == ownID then
  36.             print(id .. " That's me!")
  37.             runner.data = Player:New(id, playerTypes.runner, x, y)
  38.             player = runner
  39.         else
  40.             runner.data = Participant:New(id, playerTypes.runner, x, y)
  41.             participants[id] = runner
  42.         end
  43.     end
  44.  
  45.     function addHunter(id, x, y)
  46.         local hunter = HC:addRectangle(x, y, playerDefault.collisionWidth, playerDefault.collisionHeight)
  47.         HC.addToGroup("player", hunter)
  48.         if id == ownID then
  49.             print(id .. " That's me!")
  50.             hunter.data = Player:New(id, playerTypes.hunter, x, y)
  51.             player = hunter
  52.         else
  53.             hunter.data = Participant:New(id, playerTypes.hunter, x, y)
  54.             participants[id] = hunter
  55.         end
  56.     end
  57.  
  58.     function updatePlayer(dt)
  59.         local dx, dy = player.data.x, player.data.y
  60.         player.data:Update(dt, player.data.headingTowards.x, player.data.headingTowards.y)
  61.         dx, dy = player.data.x - dx, player.data.y - dy
  62.         player:move(dx, dy)
  63.     end
  64.    
  65.     function updateParticipant(id, playerUpdate, dt)
  66.         local participant = participants[id]
  67.         local dx, dy = playerUpdate.x - participant.data.x, playerUpdate.y - participant.data.y
  68.         participant.data:Update(dt, playerUpdate)
  69.         participant:move(dx, dy)
  70.     end
  71.  
  72.     HC = Collider.new(100)
  73.     love.graphics.setBackgroundColor(255, 255, 255)
  74.    
  75.     for i = 0,#playerTable do
  76.         if playerTable[i].playerType == playerTypes.runner then
  77.             print(playerTable[i].id .. ': Runner')
  78.             addRunner(playerTable[i].id, playerTable[i].x, playerTable[i].y)
  79.         else
  80.             print(playerTable[i].id .. ': Hunter')
  81.             addHunter(playerTable[i].id, playerTable[i].x, playerTable[i].y)
  82.         end
  83.     end
  84.    
  85.     -- set callbacks
  86.     HC:setCallbacks(collide, stop)
  87.    
  88.     cam = Camera(player.data.x, player.data.y, 1, 0)
  89.    
  90.     -- light cone
  91.     lightRadius = 350 --love.graphics.getWidth()/2
  92.     surroundingLightRadius = 50
  93.     lightCone = love.image.newImageData(2*lightRadius, 2*lightRadius)
  94.     lightCone:mapPixel(function(x, y)
  95.         xn = (x - lightRadius) / lightRadius
  96.         yn = (y - lightRadius) / lightRadius
  97.         local d = math.sqrt(xn*xn + yn*yn)
  98.            
  99.         if(d <= 1.5) then --(math.pow(d,5) < 1.5) then
  100.             local m = vector_hv(lightRadius, lightRadius)
  101.             local o = vector_hv(0, -lightRadius)
  102.             local p = vector_hv(x,y)
  103.             p = p - m
  104.             if(math.deg(math.acos(o * p / (o:len() * p:len()))) > 45) then
  105.                 if(d < surroundingLightRadius/lightRadius) then --lichtkegel
  106.                     return 255, 255, 255, math.min(1, math.pow(d + 1 - surroundingLightRadius/lightRadius, 80)) * 255
  107.                 else -- im kreis, außerhalb sichtkegel
  108.                     return 0, 0, 0, 255
  109.                 end
  110.             else -- lichtkreis direkt am spieler
  111.                 return 255, 255, 255, math.min(1, math.pow(d, 7)) * 255 -- math.pow(d, ...) bestimmt den Kegel / den Übergang
  112.             end
  113.         else -- ecken
  114.             return 255, 255, 255, 0
  115.         end
  116.     end)
  117.     lightCone = love.graphics.newImage(lightCone)
  118.    
  119.     -- corners for making a dark mask around the light radius
  120.     lightRadiusCorners = love.image.newImageData(2*lightRadius, 2*lightRadius)
  121.     lightRadiusCorners:mapPixel(function(x, y)
  122.         x = (x - lightRadius) / lightRadius
  123.         y = (y - lightRadius) / lightRadius
  124.         local d = math.sqrt(x*x + y*y)
  125.         if (d < 1 - 1/lightRadius) then
  126.             return 255, 255, 255, 0
  127.         else
  128.             return 255, 255, 255, 255
  129.         end
  130.     end)
  131.     lightRadiusCorners = love.graphics.newImage(lightRadiusCorners)
  132.    
  133.     require 'maps.map01'                                  -- noch umschreiben zu abfrage aus network info
  134.     background = love.graphics.newImage("maps/map01.png") -- dito
  135.    
  136.     shadowCasters = {}
  137.     for _,obj in pairs(obstacles) do
  138.         HC.addToGroup("obstacle", obj)
  139.         shadowCasters[#shadowCasters+1] = obj._polygon
  140.     end
  141.  
  142.     t = 0 -- zwecks network-updates
  143.  
  144. end
  145.  
  146. function game:update(dt)
  147.     --print(love.timer.getFPS() .. " fps")
  148.    
  149.     t = t + dt
  150.    
  151.     time.sinceStart = time.sinceStart + dt
  152.     time.s = math.floor(time.sinceStart) % 60
  153.     time.m = math.floor(time.sinceStart / 60) % 60
  154.     time.h = math.floor(time.sinceStart / 60 / 60) % 60
  155.    
  156.     player.data.headingTowards.x, player.data.headingTowards.y = 0, 0
  157.     if love.keyboard.isDown("w") then player.data.headingTowards.y = -1 end
  158.     if love.keyboard.isDown("s") then player.data.headingTowards.y = 1 end 
  159.     if love.keyboard.isDown("a") then player.data.headingTowards.x = -1 end
  160.     if love.keyboard.isDown("d") then player.data.headingTowards.x = 1 end
  161.     player.data.isClickingRun = love.keyboard.isDown(" ")
  162.     updatePlayer(dt)
  163.     player.data.directionSight = math.atan2(love.mouse.getX() - love.graphics.getWidth()/2, love.graphics.getHeight()/2 - love.mouse.getY())
  164.     player.data.directionHead = player.data.directionSight
  165.     local angleSightToBody = math.deg(player.data.directionSight - player.data.directionBody)
  166.     if (angleSightToBody < -90 and angleSightToBody > -180) or angleSightToBody >= 180 then
  167.         player.data.directionHead = player.data.directionBody - math.pi/2
  168.     elseif angleSightToBody > 90 or (angleSightToBody < -180 and angleSightToBody >= -270) then
  169.         player.data.directionHead = player.data.directionBody + math.pi/2
  170.     end
  171.    
  172.     -- verschicken der Updates an alle
  173.     local updateSelf = {
  174.         id = playerID,
  175.         dt = dt,
  176.         x = player.data.x,
  177.         y = player.data.y,
  178.         directionBody = player.data.directionBody,
  179.         directionHead = player.data.directionHead,
  180.         directionSight = player.data.directionSight,
  181.         speed = player.data.speed
  182.     }
  183.     if t > updateRate then
  184.         --t = 0
  185.         for i = 0,#playerTable do
  186.             if playerTable[i].id ~= playerID then
  187.                 udp:setpeername(playerTable[i].ip, playerTable[i].port)
  188.                 udp:send(json.encode(updateSelf))
  189.             end
  190.         end
  191.     end
  192.  
  193.     repeat
  194.         data, msg = udp:receive()
  195.         if data then
  196.             local update = json.decode(data)
  197.             updateParticipant(update.id, update, dt)
  198.         end
  199.     --  elseif msg ~= 'timeout' then
  200.     --      error("Network error: "..tostring(msg))
  201.     --  end    
  202.     until not data
  203.  
  204.     HC:update(dt)
  205.    
  206.     cam.x = player.data.x + player.data.offsetBody.x -- eigentlich cam:move(),
  207.     cam.y = player.data.y + player.data.offsetBody.y -- aber da das einen Vektor braucht und wir nur Koordinaten haben ...
  208. end
  209.  
  210. function game.draw()
  211.     cam:attach()
  212.    
  213.     love.graphics.setColor(255,255,255,255)
  214.     love.graphics.draw(background)
  215.    
  216.     local ppos = vector(player:center())
  217.  
  218.     player.data:Draw()
  219.     for i = 0,#participants do
  220.         if participants[i] ~= nil then
  221.             participants[i].data:Draw()
  222.         end
  223.     end
  224.  
  225.     love.graphics.setColor(0, 0, 0, 225)
  226.     love.graphics.draw(lightCone, ppos.x, ppos.y, player.data.directionSight, 1, 1, lightRadius, lightRadius)
  227.     love.graphics.setColor(0, 0, 0, 255)
  228.     love.graphics.draw(lightRadiusCorners, ppos.x, ppos.y, 0, 1, 1, lightRadius, lightRadius)
  229.     -- ___________
  230.     --|1  |      2|
  231.     --|   |_______|
  232.     --|   | p |   |
  233.     --|___|___|   |
  234.     --|4      |  3|
  235.     --|_______|___|
  236.     --
  237.     local innerLeft, innerRight = ppos.x - lightRadius, ppos.x + lightRadius
  238.     local innerTop, innerBottom = ppos.y - lightRadius, ppos.y + lightRadius
  239.     local outerLeft, outerRight = player.data.x - love.graphics.getWidth()/2, player.data.x + love.graphics.getWidth()/2
  240.     local outerTop, outerBottom = player.data.y - love.graphics.getHeight()/2, player.data.y + love.graphics.getHeight()/2
  241.     local widthWithOffset, heightWithOffset = love.graphics.getWidth()/2 + player.data.offsetBody.x, love.graphics.getHeight()/2 + player.data.offsetBody.y
  242.     love.graphics.rectangle('fill', outerLeft, outerTop,    widthWithOffset - lightRadius, heightWithOffset + lightRadius) -- 1
  243.     love.graphics.rectangle('fill', innerLeft, outerTop,    widthWithOffset + lightRadius, heightWithOffset - lightRadius) -- 2
  244.     love.graphics.rectangle('fill', innerRight, innerTop,   widthWithOffset - lightRadius, heightWithOffset + lightRadius) -- 3
  245.     love.graphics.rectangle('fill', outerLeft, innerBottom, widthWithOffset + lightRadius, heightWithOffset - lightRadius) -- 4
  246.  
  247.     -- cast shadows
  248.     love.graphics.setColor(0,0,0,255)
  249.     for _,poly in ipairs(shadowCasters) do
  250.         if ppos:dist(poly.centroid) < 5 * lightRadius then
  251.             local startv, endv = poly.vertices[1], poly.vertices[1]
  252.             local vertices = {}
  253.             for i,v in ipairs(poly.vertices) do
  254.                 local w = vector(poly.vertices[(i % #poly.vertices) + 1].x,poly.vertices[(i % #poly.vertices) + 1].y)
  255.                 local c = .5 * (w + v)
  256.                 local n = (w - v):perpendicular()
  257.                 local is_hidden = n * (c - ppos) < 0
  258.                 if is_hidden then
  259.                     vertices[1] = v.x
  260.                     vertices[2] = v.y
  261.                     vertices[3] = w.x
  262.                     vertices[4] = w.y
  263.                     v = v + (v - ppos) * 800
  264.                     w = w + (w - ppos) * 800
  265.                     vertices[5] = w.x
  266.                     vertices[6] = w.y
  267.                     vertices[7] = v.x
  268.                     vertices[8] = v.y
  269.                     love.graphics.polygon('fill', unpack(vertices))
  270.                 end
  271.             end
  272.         end
  273.     end
  274.  
  275.     drawHud()
  276.    
  277.     cam:detach()
  278. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement