]> git.lizzy.rs Git - mcl_enchanting.git/blob - book.lua
Add crafting recipe
[mcl_enchanting.git] / book.lua
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
4
5 function mcl_enchanting.schedule_book_animation(self, anim)
6         self.scheduled_anim = {timer = self.anim_length, anim = anim}
7 end
8
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
15 end
16
17 function mcl_enchanting.check_animation_schedule(self, dtime)
18         local schedanim = self.scheduled_anim
19         if schedanim then
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()
23                 end
24         end
25 end
26
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)
33 end
34
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
41                                 obj:remove()
42                         end
43                         return
44                 end
45         end
46         minetest.add_entity(obj_pos, "mcl_enchanting:book")
47 end
48
49 minetest.register_entity("mcl_enchanting:book", {
50         initial_properties = {
51                 visual = "mesh",
52                 mesh = "mcl_enchanting_book.b3d",
53                 visual_size = {x = 12.5, y = 12.5},
54                 collisionbox = {0, 0, 0},
55                 physical = false,
56                 textures = {"mcl_enchanting_book_entity.png"},
57         },
58         player_near = false,
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))
63         end,
64         on_step = function(self, dtime)
65                 local old_player_near = self.player_near
66                 local player_near = false
67                 local player
68                 for _, obj in pairs(minetest.get_objects_inside_radius(self.object:get_pos(), 2.5)) do
69                         if obj:is_player() then
70                                 player_near = true
71                                 player = obj
72                         end
73                 end
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")
80                 end
81                 if player then
82                         mcl_enchanting.look_at(self, player:get_pos())
83                 end
84                 self.player_near = player_near
85                 mcl_enchanting.check_animation_schedule(self, dtime)
86         end,
87 })