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 | |
3 | ObjectSpawner = {} |
4 | ObjectSpawner_mt = Class(ObjectSpawner); |
5 | |
6 | function ObjectSpawner:new(spawnFunction, numObjectsTarget, numSpawnsPerSecond, spawnMinRadius, spawnMaxRadius, destroyRadius, warmupCameraMove, warmupPercentage, updateFunction) |
7 | self = {}; |
8 | setmetatable(self, ObjectSpawner_mt); |
9 | |
10 | self.spawnFunction = spawnFunction; |
11 | self.updateFunction = updateFunction; |
12 | self.numObjectsTarget = numObjectsTarget; |
13 | self.spawnMinRadius = spawnMinRadius; |
14 | self.spawnMaxRadius = spawnMaxRadius; |
15 | self.destroyRadius = destroyRadius; |
16 | self.destroyRadiusSq = self.destroyRadius*self.destroyRadius; |
17 | self.warmupCameraMove = warmupCameraMove; |
18 | self.warmupCameraMoveSq = warmupCameraMove*warmupCameraMove; |
19 | self.warmupPercentage = warmupPercentage; |
20 | self.spawnInterval = 1/(numSpawnsPerSecond*0.001); |
21 | self.spawnDt = 0; |
22 | self.isWarmedUp = false; |
23 | |
24 | self.lastCx = 0; |
25 | self.lastCz = 0; |
26 | |
27 | self.objects = {}; |
28 | self.numObjects = 0; |
29 | |
30 | self.objectsCache = {}; |
31 | |
32 | return self; |
33 | end; |
34 | |
35 | function ObjectSpawner:delete() |
36 | for _, object in pairs(self.objects) do |
37 | if not object.isDeleted then |
38 | object:delete(); |
39 | end; |
40 | end; |
41 | for _, object in pairs(self.objectsCache) do |
42 | if not object.isDeleted then |
43 | object:delete(); |
44 | end; |
45 | end; |
46 | end; |
47 | |
48 | function ObjectSpawner:update(dt) |
49 | local camera = getCamera(); |
50 | local cx,cy,cz = getWorldTranslation(camera); |
51 | |
52 | |
53 | if self.updateFunction ~= nil then |
54 | self.updateFunction(self, dt); |
55 | else |
56 | for k, object in pairs(self.objects) do |
57 | if not object.isDeleted then |
58 | object:update(dt); |
59 | end; |
60 | end; |
61 | end; |
62 | for k, object in pairs(self.objects) do |
63 | if object.isDeleted then |
64 | self.objects[k] = nil; |
65 | self.numObjects = self.numObjects - 1; |
66 | else |
67 | if object.requestDelete or object:getSquaredDistanceFrom(cx, cz) > self.destroyRadiusSq then |
68 | object:removeFromScene(); |
69 | self.objects[k] = nil; |
70 | self.numObjects = self.numObjects - 1; |
71 | table.insert(self.objectsCache, object); |
72 | end; |
73 | end; |
74 | end; |
75 | |
76 | local doWarmup = not self.isWarmedUp; |
77 | self.isWarmedUp = true; |
78 | if not doWarmup then |
79 | local dx = cx - self.lastCx; |
80 | local dz = cz - self.lastCz; |
81 | if dx*dx + dz*dz > self.warmupCameraMoveSq then |
82 | doWarmup = true; |
83 | end; |
84 | end; |
85 | self.lastCx = cx; |
86 | self.lastCz = cz; |
87 | |
88 | self.spawnDt = self.spawnDt + dt; |
89 | if doWarmup or self.spawnDt > self.spawnInterval then |
90 | local numAdded = 0; |
91 | local numObjectsToAdd = 1; |
92 | if doWarmup then |
93 | numObjectsToAdd = math.ceil(self.warmupPercentage*self.numObjectsTarget); |
94 | end; |
95 | numObjectsToAdd = math.min(self.numObjectsTarget-self.numObjects, numObjectsToAdd); |
96 | for i=1, numObjectsToAdd do |
97 | local object = self.spawnFunction(self, doWarmup, cx, cy, cz); |
98 | if object ~= nil then |
99 | self.objects[object] = object; |
100 | self.numObjects = self.numObjects + 1; |
101 | numAdded = numAdded + 1; |
102 | end; |
103 | end; |
104 | if numAdded > 0 or numObjectsToAdd == 0 then |
105 | self.spawnDt = 0; |
106 | else |
107 | self.spawnDt = self.spawnInterval * 0.75; |
108 | end; |
109 | end; |
110 | end; |
111 | |
112 | function ObjectSpawner:getObjectFromCache() |
113 | local numObjects = table.getn(self.objectsCache); |
114 | while numObjects > 0 do |
115 | local object = self.objectsCache[numObjects]; |
116 | table.remove(self.objectsCache, numObjects); |
117 | numObjects = numObjects - 1; |
118 | if not object.isDeleted then |
119 | return object; |
120 | end; |
121 | end; |
122 | return nil; |
123 | end;
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de