motorboat_control.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. --global constants
  2. motorboat.motorboat_last_time_command = 0
  3. motorboat.vector_up = vector.new(0, 1, 0)
  4. function motorboat.get_pointer_angle(energy)
  5. local angle = energy * 18
  6. angle = angle - 90
  7. angle = angle * -1
  8. return angle
  9. end
  10. function motorboat.check_node_below(obj)
  11. local pos_below = obj:get_pos()
  12. pos_below.y = pos_below.y - 0.1
  13. local node_below = minetest.get_node(pos_below).name
  14. local nodedef = minetest.registered_nodes[node_below]
  15. local touching_ground = not nodedef or -- unknown nodes are solid
  16. nodedef.walkable or false
  17. local liquid_below = not touching_ground and nodedef.liquidtype ~= "none"
  18. return touching_ground, liquid_below
  19. end
  20. function motorboat.motorboat_control(self, dtime, hull_direction, longit_speed,
  21. longit_drag, later_speed, later_drag, accel)
  22. motorboat.motorboat_last_time_command = motorboat.motorboat_last_time_command + dtime
  23. if motorboat.motorboat_last_time_command > 1 then motorboat.motorboat_last_time_command = 1 end
  24. local player = minetest.get_player_by_name(self.driver_name)
  25. local retval_accel = accel;
  26. -- player control
  27. if player then
  28. --minetest.chat_send_all('teste')
  29. local ctrl = player:get_player_control()
  30. local max_speed_anchor = 0.2
  31. if ctrl.sneak and motorboat.motorboat_last_time_command > 0.3 and
  32. longit_speed < max_speed_anchor and longit_speed > -max_speed_anchor then
  33. motorboat.motorboat_last_time_command = 0
  34. if self.anchored == false then
  35. self.anchored = true
  36. self.object:set_velocity(vector.new())
  37. minetest.chat_send_player(self.driver_name, 'anchors away!')
  38. else
  39. self.anchored = false
  40. minetest.chat_send_player(self.driver_name, 'weigh anchor!')
  41. end
  42. end
  43. if ctrl.jump and self._engine_running and motorboat.motorboat_last_time_command > 0.3 then
  44. motorboat.motorboat_last_time_command = 0
  45. minetest.chat_send_player(self.driver_name, 'running in auto mode')
  46. self._auto = true
  47. end
  48. if ctrl.sneak then
  49. if longit_speed >= max_speed_anchor or longit_speed <= -max_speed_anchor then
  50. self.rudder_angle = 0
  51. end
  52. end
  53. if self.anchored == false then
  54. if self._engine_running then
  55. local engineacc
  56. if self._auto == false then
  57. if longit_speed < 8.0 and ctrl.up then
  58. engineacc = 1.5
  59. else
  60. if longit_speed > -1 and ctrl.down then
  61. engineacc = -0.1
  62. end
  63. end
  64. else
  65. --auto mode
  66. if longit_speed < 8.0 then engineacc = 1.5 end
  67. if ctrl.down or ctrl.up then
  68. minetest.chat_send_player(self.driver_name, 'auto turned off')
  69. self._auto = false
  70. end
  71. end
  72. if engineacc then retval_accel=vector.add(accel,vector.multiply(hull_direction,engineacc)) end
  73. --minetest.chat_send_all('paddle: '.. paddleacc)
  74. else
  75. local paddleacc
  76. if longit_speed < 1.0 and ctrl.up then
  77. paddleacc = 0.5
  78. elseif longit_speed > -1.0 and ctrl.down then
  79. paddleacc = -0.5
  80. end
  81. if paddleacc then retval_accel=vector.add(accel,vector.multiply(hull_direction,paddleacc)) end
  82. --minetest.chat_send_all('paddle: '.. paddleacc)
  83. end
  84. end
  85. if ctrl.aux1 then
  86. self._auto = false
  87. --sets the engine running - but sets a delay also, cause keypress
  88. if motorboat.motorboat_last_time_command > 0.3 then
  89. motorboat.motorboat_last_time_command = 0
  90. if self._engine_running then
  91. self._engine_running = false
  92. -- sound and animation
  93. if self.sound_handle then
  94. minetest.sound_stop(self.sound_handle)
  95. self.sound_handle = nil
  96. end
  97. self.engine:set_animation_frame_speed(0)
  98. elseif self._engine_running == false and self._energy > 0 then
  99. self._engine_running = true
  100. -- sound and animation
  101. self.sound_handle = minetest.sound_play({name = "engine"},
  102. {object = self.object, gain = 2.0, max_hear_distance = 32, loop = true,})
  103. self.engine:set_animation_frame_speed(30)
  104. end
  105. end
  106. end
  107. -- rudder
  108. local rudder_limit = 30
  109. if ctrl.right then
  110. self.rudder_angle = math.max(self.rudder_angle-20*dtime,-rudder_limit)
  111. elseif ctrl.left then
  112. self.rudder_angle = math.min(self.rudder_angle+20*dtime,rudder_limit)
  113. end
  114. end
  115. return retval_accel
  116. end