]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/api/timers.lua
Make head code for mobs reuse same data
[Crafter.git] / mods / mob / api / timers.lua
1 local minetest,math = minetest,math
2 mobs.create_timer_functions = function(def,mob_register)
3         --this controls how fast the mob punches
4         mob_register.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         --this controls the hostile state
16         if def.hostile == true or def.attacked_hostile == true then
17                 if def.hostile_cooldown == true then
18                         mob_register.manage_hostile_timer = function(self,dtime)
19                                 if self.hostile_timer > 0 then
20                                         self.hostile_timer = self.hostile_timer - dtime
21                                 end
22                                 if self.hostile_timer <= 0 then
23                                         self.hostile = false
24                                 end
25                         end
26                 end
27         else
28                 mob_register.manage_scared_timer = function(self,dtime)
29                         if self.scared_timer > 0 then
30                                 self.scared_timer = self.scared_timer - dtime
31                         end
32                         if self.scared_timer <= 0 then
33                                 self.scared = false
34                         end
35                 end
36         end
37
38         mob_register.manage_hurt_color_timer = function(self,dtime)
39                 if self.hurt_color_timer > 0 then
40                         self.hurt_color_timer = self.hurt_color_timer - dtime
41                         if self.hurt_color_timer  <= 0 then
42                                 self.hurt_color_timer = 0
43                                 self.object:set_texture_mod("")
44                         end
45                 end
46         end
47
48         mob_register.manage_explode_timer = function(self,dtime)
49                 self.tnt_timer = self.tnt_timer - dtime
50                 self.tnt_tick_timer = self.tnt_tick_timer  - dtime
51                 if self.tnt_tick_timer <= 0 and not self.dead then
52                         self.tnt_tick_timer = self.explosion_blink_timer
53                         self.tnt_mod_state = math.abs(self.tnt_mod_state-1)
54                         if self.tnt_mod_state == 0 then
55                                 self.object:set_texture_mod("")
56                         else
57                                 self.object:set_texture_mod("^[colorize:"..self.explosion_blink_color..":130")
58                         end
59                         --print(self.object:get_texture_mod())
60                         --self.object:set_texture_mod("^[colorize:red:130")
61                 end
62                 if self.tnt_timer <= 0 and not self.dead then
63                         
64                         self.object:set_texture_mod("^[colorize:red:130")
65                         
66                         local pos = self.object:get_pos()
67                         self.object:remove()
68                         tnt(pos,self.explosion_power,self.explosion_type)
69                 end
70         end
71
72         if def.custom_timer then
73                 mob_register.do_custom_timer = function(self,dtime)
74                         self.c_timer = self.c_timer + dtime
75                         if self.c_timer >= self.custom_timer then
76                                 self.c_timer = 0 
77                                 self.custom_timer_function(self,dtime)
78                         end
79                 end
80         end
81
82         mob_register.manage_projectile_timer = function(self,dtime)
83                 self.projectile_timer = self.projectile_timer - dtime
84         end
85
86         if def.friendly_in_daylight then
87                 mob_register.handle_friendly_in_daylight_timer = function(self,dtime)
88                         self.friendly_in_daylight_timer = self.friendly_in_daylight_timer + dtime
89                         if self.friendly_in_daylight_timer >= 2 then
90                                 self.friendly_in_daylight_timer = 0
91                                 local pos = self.object:get_pos()
92                                 local light = minetest.get_node_light(pos)
93                                 if pos and light and light >= 13 then --1 greater than torch light
94                                         if self.following == false then
95                                                 self.hostile = false
96                                         end
97                                 else
98                                         self.hostile = true
99                                 end
100                         end
101                 end
102         end
103
104         --this stops the pig from flying into the air
105         mob_register.manage_jump_timer = function(self,dtime)
106                 if self.jump_timer > 0 then
107                         self.jump_timer = self.jump_timer - dtime
108                 end
109         end
110         return(mob_register)
111 end