2
0
bjorn 10 жил өмнө
parent
commit
98d5eb1cde
3 өөрчлөгдсөн 33 нэмэгдсэн , 15 устгасан
  1. 22 8
      deps/physicsinterpolator.lua
  2. 10 6
      person.lua
  3. 1 1
      pigeon.lua

+ 22 - 8
deps/physicsinterpolator.lua

@@ -1,11 +1,13 @@
 PhysicsInterpolator = class()
 
-function PhysicsInterpolator:init(body)
-  self.body = body
+function PhysicsInterpolator:init(object, ...)
+  self.object = object
 
   self.previous = {}
   self.current = {}
 
+  self.props = {...}
+
   self:update()
 end
 
@@ -21,16 +23,28 @@ function PhysicsInterpolator:lerp()
   local y = lume.lerp(self.previous.y, self.current.y, z)
   local angle = lume.lerp(self.previous.angle, self.current.angle, z)
 
-  self.body:setPosition(x, y)
-  self.body:setAngle(angle)
+  self.object.body:setPosition(x, y)
+  self.object.body:setAngle(angle)
+
+  local result = {}
+
+  table.each(self.props, function(prop)
+    result[prop] = math.lerp(self.previous[prop], self.current[prop], z)
+  end)
+
+  return result
 end
 
 function PhysicsInterpolator:delerp()
-  self.body:setPosition(self.current.x, self.current.y)
-  self.body:setAngle(self.current.angle)
+  self.object.body:setPosition(self.current.x, self.current.y)
+  self.object.body:setAngle(self.current.angle)
 end
 
 function PhysicsInterpolator:updateState(dest)
-  dest.x, dest.y = self.body:getPosition()
-  dest.angle = self.body:getAngle()
+  table.each(self.props, function(prop)
+    dest[prop] = self.object[prop]
+  end)
+
+  dest.x, dest.y = self.object.body:getPosition()
+  dest.angle = self.object.body:getAngle()
 end

+ 10 - 6
person.lua

@@ -8,6 +8,7 @@ function Person:activate()
   self.h = 35
   self.w = self.h * widthRatio
   self.scale = self.h / self.image:getHeight()
+  self.alpha = 1
 
   self.body = love.physics.newBody(ctx.world, self.x - self.w / 2, self.y - self.h / 2, 'dynamic')
   self.shape = love.physics.newRectangleShape(self.w, self.h)
@@ -20,7 +21,7 @@ function Person:activate()
   self.fixture:setCategory(ctx.categories.person)
   self.fixture:setMask(ctx.categories.person, ctx.categories.building, ctx.categories.debris)
 
-  self.phlerp = PhysicsInterpolator(self.body)
+  self.phlerp = PhysicsInterpolator(self, 'alpha')
 
   ctx.event:emit('view.register', {object = self})
 
@@ -43,10 +44,10 @@ end
 function Person:draw()
   local g = love.graphics
 
-  self.phlerp:lerp()
-
+  local lerpd = self.phlerp:lerp()
   local x, y, angle = self.body:getX(), self.body:getY(), self.body:getAngle()
-  g.setColor(255, 255, 255)
+
+  g.setColor(255, 255, 255, 255 * math.clamp(lerpd.alpha, 0, 1))
   g.draw(self.image, x, y, angle, self.scale * self.direction, self.scale, self.image:getWidth() / 2, self.image:getHeight() / 2)
 
   self.phlerp:delerp()
@@ -89,7 +90,10 @@ end
 
 function Person.dead:update()
   local x, y = self.body:getLinearVelocity()
-  if (math.abs(x) < 1 and math.abs(y) < 1) or (math.abs(x) > 5000 and math.abs(y) > 5000) then
-    ctx.enemies:remove(self)
+  if (math.abs(x) < 1 and math.abs(y) < 1) or (math.abs(x) > 2000 and math.abs(y) > 2000) then
+    self.alpha = self.alpha - ls.tickrate
+    if self.alpha <= 0 then
+      ctx.enemies:remove(self)
+    end
   end
 end

+ 1 - 1
pigeon.lua

@@ -29,7 +29,7 @@ function Pigeon:init()
   self.fixture:setCategory(ctx.categories.pigeon)
   self.fixture:setMask(ctx.categories.oneWayPlatform, ctx.categories.person, ctx.categories.debris)
 
-  self.phlerp = PhysicsInterpolator(self.body)
+  self.phlerp = PhysicsInterpolator(self)
 
   self.fuel = self.maxFuel
   self.state = self.idle