]> git.lizzy.rs Git - Crafter.git/blob - mods/minecart/init.lua
Add in base Minecart code
[Crafter.git] / mods / minecart / init.lua
1 --[[
2
3 Basic idealogy
4
5 goal - > go to goal - > check all this and repeat
6
7
8 minecart does check axis dir then -1 1 on opposite axis (x and z)
9
10 minecart checks in front and above 
11
12 minecart checks in front and below
13
14 if in front above start moving up
15
16 if in front below start moving down
17
18 minecart checks if rail in front then if not check sides and if none then stop
19
20 if a rail in front of minecart then when past center of node center, turn towards the available rail and then recenter self onto rail on the new axis
21
22
23 keep it simple stupid
24
25 make cart make noise
26
27 ]]--
28 local path = minetest.get_modpath("minecart")
29 dofile(path.."/rail.lua")
30
31
32
33 local minecart = {
34         initial_properties = {
35                 physical = false, -- otherwise going uphill breaks
36                 collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
37                 visual = "mesh",
38                 mesh = "minecart.b3d",
39                 visual_size = {x=1, y=1},
40                 textures = {"minecart.png"},
41                 automatic_face_movement_dir = 90.0,
42         },
43
44         rider = nil,
45         punched = false,
46         
47 }
48
49 function minecart:on_rightclick(clicker)
50         if not clicker or not clicker:is_player() then
51                 return
52         end
53         local player_name = clicker:get_player_name()
54         if self.rider and player_name == self.rider then
55                 self.rider = nil
56                 carts:manage_attachment(clicker, nil)
57         elseif not self.rider then
58                 self.rider = player_name
59                 carts:manage_attachment(clicker, self.object)
60
61                 -- player_api does not update the animation
62                 -- when the player is attached, reset to default animation
63                 
64                 --player_api.set_animation(clicker, "stand")
65         end
66 end
67
68 function minecart:on_activate(staticdata, dtime_s)
69         self.object:set_armor_groups({immortal=1})
70         if string.sub(staticdata, 1, string.len("return")) ~= "return" then
71                 return
72         end
73         local data = minetest.deserialize(staticdata)
74         if type(data) ~= "table" then
75                 return
76         end
77         self.railtype = data.railtype
78         if data.old_dir then
79                 self.old_dir = data.old_dir
80         end
81 end
82
83 function minecart:get_staticdata()
84         return minetest.serialize({
85         })
86 end
87 function minecart:on_punch(puncher, time_from_last_punch, tool_capabilities, dir, damage)
88         self.object:remove()
89 end
90
91
92
93
94
95
96 --repel from players on track "push"
97 function minecart:push(self)
98         local pos = self.object:getpos()
99         for _,object in ipairs(minetest.get_objects_inside_radius(pos, 2)) do
100                 if object:is_player() then
101                         local player_pos = object:getpos()
102                 end
103         end
104 end
105
106 function minecart:ride_rail(self)
107         if self.goal then
108                 print("goal: "..dump(self.goal))
109         end
110 end
111
112
113
114 function minecart:on_step(dtime)
115         minecart:push(self)
116         minecart:ride_rail(self)
117 end
118
119 minetest.register_entity("minecart:minecart", minecart)
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 minetest.register_craftitem("minecart:minecart", {
141         description = "Minecart",
142         inventory_image = "minecartitem.png",
143         wield_image = "minecartitem.png",
144         on_place = function(itemstack, placer, pointed_thing)
145                 if not pointed_thing.type == "node" then
146                         return
147                 end
148                 
149                 if minetest.get_item_group(minetest.get_node(pointed_thing.under).name, "rail")>0 then
150                         minetest.add_entity(pointed_thing.under, "minecart:minecart")
151                 else
152                         return
153                 end
154
155                 itemstack:take_item()
156
157                 return itemstack
158         end,
159 })
160
161 minetest.register_craft({
162         output = "minecart:minecart",
163         recipe = {
164                 {"main:iron", "", "main:iron"},
165                 {"main:iron", "main:iron", "main:iron"},
166         },
167 })