]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/api/api_hook.lua
Add collsion detection and fall damage to mobs
[Crafter.git] / mods / mob / api / api_hook.lua
1 --class 
2 mobs = {}
3
4 local path = minetest.get_modpath(minetest.get_current_modname()).."/api/"
5 dofile(path.."movement.lua")
6 dofile(path.."interaction.lua")
7 dofile(path.."data_handling.lua")
8 dofile(path.."head_code.lua")
9 dofile(path.."animation.lua")
10 dofile(path.."timers.lua")
11
12 mobs.register_mob = function(def)
13
14
15
16 mob_register = {}
17
18 ------------------------------------------------
19 mob_register.initial_properties = {
20         physical = def.physical,
21         collide_with_objects = def.collide_with_objects,
22         collisionbox = def.collisionbox,
23         visual = def.visual,
24         visual_size = def.visual_size,
25         mesh = def.mesh,
26         textures = def.textures,
27         is_visible = def.is_visible,
28         pointable = def.pointable,
29         automatic_face_movement_dir = def.automatic_face_movement_dir,
30         automatic_face_movement_max_rotation_per_sec = def.automatic_face_movement_max_rotation_per_sec,
31         makes_footstep_sound = def.makes_footstep_sound,
32 }
33
34
35 mob_register.hp = def.hp
36 mob_register.max_speed = def.max_speed
37 mob_register.jump_timer = 0
38
39 mob_register.hurt_inside_timer = 0
40 mob_register.death_animation_timer = 0
41 mob_register.dead = false
42
43 mob_register.mob = true
44 mob_register.hostile = def.hostile
45
46 mob_register.hostile_timer = 0
47 mob_register.timer = 0
48
49 mob_register.state = def.state
50
51 mob_register.hunger = 200
52
53 mob_register.view_distance = def.view_distance
54
55 mob_register.punch_timer = 0
56 mob_register.punched_timer = 0
57
58 mob_register.death_rotation = def.death_rotation
59
60 mob_register.head_mount = def.head_mount
61
62 mob_register.hurt_sound = def.hurt_sound
63 mob_register.die_sound = def.die_sound
64
65 mob_register.attack_type = def.attack_type
66 mob_register.explosion_radius = def.explosion_radius
67 mob_register.explosion_power = def.explosion_power
68 mob_register.tnt_timer = nil
69 mob_register.explosion_time = def.explosion_time
70
71 mob_register.custom_function_begin = def.custom_function_begin
72 mob_register.custom_function_end = def.custom_function_end
73 mob_register.projectile_timer_cooldown = def.projectile_timer_cooldown
74
75 mob_register.projectile_timer = 0
76 mob_register.projectile_type = def.projectile_type
77
78
79 mob_register.item_drop = def.item_drop
80 mob_register.item_minimum = def.item_minimum or def.item_amount
81 mob_register.item_amount = def.item_amount
82
83 mob_register.die_in_light = def.die_in_light
84 mob_register.die_in_light_level = def.die_in_light_level
85
86 mob_register.mob = true
87
88 mob_register.collision_boundary = def.collision_boundary or 1
89
90
91 mobs.create_movement_functions(def,mob_register)
92 mobs.create_interaction_functions(def,mob_register)
93 mobs.create_data_handling_functions(def,mob_register)
94 mobs.create_animation_functions(def,mob_register)
95 mobs.create_timer_functions(def,mob_register)
96 --only creat internal head animation functions if has head
97 if def.has_head == true then
98     mobs.create_head_functions(def,mob_register)
99 end
100
101
102 mob_register.on_step = function(self, dtime)
103     if self.custom_function_begin then
104         self.custom_function_begin(self,dtime)
105     end
106     
107     self.collision_detection(self)
108     self.fall_damage(self)
109     
110         if self.dead == false and self.death_animation_timer == 0 then
111                 self.move(self,dtime)
112                 self.set_animation(self)
113         
114         if self.look_around then
115             self.look_around(self,dtime)
116         end
117         
118                 self.manage_punch_timer(self,dtime)
119                 --self.debug_nametag(self,dtime)
120         else
121                 self.manage_death_animation(self,dtime)
122         end
123         --fix zombie state again
124         if self.dead == true and self.death_animation_timer <= 0 then
125                 self.on_death(self)
126         end
127     
128     if self.tnt_timer then
129         self.manage_explode_timer(self,dtime)
130     end
131     
132     if self.projectile_timer then
133         self.manage_projectile_timer(self,dtime)
134     end
135     
136     if self.custom_function_end then
137         self.custom_function_end(self,dtime)
138     end
139 end
140
141 minetest.register_entity("mob:"..def.mobname, mob_register)
142
143
144 if def.has_head == true then
145     mob_register.head = {}
146     mob_register.head.initial_properties = {
147         hp_max = 1,
148         physical = false,
149         collide_with_objects = false,
150         collisionbox = {0, 0, 0, 0, 0, 0},
151         visual =  def.head_visual,
152         visual_size = def.head_visual_size,
153         mesh = def.head_mesh,
154         textures = def.head_textures,
155         is_visible = true,
156         pointable = false,
157         --automatic_face_movement_dir = 0.0,
158         --automatic_face_movement_max_rotation_per_sec = 600,
159     }
160
161     --remove the head if no body
162     mob_register.head.on_step = function(self, dtime)
163         if self.parent == nil then
164             self.object:remove()
165         end
166     end
167     minetest.register_entity("mob:head"..def.mobname, mob_register.head) 
168 end
169 ------------------------------------------------
170
171 end