浏览代码

Fix most collision problems;

bjorn 9 年之前
父节点
当前提交
6d5d805734
共有 8 个文件被更改,包括 28 次插入28 次删除
  1. 13 17
      app/logic/collision.lua
  2. 4 1
      app/logic/map.lua
  3. 3 3
      app/player/base.lua
  4. 2 1
      app/player/main.lua
  5. 1 1
      app/player/server.lua
  6. 2 2
      data/skill/dusk.lua
  7. 2 2
      data/skill/rocketboots.lua
  8. 1 1
      data/spell/dusk.lua

+ 13 - 17
app/logic/collision.lua

@@ -1,10 +1,7 @@
 local Collision = class()
 Collision.cellSize = 128
-Collision.onCollide = function(_, a, b, dx, dy)
-  a, b = a.owner, b.owner
-  f.exe(a.collision.with and a.collision.with[b.collision.tag], a, b, dx, dy)
-  f.exe(b.collision.with and b.collision.with[a.collision.tag], b, a, -dx, -dy)
-end
+
+local check = {}
 
 function Collision:init()
   self.hc = require 'lib/hc'
@@ -44,22 +41,21 @@ function Collision:move(data)
   end
 
   data.object.shape:moveTo(x, y)
-end
 
-function Collision:update()
-  --[[for shape in self.hc:activeShapes() do
-    if shape.owner then
-      self:move({object = shape.owner})
-    end
+  if data.resolve then
+    self:resolve(data.object)
   end
+end
 
-  self.hc:update()
+function Collision:resolve(object)
+  local a = object
+  local check = {}
 
-  for shape in self.hc:activeShapes() do
-    if shape.owner then
-      self:move({object = shape.owner})
-    end
-  end]]
+  for other, vector in pairs(self.hc.collisions(object.shape)) do
+    local b, dx, dy = other.owner, vector.x, vector.y
+    f.exe(a.collision.with and a.collision.with[b.collision.tag], a, b, dx, dy)
+    f.exe(b.collision.with and b.collision.with[a.collision.tag], b, a, -dx, -dy)
+  end
 end
 
 function Collision:pointTest(x, y, options)

+ 4 - 1
app/logic/map.lua

@@ -47,7 +47,10 @@ function Map:init(name)
   end)
 
   table.each(self.props, function(prop)
-    setmetatable(prop, {__index = data.prop[prop.kind .. (ctx.tag or ''):capitalize()] or data.prop[prop.kind], __tostring = data.prop[prop.kind].__tostring})
+    setmetatable(prop, {
+      __index = data.prop[prop.kind .. (ctx.tag or ''):capitalize()] or data.prop[prop.kind],
+      __tostring = data.prop[prop.kind].__tostring
+    })
   end)
   self.props = table.filter(self.props, function(prop) return not ctx.tag or not prop.mod or self.mods[prop.mod] end)
   table.each(self.props, function(prop, id)

+ 3 - 3
app/player/base.lua

@@ -132,7 +132,7 @@ function Player:update()
   self.silence = timer.rot(self.silence)
   if self.ded then
     self.x, self.y = 0, 0
-    ctx.event:emit('collision.move', {object = self})
+    ctx.event:emit('collision.move', {object = self, resolve = true})
   end
   if ctx.view then
     self.depth = ctx.view:threeDepth(self.x, self.y, self.z)
@@ -184,7 +184,7 @@ function Player:move(input)
   self.y = math.clamp(self.y, 0, ctx.map.height)
 
   self.moving = true
-  ctx.collision:update()
+  ctx.event:emit('collision.move', {object = self, resolve = true})
   self.moving = nil
 end
 
@@ -280,7 +280,7 @@ function Player:die()
 
   self.alpha = 0
   self.x, self.y = 0, 0
-  ctx.event:emit('collision.move', {object = self})
+  ctx.event:emit('collision.move', {object = self, resolve = true})
 end
 
 function Player:spawn()

+ 2 - 1
app/player/main.lua

@@ -69,6 +69,7 @@ function PlayerMain:draw()
 end
 
 function PlayerMain:trace(data)
+  do return end
   self.x, self.y = data.x / 10, data.y / 10
   self.health, self.shield = data.health, data.shield
 
@@ -83,7 +84,7 @@ function PlayerMain:trace(data)
   end
 
   self.moving = true
-  ctx.collision:update()
+  ctx.event:emit('collision.move', {object = self, resolve = true})
   self.moving = nil
 end
 

+ 1 - 1
app/player/server.lua

@@ -93,7 +93,7 @@ function PlayerServer:trace(data, ping)
           p.y = lerpd.y
           newData[p.id] = {p.x, p.y}
           p.angle = lerpd.angle
-          ctx.event:emit('collision.move', {object = p})
+          ctx.event:emit('collision.move', {object = p, resolve = true})
         end
       end)
     end

+ 2 - 2
data/skill/dusk.lua

@@ -52,12 +52,12 @@ function Dusk:update(owner)
       owner.x, owner.y = tx, ty
       owner.moving = true
       owner.ghosting = true
-      ctx.collision:update()
+      ctx.event:emit('collision.move', {object = owner, resolve = true})
       owner.moving = nil
       owner.ghosting = nil
       self.targetGhostX, self.targetGhostY = owner.x, owner.y
       owner.x, owner.y = ox, oy
-      ctx.event:emit('collision.move', {object = owner})
+      ctx.event:emit('collision.move', {object = owner, resolve = true})
       if math.distance(px, py, self.targetGhostX, self.targetGhostY) > 2 then
         self.ghostTick = 1
       else

+ 2 - 2
data/skill/rocketboots.lua

@@ -51,12 +51,12 @@ function RocketBoots:update(owner)
       owner.x, owner.y = tx, ty
       owner.moving = true
       owner.ghosting = true
-      ctx.collision:update()
+      ctx.event:emit('collision.move', {object = owner, resolve = true})
       owner.moving = nil
       owner.ghosting = nil
       self.targetGhostX, self.targetGhostY = owner.x, owner.y
       owner.x, owner.y = ox, oy
-      ctx.event:emit('collision.move', {object = owner})
+      ctx.event:emit('collision.move', {object = owner, resolve = true})
       if math.distance(px, py, self.targetGhostX, self.targetGhostY) > 2 then
         self.ghostTick = 1
       else

+ 1 - 1
data/spell/dusk.lua

@@ -11,7 +11,7 @@ function Dusk:activate(mx, my)
 	self.owner.x, self.owner.y = self:resolveCircle(tx, ty, self.owner.radius)
 
   self.owner.moving = true
-  ctx.collision:update()
+  ctx.event:emit('collision.move', {object = self.owner, resolve = true})
   self.owner.moving = nil
 	self:moveOwnerTo(self.owner.x, self.owner.y)