]> git.lizzy.rs Git - Crafter.git/blobdiff - mods/mob/interaction_code.lua
Fix "zombie" mob state on unload reload during die animation
[Crafter.git] / mods / mob / interaction_code.lua
index 341b1d40f2efa03f4169dafedbbe4e996cd2820d..b3631c66b5a55fa9015302ea86a436942521a1f4 100644 (file)
@@ -1,23 +1,13 @@
 --this is the file which houses the functions that control how mobs interact with the world
 
---this controls how fast the mob punches
-mob.manage_punch_timer = function(self,dtime)
-       if self.punch_timer > 0 then
-               self.punch_timer = self.punch_timer - dtime
-       end
-       --this controls how fast you can punch the mob (punched timer reset)
-       if self.punched_timer > 0 then
-               --print(self.punched_timer)
-               self.punched_timer = self.punched_timer - dtime
-       end
-end
-
 
 --this controls what happens when the mob gets punched
-mob.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
+pig.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
        local hp = self.object:get_hp()
        
        if (self.punched_timer <= 0 and hp > 1) or puncher == self.object then
+               self.hostile = true
+               self.hostile_timer = 20
                self.punched_timer = 0.8
                local hurt = tool_capabilities.damage_groups.fleshy
                if not hurt then
@@ -45,7 +35,7 @@ mob.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities,
 end
 
 --this is what happens when a mob diese
-mob.on_death = function(self, killer)
+pig.on_death = function(self, killer)
        local pos = self.object:getpos()
        --pos.y = pos.y + 0.4
        minetest.sound_play("mob_die", {pos = pos, gain = 1.0})
@@ -71,14 +61,14 @@ mob.on_death = function(self, killer)
 end
 
 --this makes the mob rotate and then die
-mob.manage_death_animation = function(self,dtime)
+pig.manage_death_animation = function(self,dtime)
        if self.death_animation_timer > 0 then
                self.death_animation_timer = self.death_animation_timer - dtime
                
                local self_rotation = self.object:get_rotation()
                
-               if self_rotation.x < math.pi/2 then
-                       self_rotation.x = self_rotation.x + (dtime*2)
+               if self_rotation.z < math.pi/2 then
+                       self_rotation.z = self_rotation.z + (dtime*2)
                        self.object:set_rotation(self_rotation)
                end
                
@@ -89,7 +79,7 @@ mob.manage_death_animation = function(self,dtime)
                acceleration = vector.multiply(acceleration, 0.05)
                self.object:add_velocity(acceleration)
                self.object:set_animation({x=0,y=0}, 15, 0, true)
-               self.return_head_to_origin(self)
+               self.return_head_to_origin(self,dtime)
                
                if self.death_animation_timer < 0 then
                        print("dead")
@@ -101,3 +91,96 @@ mob.manage_death_animation = function(self,dtime)
                end
        end
 end
+
+--the pig will look for and at players
+pig.look_around = function(self,dtime)
+       local pos = self.object:get_pos()
+       
+       --this is where the mob is actually looking
+       --local eye_ray = self.raycast_look(self,dtime)
+       --this is below where the mob is pointed, checks if ledge
+       --[[ --work on this later
+       local ledge_ray = self.look_below(self)
+               
+       local is_a_drop = true
+       --check if there's a drop
+       if ledge_ray then
+               for pointed_thing in ledge_ray do
+                       if pointed_thing then
+                               local pos2 = pointed_thing.under
+                               local distance = math.floor(vector.subtract(pos2,pos).y-self.object:get_properties().collisionbox[2]+0.5+0.5)
+                               if distance >= -3 then
+                                       is_a_drop = false
+                               end
+                       end
+               end
+       end
+       --turn around
+       if is_a_drop == true then
+               self.direction = vector.multiply(self.direction, -1)
+               print("turning around")
+       end
+       ]]--
+       
+       --a mob will check if it needs to jump
+       if eye_ray then
+               for pointed_thing in eye_ray do
+                       local pos = self.object:get_pos()
+                       local pos2 = pointed_thing.under
+                       local walkable
+                       if minetest.registered_nodes[minetest.get_node(pos2).name] then
+                               walkable = minetest.registered_nodes[minetest.get_node(pos2).name].walkable
+                       end
+                       if walkable then
+                               if vector.distance(pos,pos2) < 1 then
+                                       self.jump(self)
+                                       break
+                               end
+                       end
+               end
+       end
+       
+       
+       --STARE O_O
+       --and follow!
+       self.following = false
+       local player_found = false
+       for _,object in ipairs(minetest.get_objects_inside_radius(pos, 6)) do
+               if object:is_player() and player_found == false and object:get_hp() > 0 then
+                       --look at player's camera
+                       local pos2 = object:get_pos()
+                       pos2.y = pos2.y + 1.625
+                       
+                       player_found = true
+                       
+                       self.move_head(self,pos2,dtime)
+                       
+                       if self.hostile == true then
+                               self.direction = vector.direction(pos,pos2)
+                               local distance = vector.distance(pos,pos2)-2
+                               if distance < 0 then
+                                       distance = 0
+                               end
+                               
+                               --punch the player
+                               if distance < 1 and self.punch_timer <= 0 and object:get_hp() > 0 then
+                                       self.punch_timer = 1
+                                       object:punch(self.object, 2, 
+                                               {
+                                               full_punch_interval=1.5,
+                                               damage_groups = {fleshy=2},
+                                       },vector.direction(pos,pos2))
+                               end
+                               self.speed = distance * 3
+                               self.following = true
+                       end
+                       --only look at one player
+                       break
+               end
+       end
+       --stare straight if not found
+       if player_found == false then
+               self.move_head(self,nil,dtime)
+               self.manage_hostile_timer(self,dtime)
+       end
+end