Crafter/mods/mob/init.lua

160 lines
3.5 KiB
Lua
Raw Normal View History

2020-02-17 02:56:57 -05:00
--this is where mobs are defined
2020-02-17 23:46:10 -05:00
--this is going to be used to set an active mob limit
2020-03-25 21:17:17 -04:00
global_mob_table = {}
2020-02-17 23:46:10 -05:00
local path = minetest.get_modpath(minetest.get_current_modname())
2020-02-17 09:08:13 -05:00
2020-04-02 11:05:48 -04:00
dofile(path.."/spawning.lua")
dofile(path.."/items.lua")
2020-02-17 23:46:10 -05:00
2020-03-03 01:59:30 -05:00
--these are helpers to create entities
pig = {}
pig.initial_properties = {
2020-03-25 21:17:17 -04:00
hp_max = 1,
physical = true,
collide_with_objects = false,
collisionbox = {-0.37, -0.4, -0.37, 0.37, 0.5, 0.37},
2020-03-25 21:17:17 -04:00
visual = "mesh",
visual_size = {x = 3, y = 3},
2020-03-29 21:19:50 -04:00
mesh = "pig.x",
2020-03-25 21:17:17 -04:00
textures = {
2020-03-29 21:19:50 -04:00
"body.png","leg.png","leg.png","leg.png","leg.png"
2020-03-10 23:56:27 -07:00
},
2020-03-25 21:17:17 -04:00
is_visible = true,
pointable = true,
automatic_face_movement_dir = -90.0,
automatic_face_movement_max_rotation_per_sec = 300,
2020-03-25 21:17:17 -04:00
}
2020-03-31 05:51:18 -04:00
pig.hp = 5
pig.speed = 5
pig.jump_timer = 0
pig.death_animation_timer = 0
pig.mob = true
pig.hostile = false
pig.hostile_timer = 0
pig.timer = 0
pig.state = 0
pig.hunger = 200
pig.view_distance = 20
pig.punch_timer = 0
pig.punched_timer = 0
--head stuff
pig.head_mount = vector.new(0,1.2,1.9)
dofile(path.."/chatcommands.lua")
dofile(path.."/timers.lua")
dofile(path.."/head_code.lua")
dofile(path.."/movement_code.lua")
dofile(path.."/data_handling_code.lua")
dofile(path.."/interaction_code.lua")
----------------------------------
2020-03-25 21:17:17 -04:00
--repel from players
pig.push = function(self)
2020-03-25 21:17:17 -04:00
local pos = self.object:getpos()
local radius = 1
for _,object in ipairs(minetest.get_objects_inside_radius(pos, radius)) do
if object:is_player() or object:get_luaentity().mob == true then
local player_pos = object:getpos()
pos.y = 0
player_pos.y = 0
local currentvel = self.object:getvelocity()
local vel = vector.subtract(pos, player_pos)
vel = vector.normalize(vel)
local distance = vector.distance(pos,player_pos)
distance = (radius-distance)*10
vel = vector.multiply(vel,distance)
local acceleration = vector.new(vel.x-currentvel.x,0,vel.z-currentvel.z)
self.object:add_velocity(acceleration)
2020-03-30 03:00:49 -04:00
acceleration = vector.multiply(acceleration, 5)
2020-03-25 21:17:17 -04:00
object:add_player_velocity(acceleration)
2020-03-10 23:56:27 -07:00
end
2020-03-25 21:17:17 -04:00
end
end
2020-03-25 21:17:17 -04:00
--sets the mob animation and speed
pig.set_animation = function(self)
if self.speed == 0 or vector.equals(self.direction,vector.new(0,0,0)) then
self.object:set_animation({x=0,y=0}, 1, 0, true)
else
self.object:set_animation({x=5,y=15}, 1, 0, true)
self.object:set_animation_frame_speed(self.speed*5)
end
end
--this depletes the mobs hunger
pig.do_hunger = function(self,dtime)
self.hunger = self.hunger - dtime
end
--this sets the state of the mob
pig.set_state = function(self,dtime)
self.do_hunger(self,dtime)
end
2020-03-30 03:00:49 -04:00
pig.on_step = function(self, dtime)
self.manage_death_animation(self,dtime)
if self.death_animation_timer == 0 then
self.set_state(self,dtime)
self.move(self,dtime)
self.set_animation(self)
self.look_around(self,dtime)
self.manage_punch_timer(self,dtime)
pig.debug_nametag(self,dtime)
end
2020-03-25 21:17:17 -04:00
end
2020-03-29 21:19:50 -04:00
minetest.register_entity("mob:pig", pig)
2020-03-29 21:19:50 -04:00
------------------------------------------------------------------------the head
pig.head = {}
pig.head.initial_properties = {
2020-03-29 21:19:50 -04:00
hp_max = 1,
physical = false,
collide_with_objects = false,
collisionbox = {0, 0, 0, 0, 0, 0},
visual = "mesh",
visual_size = {x = 1.1, y = 1.1},
mesh = "pig_head.x",
textures = {
"head.png","nose.png"
},
is_visible = true,
pointable = false,
--automatic_face_movement_dir = 0.0,
--automatic_face_movement_max_rotation_per_sec = 600,
}
--remove the head if no body
pig.head.on_step = function(self, dtime)
2020-03-30 03:00:49 -04:00
if self.parent == nil then
2020-03-29 21:19:50 -04:00
self.object:remove()
end
end
minetest.register_entity("mob:head", pig.head)