]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/pig/interaction_code.lua
Add in slime prototype
[Crafter.git] / mods / mob / pig / interaction_code.lua
1 --this is the file which houses the functions that control how mobs interact with the world
2
3
4 --this controls what happens when the mob gets punched
5 pig.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
6         local hp = self.hp
7         
8         local hurt = tool_capabilities.damage_groups.damage
9         
10         if not hurt then
11                 hurt = 1
12         end
13         
14         local hp = hp-hurt
15         
16         if (self.punched_timer <= 0 and hp > 1) or puncher == self.object then
17                 self.hostile = true
18                 self.hostile_timer = 20
19                 self.punched_timer = 0.8
20                 
21                 if hp > 1 then
22                         minetest.sound_play("pig", {object=self.object, gain = 1.0, max_hear_distance = 60,pitch = math.random(80,100)/100})
23                 end
24                 
25                 self.hp = hp
26                 
27
28                 self.direction = vector.multiply(dir,-1)
29                 self.speed = 5
30
31                 dir = vector.multiply(dir,10)
32                 dir.y = 4
33                 self.object:add_velocity(dir)
34         elseif self.punched_timer <= 0 and self.death_animation_timer == 0 then
35                 self.death_animation_timer = 1
36                 self.dead = true
37                 minetest.sound_play("pig_die", {object=self.object, gain = 1.0, max_hear_distance = 60,pitch = math.random(80,100)/100})
38                 --self.object:set_texture_mod("^[colorize:red:90")
39                 --self.child:set_texture_mod("^[colorize:red:90")
40         end
41 end
42
43 --this is what happens when a mob diese
44 pig.on_death = function(self, killer)
45         local pos = self.object:getpos()
46         --pos.y = pos.y + 0.4
47         minetest.sound_play("mob_die", {pos = pos, gain = 1.0})
48         minetest.add_particlespawner({
49                 amount = 40,
50                 time = 0.001,
51                 minpos = pos,
52                 maxpos = pos,
53                 minvel = vector.new(-5,-5,-5),
54                 maxvel = vector.new(5,5,5),
55                 minacc = {x=0, y=0, z=0},
56                 maxacc = {x=0, y=0, z=0},
57                 minexptime = 1.1,
58                 maxexptime = 1.5,
59                 minsize = 1,
60                 maxsize = 2,
61                 collisiondetection = false,
62                 vertical = false,
63                 texture = "smoke.png",
64         })
65         local obj = minetest.add_item(pos,"mob:raw_porkchop")
66         self.child:get_luaentity().parent = nil
67         self.object:remove()
68 end
69
70 --this makes the mob rotate and then die
71 pig.manage_death_animation = function(self,dtime)
72         if self.death_animation_timer >= 0 and self.dead == true then
73                 self.death_animation_timer = self.death_animation_timer - dtime
74                 
75                 local self_rotation = self.object:get_rotation()
76                 
77                 if self_rotation.z < math.pi/2 then
78                         self_rotation.z = self_rotation.z + (dtime*2)
79                         self.object:set_rotation(self_rotation)
80                 end
81                 
82                 --print(self.death_animation_timer)
83                 local currentvel = self.object:getvelocity()
84                 local goal = vector.new(0,0,0)
85                 local acceleration = vector.new(goal.x-currentvel.x,0,goal.z-currentvel.z)
86                 acceleration = vector.multiply(acceleration, 0.05)
87                 self.object:add_velocity(acceleration)
88                 self.object:set_animation({x=0,y=0}, 15, 0, true)
89                 self.return_head_to_origin(self,dtime)
90                 
91                 if self.death_animation_timer <= 0 then
92                         self.on_death(self)
93                 end
94         end
95 end
96
97 --the pig will look for and at players
98 pig.look_around = function(self,dtime)
99         local pos = self.object:get_pos()
100         
101         --STARE O_O
102         --and follow!
103         self.following = false
104         local player_found = false
105         for _,object in ipairs(minetest.get_objects_inside_radius(pos, 6)) do
106                 if object:is_player() and player_found == false and object:get_hp() > 0 then
107                         --look at player's camera
108                         local pos2 = object:get_pos()
109                         pos2.y = pos2.y + 1.625
110                         
111                         player_found = true
112                         
113                         self.move_head(self,pos2,dtime)
114                         
115                         if self.hostile == true then
116                                 self.direction = vector.direction(pos,pos2)
117                                 local distance = vector.distance(pos,pos2)-2
118                                 if distance < 0 then
119                                         distance = 0
120                                 end
121                                 
122                                 --punch the player
123                                 if distance < 1 and self.punch_timer <= 0 and object:get_hp() > 0 then
124                                         self.punch_timer = 1
125                                         object:punch(self.object, 2, 
126                                                 {
127                                                 full_punch_interval=1.5,
128                                                 damage_groups = {fleshy=2},
129                                         },vector.direction(pos,pos2))
130                                 end
131                                 self.speed = distance * 3
132                                 self.following = true
133                         end
134                         --only look at one player
135                         break
136                 end
137         end
138         --stare straight if not found
139         if player_found == false then
140                 self.move_head(self,nil,dtime)
141                 self.manage_hostile_timer(self,dtime)
142         end
143 end