Action denied

Vehicles/VehicleScripts/AnimatedParts.lua

AnimatedParts allows to add doors or other animated parts to a vehicle. The player can then click onto these parts to open or close them (one animation per part allowed).

  25  AnimatedParts                       = AnimatedParts or {};

AnimatedParts:load(dataTable)

Initializes this vehicle script and fetches some data from the vehicle's configuration.

  29  function AnimatedParts:load(dataTable)
  30      -- currently only supports doors
  31      self.setAnimatedPartState       = VehicleManager:newFunction("setAnimatedPartState");
  32  
  33      self.animatedParts              = {};
  34  
  35      if dataTable.animatedParts ~= nil then
  36          for k, v in pairs(dataTable.animatedParts) do
  37              local id                = getChild(self.id, v.index or "");
  38  
  39              if id ~= 0 then
  40                  local ap            = {};
  41                  local key           = #self.animatedParts + 1;
  42                  ap.id               = id;
  43                  ap.colId            = v.collision == nil and id or getChild(self.id, v.collision or "");
  44                  ap.animationLength  = Animation.getLength(ap.id);
  45  
  46                  ap.textOff          = l10n.getDollar(v.textOff  or "textOff");
  47                  ap.textOn           = l10n.getDollar(v.textOn   or "textOn");
  48                  ap.state            = v.defaultState or false;
  49                  ap.targetPosition   = ap.state and ap.animationLength or 0;
  50                  ap.position         = ap.targetPosition;
  51                  ap.onEnterState     = v.onEnterState;
  52                  ap.onLeaveState     = v.onLeaveState;
  53  
  54                  Animation.sampleTime(ap.id, ap.position);
  55  
  56                  ControlElement.new(ap.colId);
  57                  ControlElement.setCallback(ap.colId, 1, function() self:setAnimatedPartState(key, not ap.state); end);
  58                  ControlElement.setName(ap.colId, ap.state and ap.textOff or ap.textOn);
  59  
  60                  table.insert(self.animatedParts, ap);
  61              end;
  62          end;
  63      end;
  64  end;

AnimatedParts:saveToTable(tbl)

Writes the current states of all animated parts to the savegame.

  68  function AnimatedParts:saveToTable(tbl)
  69      if tbl == nil then return end;
  70  
  71      tbl.animatedParts               = {};
  72      for k, ap in pairs(self.animatedParts) do
  73          tbl.animatedParts[k]        = {
  74              state                   = ap.state or false,
  75              position                = ap.position,
  76          };
  77      end;
  78  end;

AnimatedParts:loadFromTable(tbl)

Reads the current states of our animated parts from the savegame.

  82  function AnimatedParts:loadFromTable(tbl)
  83      if tbl == nil then return end;
  84      if tbl.animatedParts == nil then return end;
  85  
  86      for k, ap in pairs(self.animatedParts) do
  87          local data                  = tbl.animatedParts[k];
  88  
  89          if data ~= nil then
  90              ap.state                = getNoNil(data.state,      ap.state);
  91              ap.position             = getNoNil(data.position,   ap.position);
  92  
  93              self:setAnimatedPartState(k, ap.state);
  94  
  95              -- force sample time
  96              Animation.sampleTime(ap.id, ap.position);
  97          end;
  98      end;
  99  end;

AnimatedParts:setAnimatedPartState(key, value)

Call this function to move the animated part to the specified position. key is the animated part's index, value (bool) is the target position (true or false). For doors, true usually represents open.

 103  function AnimatedParts:setAnimatedPartState(key, value)
 104      local ap                        = self.animatedParts[key];
 105  
 106      if ap == nil then return end;
 107  
 108      ap.state                        = value;
 109      ap.targetPosition               = ap.state and ap.animationLength or 0;
 110      ControlElement.setName(ap.colId, ap.state and ap.textOff or ap.textOn);
 111  end;

AnimatedParts:onEnter()

For some vehicles, it makes sense to open or close doors (depending on the animated part's onEnterState) when the player enters the vehicle.

 115  function AnimatedParts:onEnter()
 116      if not GameplaySettings.autoCloseDoors then
 117          return;
 118      end;
 119  
 120      for key, ap in pairs(self.animatedParts) do
 121          if ap.onEnterState ~= nil then
 122              self:setAnimatedPartState(key, ap.onEnterState);
 123          end;
 124      end;
 125  end;

AnimatedParts:onLeave()

For some vehicles, it can also make sense to open or close doors (depending on the animated part's onLeaveState) when the player leaves the vehicle.

 129  function AnimatedParts:onLeave()
 130      if not GameplaySettings.autoCloseDoors then
 131          return;
 132      end;
 133      
 134      for key, ap in pairs(self.animatedParts) do
 135          if ap.onLeaveState ~= nil then
 136              self:setAnimatedPartState(key, ap.onLeaveState);
 137          end;
 138      end;
 139  end;

AnimatedParts:update(dt)

Updates the door positions every frame, and if necessary, resamples the animation.

 143  function AnimatedParts:update(dt)
 144      for key, ap in pairs(self.animatedParts) do
 145          if ap.position ~= ap.targetPosition then
 146              ap.position             = Utils.moveTowards(ap.position, ap.targetPosition, dt);
 147              Animation.sampleTime(ap.id, ap.position);
 148          end;
 149      end;
 150  end;

All contents of this page may be used for modding use for Winter Resort Simulator only. Any use exceeding this regulation is not permitted.

Copyright (C) HR Innoways, 2020. All Rights Reserved.