mob_sitting.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. -------------------------------------------------------------------------------
  2. -- allows mobs to use furniture for sitting/lying on them
  3. -------------------------------------------------------------------------------
  4. -- * recognizes bences, chairs, armchairs and toilets as things to sit on (from cottages, 3dforniture and homedecor)
  5. -- * acceptable beds come from cottages, papyrus_bed, beds and colouredbeds
  6. mob_sitting = {}
  7. -- let the entity entity sleep on the bed at position t_pos (rotated according to pos.p2 or node.param2)
  8. mob_sitting.sleep_on_bed = function( self, pos )
  9. if( not( self ) or not( self.object ) or not( pos )) then
  10. return;
  11. end
  12. -- copy target position because we will slightly adjust it
  13. local t_pos = {x=pos.x, y=pos.y, z=pos.z, p2 = pos.p2};
  14. local param2 = pos.p2;
  15. if( not( pos.p2 )) then
  16. local node = minetest.get_node( pos );
  17. param2 = node.param2;
  18. end
  19. -- store in the mob data that the mob is sleeping here (else he would be standing
  20. -- after next initialization)
  21. self.trader_uses = {x=t_pos.x, y=t_pos.y, z=t_pos.z, p2 = param2};
  22. self.trader_does = 'sleep';
  23. local yaw = 0;
  24. if( param2==0 ) then
  25. yaw = 180; --270;
  26. t_pos.z = t_pos.z - 0.5;
  27. elseif( param2==1 ) then
  28. yaw = 90; --180;
  29. t_pos.x = t_pos.x - 0.5;
  30. elseif( param2==2 ) then
  31. yaw = 0; --90;
  32. t_pos.z = t_pos.z + 0.5;
  33. elseif( param2==3 ) then
  34. yaw = 270; --0;
  35. t_pos.x = t_pos.x + 0.5;
  36. end
  37. -- rotate the npc in the right direction
  38. self.object:set_yaw( math.rad( yaw ));
  39. -- move the entity on the furniture; the entity has already been rotated accordingly
  40. self.object:set_pos( {x=t_pos.x, y=t_pos.y+1.1,z=t_pos.z} );
  41. -- wield nothing
  42. mob_basics.update_texture( self, 'trader', {} );
  43. -- set sleep animation
  44. mob_sitting.set_animation( self, 'sleep' );
  45. return t_pos;
  46. end
  47. -- TODO: better place that in a more general mod...
  48. mob_sitting.set_animation = function( entity, anim )
  49. local a = 0; -- startframe of animation
  50. local b = 79; -- endframe of animation
  51. local speed = 30;
  52. if( anim=='stand' ) then
  53. a = 0;
  54. b = 79;
  55. elseif( anim=='sit' ) then
  56. a = 81;
  57. b = 148;
  58. elseif( anim=='sleep' ) then
  59. a = 164;
  60. b = 164;
  61. end
  62. entity.object:set_animation({x=a, y=b}, speed)
  63. end