]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/interaction_code.lua
Adjust the death particle and item height on pigs
[Crafter.git] / mods / mob / interaction_code.lua
1 --this is the file which houses the functions that control how mobs interact with the world
2
3 --this controls how fast the mob punches
4 mob.manage_punch_timer = function(self,dtime)
5         if self.punch_timer > 0 then
6                 self.punch_timer = self.punch_timer - dtime
7         end
8         --this controls how fast you can punch the mob (punched timer reset)
9         if self.punched_timer > 0 then
10                 --print(self.punched_timer)
11                 self.punched_timer = self.punched_timer - dtime
12         end
13 end
14
15
16 --this controls what happens when the mob gets punched
17 mob.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
18         local hp = self.object:get_hp()
19         
20         if (self.punched_timer <= 0 and hp > 1) or puncher == self.object then
21                 self.punched_timer = 0.8
22                 local hurt = tool_capabilities.damage_groups.fleshy
23                 if not hurt then
24                         hurt = 1
25                 end
26                 
27                 self.object:set_hp(hp-hurt)
28                 if hp > 1 then
29                         minetest.sound_play("pig", {object=self.object, gain = 1.0, max_hear_distance = 60,pitch = math.random(80,100)/100})
30                 end
31                 
32                 self.hp = hp-hurt
33                 
34
35                 self.direction = vector.multiply(dir,-1)
36                 self.speed = 5
37
38                 dir = vector.multiply(dir,10)
39                 dir.y = 4
40                 self.object:add_velocity(dir)
41         elseif self.punched_timer <= 0 and self.death_animation_timer == 0 then
42                 self.death_animation_timer = 1
43                 minetest.sound_play("pig_die", {object=self.object, gain = 1.0, max_hear_distance = 60,pitch = math.random(80,100)/100})
44         end
45 end
46
47 --this is what happens when a mob diese
48 mob.on_death = function(self, killer)
49         local pos = self.object:getpos()
50         --pos.y = pos.y + 0.4
51         minetest.sound_play("mob_die", {pos = pos, gain = 1.0})
52         minetest.add_particlespawner({
53                 amount = 40,
54                 time = 0.001,
55                 minpos = pos,
56                 maxpos = pos,
57                 minvel = vector.new(-5,-5,-5),
58                 maxvel = vector.new(5,5,5),
59                 minacc = {x=0, y=0, z=0},
60                 maxacc = {x=0, y=0, z=0},
61                 minexptime = 1.1,
62                 maxexptime = 1.5,
63                 minsize = 1,
64                 maxsize = 2,
65                 collisiondetection = false,
66                 vertical = false,
67                 texture = "smoke.png",
68         })
69         local obj = minetest.add_item(pos,"mob:raw_porkchop")
70         self.child:get_luaentity().parent = nil
71 end
72
73 --this makes the mob rotate and then die
74 mob.manage_death_animation = function(self,dtime)
75         if self.death_animation_timer > 0 then
76                 self.death_animation_timer = self.death_animation_timer - dtime
77                 
78                 local self_rotation = self.object:get_rotation()
79                 
80                 if self_rotation.x < math.pi/2 then
81                         self_rotation.x = self_rotation.x + (dtime*2)
82                         self.object:set_rotation(self_rotation)
83                 end
84                 
85                 --print(self.death_animation_timer)
86                 local currentvel = self.object:getvelocity()
87                 local goal = vector.new(0,0,0)
88                 local acceleration = vector.new(goal.x-currentvel.x,0,goal.z-currentvel.z)
89                 acceleration = vector.multiply(acceleration, 0.05)
90                 self.object:add_velocity(acceleration)
91                 self.object:set_animation({x=0,y=0}, 15, 0, true)
92                 self.return_head_to_origin(self)
93                 
94                 if self.death_animation_timer < 0 then
95                         print("dead")
96                         self.object:punch(self.object, 2, 
97                                                 {
98                                                 full_punch_interval=1.5,
99                                                 damage_groups = {fleshy=2},
100                                         })
101                 end
102         end
103 end