Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1 | -- ChurchClock class |
2 | -- |
3 | -- ChurchClock rotates two hands of four clock faces to show the current time |
4 | -- All eight hands need to be direct children (short hand first, then long hand) |
5 | -- |
6 | -- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved. |
7 | |
8 | ChurchClock = {} |
9 | |
10 | local ChurchClock_mt = Class(ChurchClock); |
11 | |
12 | function ChurchClock:onCreate(id) |
13 | g_currentMission:addNonUpdateable(ChurchClock:new(id)); |
14 | end; |
15 | |
16 | function ChurchClock:new(name) |
17 | local instance = {}; |
18 | setmetatable(instance, ChurchClock_mt); |
19 | |
20 | instance.timerId = 0; |
21 | instance.tollCounter = 0; |
22 | instance.waitingForBell = false; |
23 | instance.tollNow = true; |
24 | |
25 | instance.shortHands = {}; |
26 | instance.longHands = {}; |
27 | instance.init = false; |
28 | instance.lastHourTolled = 0; |
29 | |
30 | if getNumOfChildren(name) == 8 then |
31 | for i = 1, (getNumOfChildren(name) / 2) do |
32 | instance.shortHands[i] = getChildAt(name, i * 2 - 2); |
33 | instance.longHands[i] = getChildAt(name, i * 2 - 1); |
34 | end; |
35 | instance.init = true; |
36 | end; |
37 | |
38 | instance.soundId = createAudioSource("churchBellSample", "data/maps/sounds/churchBell01.wav", 300, 50, 1, 1); |
39 | link(name, instance.soundId); |
40 | setVisibility(instance.soundId, false); |
41 | |
42 | g_currentMission.environment:addMinuteChangeListener(instance); |
43 | |
44 | return instance; |
45 | end; |
46 | |
47 | function ChurchClock:delete() |
48 | if self.timerId ~= 0 then |
49 | removeTimer(self.timerId); |
50 | end; |
51 | if g_currentMission ~= nil and g_currentMission.environment ~= nil then |
52 | g_currentMission.environment:removeMinuteChangeListener(self); |
53 | end; |
54 | end; |
55 | |
56 | function ChurchClock:minuteChanged() |
57 | if self.init then |
58 | local shortHandRot = (2 * math.pi) * (g_currentMission.environment.dayTime / (1000 * 60 * 60 * 12)); |
59 | local longHandRot = (2 * math.pi) * (g_currentMission.environment.dayTime / (1000 * 60 * 60)); |
60 | |
61 | setRotation(self.shortHands[1], 0, 0, -shortHandRot); |
62 | setRotation(self.longHands[1], 0, 0, -longHandRot); |
63 | |
64 | setRotation(self.shortHands[2], -shortHandRot, 0, 0); |
65 | setRotation(self.longHands[2], -longHandRot, 0, 0); |
66 | |
67 | setRotation(self.shortHands[3], 0, 0, shortHandRot); |
68 | setRotation(self.longHands[3], 0, 0, longHandRot); |
69 | |
70 | setRotation(self.shortHands[4], shortHandRot, 0, 0); |
71 | setRotation(self.longHands[4], longHandRot, 0, 0); |
72 | |
73 | -- check if we're at the start of a new hour |
74 | if (math.floor(g_currentMission.environment.dayTime / (1000 * 60 * 60)) ~= self.lastHourTolled) then |
75 | |
76 | -- if the game just started, don't ring the bell (or it'll toll each time the player starts a game) |
77 | if self.lastHourTolled == 0 then |
78 | self.lastHourTolled = math.floor(g_currentMission.environment.dayTime / (1000 * 60 * 60)); |
79 | return; |
80 | end; |
81 | |
82 | self.lastHourTolled = math.floor(g_currentMission.environment.dayTime / (1000 * 60 * 60)); |
83 | if not self.waitingForBell then |
84 | self.tollCounter = self.lastHourTolled % 12; |
85 | if self.tollCounter == 0 then |
86 | self.tollCounter = 12; |
87 | end; |
88 | |
89 | self.tollCounter = self.tollCounter * 2 - 1; |
90 | local sampleDuration = getSampleDuration(getAudioSourceSample(self.soundId)); |
91 | self.timerId = addTimer(sampleDuration / 2, "churchTimerCallback", self); |
92 | setVisibility(self.soundId, true); |
93 | self.tollNow = false; |
94 | self.waitingForBell = true; |
95 | end; |
96 | end; |
97 | end; |
98 | end; |
99 | |
100 | function ChurchClock:churchTimerCallback() |
101 | if self.tollCounter > 0 then |
102 | -- alternate between turning sound on and off (i.e. making sound source visible/invisible) |
103 | setVisibility(self.soundId, self.tollNow); |
104 | self.tollNow = not self.tollNow; |
105 | self.tollCounter = self.tollCounter - 1; |
106 | return true; -- keep timer |
107 | end; |
108 | self.timerId = 0; |
109 | self.waitingForBell = false; |
110 | return false; -- remove timer |
111 | end;
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de