aboutsummaryrefslogtreecommitdiff
path: root/wagons.lua
diff options
context:
space:
mode:
authororwell96 <mono96.mml@gmail.com>2016-09-28 23:12:01 +0200
committerorwell96 <mono96.mml@gmail.com>2016-09-28 23:12:01 +0200
commite046e1926dcdb7e92299d08e5f11738302292557 (patch)
treea5000dee3964ff497f90dc6786dcabb8571dd2a8 /wagons.lua
parent8f4da0be774b1b41e2024c196a4a7209ab43dccb (diff)
downloadadvtrains-e046e1926dcdb7e92299d08e5f11738302292557.tar.gz
advtrains-e046e1926dcdb7e92299d08e5f11738302292557.tar.bz2
advtrains-e046e1926dcdb7e92299d08e5f11738302292557.zip
adding ability for custom on_step/destroy functions in wagon definition
Diffstat (limited to 'wagons.lua')
-rw-r--r--wagons.lua59
1 files changed, 38 insertions, 21 deletions
diff --git a/wagons.lua b/wagons.lua
index ed59ed2..7f9bee7 100644
--- a/wagons.lua
+++ b/wagons.lua
@@ -111,31 +111,43 @@ end
-- Remove the wagon
function wagon:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
- if not puncher or not puncher:is_player() or puncher == self.driver then
+ if not puncher or not puncher:is_player() or self.driver then
return
end
- self.object:remove()
- if not self.initialized then return end
-
- local inv = puncher:get_inventory()
- if minetest.setting_getbool("creative_mode") then
- if not inv:contains_item("main", self.name) then
- inv:add_item("main", self.name)
- end
- else
+ if self.custom_may_destroy then
+ if not self.custom_may_destroy(self, puncher, time_from_last_punch, tool_capabilities, direction) then
+ return
+ end
+ end
+ if self.custom_on_destroy then
+ self.custom_on_destroy(self, puncher, time_from_last_punch, tool_capabilities, direction)
+ end
+
+ self:destroy()
+
+ local inv = puncher:get_inventory()
+ if minetest.setting_getbool("creative_mode") then
+ if not inv:contains_item("main", self.name) then
inv:add_item("main", self.name)
end
-
- 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 then self.discouple.object:remove() end--will have no effect on unloaded objects
- return
+ else
+ inv:add_item("main", self.name)
+ end
+end
+function wagon:destroy()
+ self.object:remove()
+ if not self.initialized then return end
+ 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 then self.discouple.object:remove() end--will have no effect on unloaded objects
+ return
end
+
function wagon:on_step(dtime)
local t=os.clock()
local pos = self.object:getpos()
@@ -144,7 +156,7 @@ function wagon:on_step(dtime)
self.object:setvelocity({x=0,y=0,z=0})
return
end
-
+
self.entity_name=self.name
--does this object already have an ID?
if not self.unique_id then
@@ -158,7 +170,7 @@ function wagon:on_step(dtime)
elseif not self.initialized then
self.initialized=true
end
-
+
--re-attach driver if he got lost
if not self.driver and self.driver_name then
local clicker=minetest.get_player_by_name(self.driver_name)
@@ -169,7 +181,12 @@ function wagon:on_step(dtime)
clicker:set_eye_offset(self.view_offset, self.view_offset)
end
end
-
+
+ --custom on_step function
+ if self.custom_on_step then
+ self.custom_on_step(self, dtime)
+ end
+
--driver control
if self.driver and self.is_locomotive then
if self.driver:get_player_control_bits()~=self.old_player_control_bits then
@@ -197,9 +214,9 @@ function wagon:on_step(dtime)
end
advtrains.set_trainhud(self.driver:get_player_name(), advtrains.hud_train_format(self:train(), self.wagon_flipped))
end
-
+
local gp=self:train()
-
+
--DisCouple
if self.pos_in_trainparts and self.pos_in_trainparts>1 then
if gp.velocity==0 then