Fountain

A 2D jet aimed by turning its emitter: +Y points down the screen, so the nozzle is rotated half a turn and gravity brings the water back.


    
main.lua
local natiny = require("natiny")

natiny.backend.init(arg and arg[1] or natiny.backend.AUTO)

local win = natiny.window.create("Natiny - Fountain", 900, 600)
natiny.window.set_scale_mode(win, natiny.window.SCALE_MODE_STRETCH)

local BG = natiny.color.from_hex("#151E2C")
local sprite = natiny.texture.create(natiny.resource.load("common/textures/particle.png"))

local jet = natiny.particles.create(1500)
natiny.particles.set_texture(jet, sprite)
natiny.particles.set_rate(jet, 600)          -- particles a second
natiny.particles.set_spread(jet, 26)         -- degrees of scatter at the nozzle
natiny.particles.set_speed(jet, 640, 960)    -- pixels a second
natiny.particles.set_lifetime(jet, 1.4, 2.2)
natiny.particles.set_size(jet, 8, 18)
natiny.particles.set_sizes(jet, { 1.0, 0.8, 0.0 })
natiny.particles.set_gravity(jet, 0, 950)    -- +Y is down the screen in 2D
natiny.particles.set_damping(jet, 0.2)
natiny.particles.set_colors(jet, {
    natiny.color.from_hex("#FFFFFFFF"),
    natiny.color.from_hex("#5AD2FFCC"),
    natiny.color.from_hex("#3A6BFF00"),
})
natiny.particles.set_blend(jet, natiny.particles.BLEND_ADD)

local time = 0

natiny.backend.loop(function(dt)
    time = time + dt

    natiny.window.bind(win, function()
        local w = natiny.window.get_width(win)
        local h = natiny.window.get_height(win)

        -- A system emits along its own +Y, which points down the screen, so
        -- the nozzle is turned half a turn to aim at the top of the window.
        -- Turning it a little further each way is the whole of the sway.
        natiny.entity.set_position(jet, natiny.entity.SPACE_WORLD, w / 2, h - 30, 0)
        natiny.entity.set_rotation(jet, natiny.entity.SPACE_WORLD,
                                   0, 0, 180 + math.sin(time * 0.7) * 20)

        natiny.render.clear(BG)
        natiny.render.particles(jet)
    end)
end)

natiny.backend.shutdown()