]> git.lizzy.rs Git - Crafter.git/blobdiff - mods/player_mechanics/eating_mechanics.lua
Remove inverter self indexing
[Crafter.git] / mods / player_mechanics / eating_mechanics.lua
index ff75762a01cd81d880a12232ef504272b5fb1f27..2efddd14e808c8ad3f08cb31f03feeb6b5647daf 100644 (file)
@@ -1,20 +1,9 @@
-local minetest,math,vector,ipairs,pairs,table = minetest,math,vector,ipairs,pairs,table
-
-local food_class         = {}
-local food_control_pool  = {} -- holds eating data
-food_class.copy          = table.copy
-food_class.food_data     = {}
-food_class.ipairs        = ipairs
-food_class.pairs         = pairs
-food_class.play          = minetest.sound_play
-food_class.get_group     = minetest.get_item_group
-food_class.get_connected = minetest.get_connected_players
-food_class.add_vector    = vector.add
-food_class.multiply      = vector.multiply
-food_class.random        = math.random
-food_class.add_ps        = minetest.add_particlespawner
-
-food_class.particle_constant = {
+local minetest,math,vector,ipairs,pairs,table = 
+      minetest,math,vector,ipairs,pairs,table
+
+local food_control_pool  = {}
+
+local particle_constant = {
     amount = 12,
     time = 0.01,
     minpos = {x=-0.1,y=-0.1,z=-0.1},
@@ -31,145 +20,142 @@ food_class.particle_constant = {
     vertical = false,
 }
 
-
 -- creates volitile data for the game to use
-food_class.create_data = function(player)
-       food_class.name = player:get_player_name()
-       if not food_control_pool[food_class.name] then
-               food_control_pool[food_class.name] = {
+local name
+local create_data = function(player)
+    name = player:get_player_name()
+    if not food_control_pool[name] then
+               food_control_pool[name] = {
                        eating_step  = 0,
                        eating_timer = 0,
-               }
-       end
-end
-
--- sets data for the game to use
-food_class.set_data = function(player,data)
-       food_class.name = player:get_player_name()
-       if food_control_pool[food_class.name] then
-               for index,i_data in food_class.pairs(data) do
-                       if food_control_pool[food_class.name][index] ~= nil then
-                               food_control_pool[food_class.name][index] = i_data
-                       end
-               end
-       else
-               movement_class.create_movement_variables(player)
-       end
+        }
+    end
 end
 
--- retrieves data for the game to use
-food_class.get_data = function(player)
-       food_class.name = player:get_player_name()
-       if food_control_pool[food_class.name] then
-               return({
-                       eating_step  = food_control_pool[food_class.name].eating_step ,
-                       eating_timer = food_control_pool[food_class.name].eating_timer,
-               })
-       end
-end
 
 -- removes movement data
-food_class.terminate = function(player)
-       food_class.name = player:get_player_name()
-       if food_control_pool[food_class.name] then
-               food_control_pool[food_class.name] = nil
+local name
+local terminate = function(player)
+       name = player:get_player_name()
+       if food_control_pool[name] then
+               food_control_pool[name] = nil
        end
 end
 
 minetest.register_on_joinplayer(function(player)
-       food_class.create_data(player)
+       create_data(player)
 end)
 minetest.register_on_leaveplayer(function(player)
-       food_class.terminate(player)
+       terminate(player)
 end)
 
-food_class.manage_eating_effects = function(player,timer,sneaking,item)
-    food_class.local_player = player
-    food_class.pos = food_class.local_player:get_pos()
-    food_class.speed = food_class.local_player:get_player_velocity()
+-- manages player eating effects
+local position
+local velocity
+local offset
+local temp_particle
+local manage_eating_effects = function(player,timer,sneaking,item)
+    position    = player:get_pos()
+    velocity    = player:get_player_velocity()
+
     if sneaking then
-        food_class.pos.y  = food_class.pos.y + 1.2
-        food_class.offset = 0.6
+        position.y  = position.y + 1.2
+        offset = 0.6
     else
-        food_class.pos.y = food_class.pos.y + 1.3
-        food_class.offset = 0.3
+        position.y = position.y + 1.3
+        offset = 0.3
     end
 
-    food_class.pos = food_class.add_vector(food_class.pos, food_class.multiply(food_class.local_player:get_look_dir(),food_class.offset))
+    position = vector.add(position, vector.multiply(player:get_look_dir(),offset))
+
+    temp_particle = table.copy(particle_constant)
+    temp_particle.minpos = vector.add(position,temp_particle.minpos)
+    temp_particle.maxpos = vector.add(position,temp_particle.maxpos)
+    temp_particle.minvel = vector.add(velocity,temp_particle.minvel)
+    temp_particle.maxvel = vector.add(velocity,temp_particle.maxvel)
+    temp_particle.node   = {name=item.."node"}
 
-    food_class.temp_particle = food_class.copy(food_class.particle_constant)
-    food_class.temp_particle.minpos = food_class.add_vector(food_class.pos,food_class.temp_particle.minpos)
-    food_class.temp_particle.maxpos = food_class.add_vector(food_class.pos,food_class.temp_particle.maxpos)
-    food_class.temp_particle.minvel = food_class.add_vector(food_class.speed,food_class.temp_particle.minvel)
-    food_class.temp_particle.maxvel = food_class.add_vector(food_class.speed,food_class.temp_particle.maxvel)
-    food_class.temp_particle.node   = {name=item.."node"}
+    minetest.add_particlespawner(temp_particle)
 
-    food_class.add_ps(food_class.temp_particle)
     if timer >= 0.2 then
-        food_class.play("eat", {
-            object = food_class.local_player,
+        minetest.sound_play("eat", {
+            object = player,
             gain = 0.2                      ,
-            pitch = food_class.random(60,85)/100}
+            pitch = math.random(60,85)/100}
         )
         return(0)
     end
     return(timer)
 end
 
-food_class.finish_eating = function(player,timer)
+
+local item
+local finish_eating = function(player,timer)
     if timer >= 1 then
-        food_class.item = player:get_wielded_item()
-        hunger_pointer.eat_food(player,food_class.item)
-        food_class.play("eat_finish", {
-            object = food_class.local_player,
+        item = player:get_wielded_item()
+
+        player_eat_food(player,item)
+
+        minetest.sound_play("eat_finish", {
+            object = player,
             gain = 0.025                      ,
-            pitch = food_class.random(60,85)/100}
+            pitch = math.random(60,85)/100}
         )
         return(0)
     end
     return(timer)
 end
 
-food_class.manage_eating = function(player,dtime)
-    food_class.control = player:get_player_control()
+
+local name
+local control
+local item
+local satiation
+local hunger
+local eating_step
+local eating_timer
+local pool
+local manage_eating = function(player,dtime)
+    control = player:get_player_control()
+    name    = player:get_player_name()
+    pool    = food_control_pool[name]
     --eating
-    if food_class.control.RMB then
+    if control.RMB then
+        item      = player:get_wielded_item():get_name()
 
-        food_class.item = player:get_wielded_item():get_name()
-        food_class.food_data.satiation = food_class.get_group( food_class.item, "satiation")
-        food_class.food_data.hunger    = food_class.get_group( food_class.item, "hunger"   )
-        
-        if food_class.food_data.hunger > 0 or food_class.food_data.satiation > 0  then
+        satiation = minetest.get_item_group( item, "satiation")
+        hunger    = minetest.get_item_group( item, "hunger"   )
 
-            food_class.eating_data = food_class.get_data(player)
-            
-            food_class.eating_data.eating_step  = food_class.eating_data.eating_step  + dtime
-            food_class.eating_data.eating_timer = food_class.eating_data.eating_timer + dtime
+        if hunger > 0 or satiation > 0  then
 
-            food_class.eating_data.eating_timer = food_class.manage_eating_effects(player, food_class.eating_data.eating_timer, food_class.control.sneak,food_class.item)
+            pool.eating_step  = pool.eating_step  + dtime
+            pool.eating_timer = pool.eating_timer + dtime
 
-            food_class.eating_data.eating_step = food_class.finish_eating(player,food_class.eating_data.eating_step)
+            pool.eating_timer = manage_eating_effects(
+                player,
+                pool.eating_timer,
+                control.sneak,
+                item
+            )
+
+            pool.eating_step = finish_eating(
+                player,
+                pool.eating_step
+            )
 
-            food_class.set_data(player,{
-                eating_step  = food_class.eating_data.eating_step ,
-                eating_timer = food_class.eating_data.eating_timer,
-            })
         else
-            food_class.set_data(player,{
-                eating_step  = 0,
-                eating_timer = 0,
-            })
+            pool.eating_step  = 0
+            pool.eating_timer = 0
         end
     else
-        food_class.set_data(player,{
-            eating_step  = 0,
-            eating_timer = 0,
-        })
+        pool.eating_step  = 0
+        pool.eating_timer = 0
     end
 end
 
+local player
 minetest.register_globalstep(function(dtime)
-       for _,player in food_class.ipairs(food_class.get_connected()) do
-               food_class.manage_eating(player,dtime)
+       for _,player in ipairs(minetest.get_connected_players()) do
+               manage_eating(player,dtime)
        end
-end)
\ No newline at end of file
+end)