Capricorn 76
Particle system example.
Particle systems are graphical special fx, to simulate all sorts of things like rain, sparks, fountains, smoke, etc.

---------------------------
---------------------------
-- Particle system example
-- Copyright (c) 2007-09 Capricorn 76 Pty. Ltd.
--
-- Particle systems are graphical special fx,
-- to simulate all sorts of things like rain,
-- sparks, fountains, smoke, etc.
--
-- They can be added in the viewer or
-- from your scripts using the IWorld:createParticleSystem()
-- and associated functions.
--
-- NOTE:
-- The ./Demos/runDemo.e76script is a utility script used to run all the examples.
-- It loads the world, calls the OnWorldLoad(), OnWorldUnload() functions, and runs the graphics engine loop, waiting for the user to press escape.
---------------------------
---------------------------

local subMouse;

---------------------------
-- runDemo.e76script calls this function when the world is loaded
---------------------------
function OnWorldLoad(worldName)
    --print('OnWorldLoad');

    ---------------------------
    -- The SDK world editor creates a worldIds.e76script when the world is saved. It contains all the entity ids.
    -- We load the IDs so that we can refer to the world entities using friendly names.
    ---------------------------
    IApp:loadScript(IWorld:getNameLong() .. '/worldIds');
    IWorld:setActiveCameraId(idCamera_cam1);

    ---------------------------
    -- Load example's GUI
    ---------------------------
    OnWorldLoadGui();

    ---------------------------
    -- Enable the auto camera management.
    -- The active camera will be moved in response to its type, and the mouse/key input.
    -- In this example, the active camera type is 'fps' or first-person-shooter style.
    ---------------------------
    IWorld:setActiveCameraAutoProcessed(true);

    ---------------------------
    -- Subscribe to tick events at a predefined rate.
    -- The subscriber identifier returned from the function
    -- is used to later unsubscribe this event handler.
    ---------------------------
    subMouse = IEvents:subscribeMouse('OnMoveSystem');

    IMouse:show(false);
end

---------------------------
-- Load example's GUI
---------------------------
function OnWorldLoadGui()
    IGraphics:loadGui('help');
end

---------------------------
-- runDemo.e76script calls this function when the world is unloaded
---------------------------
function OnWorldUnload(worldName)
    --print('OnWorldUnload');

    ---------------------------
    -- Unsubscribe to events.
    -- We no longet will be notified.
    ---------------------------
    IEvents:unsubscribe(subMouse);

    IMouse:show(true);
end

------------------------
-- Handle mouse button events.
-- The graphics engine will call this callback function
-- whenever the user clicks a mouse button.
------------------------
function OnMoveSystem(eventType, mouseX, mouseY, mouseWheel, userData)
    ---------------------------
    -- Change the position of the particle system.
    -- Fire a ray from the current mouse position into the world to determine where to move particle system.
    ---------------------------
    local rayVector = IWorld:getCameraLookVector(IWorld:getActiveCameraId(), mouseX, mouseY);
    local dir = IMisc:vectorSubtract(rayVector['end'], rayVector['start']);
    dir = IMisc:vectorNormaliseFast(dir);
    local positionOffset = IMisc:vectorMultiply(dir, 5);    -- How far from the camera we'll position the system
    local newPosition = IMisc:vectorAdd(IWorld:getEntityPosition(IWorld:getActiveCameraId()), positionOffset);
    IWorld:setEntityPosition(idParticle_p1, newPosition);

    if (IMouse:leftButtonClicked(eventType)) then
        local newParticleSystem = IWorld:copyEntity(idParticle_p1);
        return true;
    end
end

Copyright © 2006-23 Sep 2009 Capricorn 76 Pty. Ltd. (created on Wed Sep 23 16:49:12 2009)