Add "lightweight" mob physics

This commit is contained in:
ElCeejo 2020-11-25 00:28:24 -08:00 committed by GitHub
parent fc8d9a6fc6
commit 5e62251465
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -525,3 +525,59 @@ function better_fauna.on_activate(self, staticdata, dtime_s)
self.breeding_time = mobkit.recall(self, "breeding_time") or 0 self.breeding_time = mobkit.recall(self, "breeding_time") or 0
self.breeding_cooldown = mobkit.recall(self, "breeding_cooldown") or 0 self.breeding_cooldown = mobkit.recall(self, "breeding_cooldown") or 0
end end
-------------
-- Physics --
-------------
function better_fauna.lightweight_physics(self)
local vel = self.object:get_velocity()
if self.isonground and not self.isinliquid then
self.object:set_velocity({x= vel.x> 0.2 and vel.x*mobkit.friction or 0,
y=vel.y,
z=vel.z > 0.2 and vel.z*mobkit.friction or 0})
end
if self.springiness and self.springiness > 0 then
local vnew = vector.new(vel)
if not self.collided then
for _,k in ipairs({'y','z','x'}) do
if vel[k]==0 and abs(self.lastvelocity[k])> 0.1 then
vnew[k]=-self.lastvelocity[k]*self.springiness
end
end
end
if not vector.equals(vel,vnew) then
self.collided = true
else
if self.collided then
vnew = vector.new(self.lastvelocity)
end
self.collided = false
end
self.object:set_velocity(vnew)
end
local surface = nil
local surfnodename = nil
local spos = mobkit.get_stand_pos(self)
spos.y = spos.y+0.01
local snodepos = mobkit.get_node_pos(spos)
local surfnode = mobkit.nodeatpos(spos)
while surfnode and surfnode.drawtype == 'liquid' do
surfnodename = surfnode.name
surface = snodepos.y+0.5
if surface > spos.y+self.height then break end
snodepos.y = snodepos.y+1
surfnode = mobkit.nodeatpos(snodepos)
end
self.isinliquid = surfnodename
if surface then
local submergence = min(surface-spos.y,self.height)/self.height
local buoyacc = mobkit.gravity*(self.buoyancy-submergence)
mobkit.set_acceleration(self.object,
{x=-vel.x*self.water_drag,y=buoyacc-vel.y*abs(vel.y)*0.4,z=-vel.z*self.water_drag})
else
self.object:set_acceleration({x=0,y=-2.8,z=0})
end
end