From f42503fdc6740aa61557f648363a1e72c164044c Mon Sep 17 00:00:00 2001 From: Sumyjkl Date: Wed, 11 Jan 2023 15:16:55 +1100 Subject: [PATCH] Update balloon.lua and init.lua --- balloon.lua | 136 +++++++++++++++++++++++++++++++++++++++------------- init.lua | 4 -- 2 files changed, 102 insertions(+), 38 deletions(-) diff --git a/balloon.lua b/balloon.lua index 7dc2021..2b25655 100644 --- a/balloon.lua +++ b/balloon.lua @@ -1,19 +1,19 @@ local mod_name = minetest.get_current_modname() local mod_path = minetest.get_modpath(mod_name) -local me = sum_airship - -me.vars = { +me = { lift = 4, speed = 3, - fuel_time = 30, + fuel_time = 10, + speed_mult = 4, + i = {}, } local ship = { physical = true, pointable = true, - collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.2, 0.5}, - selectionbox = {-0.5, -0.5, -0.5, 0.5, 0.2, 0.5}, + collisionbox = {-0.6, -0.2, -0.6, 0.6, 0.3, 0.6}, + selectionbox = {-0.7, -0.35, -0.7, 0.7, 0.3, 0.7}, hp_max = 3, visual = "mesh", backface_culling = false, @@ -32,20 +32,6 @@ local ship = { _vel = 0, _regen_timer = 0, _fuel = 0, - _sounds = { - engine = { - handle = nil, - gain = 0.1, - playing = false, - time_elapsed = 0, - }, - engine_stop = { - handle = nil, - gain = 0.1, - playing = false, - time_elapsed = 0, - }, - } } local sounds = { @@ -77,7 +63,7 @@ function me.sound_play(self, sound_obj, sound_instance) gain = sound_obj.gain, max_hear_distance = sound_obj.max_hear_distance, loop = sound_obj.loop, - pitch = sound_obj.pitch or 1, + pitch = sound_obj.pitch, object = self.object, }) sound_instance.playing = true @@ -93,10 +79,18 @@ function me.sound_stop(sound_instance) sound_instance.handle = nil end +function me.sound_countdown(self, dtime) + for _, sound in pairs(self._sounds) do + if sound.playing then + sound.time_elapsed = sound.time_elapsed + dtime + end + end +end + function me.update_sound(self, dtime, forward) - local fuel = self._fuel - local is_thrust = forward ~= 0 and self._driver - if not fuel or not type(fuel) == "number" then self._fuel = 0 end + me.sound_countdown(self, dtime) + + local is_thrust = (forward ~= 0) and self._driver if self._sounds.engine.time_elapsed > 2.1 and self._sounds.engine.handle @@ -119,20 +113,21 @@ function me.update_sound(self, dtime, forward) and self._sounds.engine_stop.time_elapsed == 0 then me.sound_play(self, sounds.engine_stop, self._sounds.engine_stop) end - if not is_thrust - or (self._sounds.engine_stop.time_elapsed == 0 - and self._sounds.engine_stop.playing) then - me.sound_stop(self._sounds.engine) - end + if (not is_thrust) then + me.sound_stop(self._sounds.engine) + end end end + function ship.on_activate(self, staticdata, dtime_s) local data = minetest.deserialize(staticdata) if type(data) == "table" then + self._vel = data.v self._itemstring = data.itemstring + self._fuel = data.fuel if data._driver then - self._driver = minetest.get_player_by_name((data._driver:get_player_name())) + self._driver = minetest.get_player_by_name((data._driver)) else self._driver = nil end @@ -140,6 +135,20 @@ function ship.on_activate(self, staticdata, dtime_s) if self._driver then me.detach(self) end end self.object:set_animation(ship._animations.idle, 24) + self._sounds = { -- workaround for copy vs reference issue + engine = { + handle = nil, + gain = 0.1, + playing = false, + time_elapsed = 0, + }, + engine_stop = { + handle = nil, + gain = 0.1, + playing = false, + time_elapsed = 0, + }, + } end function ship.get_staticdata(self) @@ -148,6 +157,8 @@ function ship.get_staticdata(self) _driver = ((self._driver and self._driver:is_player()) and self._driver:get_player_name()) or nil, _flags = self._flags, + v = self._vel, + fuel = self._fuel, }) end @@ -191,7 +202,7 @@ function ship.on_rightclick(self, clicker) item:take_item() clicker:set_wielded_item(item) end - self._fuel = self._fuel + sum_airship.fuel_time + self._fuel = self._fuel + me.fuel_time me.sound_stop(self._sounds.engine) minetest.sound_play("sum_airship_fire", { gain = 1, @@ -254,6 +265,19 @@ function me.get_balloon_collide(self) return force end +me.chimney_dist = -1.0 +me.chimney_yaw = 0.13 +me.chimney_height = 0.9 +function me.get_chimney_pos(self) + local p = self.object:get_pos() + local yaw = self.object:get_yaw() + local ret = { + x = p.x + (me.chimney_dist * math.sin(-yaw + me.chimney_yaw)), + y = p.y + me.chimney_height, + z = p.z + (me.chimney_dist * math.cos(-yaw + me.chimney_yaw))} + return ret +end + function ship.on_step(self, dtime, moveresult) local exit = false local pi = nil @@ -278,6 +302,12 @@ function ship.on_step(self, dtime, moveresult) local in_water = minetest.get_item_group(minetest.get_node(p).name, "liquid") ~= 0 local on_water = (minetest.get_item_group(minetest.get_node(vector.offset(p, 0, -0.2, 0)).name, "liquid") ~= 0 and not in_water) + local speedboost = 1 + if self._fuel > 0 then + self._fuel = self._fuel - dtime + speedboost = 2 + end + if pi then if pi.ctrl.up then forward = 1 elseif pi.ctrl.down then forward = -1 end @@ -285,17 +315,17 @@ function ship.on_step(self, dtime, moveresult) elseif pi.ctrl.aux1 then climb = -1 end if pi.ctrl.right then right = 1 elseif pi.ctrl.left then right = -1 end + local yaw = self.object:get_yaw() local dir = minetest.yaw_to_dir(yaw) self.object:set_yaw(yaw - right * dtime) - local added_vel = vector.multiply(dir, forward * dtime * me.vars.speed) - added_vel.y = added_vel.y + (climb * dtime * me.vars.lift) + local added_vel = vector.multiply(dir, forward * dtime * me.speed * speedboost) + added_vel.y = added_vel.y + (climb * dtime * me.lift) v = vector.add(v, added_vel) end if self._driver then local collide_force = me.get_balloon_collide(self) - -- collide_force = vector.normalize(collide_force) if collide_force ~= vector.new() then collide_force = vector.multiply(collide_force, 0.1) v = vector.multiply(v, 0.95) @@ -329,7 +359,45 @@ function ship.on_step(self, dtime, moveresult) v.y = v.y * (1 - (dtime * 0.5)) v.z = v.z * (1 - (dtime * 0.5)) end + + local wind_vel = vector.new(0,0,0) + if minetest.get_modpath("sum_air_currents") ~= nil then + wind_vel = sum_air_currents.get_wind(p) + if self._driver or not is_on_floor then + v = vector.add(wind_vel, v) + end + end + + self.object:set_velocity(v) + + me.update_sound(self, dtime, forward) + + + local is_thrust = self._driver and forward ~= 0 + + local chimney_pos = me.get_chimney_pos(self) + + local spread = 0.06 + if self._fuel > 0 or (math.random(0,100) > 80 and is_thrust) or math.random(0,100) > 95 then + minetest.add_particle({ + pos = vector.offset(chimney_pos, math.random(-1, 1)*spread, 0, math.random(-1, 1)*spread), + velocity = vector.add(wind_vel, {x=0, y=math.random(0.2*100,0.7*100)/100, z=0}), + expirationtime = math.random(0.5, 2), + size = math.random(0.1, 4), + collisiondetection = false, + vertical = false, + texture = "sum_airship_smoke.png", + }) + end + -- animations + if self._fuel > 0 then + self.object:set_animation(self._animations.boost, 25) + elseif is_thrust then + self.object:set_animation(self._animations.fly, 25) + else + self.object:set_animation(self._animations.idle, 25) + end end diff --git a/init.lua b/init.lua index 462ad5e..ee145c5 100644 --- a/init.lua +++ b/init.lua @@ -1,8 +1,4 @@ local mod_name = minetest.get_current_modname() local mod_path = minetest.get_modpath(mod_name) - -sum_airship = {} - - dofile(mod_path .. DIR_DELIM .. "balloon.lua")