mirror of
https://gitlab.com/rautars/weather_pack.git
synced 2025-07-16 05:40:22 +02:00
start using happy_weather_api, fix #8 sky reseting issue
This commit is contained in:
194
snow.lua
194
snow.lua
@ -1,90 +1,122 @@
|
||||
snow = {}
|
||||
------------------------------
|
||||
-- Happy Weather: Light Rain
|
||||
|
||||
snow.particles_count = 15
|
||||
snow.init_done = false
|
||||
-- License: MIT
|
||||
|
||||
-- calculates coordinates and draw particles for snow weather
|
||||
snow.add_rain_particles = function(player)
|
||||
rain.last_rp_count = 0
|
||||
for i=snow.particles_count, 1,-1 do
|
||||
local random_pos_x, random_pos_y, random_pos_z = weather.get_random_pos_by_player_look_dir(player)
|
||||
random_pos_y = math.random() + math.random(player:getpos().y - 1, player:getpos().y + 7)
|
||||
if minetest.get_node_light({x=random_pos_x, y=random_pos_y, z=random_pos_z}, 0.5) == 15 then
|
||||
rain.last_rp_count = rain.last_rp_count + 1
|
||||
minetest.add_particle({
|
||||
pos = {x=random_pos_x, y=random_pos_y, z=random_pos_z},
|
||||
velocity = {x = math.random(-1,-0.5), y = math.random(-2,-1), z = math.random(-1,-0.5)},
|
||||
acceleration = {x = math.random(-1,-0.5), y=-0.5, z = math.random(-1,-0.5)},
|
||||
expirationtime = 2.0,
|
||||
size = math.random(0.5, 2),
|
||||
collisiondetection = true,
|
||||
collision_removal = true,
|
||||
vertical = true,
|
||||
texture = snow.get_texture(),
|
||||
playername = player:get_player_name()
|
||||
})
|
||||
end
|
||||
-- Credits: xeranas
|
||||
------------------------------
|
||||
|
||||
local snow = {}
|
||||
|
||||
-- Weather identification code
|
||||
snow.code = "snow"
|
||||
|
||||
-- Manual triggers flags
|
||||
local manual_trigger_start = false
|
||||
local manual_trigger_end = false
|
||||
|
||||
-- Skycolor layer id
|
||||
local SKYCOLOR_LAYER = "happy_weather_snow_sky"
|
||||
|
||||
snow.is_starting = function(dtime, position)
|
||||
if manual_trigger_start then
|
||||
manual_trigger_start = false
|
||||
return true
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
snow.is_ending = function(dtime)
|
||||
if manual_trigger_end then
|
||||
manual_trigger_end = false
|
||||
return true
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
local set_sky_box = function(player_name)
|
||||
local sl = {}
|
||||
sl.layer_type = skylayer.SKY_PLAIN
|
||||
sl.name = SKYCOLOR_LAYER
|
||||
sl.data = {gradient_data={}}
|
||||
sl.data.gradient_data.colors = {
|
||||
{r=0, g=0, b=0},
|
||||
{r=241, g=244, b=249},
|
||||
{r=0, g=0, b=0}
|
||||
}
|
||||
skylayer.add_layer(player_name, sl)
|
||||
end
|
||||
|
||||
snow.add_player = function(player)
|
||||
set_sky_box(player:get_player_name())
|
||||
end
|
||||
|
||||
snow.remove_player = function(player)
|
||||
skylayer.remove_layer(player:get_player_name(), SKYCOLOR_LAYER)
|
||||
end
|
||||
|
||||
-- Random texture getter
|
||||
local choice_random_rain_drop_texture = function()
|
||||
local texture_name
|
||||
local random_number = math.random()
|
||||
if random_number > 0.33 then
|
||||
texture_name = "happy_weather_light_snow_snowflake_1.png"
|
||||
elseif random_number > 0.66 then
|
||||
texture_name = "happy_weather_light_snow_snowflake_2.png"
|
||||
else
|
||||
texture_name = "happy_weather_light_snow_snowflake_3.png"
|
||||
end
|
||||
return texture_name;
|
||||
end
|
||||
|
||||
local add_particle = function(player)
|
||||
local offset = {
|
||||
front = 5,
|
||||
back = 2,
|
||||
top = 4
|
||||
}
|
||||
|
||||
local random_pos = hw_utils.get_random_pos(player, offset)
|
||||
|
||||
if hw_utils.is_outdoor(random_pos) then
|
||||
minetest.add_particle({
|
||||
pos = {x=random_pos.x, y=random_pos.y, z=random_pos.z},
|
||||
velocity = {x = math.random(-1,-0.5), y = math.random(-2,-1), z = math.random(-1,-0.5)},
|
||||
acceleration = {x = math.random(-1,-0.5), y=-0.5, z = math.random(-1,-0.5)},
|
||||
expirationtime = 2.0,
|
||||
size = math.random(0.5, 2),
|
||||
collisiondetection = true,
|
||||
collision_removal = true,
|
||||
vertical = true,
|
||||
texture = choice_random_rain_drop_texture(),
|
||||
playername = player:get_player_name()
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
local display_particles = function(player)
|
||||
if hw_utils.is_underwater(player) then
|
||||
return
|
||||
end
|
||||
|
||||
add_particle(player)
|
||||
end
|
||||
|
||||
local particles_number_per_update = 10
|
||||
snow.render = function(dtime, player)
|
||||
for i=particles_number_per_update, 1,-1 do
|
||||
display_particles(player)
|
||||
end
|
||||
end
|
||||
|
||||
snow.set_sky_box = function()
|
||||
skycolor.add_layer(
|
||||
"weather-pack-snow-sky",
|
||||
{{r=0, g=0, b=0},
|
||||
{r=241, g=244, b=249},
|
||||
{r=0, g=0, b=0}}
|
||||
)
|
||||
skycolor.active = true
|
||||
snow.start = function()
|
||||
manual_trigger_start = true
|
||||
end
|
||||
|
||||
snow.clear = function()
|
||||
skycolor.remove_layer("weather-pack-snow-sky")
|
||||
snow.init_done = false
|
||||
end
|
||||
|
||||
-- Simple random texture getter
|
||||
snow.get_texture = function()
|
||||
local texture_name
|
||||
local random_number = math.random()
|
||||
if random_number > 0.5 then
|
||||
texture_name = "weather_pack_snow_snowflake1.png"
|
||||
else
|
||||
texture_name = "weather_pack_snow_snowflake2.png"
|
||||
end
|
||||
return texture_name;
|
||||
end
|
||||
|
||||
local timer = 0
|
||||
minetest.register_globalstep(function(dtime)
|
||||
if weather.state ~= "snow" then
|
||||
return false
|
||||
end
|
||||
|
||||
timer = timer + dtime;
|
||||
if timer >= 0.5 then
|
||||
timer = 0
|
||||
else
|
||||
return
|
||||
end
|
||||
|
||||
if snow.init_done == false then
|
||||
snow.set_sky_box()
|
||||
snow.init_done = true
|
||||
end
|
||||
|
||||
for _, player in ipairs(minetest.get_connected_players()) do
|
||||
if (weather.is_underwater(player)) then
|
||||
return false
|
||||
end
|
||||
snow.add_rain_particles(player)
|
||||
end
|
||||
end)
|
||||
|
||||
-- register snow weather
|
||||
if weather.reg_weathers.snow == nil then
|
||||
weather.reg_weathers.snow = {
|
||||
chance = 10,
|
||||
clear = snow.clear
|
||||
}
|
||||
snow.stop = function()
|
||||
manual_trigger_end = true
|
||||
end
|
||||
|
||||
happy_weather.register_weather(snow)
|
Reference in New Issue
Block a user