Advertisement
Guest User

Untitled

a guest
Jan 28th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.62 KB | None | 0 0
  1.  
  2. -- Some parts copied and edited from player.lua in default mod.
  3.  
  4. -- Configuration
  5. local UPDATE_INTERVAL = 0.2;
  6.  
  7. -- Player stats and animations
  8. local player_anim = {}
  9. local player_sneak = {}
  10. local player_idle = {}
  11. local player_last_pitch = {}
  12. local ANIM_STAND = 1
  13. local ANIM_SIT = 2
  14. local ANIM_LAY = 3
  15. local ANIM_WALK  = 4
  16. local ANIM_WALK_MINE = 5
  17. local ANIM_MINE = 6
  18. local ANIM_RUN = 7
  19. -- Called when a player's appearance needs to be updated
  20. minetest.register_on_joinplayer(function ( pl )
  21.     local name = pl:get_player_name()
  22.  
  23.     player_anim[name] = 0 -- Animation will be set further below immediately
  24.     player_sneak[name] = false
  25.     player_last_pitch[name] = pl:get_look_pitch();
  26.     player_idle[name] = false;
  27.     prop = {
  28.         mesh = "character.b3d",
  29.         textures = {"character.png", },
  30.         visual = "mesh",
  31.         visual_size = {x=1, y=1},
  32.     }
  33.     pl:set_properties(prop)
  34. end)
  35.  
  36. local bone_pos = {
  37.     Head = { x=0, y=6.75, z=0 };
  38.     Arm_Left = { x=-3.9, y=6.5, z=0 };
  39.     Arm_Right = { x=3.9, y=6.5, z=0 };
  40.     Leg_Left = { x=-1.15, y=0, z=0 };
  41.     Leg_Right = { x=1.15, y=0, z=0 };
  42. };
  43.  
  44. local HEAD = "Head";
  45. local LARM = "Arm_Left";
  46. local RARM = "Arm_Right";
  47. local LLEG = "Leg_Left";
  48. local RLEG = "Leg_Right";
  49.  
  50. local function rotbone ( player, bone, x, y, z )
  51.     player:set_bone_position(bone, bone_pos[bone], { x=x, y=y, z=z });
  52. end
  53.  
  54. local step = 0;
  55. local FULL_STEP = 40;
  56.  
  57. local anims = {
  58.  
  59.     [ANIM_STAND] = function ( player, name )
  60.         if (player_idle[name]) then return; end
  61.         rotbone(player, LARM, 180, 0, 0);
  62.         rotbone(player, RARM, 180, 0, 0);
  63.         rotbone(player, LLEG, 0, 0, 0);
  64.         rotbone(player, RLEG, 0, 0, 0);
  65.         print("Player "..name.." is idling");
  66.         player_idle[name] = true;
  67.     end;
  68.  
  69.     [ANIM_WALK] = function ( player, name )
  70.         local m = step / FULL_STEP;
  71.         local r = math.sin(m * math.rad(360));
  72.         rotbone(player, LARM, (r * 30) + 180, 0, 0);
  73.         rotbone(player, RARM, (r * -30) + 180, 0, 0);
  74.         rotbone(player, LLEG, (r * 30), 0, 0);
  75.         rotbone(player, RLEG, (r * -30), 0, 0);
  76.         player_idle[name] = false;
  77.     end;
  78.  
  79.     [ANIM_MINE] = function ( player, name )
  80.         local m = step / FULL_STEP;
  81.         local r2 = math.sin((m*3) * math.rad(360));
  82.         local look = math.deg(player:get_look_pitch());
  83.         rotbone(player, LARM, 180, 0, 0);
  84.         rotbone(player, RARM, (r2 * -15) + 270 + look, 20, 0);
  85.         rotbone(player, LLEG, 0, 0, 0);
  86.         rotbone(player, RLEG, 0, 0, 0);
  87.         player_idle[name] = false;
  88.     end;
  89.  
  90.     [ANIM_WALK_MINE] = function ( player, name )
  91.         local m = step / FULL_STEP;
  92.         local r = math.sin(m * math.rad(360));
  93.         local r2 = math.sin((m/2) * math.rad(360));
  94.         local look = math.deg(player:get_look_pitch());
  95.         rotbone(player, LARM, (r * 30) + 180, 0, 0);
  96.         rotbone(player, RARM, (r2 * -15) + 270 + look, 20, 0);
  97.         rotbone(player, LLEG, (r * 30), 0, 0);
  98.         rotbone(player, RLEG, (r * -30), 0, 0);
  99.         player_idle[name] = false;
  100.     end;
  101.  
  102.     [ANIM_RUN] = function ( player, name )
  103.         local m = step / FULL_STEP;
  104.         local r = math.sin(m * math.rad(360));
  105.         rotbone(player, LARM, (r * 60) + 180, 0, 0);
  106.         rotbone(player, RARM, (r * -60) + 180, 0, 0);
  107.         rotbone(player, LLEG, (r * 60), 0, 0);
  108.         rotbone(player, RLEG, (r * -60), 0, 0);
  109.         player_idle[name] = false;
  110.     end;
  111.  
  112. };
  113.  
  114. local function player_animate ( player, name, anim )
  115.     if (not anims[anim]) then return; end
  116.     anims[anim](player, name);
  117.     local pitch = player:get_look_pitch();
  118.     if (pitch ~= player_last_pitch[name]) then
  119.         player_last_pitch[name] = pitch;
  120.         rotbone(player, HEAD, math.deg(pitch), 0, 0);
  121.     end
  122. end
  123.  
  124. local timer = 0;
  125.  
  126. -- Check each player and apply animations
  127. minetest.register_globalstep(function ( dtime )
  128.     for _, pl in pairs(minetest.get_connected_players()) do
  129.         local name = pl:get_player_name()
  130.         local controls = pl:get_player_control()
  131.         local walking = false
  132.         local animation_speed_mod = animation_speed
  133.  
  134.         -- Determine if the player is walking
  135.         if controls.up or controls.down or controls.left or controls.right then
  136.             walking = true
  137.         end
  138.  
  139.         -- Determine if the player is sneaking, and reduce animation speed if so
  140.         if controls.sneak and pl:get_hp() ~= 0 and (walking or controls.LMB) then
  141.             animation_speed_mod = animation_speed_mod / 2
  142.             -- Refresh player animation below if sneak state changed
  143.             if not player_sneak[name] then
  144.                 player_anim[name] = 0
  145.                 player_sneak[name] = true
  146.             end
  147.         else
  148.             -- Refresh player animation below if sneak state changed
  149.             if player_sneak[name] then
  150.                 player_anim[name] = 0
  151.                 player_sneak[name] = false
  152.             end
  153.         end
  154.  
  155.         -- Apply animations based on what the player is doing
  156.         if pl:get_hp() == 0 then
  157.             if player_anim[name] ~= ANIM_LAY then
  158.                 player_anim[name] = ANIM_LAY
  159.             end
  160.         elseif walking and controls.LMB then
  161.             if player_anim[name] ~= ANIM_WALK_MINE then
  162.                 player_anim[name] = ANIM_WALK_MINE
  163.             end
  164.         elseif walking then
  165.             if player_anim[name] ~= ANIM_WALK then
  166.                 player_anim[name] = ANIM_WALK
  167.             end
  168.         elseif controls.LMB then
  169.             if player_anim[name] ~= ANIM_MINE then
  170.                 player_anim[name] = ANIM_MINE
  171.             end
  172.         elseif player_anim[name] ~= ANIM_STAND then
  173.             player_anim[name] = ANIM_STAND
  174.         end
  175.         step = step + 1;
  176.         if (step > FULL_STEP) then step = 0; end
  177.         timer = timer + dtime;
  178.         if (timer >= UPDATE_INTERVAL) then
  179.             timer = timer - UPDATE_INTERVAL;
  180.             player_animate(pl, name, player_anim[name]);
  181.         end
  182.     end
  183. end);
  184.  
  185. -- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement