From 1678a6b1c7f4f916c661b4da6bf01491919371e2 Mon Sep 17 00:00:00 2001 From: Alexsandro Percy Date: Tue, 27 Jun 2023 20:21:25 -0300 Subject: [PATCH] improved reload and controls --- lib_planes/control.lua | 60 +++++++++++++++++++++++++++++++--------- lib_planes/entities.lua | 3 +- lib_planes/forms.lua | 35 +++++++++++++++++++++-- lib_planes/utilities.lua | 19 +++++++++++-- 4 files changed, 98 insertions(+), 19 deletions(-) diff --git a/lib_planes/control.lua b/lib_planes/control.lua index f4170f4..f3959de 100755 --- a/lib_planes/control.lua +++ b/lib_planes/control.lua @@ -46,15 +46,11 @@ function airutils.control(self, dtime, hull_direction, longit_speed, longit_drag --engine and power control if ctrl.aux1 and self._last_time_command > 0.5 then self._last_time_command = 0 - if self._engine_running then - self._engine_running = false - self._autopilot = false - self._power_lever = 0 --zero power - self._last_applied_power = 0 --zero engine - elseif self._engine_running == false and self._energy > 0 then - self._engine_running = true - self._last_applied_power = -1 --send signal to start - end + if self._yaw_by_mouse == true then + self._yaw_by_mouse = false + else + self._yaw_by_mouse = true + end end self._acceleration = 0 @@ -113,13 +109,22 @@ function airutils.control(self, dtime, hull_direction, longit_speed, longit_drag --pitch local pitch_cmd = 0 - if ctrl.up then pitch_cmd = 1 elseif ctrl.down then pitch_cmd = -1 end - airutils.set_pitch(self, pitch_cmd, dtime) + if self._yaw_by_mouse == true then + airutils.set_pitch_by_mouse(self, player) + else + if ctrl.up then pitch_cmd = 1 elseif ctrl.down then pitch_cmd = -1 end + airutils.set_pitch(self, pitch_cmd, dtime) + end -- yaw local yaw_cmd = 0 - if ctrl.right then yaw_cmd = 1 elseif ctrl.left then yaw_cmd = -1 end - airutils.set_yaw(self, yaw_cmd, dtime) + if self._yaw_by_mouse == true then + local rot_y = math.deg(player:get_look_horizontal()) + airutils.set_yaw_by_mouse(self, rot_y) + else + if ctrl.right then yaw_cmd = 1 elseif ctrl.left then yaw_cmd = -1 end + airutils.set_yaw(self, yaw_cmd, dtime) + end --I'm desperate, center all! if ctrl.right and ctrl.left then @@ -145,6 +150,14 @@ function airutils.control(self, dtime, hull_direction, longit_speed, longit_drag return retval_accel, stop end +function airutils.set_pitch_by_mouse(self, player) + local vehicle_rot = self.object:get_rotation() + local rot_x = player:get_look_vertical()-vehicle_rot.x + self._elevator_angle = -(rot_x * self._elevator_limit)*5 + if self._elevator_angle > self._elevator_limit then self._elevator_angle = self._elevator_limit end + if self._elevator_angle < -self._elevator_limit then self._elevator_angle = -self._elevator_limit end +end + function airutils.set_pitch(self, dir, dtime) local pitch_factor = self._pitch_intensity or 0.6 local multiplier = pitch_factor*(dtime/airutils.ideal_step) @@ -159,6 +172,27 @@ function airutils.set_pitch(self, dir, dtime) end end +function airutils.set_yaw_by_mouse(self, dir) + local rotation = self.object:get_rotation() + local rot_y = math.deg(rotation.y) + + local total = math.abs(math.floor(rot_y/360)) + + if rot_y < 0 then rot_y = rot_y + (360*total) end + if rot_y > 360 then rot_y = rot_y - (360*total) end + if rot_y >= 270 and dir <= 90 then dir = dir + 360 end + if rot_y <= 90 and dir >= 270 then dir = dir - 360 end + + local intensity = 2 + if self._intensity then intensity = self._intensity end + local command = (rot_y - dir) * intensity + if command < -90 then command = -90 + elseif command > 90 then command = 90 end + --minetest.chat_send_all("rotation y: "..rot_y.." - dir: "..dir.." - command: "..command) + + self._rudder_angle = (-command * self._rudder_limit)/90 +end + function airutils.set_yaw(self, dir, dtime) local yaw_factor = self._yaw_intensity or 25 if dir == 1 then diff --git a/lib_planes/entities.lua b/lib_planes/entities.lua index 24d36ba..c0f4d3a 100644 --- a/lib_planes/entities.lua +++ b/lib_planes/entities.lua @@ -442,7 +442,8 @@ function airutils.logic(self) end if player then - local new_eye_offset = airutils.camera_reposition(player, newpitch, math.rad(self._rudder_angle)) + --minetest.chat_send_all(dump(newroll)) + local new_eye_offset = airutils.camera_reposition(player, newpitch, newroll) player:set_eye_offset(new_eye_offset, {x = 0, y = 1, z = -30}) end diff --git a/lib_planes/forms.lua b/lib_planes/forms.lua index 7f1d547..912c7ce 100644 --- a/lib_planes/forms.lua +++ b/lib_planes/forms.lua @@ -16,11 +16,24 @@ end function airutils.pilot_formspec(name) local basic_form = table.concat({ "formspec_version[3]", - "size[6,6]", + "size[6.0,7.0]", }, "") - basic_form = basic_form.."button[1,1.0;4,1;go_out;Go Offboard]" - basic_form = basic_form.."button[1,2.5;4,1;hud;Show/Hide Gauges]" + local player = minetest.get_player_by_name(name) + local plane_obj = airutils.getPlaneFromPlayer(player) + if plane_obj == nil then + return + end + local ent = plane_obj:get_luaentity() + + local yaw = "false" + if ent._yaw_by_mouse then yaw = "true" end + + basic_form = basic_form.."button[1,1.0;4,1;turn_on;Start/Stop Engines]" + basic_form = basic_form.."button[1,2.1;4,1;go_out;Go Offboard]" + basic_form = basic_form.."button[1,3.2;4,1;hud;Show/Hide Gauges]" + basic_form = basic_form.."button[1,4.3;4,1;inventory;Show Inventory]" + basic_form = basic_form.."checkbox[1,5.7;yaw;Yaw by mouse;"..yaw.."]" minetest.show_formspec(name, "lib_planes:pilot_main", basic_form) end @@ -31,6 +44,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) local plane_obj = airutils.getPlaneFromPlayer(player) if plane_obj then local ent = plane_obj:get_luaentity() + if fields.turn_on then + airutils.start_engine(ent) + end if fields.hud then if ent._show_hud == true then ent._show_hud = false @@ -64,8 +80,21 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) ent._instruction_mode = true end end + airutils.dettachPlayer(ent, player) end + if fields.inventory then + if ent._trunk_slots then + airutils.show_vehicle_trunk_formspec(ent, player, ent._trunk_slots) + end + end + if fields.yaw then + if ent._yaw_by_mouse == true then + ent._yaw_by_mouse = false + else + ent._yaw_by_mouse = true + end + end end minetest.close_formspec(name, "lib_planes:pilot_main") end diff --git a/lib_planes/utilities.lua b/lib_planes/utilities.lua index 416cd3c..ebb60ec 100644 --- a/lib_planes/utilities.lua +++ b/lib_planes/utilities.lua @@ -317,7 +317,7 @@ function airutils.testImpact(self, velocity, position) fade = 0.0, pitch = 1.0, }, true) - self.hp_max = self.hp_max - 5 + self.hp_max = self.hp_max - self._damage_by_wind_speed if self.driver_name then local player_name = self.driver_name airutils.setText(self, self.infotext) @@ -585,6 +585,18 @@ function airutils.add_destruction_effects(pos, radius) }) end +function airutils.start_engine(self) + if self._engine_running then + self._engine_running = false + self._autopilot = false + self._power_lever = 0 --zero power + self._last_applied_power = 0 --zero engine + elseif self._engine_running == false and self._energy > 0 then + self._engine_running = true + self._last_applied_power = -1 --send signal to start + end +end + function airutils.get_xz_from_hipotenuse(orig_x, orig_z, yaw, distance) --cara, o minetest é bizarro, ele considera o eixo no sentido ANTI-HORÁRIO... Então pra equação funcionar, subtrair o angulo de 360 antes yaw = math.rad(360) - yaw @@ -594,13 +606,16 @@ function airutils.get_xz_from_hipotenuse(orig_x, orig_z, yaw, distance) end function airutils.camera_reposition(player, pitch, roll) + if roll < -0.4 then roll = -0.4 end + if roll > 0.4 then roll = 0.4 end + local player_properties = player:get_properties() local new_eye_offset = vector.new() local z, y = airutils.get_xz_from_hipotenuse(0, player_properties.eye_height, pitch, player_properties.eye_height) new_eye_offset.z = z*7 new_eye_offset.y = y*1.5 local x, _ = airutils.get_xz_from_hipotenuse(0, player_properties.eye_height, roll, player_properties.eye_height) - new_eye_offset.x = x*7 + new_eye_offset.x = -x*15 return new_eye_offset end