]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/interaction_code.lua
b3631c66b5a55fa9015302ea86a436942521a1f4
[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
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.object:get_hp()
7         
8         if (self.punched_timer <= 0 and hp > 1) or puncher == self.object then
9                 self.hostile = true
10                 self.hostile_timer = 20
11                 self.punched_timer = 0.8
12                 local hurt = tool_capabilities.damage_groups.fleshy
13                 if not hurt then
14                         hurt = 1
15                 end
16                 
17                 self.object:set_hp(hp-hurt)
18                 if hp > 1 then
19                         minetest.sound_play("pig", {object=self.object, gain = 1.0, max_hear_distance = 60,pitch = math.random(80,100)/100})
20                 end
21                 
22                 self.hp = hp-hurt
23                 
24
25                 self.direction = vector.multiply(dir,-1)
26                 self.speed = 5
27
28                 dir = vector.multiply(dir,10)
29                 dir.y = 4
30                 self.object:add_velocity(dir)
31         elseif self.punched_timer <= 0 and self.death_animation_timer == 0 then
32                 self.death_animation_timer = 1
33                 minetest.sound_play("pig_die", {object=self.object, gain = 1.0, max_hear_distance = 60,pitch = math.random(80,100)/100})
34         end
35 end
36
37 --this is what happens when a mob diese
38 pig.on_death = function(self, killer)
39         local pos = self.object:getpos()
40         --pos.y = pos.y + 0.4
41         minetest.sound_play("mob_die", {pos = pos, gain = 1.0})
42         minetest.add_particlespawner({
43                 amount = 40,
44                 time = 0.001,
45                 minpos = pos,
46                 maxpos = pos,
47                 minvel = vector.new(-5,-5,-5),
48                 maxvel = vector.new(5,5,5),
49                 minacc = {x=0, y=0, z=0},
50                 maxacc = {x=0, y=0, z=0},
51                 minexptime = 1.1,
52                 maxexptime = 1.5,
53                 minsize = 1,
54                 maxsize = 2,
55                 collisiondetection = false,
56                 vertical = false,
57                 texture = "smoke.png",
58         })
59         local obj = minetest.add_item(pos,"mob:raw_porkchop")
60         self.child:get_luaentity().parent = nil
61 end
62
63 --this makes the mob rotate and then die
64 pig.manage_death_animation = function(self,dtime)
65         if self.death_animation_timer > 0 then
66                 self.death_animation_timer = self.death_animation_timer - dtime
67                 
68                 local self_rotation = self.object:get_rotation()
69                 
70                 if self_rotation.z < math.pi/2 then
71                         self_rotation.z = self_rotation.z + (dtime*2)
72                         self.object:set_rotation(self_rotation)
73                 end
74                 
75                 --print(self.death_animation_timer)
76                 local currentvel = self.object:getvelocity()
77                 local goal = vector.new(0,0,0)
78                 local acceleration = vector.new(goal.x-currentvel.x,0,goal.z-currentvel.z)
79                 acceleration = vector.multiply(acceleration, 0.05)
80                 self.object:add_velocity(acceleration)
81                 self.object:set_animation({x=0,y=0}, 15, 0, true)
82                 self.return_head_to_origin(self,dtime)
83                 
84                 if self.death_animation_timer < 0 then
85                         print("dead")
86                         self.object:punch(self.object, 2, 
87                                                 {
88                                                 full_punch_interval=1.5,
89                                                 damage_groups = {fleshy=2},
90                                         })
91                 end
92         end
93 end
94
95 --the pig will look for and at players
96 pig.look_around = function(self,dtime)
97         local pos = self.object:get_pos()
98         
99         --this is where the mob is actually looking
100         --local eye_ray = self.raycast_look(self,dtime)
101         --this is below where the mob is pointed, checks if ledge
102         --[[ --work on this later
103         local ledge_ray = self.look_below(self)
104                 
105         local is_a_drop = true
106         --check if there's a drop
107         if ledge_ray then
108                 for pointed_thing in ledge_ray do
109                         if pointed_thing then
110                                 local pos2 = pointed_thing.under
111                                 local distance = math.floor(vector.subtract(pos2,pos).y-self.object:get_properties().collisionbox[2]+0.5+0.5)
112                                 if distance >= -3 then
113                                         is_a_drop = false
114                                 end
115                         end
116                 end
117         end
118         --turn around
119         if is_a_drop == true then
120                 self.direction = vector.multiply(self.direction, -1)
121                 print("turning around")
122         end
123         ]]--
124         
125         --a mob will check if it needs to jump
126         if eye_ray then
127                 for pointed_thing in eye_ray do
128                         local pos = self.object:get_pos()
129                         local pos2 = pointed_thing.under
130                         local walkable
131                         if minetest.registered_nodes[minetest.get_node(pos2).name] then
132                                 walkable = minetest.registered_nodes[minetest.get_node(pos2).name].walkable
133                         end
134                         if walkable then
135                                 if vector.distance(pos,pos2) < 1 then
136                                         self.jump(self)
137                                         break
138                                 end
139                         end
140                 end
141         end
142         
143         
144         --STARE O_O
145         --and follow!
146         self.following = false
147         local player_found = false
148         for _,object in ipairs(minetest.get_objects_inside_radius(pos, 6)) do
149                 if object:is_player() and player_found == false and object:get_hp() > 0 then
150                         --look at player's camera
151                         local pos2 = object:get_pos()
152                         pos2.y = pos2.y + 1.625
153                         
154                         player_found = true
155                         
156                         self.move_head(self,pos2,dtime)
157                         
158                         if self.hostile == true then
159                                 self.direction = vector.direction(pos,pos2)
160                                 local distance = vector.distance(pos,pos2)-2
161                                 if distance < 0 then
162                                         distance = 0
163                                 end
164                                 
165                                 --punch the player
166                                 if distance < 1 and self.punch_timer <= 0 and object:get_hp() > 0 then
167                                         self.punch_timer = 1
168                                         object:punch(self.object, 2, 
169                                                 {
170                                                 full_punch_interval=1.5,
171                                                 damage_groups = {fleshy=2},
172                                         },vector.direction(pos,pos2))
173                                 end
174                                 self.speed = distance * 3
175                                 self.following = true
176                         end
177                         --only look at one player
178                         break
179                 end
180         end
181         --stare straight if not found
182         if player_found == false then
183                 self.move_head(self,nil,dtime)
184                 self.manage_hostile_timer(self,dtime)
185         end
186 end