added discouple entity

master
orwell96 2016-06-01 11:03:50 +02:00
parent 04e48bab20
commit 8954740dd7
3 changed files with 41 additions and 11 deletions

View File

@ -5,7 +5,7 @@
--set into existing trains to split them when punched.
--they are attached to the wagons.
--[[fields
wagon_id
wagon
wagons keep their couple entity minetest-internal id inside the field discouple_id. if it refers to nowhere, they will spawn a new one if player is near
]]
@ -28,14 +28,21 @@ minetest.register_entity("advtrains:discouple", {
end
end,
get_staticdata=function() return "DISCOUPLE" end,
on_punch=function()
for _,wagon in pairs(minetest.luaentities) do
if wagon.is_wagon and wagon.initialized and wagon.unique_id==self.wagon_id then
advtrains.split_train_at_wagon(wagon)--found in trainlogic.lua
end
on_punch=function(self)
advtrains.split_train_at_wagon(self.wagon)--found in trainlogic.lua
end,
on_step=function(self, dtime)
if not self.wagon then
self.object:remove()
end
end
local velocityvec=self.wagon.object:getvelocity()
self.updatepct_timer=(self.updatepct_timer or 0)-dtime
if not self.old_velocity_vector or not vector.equals(velocityvec, self.old_velocity_vector) or self.updatepct_timer<=0 then--only send update packet if something changed
self.object:setpos(vector.add(self.wagon.object:getpos(), {y=0, x=-math.sin(self.wagon.object:getyaw())*self.wagon.wagon_span, z=math.cos(self.wagon.object:getyaw())*self.wagon.wagon_span}))
self.object:setvelocity(velocityvec)
self.updatepct_timer=2
end
end,
})
--advtrains:couple

View File

@ -473,8 +473,8 @@ end
function advtrains.split_train_at_wagon(wagon)
--get train
local train=advtrains.trains[wagon.train_id]
local pos_for_new_train=advtrains.get_or_create_path(wagon.train_id, train)[math.floor((train.index or 0)-wagon.pos_in_train-0.5)]
local pos_for_new_train_prev=advtrains.get_or_create_path(wagon.train_id, train)[math.floor((train.index or 0)-wagon.pos_in_train-1.5)]
local pos_for_new_train=advtrains.get_or_create_path(wagon.train_id, train)[math.floor((train.index or 0)-wagon.pos_in_train+wagon.wagon_span)]
local pos_for_new_train_prev=advtrains.get_or_create_path(wagon.train_id, train)[math.floor((train.index or 0)-wagon.pos_in_train-1+wagon.wagon_span)]
--before doing anything, check if both are rails. else do not allow
if not pos_for_new_train then

View File

@ -127,6 +127,7 @@ function wagon:on_punch(puncher, time_from_last_punch, tool_capabilities, direct
table.remove(self:train().trainparts, self.pos_in_trainparts)
advtrains.update_trainpart_properties(self.train_id)
advtrains.wagon_save[self.unique_id]=nil
if self.discouple_id and minetest.object_refs[self.discouple_id] then minetest.object_refs[self.discouple_id]:remove() end
return
@ -154,6 +155,28 @@ function wagon:on_step(dtime)
self.initialized=true
end
--DisCouple
if self.pos_in_trainparts and self.pos_in_trainparts>1 then
if not self.discouple_id or not minetest.luaentities[self.discouple_id] then
local object=minetest.add_entity(pos, "advtrains:discouple")
if object then
print("spawning discouple")
local le=object:get_luaentity()
le.wagon=self
--box is hidden when attached, so unuseful.
--object:set_attach(self.object, "", {x=0, y=0, z=self.wagon_span*10}, {x=0, y=0, z=0})
--find in object_refs
for aoi, compare in pairs(minetest.object_refs) do
if compare==object then
self.discouple_id=aoi
end
end
else
print("Couldn't spawn DisCouple")
end
end
end
--driver control
if self.driver and self.is_locomotive then
if self.driver:get_player_control_bits()~=self.old_player_control_bits then
@ -251,7 +274,7 @@ function wagon:on_step(dtime)
end
self.updatepct_timer=(self.updatepct_timer or 0)-dtime
if true or not self.old_velocity_vector or not vector.equals(velocityvec, self.old_velocity_vector) or self.old_yaw~=yaw or self.updatepct_timer<=0 then--only send update packet if something changed
if not self.old_velocity_vector or not vector.equals(velocityvec, self.old_velocity_vector) or self.old_yaw~=yaw or self.updatepct_timer<=0 then--only send update packet if something changed
self.object:setpos(actual_pos)
self.object:setvelocity(velocityvec)
self.object:setyaw(yaw)