- Closes #22
- Closes #23
This commit is contained in:
Leslie Krause 2020-05-15 23:46:00 -04:00
parent 497d5e29f7
commit 6bd035b69f
1 changed files with 42 additions and 33 deletions

View File

@ -492,6 +492,10 @@ mobs.register_mob = function ( name, def )
return abs( normalize_angle( yaw - self.yaw ) ) return abs( normalize_angle( yaw - self.yaw ) )
end, end,
get_target_distance = function ( self )
return vector.distance( self.pos, self.target.pos )
end,
-- collision processing -- -- collision processing --
get_pos_ahead = function ( self, outset, angle ) get_pos_ahead = function ( self, outset, angle )
@ -834,15 +838,15 @@ mobs.register_mob = function ( name, def )
end end
end end
local target_pos = self.target.pos
local dist = self:get_target_distance( )
if cycles % 2 == 0 then if cycles % 2 == 0 then
if self:get_target_yaw_delta( ) > rad_45 or random( 5 ) == 1 then if self:get_target_yaw_delta( ) > rad_45 or random( 5 ) == 1 then
self:turn_to( self:get_target_yaw( rad_20 ), 10 ) self:turn_to( self:get_target_yaw( rad_20 ), 10 )
end end
end end
local target_pos = self.target.pos
local dist = vector.distance( self.pos, target_pos )
if dist <= self.follow_range then if dist <= self.follow_range then
if self.speed > 0 then if self.speed > 0 then
self:set_speed( 0 ) self:set_speed( 0 )
@ -868,7 +872,7 @@ mobs.register_mob = function ( name, def )
if self.yield_level < 3 and self.move_result.collides_xz then if self.yield_level < 3 and self.move_result.collides_xz then
self.yield_level = self.yield_level + 1 self.yield_level = self.yield_level + 1
self.object:set_velocity_vert( 5 ) self.object:set_velocity_vert( 5 )
elseif random( 2 ) == 1 then elseif random( 5 ) == 1 then
self.object:set_velocity_vert( 5 ) self.object:set_velocity_vert( 5 )
end end
end end
@ -879,9 +883,16 @@ mobs.register_mob = function ( name, def )
start_search_action = function ( self ) start_search_action = function ( self )
assert( self.target.pos ) -- sanity check assert( self.target.pos ) -- sanity check
self:turn_to( self:get_target_yaw( rad_5 ), 10 ) local dist = self:get_target_distance( )
self:set_speed( self.walk_velocity )
self:turn_to( self:get_target_yaw( rad_0 ), 20 )
if self:get_target_yaw_delta( ) > rad_10 or dist <= self.search_range then
self:set_speed( 0 )
self:set_animation( "stand" )
else
self:set_speed( self.sneak_velocity )
self:set_animation( "walk" ) self:set_animation( "walk" )
end
self.timekeeper.clear( "hunger" ) self.timekeeper.clear( "hunger" )
self.timekeeper.start( 0.5, "action", self.on_search_action ) self.timekeeper.start( 0.5, "action", self.on_search_action )
@ -897,7 +908,7 @@ mobs.register_mob = function ( name, def )
end end
local target_pos = self.target.pos local target_pos = self.target.pos
local dist = vector.distance( self.pos, target_pos ) local dist = self:get_target_distance( )
-- go to last known position of target and look around -- go to last known position of target and look around
if dist <= self.search_range then if dist <= self.search_range then
@ -907,14 +918,15 @@ mobs.register_mob = function ( name, def )
end end
if random( 4 ) == 1 then if random( 4 ) == 1 then
self:turn_to( self:get_random_yaw( rad_180 ), 20 ) self:turn_to( self:get_random_yaw( rad_180 ), 10 )
end end
else else
if self.speed == 0 then -- wait at least 2 seconds after turning to start walking
self:set_speed( self.walk_velocity ) if self.speed == 0 and cycles >= 4 and random( 3 ) == 1 then
self:set_speed( self.sneak_velocity )
self:set_animation( "walk" ) self:set_animation( "walk" )
end
else
if self.can_fly then if self.can_fly then
-- descend or ascend to slightly above player altitude, but prevent incessant bobbing -- descend or ascend to slightly above player altitude, but prevent incessant bobbing
local v = 0 local v = 0
@ -929,9 +941,6 @@ mobs.register_mob = function ( name, def )
if self.yield_level < 3 and self.move_result.collides_xz then if self.yield_level < 3 and self.move_result.collides_xz then
self.yield_level = self.yield_level + 1 self.yield_level = self.yield_level + 1
self.object:set_velocity_vert( 5 ) self.object:set_velocity_vert( 5 )
elseif random( 2 ) == 1 then
self.object:set_velocity_vert( 5 )
end
end end
end end
end end
@ -940,7 +949,7 @@ mobs.register_mob = function ( name, def )
start_escape_action = function ( self ) start_escape_action = function ( self )
assert( self.target.pos ) -- sanity check assert( self.target.pos ) -- sanity check
local dist = vector.distance( self.pos, self.target.pos ) local dist = self:get_target_distance( )
if self:get_target_yaw_delta( ) < rad_60 and dist <= self.escape_range then if self:get_target_yaw_delta( ) < rad_60 and dist <= self.escape_range then
-- recoil if facing intruder -- recoil if facing intruder
self:set_speed( -self.recoil_velocity ) self:set_speed( -self.recoil_velocity )
@ -978,7 +987,7 @@ mobs.register_mob = function ( name, def )
end end
local target_pos = self.target.pos local target_pos = self.target.pos
local dist = vector.distance( self.pos, target_pos ) local dist = self:get_target_distance( )
if dist <= self.escape_range then if dist <= self.escape_range then
-- if close, keep backtracking -- if close, keep backtracking
@ -1053,7 +1062,7 @@ mobs.register_mob = function ( name, def )
end end
local target_pos = self.target.pos local target_pos = self.target.pos
local dist = vector.distance( self.pos, target_pos ) local dist = self:get_target_distance( )
self:turn_to( self:get_target_yaw( rad_0 ), 5 ) self:turn_to( self:get_target_yaw( rad_0 ), 5 )
@ -1588,11 +1597,11 @@ mobs.presets = {
texture = "heart.png", texture = "heart.png",
} ) } )
end end
return nil return "ignore"
end end
end end
return random( wait_chance ) == 1 and "follow" or nil return random( wait_chance ) == 1 and "follow" or "ignore"
end end
end, end,
} }