1-- Desert cave scripted animation
2
3local ns = {}
4setmetatable(ns, {__index = _G})
5layna_forest_caves_background_anim = ns;
6setfenv(1, ns);
7
8-- Animated image members
9local fog = nil
10local background = nil
11
12-- Other fog related members
13local fog_x_position = 300.0;
14local fog_y_position = 500.0;
15local fog_alpha = 0.0;
16local fog_timer;
17local fog_time_length = 8000;
18
19-- c++ objects instances
20local Map = nil
21local Script = nil
22local Effects = nil
23
24function Initialize(map_instance)
25    Map = map_instance;
26    Script = Map:GetScriptSupervisor();
27    Effects = Map:GetEffectSupervisor();
28    -- Load the creatures animated background
29    background = Script:CreateImage("data/visuals/backgrounds/cave_background.png");
30    background:SetDimensions(1024.0, 768.0);
31
32    -- Construct a timer used to display the fog with a custom alpha value and position
33    fog_timer = vt_system.SystemTimer(fog_time_length, 0);
34    -- Load a fog image used later to be displayed dynamically on the battle ground
35    fog = Script:CreateImage("data/visuals/ambient/fog.png");
36    fog:SetDimensions(320.0, 256.0);
37
38    fog_timer:Run();
39end
40
41function Update()
42    -- fog
43    -- Start the timer only at normal battle stage
44    if (fog_timer:IsRunning() == false) then
45        fog_timer:Run();
46    end
47    if (fog_timer:IsFinished()) then
48        fog_timer:Initialize(fog_time_length, 0);
49        fog_timer:Run();
50        -- Make the fog appear at random position
51        fog_x_position = math.random(300.0, 600.0);
52        fog_y_position = math.random(300.0, 450.0);
53        fog_alpha = 0.0;
54    end
55    fog_timer:Update();
56    -- update fog position and alpha
57    -- Apply a small shifting
58    fog_x_position = fog_x_position - (0.5 * fog_timer:PercentComplete());
59
60    -- Apply parallax (the camera movement)
61    fog_x_position = fog_x_position + Effects:GetCameraXMovement();
62    -- Inverted y coords
63    fog_y_position = fog_y_position + Effects:GetCameraYMovement();
64
65    if (fog_timer:PercentComplete() <= 0.5) then
66        -- fade in
67        fog_alpha = fog_timer:PercentComplete() * 0.3 / 0.5;
68    else
69        -- fade out
70        fog_alpha = 0.3 - (0.3 * (fog_timer:PercentComplete() - 0.5) / 0.5);
71    end
72end
73
74local white_color = vt_video.Color(1.0, 1.0, 1.0, 1.0);
75
76function DrawBackground()
77    -- Draw background animation
78    VideoManager:Move(512.0, 768.0);
79    background:Draw(white_color);
80end
81
82local fog_color = vt_video.Color(1.0, 1.0, 1.0, 1.0);
83
84function DrawForeground()
85    -- Draw a random fog effect
86    fog_color:SetAlpha(fog_alpha);
87    VideoManager:Move(fog_x_position, fog_y_position);
88    fog:Draw(fog_color);
89end
90