Sprache Deutsch Language English

Script Dokumentation LS 2015 - FollowerSound (Patch 1.3)

Script Dokumentation Übersicht

scripts/objects/FollowerSound.lua

Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1-- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved.
2
3FollowerSound = {}
4
5local FollowerSound_mt = Class(FollowerSound);
6
7function FollowerSound:onCreate(id)
8 g_currentMission:addUpdateable(FollowerSound:new(id));
9 --print("created FollowerSound, id: ", id);
10end;
11
12function FollowerSound:new(id)
13 local self = {};
14 setmetatable(self, FollowerSound_mt);
15
16 self.splineId = getChildAt(id, 0);
17 self.soundId = getChildAt(id, 1);
18 self.currentDelay = 200;
19 self.splineCVs = {};
20 self.followAxis = Utils.getNoNil(getUserAttribute(id, "followAxis"), 1);
21 self.splineCVCount = getSplineNumOfCV(self.splineId);
22 self.backwards = false;
23
24 for i = 1, self.splineCVCount do
25 local splineCV = {};
26 splineCV[1], splineCV[2], splineCV[3] = getSplineCV(self.splineId, i - 1);
27 table.insert(self.splineCVs, splineCV);
28 end;
29
30 if self.splineCVs[2][self.followAxis] < self.splineCVs[1][self.followAxis] then
31 self.backwards = true;
32 end;
33
34 return self;
35end;
36
37function FollowerSound:delete()
38
39end;
40
41function FollowerSound:update(dt)
42 if self.currentDelay > 0 then
43 self.currentDelay = self.currentDelay - dt;
44 else
45 self.currentDelay = 200;
46 local playerPosition = {0, 0, 0};
47 if g_currentMission.controlPlayer then
48 if g_currentMission.player ~= nil then
49 playerPosition[1], playerPosition[2], playerPosition[3] = getWorldTranslation(g_currentMission.player.rootNode);
50 end
51 elseif g_currentMission.controlledVehicle ~= nil then
52 playerPosition[1], playerPosition[2], playerPosition[3] = getWorldTranslation(g_currentMission.controlledVehicle.rootNode);
53 end;
54
55 for i = 1, self.splineCVCount - 1 do
56 -- determine which control vertices the player is currently standing between
57
58 if playerPosition[self.followAxis] >= self.splineCVs[i][self.followAxis] and playerPosition[self.followAxis] <= self.splineCVs[i + 1][self.followAxis] then
59
60 local normalizedPos = (playerPosition[self.followAxis] - self.splineCVs[i][self.followAxis]) / (self.splineCVs[i + 1][self.followAxis] - self.splineCVs[i][self.followAxis]);
61 local newPos = {};
62 for j = 1, 3 do
63 newPos[j] = (1 - normalizedPos) * self.splineCVs[i][j] + normalizedPos * self.splineCVs[i + 1][j];
64 end;
65 setTranslation(self.soundId, newPos[1], newPos[2], newPos[3]);
66
67 -- backwards case
68 elseif playerPosition[self.followAxis] <= self.splineCVs[i][self.followAxis] and playerPosition[self.followAxis] >= self.splineCVs[i + 1][self.followAxis] then
69
70 local normalizedPos = (playerPosition[self.followAxis] - self.splineCVs[i + 1][self.followAxis]) / (self.splineCVs[i][self.followAxis] - self.splineCVs[i + 1][self.followAxis]);
71 local newPos = {};
72 for j = 1, 3 do
73 newPos[j] = (1 - normalizedPos) * self.splineCVs[i + 1][j] + normalizedPos * self.splineCVs[i][j];
74 end;
75 setTranslation(self.soundId, newPos[1], newPos[2], newPos[3]);
76
77 end;
78
79 end;
80
81 -- handle special case if player is outside spline's length
82 if self.backwards then
83 if playerPosition[self.followAxis] < self.splineCVs[self.splineCVCount][self.followAxis] then
84 setTranslation(self.soundId, self.splineCVs[self.splineCVCount][1], self.splineCVs[self.splineCVCount][2], self.splineCVs[self.splineCVCount][3]);
85 elseif playerPosition[self.followAxis] > self.splineCVs[1][self.followAxis] then
86 setTranslation(self.soundId, self.splineCVs[1][1], self.splineCVs[1][2], self.splineCVs[1][3]);
87 end;
88 else
89 if playerPosition[self.followAxis] < self.splineCVs[1][self.followAxis] then
90 setTranslation(self.soundId, self.splineCVs[1][1], self.splineCVs[1][2], self.splineCVs[1][3]);
91 elseif playerPosition[self.followAxis] > self.splineCVs[self.splineCVCount][self.followAxis] then
92 setTranslation(self.soundId, self.splineCVs[self.splineCVCount][1], self.splineCVs[self.splineCVCount][2], self.splineCVs[self.splineCVCount][3]);
93 end;
94 end;
95
96 end;
97end;
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
Script Dokumentation Übersicht