Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1 | -- |
2 | -- Barn Mover Trigger |
3 | -- |
4 | -- @author Christian Ammann |
5 | -- @date 29/12/08 |
6 | -- |
7 | -- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved. |
8 | |
9 | BarnMoverTrigger = {}; |
10 | |
11 | local BarnMoverTrigger_mt = Class(BarnMoverTrigger); |
12 | |
13 | function BarnMoverTrigger:onCreate(id) |
14 | g_currentMission:addUpdateable(BarnMoverTrigger:new(id)); |
15 | --print("created barn mover trigger, id: ", id); |
16 | end; |
17 | |
18 | function BarnMoverTrigger:new(id, customMt) |
19 | |
20 | local instance = {}; |
21 | if customMt ~= nil then |
22 | setmetatable(instance, customMt); |
23 | else |
24 | setmetatable(instance, BarnMoverTrigger_mt); |
25 | end; |
26 | |
27 | instance.triggerId = getChildAt(id, 0); |
28 | instance.triggerTargetId = getChildAt(id, 1); |
29 | if g_currentMission:getIsServer() then |
30 | addTrigger(instance.triggerId, "triggerCallback", instance); |
31 | end; |
32 | addTrigger(instance.triggerTargetId, "triggerCallbackTarget", instance); |
33 | |
34 | instance.dirLength = 0.008; |
35 | instance.dirX, instance.dirY, instance.dirZ = localDirectionToWorld(instance.triggerId, 0, 0, 1); |
36 | instance.dirX = instance.dirX*instance.dirLength; |
37 | instance.dirY = instance.dirY*instance.dirLength; |
38 | instance.dirZ = instance.dirZ*instance.dirLength; |
39 | instance.targetVelocity = 2; |
40 | |
41 | instance.touched = {}; |
42 | |
43 | return instance; |
44 | end; |
45 | |
46 | function BarnMoverTrigger:delete() |
47 | |
48 | if g_currentMission:getIsServer() then |
49 | removeTrigger(self.triggerId); |
50 | end; |
51 | removeTrigger(self.triggerTargetId); |
52 | |
53 | end; |
54 | |
55 | function BarnMoverTrigger:update(dt) |
56 | |
57 | for k,touched in pairs(self.touched) do |
58 | local vx, vy, vz = getLinearVelocity(k); |
59 | local dot = vx*self.dirX + vy*self.dirY + vz*self.dirZ; |
60 | local v = dot/self.dirLength; |
61 | |
62 | if v < self.targetVelocity then |
63 | local scale = dt*touched.mass; |
64 | addForce(k, self.dirX*dt, self.dirY*dt, self.dirZ*dt, 0, 0, 0, true); |
65 | end; |
66 | end; |
67 | |
68 | end; |
69 | |
70 | function BarnMoverTrigger:triggerCallback(triggerId, otherId, onEnter, onLeave, onStay) |
71 | |
72 | if onEnter then |
73 | local touched = self.touched[otherId]; |
74 | if touched ~= nil then |
75 | touched.count = touched.count+1; |
76 | else |
77 | local mass = getMass(otherId); |
78 | self.touched[otherId] = {mass=mass, count=1}; |
79 | end; |
80 | elseif onLeave then |
81 | local touched = self.touched[otherId]; |
82 | if touched ~= nil then |
83 | if touched.count > 1 then |
84 | touched.count = touched.count-1; |
85 | else |
86 | self.touched[otherId] = nil; |
87 | end; |
88 | end; |
89 | end; |
90 | |
91 | end; |
92 | |
93 | function BarnMoverTrigger:triggerCallbackTarget(triggerId, otherId, onEnter, onLeave, onStay) |
94 | |
95 | if onEnter then |
96 | -- this happens, if a compound child of a deleted compound is entering |
97 | if otherId ~= 0 then |
98 | local object = g_currentMission:getNodeObject(otherId); |
99 | --local isStrawbale = getUserAttribute(otherId, "isStrawbale"); |
100 | --local isHaybale = getUserAttribute(otherId, "isHaybale"); |
101 | |
102 | self.touched[otherId] = nil; |
103 | --if (isStrawbale ~= nil and isStrawbale) or (isHaybale ~= nil and isHaybale) then |
104 | if object ~= nil then |
105 | if object:isa(Bale) then |
106 | playSample(g_currentMission.cashRegistrySound, 1, 1, 0); |
107 | |
108 | if g_currentMission:getIsServer() then |
109 | --print("Bale stored: ", tostring(otherId)); |
110 | local difficultyMultiplier = 2 ^ (3 - g_currentMission.missionStats.difficulty); |
111 | local baseValue = object:getValue(); --400; |
112 | --if isHaybale ~= nil and isHaybale then |
113 | -- baseValue = 800; |
114 | --end; |
115 | g_currentMission:addSharedMoney(baseValue * difficultyMultiplier, "other"); |
116 | g_currentMission:addMoneyChange(baseValue * difficultyMultiplier, FSBaseMission.MONEY_TYPE_SINGLE, true); |
117 | object:delete(); |
118 | end; |
119 | elseif not object:isa(Vehicle) then -- do not delete vehicles, but everything else |
120 | if g_currentMission:getIsServer() then |
121 | object:delete(); |
122 | end; |
123 | end; |
124 | else |
125 | if g_currentMission.nodeToVehicle[otherId] == nil then |
126 | -- there is nothing that could end up here, since physics objects are an object, thus this could only introduce bugs |
127 | --delete(otherId); |
128 | end; |
129 | end; |
130 | |
131 | end; |
132 | end; |
133 | |
134 | end;
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de