Advertisement
Guest User

hkl

a guest
Sep 4th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.32 KB | None | 0 0
  1.  
  2. HKL = {};
  3.  
  4. function HKL.prerequisitesPresent(specializations)
  5. Vehicle.registerJointType("HookJoint");
  6. print("register joint type: HookJoint");
  7. return true;
  8. end;
  9.  
  10. function HKL:load(xmlFile)
  11.  
  12. self.isSelectable = true;
  13. self.setRpmUp = SpecializationUtil.callSpecializationsFunction("setRpmUp");
  14.  
  15. local hydraulicsCount = Utils.getNoNil(getXMLInt(xmlFile, "vehicle.hydraulics#count"), 0);
  16. self.hydraulics = {};
  17. for i=1, hydraulicsCount do
  18. local hydraulicName = string.format("vehicle.hydraulics.hydraulic%d", i);
  19. self.hydraulics[i] = {};
  20. self.hydraulics[i].node = Utils.indexToObject(self.components, getXMLString(xmlFile, hydraulicName .. "#index"));
  21. self.hydraulics[i].punch = Utils.indexToObject(self.components, getXMLString(xmlFile, hydraulicName .. "#punch"));
  22. self.hydraulics[i].translationPunch = Utils.indexToObject(self.components, getXMLString(xmlFile, hydraulicName .. "#punchFixpoint"));
  23. self.hydraulics[i].fixPoint = Utils.indexToObject(self.components, getXMLString(xmlFile, hydraulicName .. "#fixpoint"));
  24. if self.hydraulics[i].punch ~= nil and self.hydraulics[i].translationPunch ~= nil then
  25. local ax, ay, az = getWorldTranslation(self.hydraulics[i].punch);
  26. local bx, by, bz = getWorldTranslation(self.hydraulics[i].translationPunch);
  27. self.hydraulics[i].punchDistance = Utils.vector3Length(ax-bx, ay-by, az-bz);
  28. end;
  29. end;
  30.  
  31. local tipAnimRootNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.tipAnimation#rootNode"));
  32. self.tipAnimCharSet = 0;
  33. if tipAnimRootNode ~= nil and tipAnimRootNode ~= 0 then
  34. self.tipAnimCharSet = getAnimCharacterSet(tipAnimRootNode);
  35. if self.tipAnimCharSet ~= 0 then
  36. local clip = getAnimClipIndex(self.tipAnimCharSet, getXMLString(xmlFile, "vehicle.tipAnimation#clip"));
  37. assignAnimTrackClip(self.tipAnimCharSet, 0, clip);
  38. setAnimTrackLoopState(self.tipAnimCharSet, 0, false);
  39. self.tipAnimSpeedScale = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.tipAnimation#speedScale"), 1);
  40. self.tipAnimDuration = getAnimClipDuration(self.tipAnimCharSet, clip);
  41. end;
  42. end;
  43. self.tipReferencePoint = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.tipReferencePoint#index"));
  44.  
  45. self.attacherJoints[1].jointType = Vehicle.JOINTTYPE_HOOKJOINT;
  46. self.firstAttachOnLoad = false;
  47. self.firstAttachTime = 4000;
  48. self.attachableObject = nil;
  49. self.isHakenliftTrailer = true;
  50. self.saveMinRpm = 0;
  51.  
  52. self.objectTipAnimDuration = nil;
  53. self.objectTipAnimSpeedScale = nil;
  54. self.objectTipDischargeEndTime = nil;
  55. self.objectTipState = nil;
  56. self.objectTipReferencePoint = nil;
  57. self.tipState = 0;
  58.  
  59. end;
  60.  
  61. function HKL:delete()
  62. end;
  63.  
  64. function HKL:readStream(streamId, connection)
  65.  
  66. end;
  67.  
  68. function HKL:writeStream(streamId, connection)
  69.  
  70. end;
  71.  
  72. function HKL:mouseEvent(posX, posY, isDown, isUp, button)
  73.  
  74. end;
  75.  
  76. function HKL:keyEvent(unicode, sym, modifier, isDown)
  77.  
  78. end;
  79.  
  80. function HKL:update(dt)
  81.  
  82. if self.firstAttachTime > 0 then
  83. self.firstAttachTime = self.firstAttachTime-dt;
  84. if not self.firstAttachOnLoad then
  85. local attacherJoint = self.attacherJoints[1];
  86. if attacherJoint.jointIndex == 0 then
  87. for k, attachable in pairs(g_currentMission.attachables) do
  88. if attachable.attacherVehicle == nil and attachable.attacherJoint.jointType == attacherJoint.jointType then
  89. local px, py, pz = getWorldTranslation(attacherJoint.jointTransform);
  90. local vx, vy, vz = getWorldTranslation(attachable.attacherJoint.node);
  91. local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
  92. if distance < 0.6 then
  93. self:attachImplement(attachable, 1);
  94. self.firstAttachOnLoad = true;
  95. end;
  96. end;
  97. end;
  98. end;
  99. end;
  100. end;
  101.  
  102. if self:getIsActiveForInput() then
  103. local isKeyEvent = false;
  104. if self.tipState == 0 then
  105. if InputBinding.isPressed(InputBinding.HKL_DOWN1) and self:getIsActiveForInput() then
  106. self:setAnimationTime(1, self.animationParts[1].currentPosition+(self.animationParts[1].offSet*(dt/5)));
  107. isKeyEvent = true;
  108. elseif InputBinding.isPressed(InputBinding.HKL_UP1) and self:getIsActiveForInput() then
  109. self:setAnimationTime(1, self.animationParts[1].currentPosition-(self.animationParts[1].offSet*(dt/5)));
  110. isKeyEvent = true;
  111. end;
  112. end;
  113. self:setRpmUp(dt, isKeyEvent);
  114. end;
  115.  
  116.  
  117. setJointFrame(self.attacherJoints[1].jointIndex, 0, self.attacherJoints[1].jointTransform);
  118. local jointDesc = self.attacherJoints[1];
  119. local jointLimit1 = (50/2000)*math.max(self.animationParts[1].currentPosition-2750, 0);
  120. local jointLimit2 = (130/2000)*math.max(self.animationParts[1].currentPosition-2750, 0);
  121. setJointRotationLimit(jointDesc.jointIndex, 0, true, Utils.degToRad(-0), Utils.degToRad(0));
  122. setJointRotationLimit(jointDesc.jointIndex, 1, true, Utils.degToRad(-jointLimit1), Utils.degToRad(jointLimit1));
  123. setJointRotationLimit(jointDesc.jointIndex, 2, true, Utils.degToRad(-jointLimit2), Utils.degToRad(jointLimit2));
  124.  
  125. for k, implements in pairs(self.attachedImplements) do
  126. local implement = implements.object;
  127. if implement.isHakenliftObject then
  128. if self.animationParts[1].clipStartTime then
  129. implement.tipReferencePoint = self.tipReferencePoint;
  130. else
  131. implement.tipReferencePoint = nil;
  132. end;
  133. self.objectTipAnimDuration = implement.tipAnimations[1].animationDuration;
  134. self.objectTipAnimSpeedScale = implement.tipAnimations[1].animationOpenSpeedScale;
  135. self.objectTipDischargeEndTime = implement.tipAnimations[1].dischargeEndTime ;
  136. self.objectTipState = implement.tipState;
  137. self.tipState = implement.tipState;
  138. else
  139. self.objectTipAnimDuration = nil;
  140. self.objectTipAnimSpeedScale = nil;
  141. self.objectTipDischargeEndTime = nil;
  142. self.objectTipState = nil;
  143. end;
  144. end;
  145. if self.objectTipAnimDuration ~= nil and self.objectTipAnimSpeedScale ~= nil and self.objectTipDischargeEndTime ~= nil and self.objectTipState ~= nil then
  146. local animDuration = self.objectTipAnimDuration;
  147. local speedScale = self.objectTipAnimSpeedScale;
  148. self.tipAnimSpeedScale = self.tipAnimDuration/(animDuration/speedScale);
  149. if self.objectTipState == Trailer.TIPSTATE_CLOSING then
  150. if self.tipAnimCharSet ~= 0 then
  151. if getAnimTrackTime(self.tipAnimCharSet, 0) > self.tipAnimDuration then
  152. setAnimTrackTime(self.tipAnimCharSet, 0, self.tipAnimDuration);
  153. end;
  154. setAnimTrackSpeedScale(self.tipAnimCharSet, 0, -self.tipAnimSpeedScale);
  155. enableAnimTrack(self.tipAnimCharSet, 0);
  156. end;
  157. elseif self.objectTipState == Trailer.TIPSTATE_OPENING then
  158. if self.tipAnimCharSet ~= 0 then
  159. if getAnimTrackTime(self.tipAnimCharSet, 0) < 0.0 then
  160. setAnimTrackTime(self.tipAnimCharSet, 0, 0.0);
  161. end;
  162. setAnimTrackSpeedScale(self.tipAnimCharSet, 0, self.tipAnimSpeedScale);
  163. enableAnimTrack(self.tipAnimCharSet, 0);
  164. end;
  165. elseif self.objectTipState == Trailer.TIPSTATE_CLOSED then
  166. disableAnimTrack(self.tipAnimCharSet, 0);
  167. end;
  168. else
  169. setAnimTrackTime(self.tipAnimCharSet, 0, getAnimTrackTime(self.tipAnimCharSet, 0));
  170. setAnimTrackSpeedScale(self.tipAnimCharSet, 0, -self.tipAnimSpeedScale);
  171. enableAnimTrack(self.tipAnimCharSet, 0);
  172. if getAnimTrackTime(self.tipAnimCharSet, 0) <= 0.0 then
  173. disableAnimTrack(self.tipAnimCharSet, 0);
  174. end;
  175. end;
  176. if self.updateSteeringAxleAngle then
  177. if self.attacherVehicle ~= nil and self.movingDirection < 0 then
  178. local x,y,z = worldDirectionToLocal(self.steeringAxleNode, localDirectionToWorld(self.attacherVehicle.steeringAxleNode, 0, 0, 1));
  179. local dot = z;
  180. dot = dot / Utils.vector2Length(x,z);
  181. local angle = math.acos(dot);
  182. if x < 0 then
  183. angle = -angle;
  184. end;
  185. local startSpeed = self.steeringAxleAngleScaleStart;
  186. local endSpeed = self.steeringAxleAngleScaleEnd;
  187. local scale = Utils.clamp(1 + (self.lastSpeed*-3600-startSpeed) * 1.0/(startSpeed-endSpeed), 0, 1);
  188. self.steeringAxleAngle = angle*scale;
  189. end;
  190. end;
  191. end;
  192.  
  193. function HKL:updateTick(dt)
  194.  
  195. for i=1, table.getn(self.hydraulics) do
  196. local ax, ay, az = getWorldTranslation(self.hydraulics[i].node);
  197. local bx, by, bz = getWorldTranslation(self.hydraulics[i].fixPoint);
  198. if bx ~= nil and by ~= nil and bz ~= nil then
  199. local x, y, z = worldDirectionToLocal(getParent(self.hydraulics[i].node), bx-ax, by-ay, bz-az);
  200. setDirection(self.hydraulics[i].node, x, y, z, 0, 0, 1);
  201. local distance = Utils.vector3Length(ax-bx, ay-by, az-bz);
  202. if self.hydraulics[i].punch ~= nil and self.hydraulics[i].punchDistance ~= nil and self.hydraulics[i].translationPunch then
  203. if self.hydraulics[i].doScale then
  204. local xScale, yScale, zScale = getScale(self.hydraulics[i].punch);
  205. local newScale = xScale * (distance / self.hydraulics[i].punchDistance);
  206. setScale(self.hydraulics[i].punch, 1, 1, newScale);
  207. else
  208. setTranslation(self.hydraulics[i].punch, 0, 0, distance-self.hydraulics[i].punchDistance);
  209. end;
  210. end;
  211. end;
  212. end;
  213. end;
  214.  
  215. function HKL:setRpmUp(dt, isActive)
  216. if self.attacherVehicle ~= nil and self.saveMinRpm ~= 0 then
  217. if dt ~= nil then
  218. if isActive == true then
  219. self.attacherVehicle.motor.minRpm = math.max(self.attacherVehicle.motor.minRpm-dt, -1000);
  220. else
  221. self.attacherVehicle.motor.minRpm = math.min(self.attacherVehicle.motor.minRpm+dt*2, self.saveMinRpm);
  222. end;
  223. else
  224. self.attacherVehicle.motor.minRpm = self.saveMinRpm;
  225. end;
  226. end;
  227. end;
  228.  
  229. function HKL:onAttach(attacherVehicle)
  230. self.attacherVehicle = attacherVehicle;
  231. if self.attacherVehicleCopy == nil then
  232. self.attacherVehicleCopy = self.attacherVehicle;
  233. end;
  234. self.saveMinRpm = self.attacherVehicle.motor.minRpm;
  235. end;
  236.  
  237. function HKL:onDetach()
  238. if self.deactivateOnDetach then
  239. self:onDeactivate(self);
  240. else
  241. self:onDeactivateSounds(self);
  242. end;
  243. for k, steerable in pairs(g_currentMission.steerables) do
  244. if self.attacherVehicleCopy == steerable then
  245. steerable.motor.minRpm = self.saveMinRpm;
  246. self.attacherVehicleCopy = nil;
  247. end;
  248. end;
  249. end;
  250.  
  251. function HKL:draw()
  252.  
  253. if self.animationParts[1].clip then
  254. g_currentMission:addHelpButtonText(string.format(g_i18n:getText("HKL_UP1")), InputBinding.HKL_UP1);
  255. g_currentMission:addHelpButtonText(string.format(g_i18n:getText("HKL_DOWN1")), InputBinding.HKL_DOWN1);
  256. end;
  257.  
  258. end;
  259.  
  260. function HKL:validateAttacherJoint(HookJoint, jointDesc, dt)
  261. return true;
  262. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement