#version 430 core
#define pi 3.141592653589793238462643383279

struct explosion {
        vec4        positionVelocity;
        vec4        color;
};

layout (std430, binding = 0) buffer entities {
    explosion   e[];
};

layout(binding = 1, offset = 0) uniform atomic_uint lastCounter;

uniform float delta;

layout(local_size_x = 512) in;

void main() {
    uint gid = gl_GlobalInvocationID.x;
    uint last = atomicCounter(lastCounter);

    if (gid < last) {
        vec2 position = e[gid].positionVelocity.xy;
        vec2 velocity = e[gid].positionVelocity.zw;
        vec4 color = e[gid].color;

        if (color.a > 0.0) {
            position = position + velocity * delta * color.a;
            color.a = color.a - (delta / 2);
        } else {
            color.a = 0.0;
        }

        e[gid].positionVelocity = vec4(position, velocity);
        e[gid].color = color;
    }
}
