new machine: basic_machines:ball_spawner: when activated spawns a ball. ball can be moved with mover and will disappear when hitting solid node, flying more than 20 blocks away or flying too long. On node hit it will attempt to activate target node.

This commit is contained in:
rnd1 2016-05-12 12:23:42 +02:00
parent 6cb13f8fa4
commit ba4ab93c49
4 changed files with 111 additions and 2 deletions

102
ball.lua Normal file
View File

@ -0,0 +1,102 @@
-- BALL: energy ball that flies around, can bounce and activate stuff
-- rnd 2016:
--local obj = minetest.add_entity(pos, "basic_machines:ball")
minetest.register_entity("basic_machines:ball",{
timer = 0,
lifetime = 20, -- how long it exists before disappearing
owner = "",
origin = {x=0,y=0,z=0},
hp_max = 1,
visual="sprite",
visual_size={x=.50,y=.50},
collisionbox = {0,0,0,0,0,0},
physical=false,
--textures={"basic_machines_ball"},
on_punch=function(self, puncher, time_from_last_punch, tool_capabilities, dir) -- ball is punched
end,
on_activate = function(self, staticdata)
self.object:set_properties({textures={"basic_machines_ball.png"}})
self.object:setvelocity({x=0,y=0,z=0})
self.timer = 0;self.owner = "";
self.origin = self.object:getpos();
end,
on_step = function(self, dtime)
self.timer=self.timer+dtime
if self.timer>self.lifetime then
self.object:remove()
return
end
local pos=self.object:getpos()
local origin = self.origin;
local r = 20;
if math.abs(pos.x-origin.x)>r or math.abs(pos.y-origin.y)>r or math.abs(pos.z-origin.z)>r then -- remove if it goes too far
self.object:remove()
return
end
local nodename = minetest.get_node(pos).name;
local walkable = false;
if nodename ~= "air" then
walkable = minetest.registered_nodes[nodename].walkable;
end
if walkable then -- we hit a node
--minetest.chat_send_all(" hit node at " .. minetest.pos_to_string(pos))
self.object:remove()
local node = minetest.get_node(pos);
local table = minetest.registered_nodes[node.name];
if not table then return end -- error
if not table.mesecons then return end
if not table.mesecons.effector then return end
local effector=table.mesecons.effector; if not effector.action_on then return end
effector.action_on(pos,node,16);
--self.object:setvelocity({x=0, y=0, z=0})
--self.object:setacceleration({x=0, y=0, z=0})
return
--return self
end
--self.object:setacceleration({x=0, y=0, z=0}) -- no gravity!
end,
})
minetest.register_node("basic_machines:ball_spawner", {
description = "Spawns energy ball one block above",
tiles = {"basic_machines_ball.png"},
groups = {oddly_breakable_by_hand=2,mesecon_effector_on = 1},
drawtype = "liquid",
walkable = false,
alpha = 150,
sounds = default.node_sound_wood_defaults(),
after_place_node = function(pos, placer)
local meta = minetest.env:get_meta(pos)
meta:set_string("owner", placer:get_player_name());
end,
mesecons = {effector = {
action_on = function (pos, node,ttl)
if ttl<0 then return end
minetest.add_entity({x=pos.x,y=pos.y,z=pos.z}, "basic_machines:ball")
end
}
},
})
minetest.register_craft({
output = "basic_machines:ball_spawner",
recipe = {
{"basic_machines:power_cell"},
{"basic_machines:keypad"}
}
})

View File

@ -25,10 +25,12 @@ dofile(minetest.get_modpath("basic_machines").."/technic_power.lua") -- technic
dofile(minetest.get_modpath("basic_machines").."/recycler.lua")
dofile(minetest.get_modpath("basic_machines").."/grinder.lua")
dofile(minetest.get_modpath("basic_machines").."/autocrafter.lua") -- borrowed and adapted from pipeworks mod
--dofile(minetest.get_modpath("basic_machines").."/cpu.lua") -- experimental
-- OPTIONAL ADDITIONAL STUFF ( comment to disable )
dofile(minetest.get_modpath("basic_machines").."/ball.lua")
dofile(minetest.get_modpath("basic_machines").."/enviro.lua") -- enviro blocks that can change surrounding enviroment physics, uncomment spawn/join code to change global physics, disabled by default
minetest.after(0, function()
dofile(minetest.get_modpath("basic_machines").."/mesecon_doors.lua") -- if you want open/close doors with signal, also steel doors are made impervious to dig through, removal by repeat punch

View File

@ -410,8 +410,13 @@ minetest.register_node("basic_machines:mover", {
obj:setvelocity(velocityv);
if obj:get_luaentity() then -- interaction with objects like carts
local luaent = obj:get_luaentity();
if luaent.name then -- just accelerate cart
if luaent.name == "carts:cart" then
if luaent.name then
if luaent.name == "basic_machines:ball" then -- move balls for free
luaent.velocity = {x=velocityv.x*times,y=velocityv.y*times,z=velocityv.z*times};
luaent.owner = owner;
return
end
if luaent.name == "carts:cart" then -- just accelerate cart
luaent.velocity = {x=velocityv.x*times,y=velocityv.y*times,z=velocityv.z*times};
fuel = fuel - fuel_cost; meta:set_float("fuel",fuel);
meta:set_string("infotext", "Mover block. Fuel "..fuel);

Binary file not shown.

After

Width:  |  Height:  |  Size: 522 B