Rotate player based on his rotation, get off when jumping
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Wed, 28 Jun 2017 16:33:05 +0000 (18:33 +0200)
committerGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Wed, 28 Jun 2017 16:33:05 +0000 (18:33 +0200)
init.lua

index 7bd6ac0..ecb8f37 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -19,6 +19,7 @@ minetest.register_entity("walkway:moving_dummy",{
                               name = self.player:get_player_name()
                            elseif not self.player then
                               self.object:remove()
+                              return
                            end
                            local pos = self.object:getpos()
                            local napos = minetest.get_node(pos) 
@@ -28,12 +29,12 @@ minetest.register_entity("walkway:moving_dummy",{
                            if name ~="" then
                               -- based on code from the tower crane mod --
                               local ctrl = self.player:get_player_control()
-                              local yaw = self.player:get_look_horizontal()
+                              local yaw = self.player:get_look_horizontal()                      
                               local pos = self.player:getpos()
                               local walk_speed = minetest.settings:get("movement_speed_walk") or 4
                               local speed_forward = 0
                               local speed_right= 0
-
+                              self.object:setyaw(yaw)
                               if (ctrl.up or ctrl.down or ctrl.left or ctrl.right) then
                                  default.player_set_animation(self.player, "walk" , 30)
                               else
@@ -52,6 +53,16 @@ minetest.register_entity("walkway:moving_dummy",{
                                  speed_right = -walk_speed
                               end
 
+                              if ctrl.jump then
+                                 -- detach on jump
+                                 local veldir = self.object:getvelocity();
+                                 self.player:set_detach()
+                                 self.player:setpos({x = pos.x + veldir.x / 3, y = pos.y, z = pos.z + veldir.z / 3})
+                                 self.object:remove()
+                                 default.player_attached[name] = false
+                                 return
+                              end
+
                               -- calculate the direction vector
                               vx = math.cos(yaw+math.pi/2) * speed_forward + math.cos(yaw) * speed_right
                               vz = math.sin(yaw+math.pi/2) * speed_forward + math.sin(yaw) * speed_right
@@ -60,12 +71,15 @@ minetest.register_entity("walkway:moving_dummy",{
                               elseif dir.z == 0 then
                                  vz = 0
                               end
+                           elseif not self.player:is_player() then
+                              self.object:setyaw(self.player:getyaw())
                            end
 
                            
                            if napos.name == "walkway:belt_straight" then
                               self.object:setvelocity({x = dir.x / speed, y = 0, z = dir.z / speed})
                            elseif napos.name == "walkway:belt" then
+
                               if dir.x == 0 then
                                  dir.x = (math.floor(pos.x + 0.5) - pos.x) * 2
                               elseif dir.z == 0 then
@@ -74,7 +88,9 @@ minetest.register_entity("walkway:moving_dummy",{
                               self.object:setvelocity({x = dir.x / speed + vx, y = 0, z = dir.z / speed+vz})
                            else
                               if self.player then
+                                 local veldir = self.object:getvelocity();
                                  self.player:set_detach()
+                                 self.player:setpos({x = pos.x + veldir.x / 3, y = pos.y, z = pos.z + veldir.z / 3})
                                  self.object:remove()
                               else
                                  self.object:remove()