-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-minetest.register_entity("minecart:minecart", {
- initial_properties = {
- physical = true, -- otherwise going uphill breaks
- collisionbox = {-0.4, -0.5, -0.4, 0.4, 0.45, 0.4},--{-0.5, -0.4, -0.5, 0.5, 0.25, 0.5},
- visual = "mesh",
- mesh = "minecart.obj",
- visual_size = {x=1, y=1},
- textures = {"minecart.png"},
- automatic_face_movement_dir = 90.0,
- automatic_face_movement_max_rotation_per_sec = 600,
- },
-
- rider = nil,
- punched = false,
- speed = 0,
- turn_timer = 0,
- incline = nil,
- turn_timer = 5,
-
- on_rightclick = function(self,clicker)
- if not clicker or not clicker:is_player() then
- return
- end
- local player_name = clicker:get_player_name()
-
- if self.rider and player_name == self.rider then
- self.rider = nil
- --carts:manage_attachment(clicker, nil)
- elseif not self.rider then
- self.rider = player_name
- clicker:set_attach(self.object, "", {x=0, y=-4.5, z=0}, {x=0, y=0, z=0})
- --player:set_eye_offset({x=0, y=-4, z=0},{x=0, y=-4, z=0})
- --carts:manage_attachment(clicker, self.object)
-
- -- player_api does not update the animation
- -- when the player is attached, reset to default animation
-
- --player_api.set_animation(clicker, "stand")
- end
- end,
-
- on_activate = function(self,staticdata, dtime_s)
- self.object:set_armor_groups({immortal=1})
- if string.sub(staticdata, 1, string.len("return")) ~= "return" then
- return
- end
- local data = minetest.deserialize(staticdata)
- if type(data) ~= "table" then
- return
- end
- self.railtype = data.railtype
- if data.old_dir then
- self.old_dir = data.old_dir
- end
- end,
-
- get_staticdata = function(self)
- return minetest.serialize({
- })
- end,
-
- on_punch = function(self,puncher, time_from_last_punch, tool_capabilities, dir, damage)
- local obj = minetest.add_item(self.object:getpos(), "minecart:minecart")
- obj:get_luaentity().collection_timer = 2
- self.object:remove()
- end,
-
- --repel from players on track "push"
- push = function(self)
- if self.turn_timer > 0.3 then
- local pos = self.object:getpos()
- local radius = 1.2
- for _,object in ipairs(minetest.get_objects_inside_radius(pos, radius)) do
- if object:is_player() and object:get_player_name() ~= self.rider 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)*20
- vel = vector.multiply(vel,distance)
- local acceleration = vector.new(vel.x-currentvel.x,0,vel.z-currentvel.z)
-
-
- if self.axis == "x" then
- self.object:add_velocity(vector.new(acceleration.x,0,0))
- elseif self.axis == "z" then
- self.object:add_velocity(vector.new(0,0,acceleration.z))
- else
- self.object:add_velocity(acceleration)
- end
-
- acceleration = vector.multiply(acceleration, -0.5)
- object:add_player_velocity(acceleration)
- end
- end
- end
- end,
-
- --slows the minecart down
- slowdown = function(self)
- if not self.moving == true then
- local vel = self.object:getvelocity()
- local deceleration = vector.multiply(vel, -0.01)
- self.object:add_velocity(deceleration)
- end
- end,
-
- --mechanics to follow rails
- ride_rail = function(self,dtime)
- minecart_brain(self,dtime)
- end,
-
- on_step = function(self,dtime)
- self.push(self)
- self.slowdown(self)
- self.ride_rail(self,dtime)
- end,
-
-})