Advertisement
stasiu112

Untitled

Feb 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.44 KB | None | 0 0
  1. AnimationNetwort = {};
  2.  
  3. function AnimationNetwort.prerequisitesPresent(specializations)
  4.     return true;
  5. end;
  6.  
  7. function AnimationNetwort:load(savegame)
  8.  
  9.     self.setAnimationTime = SpecializationUtil.callSpecializationsFunction("setAnimationTime");
  10.    
  11.     self.animationParts = {};
  12.     local i = 0;
  13.     while true do
  14.         local partName = string.format("vehicle.animationParts.animationPart(%d)", i);
  15.         local animationPart = {};
  16.         local partStr = getXMLString(self.xmlFile, partName .. "#rootNode");
  17.         if partStr == nil then
  18.             break;
  19.         end;
  20.         animationPart.rootNode = Utils.indexToObject(self.components, partStr);
  21.         local charSet = getAnimCharacterSet(animationPart.rootNode);
  22.         if charSet == nil then
  23.             print("Error: invalid animation rootNode " .. partStr);
  24.             break;
  25.         else
  26.             animationPart.animCharSet = charSet;
  27.             animationPart.clip = getAnimClipIndex(animationPart.animCharSet, getXMLString(self.xmlFile, partName.."#clipName"));
  28.             assignAnimTrackClip(animationPart.animCharSet, 0, animationPart.clip);
  29.             animationPart.clipSpeed = Utils.getNoNil(getXMLFloat(self.xmlFile, partName .. "#clipSpeed"), 1);
  30.             setAnimTrackSpeedScale(animationPart.animCharSet, animationPart.clip, animationPart.clipSpeed);
  31.             animationPart.loop = Utils.getNoNil(getXMLBool(self.xmlFile, partName.."#loop"), false);
  32.             setAnimTrackLoopState(animationPart.animCharSet, 0, animationPart.loop);
  33.             animationPart.startPosition =  Utils.getNoNil(getXMLInt(self.xmlFile, partName .. "#startPosition"), 0);
  34.             animationPart.currentPosition = animationPart.startPosition;
  35.             setAnimTrackTime(animationPart.animCharSet, 0, animationPart.currentPosition);
  36.             animationPart.accerlation = Utils.getNoNil(getXMLFloat(self.xmlFile, partName .. "#accerlation"), 0);
  37.             animationPart.deAccerlation = Utils.getNoNil(getXMLFloat(self.xmlFile, partName .. "#deAccerlation"), 0)*-1;
  38.             animationPart.loopSpeed = 0;
  39.             local numJoints = Utils.getNoNil(getXMLInt(self.xmlFile, partName .. "#numJoints"), 0);
  40.             if numJoints > 0 then
  41.                 animationPart.joints = {};
  42.                 for j=1, numJoints do
  43.                     local jointString = string.format("%s.componentJoint%d", partName, j);
  44.                     local index = Utils.getNoNil(getXMLInt(self.xmlFile, jointString .."#index"), 0);
  45.                     if index == 0 then
  46.                         print("Error: Invalid ComponenteJointIndex: "..index.."");
  47.                         break;
  48.                     end;
  49.                     local jointIndex = self.componentJoints[index];
  50.                     setJointFrame(jointIndex.jointIndex, 0, jointIndex.jointNode);
  51.                     table.insert(animationPart.joints, jointIndex);
  52.                 end;
  53.             end;
  54.             animationPart.offSet = Utils.getNoNil(getXMLInt(self.xmlFile, partName .. "#offSet"), 50);
  55.             animationPart.loadSave = Utils.getNoNil(getXMLBool(self.xmlFile, partName.."#loadSave"), false);
  56.             animationPart.animDuration = getAnimClipDuration(animationPart.animCharSet, animationPart.clip);
  57.             if animationPart.currentPosition >= animationPart.animDuration then
  58.                 print("Error: Animation Part"..i.." startPosition larger or exakt then animation clip duration!");
  59.                 break;
  60.             end;
  61.             animationPart.animationEnabled = false;
  62.             animationPart.inputTime = animationPart.currentPosition;
  63.             animationPart.inputDone = false;
  64.             animationPart.clipEndTime = false;
  65.             animationPart.clipStartTime = false;
  66.             animationPart.isLoading = false;
  67.             table.insert(self.animationParts, animationPart);
  68.         end;
  69.         i = i + 1;
  70.     end;
  71. end;
  72.  
  73. function AnimationNetwort:delete()
  74. end;
  75.  
  76. function AnimationNetwort:getSaveAttributesAndNodes(nodeIdent)
  77.     local attributes = nil;
  78.     local numToSave = 0;
  79.     for _, animationPart in pairs(self.animationParts) do
  80.         if animationPart.loadSave then
  81.             numToSave = numToSave + 1;
  82.         end;
  83.     end;
  84.     for k, animationPart in pairs(self.animationParts) do
  85.         if animationPart.loadSave then
  86.             local minTime = math.max(animationPart.currentPosition, 0);
  87.             local maxTime = math.min(minTime, animationPart.animDuration);
  88.             local currentTime = string.format("%d", maxTime);
  89.             local saveAttributes = "animation" .. k .. "=\"" .. currentTime .. "\"";
  90.             if numToSave > 1 and attributes ~= nil then
  91.                 attributes = attributes .. " " .. saveAttributes;
  92.             else
  93.                 attributes = saveAttributes;
  94.             end;
  95.         end;
  96.     end;
  97.     return attributes, nil;
  98. end;
  99.  
  100. function AnimationNetwort:postLoad(savegame)
  101.     if savegame ~= nil then
  102.         for k, animationPart in pairs(self.animationParts) do
  103.             local keyString = string.format("#animation%d", k);
  104.             local inputTime = Utils.getNoNil(getXMLInt(savegame.xmlFile, savegame.key .. keyString), animationPart.startPosition);
  105.             self:setAnimationTime(k, inputTime, true);
  106.             animationPart.isLoading = animationPart.loadSave;
  107.         end;
  108.     end;
  109. end;
  110.  
  111. function AnimationNetwort:readStream(streamId, connection)
  112.     for k, animationPart in pairs(self.animationParts) do
  113.         local timeInput = streamReadInt32(streamId);
  114.         self:setAnimationTime(k, timeInput, true);
  115.         animationPart.isLoading = true;
  116.     end;
  117. end;
  118.  
  119. function AnimationNetwort:writeStream(streamId, connection)
  120.     for k, animationPart in pairs(self.animationParts) do
  121.         streamWriteInt32(streamId, animationPart.inputTime);
  122.     end;
  123. end;
  124.  
  125. function AnimationNetwort:mouseEvent(posX, posY, isDown, isUp, button)
  126. end;
  127.  
  128. function AnimationNetwort:keyEvent(unicode, sym, modifier, isDown)
  129. end;
  130.  
  131. function AnimationNetwort:update(dt)
  132. end;
  133.  
  134. function AnimationNetwort:updateTick(dt)
  135.     for k, animationPart in pairs(self.animationParts) do
  136.         if animationPart.animationEnabled then
  137.             local currentTime = animationPart.currentPosition;
  138.             local timeInput = animationPart.inputTime;
  139.             local clipSpeed = animationPart.clipSpeed;
  140.             local loopState = animationPart.loop;
  141.             local duration = animationPart.animDuration;
  142.             local accerlationSpeed = animationPart.loopSpeed;
  143.             if animationPart.isLoading then
  144.                 clipSpeed = animationPart.clipSpeed*animationPart.offSet;
  145.                 animationPart.isLoading = false;
  146.             end;
  147.             if loopState == true then
  148.                 if timeInput ~= 0 then
  149.                     if animationPart.accerlation ~= 0 then
  150.                         accerlationSpeed = math.min(accerlationSpeed+((accerlationSpeed+animationPart.accerlation)*animationPart.accerlation), clipSpeed);
  151.                         setAnimTrackSpeedScale(animationPart.animCharSet, animationPart.clip, accerlationSpeed);
  152.                         if accerlationSpeed >= clipSpeed then
  153.                             animationPart.inputDone = true;
  154.                             animationPart.loopSpeed = clipSpeed;
  155.                         else
  156.                             animationPart.loopSpeed = accerlationSpeed;
  157.                         end;
  158.                     else
  159.                         setAnimTrackSpeedScale(animationPart.animCharSet, animationPart.clip, clipSpeed);
  160.                         animationPart.inputDone = true;
  161.                     end;
  162.                 elseif timeInput == 0 then
  163.                     if animationPart.deAccerlation ~= 0 then
  164.                         accerlationSpeed = math.max(accerlationSpeed+(accerlationSpeed*animationPart.deAccerlation), 0);
  165.                         setAnimTrackSpeedScale(animationPart.animCharSet, animationPart.clip, accerlationSpeed);
  166.                         if accerlationSpeed <= 0 then
  167.                             animationPart.inputDone = true;
  168.                             animationPart.loopSpeed = 0;
  169.                         else
  170.                             animationPart.loopSpeed = accerlationSpeed;
  171.                         end;
  172.                     else
  173.                         setAnimTrackSpeedScale(animationPart.animCharSet, animationPart.clip, 0);
  174.                         disableAnimTrack(animationPart.animCharSet, animationPart.clip);
  175.                         animationPart.inputDone = true;
  176.                     end;
  177.                 end;
  178.             elseif loopState == false then
  179.                 if currentTime < timeInput and animationPart.inputDone == false then
  180.                     setAnimTrackSpeedScale(animationPart.animCharSet, animationPart.clip, clipSpeed);
  181.                     if currentTime+animationPart.offSet >= timeInput then
  182.                         animationPart.inputDone = true;
  183.                         setAnimTrackTime(animationPart.animCharSet, animationPart.clip, timeInput);
  184.                         setAnimTrackSpeedScale(animationPart.animCharSet, animationPart.clip, 0);
  185.                     end;
  186.                 elseif currentTime > timeInput and animationPart.inputDone == false then
  187.                     setAnimTrackSpeedScale(animationPart.animCharSet, animationPart.clip, -clipSpeed);
  188.                     if currentTime-animationPart.offSet <= timeInput then
  189.                         animationPart.inputDone = true;
  190.                         setAnimTrackTime(animationPart.animCharSet, animationPart.clip, timeInput);
  191.                         setAnimTrackSpeedScale(animationPart.animCharSet, animationPart.clip, 0);
  192.                     end;
  193.                 end;
  194.                 if currentTime >= duration-animationPart.offSet then
  195.                     animationPart.clipEndTime = true;
  196.                 elseif currentTime <= animationPart.offSet then
  197.                     animationPart.clipStartTime = true;
  198.                 elseif currentTime == animationPart.startPosition then
  199.                     animationPart.clipStartTime = true;
  200.                 else
  201.                     animationPart.clipStartTime = false;
  202.                     animationPart.clipEndTime = false;
  203.                 end;
  204.             end;
  205.             if animationPart.joints ~= nil then
  206.                 for k, joint in pairs(animationPart.joints) do
  207.                     setJointFrame(joint.jointIndex, 0, joint.jointNode);
  208.                 end;
  209.             end;
  210.             animationPart.currentPosition = getAnimTrackTime(animationPart.animCharSet, animationPart.clip);
  211.         else
  212.             enableAnimTrack(animationPart.animCharSet, animationPart.clip);
  213.         end;
  214.         animationPart.animationEnabled = isAnimTrackEnabled(animationPart.animCharSet, animationPart.clip);
  215.     end;
  216. end;
  217.  
  218. function AnimationNetwort:setAnimationTime(animationPart, timeInput, noEventSend)
  219.     local minTime = math.max(timeInput, 0);
  220.     local maxTime = math.min(minTime, self.animationParts[animationPart].animDuration);
  221.     SetAnimationEvent.sendEvent(self, animationPart, maxTime, noEventSend);
  222.     if maxTime ~= self.animationParts[animationPart].currentPosition then
  223.         self.animationParts[animationPart].inputDone = false;
  224.         self.animationParts[animationPart].inputTime = maxTime;
  225.     end;
  226. end;
  227.  
  228. function AnimationNetwort:draw()   
  229. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement