1 local book_animations = {["close"] = 1, ["opening"] = 2, ["open"] = 3, ["closing"] = 4}
2 local book_animation_steps = {0, 640, 680, 700, 740}
3 local book_animation_speed = 40
5 function mcl_enchanting.schedule_book_animation(self, anim)
6 self.scheduled_anim = {timer = self.anim_length, anim = anim}
9 function mcl_enchanting.set_book_animation(self, anim)
10 local anim_index = book_animations[anim]
11 local start, stop = book_animation_steps[anim_index], book_animation_steps[anim_index + 1]
12 self.object:set_animation({x = start, y = stop}, book_animation_speed)
13 self.scheduled_anim = nil
14 self.anim_length = (stop - start) / 40
17 function mcl_enchanting.check_animation_schedule(self, dtime)
18 local schedanim = self.scheduled_anim
20 schedanim.timer = schedanim.timer - dtime
21 if schedanim.timer <= 0 then
22 mcl_enchanting.set_book_animation(self, schedanim.anim)local pos1=self.object:get_pos()
27 function mcl_enchanting.look_at(self, pos2)
28 local pos1 = self.object:get_pos()
29 local vec = vector.subtract(pos1, pos2)
30 local yaw = math.atan(vec.z / vec.x) - math.pi/2
31 yaw = yaw + (pos1.x >= pos2.x and math.pi or 0)
32 self.object:set_yaw(yaw + math.pi)
35 function mcl_enchanting.check_book(pos)
36 obj_pos = vector.add(pos, mcl_enchanting.book_offset)
37 for _, obj in pairs(minetest.get_objects_inside_radius(obj_pos, 0.1)) do
38 local luaentity = obj:get_luaentity()
39 if luaentity and luaentity.name == "mcl_enchanting:book" then
40 if minetest.get_node(pos).name ~= "mcl_enchanting:table" then
46 minetest.add_entity(obj_pos, "mcl_enchanting:book")
49 minetest.register_entity("mcl_enchanting:book", {
50 initial_properties = {
52 mesh = "mcl_enchanting_book.b3d",
53 visual_size = {x = 12.5, y = 12.5},
54 collisionbox = {0, 0, 0},
56 textures = {"mcl_enchanting_book_entity.png"},
59 on_activate = function(self)
60 self.object:set_armor_groups({immortal = 1})
61 mcl_enchanting.set_book_animation(self, "close")
62 mcl_enchanting.check_book(vector.subtract(self.object:get_pos(), mcl_enchanting.book_offset))
64 on_step = function(self, dtime)
65 local old_player_near = self.player_near
66 local player_near = false
68 for _, obj in pairs(minetest.get_objects_inside_radius(self.object:get_pos(), 2.5)) do
69 if obj:is_player() then
74 if player_near and not old_player_near then
75 mcl_enchanting.set_book_animation(self, "opening")
76 mcl_enchanting.schedule_book_animation(self, "open")
77 elseif old_player_near and not player_near then
78 mcl_enchanting.set_book_animation(self, "closing")
79 mcl_enchanting.schedule_book_animation(self, "close")
82 mcl_enchanting.look_at(self, player:get_pos())
84 self.player_near = player_near
85 mcl_enchanting.check_animation_schedule(self, dtime)