Action denied

Vehicles/VehicleScripts/SnowcatParticles.lua

This script implements particle systems for snowcates such as groomer or snow blade particles.

  24  SnowcatParticles                = SnowcatParticles or {};

SnowcatParticles:load(dataTable)

Load the configuration data of the particle systems.

  28  function SnowcatParticles:load(dataTable)
  29      -- load snow groomer
  30      self.snowGroomerParticles           = {};
  31  
  32      if dataTable.snowGroomerParticles ~= nil then
  33          for k, v in pairs(dataTable.snowGroomerParticles) do
  34              local id                    = getChild(self.id, v.index);
  35              if id ~= 0 then
  36                  local particleSystemId  = Utils.loadBundleGameObject(self.bundleId, v.particleSystem or "$vehicles/particles/SnowGroomer600");
  37  
  38                  setParent(particleSystemId, id);
  39                  setActive(particleSystemId, true);
  40                  setPosition(particleSystemId, 0,0,0);
  41                  setRotation(particleSystemId, 0,0,0);
  42  
  43                  local particleSystem    = {
  44                      id                  = particleSystemId,
  45                      amount              = getNoNil(v.particleAmount, 30),
  46                      isActive            = false
  47                  };
  48                  
  49                  table.insert(self.snowGroomerParticles, particleSystem);
  50              end;
  51          end;
  52          
  53          -- stop particle system to avoid emitted particles
  54          SnowcatParticles.setParticleSystemState(self.snowGroomerParticles,  false);
  55      end;
  56  
  57      -- load snow blade
  58      self.snowBladeParticles     = {};
  59  
  60      if dataTable.snowBladeParticles ~= nil then
  61          for k, v in pairs(dataTable.snowBladeParticles) do
  62              local id                    = getChild(self.id, v.index);
  63              if id ~= 0 then
  64                  local particleSystemId  = Utils.loadBundleGameObject(self.bundleId, v.particleSystem or "$vehicles/particles/SnowBlade600");
  65  
  66                  setParent(particleSystemId, id);
  67                  setActive(particleSystemId, true);
  68                  setPosition(particleSystemId, 0,0,0);
  69                  setRotation(particleSystemId, 0,0,0);
  70  
  71                  local particleSystem    = {
  72                      id                  = particleSystemId,
  73                      amount              = getNoNil(v.particleAmount, 20),
  74                      isActive            = false
  75                  };
  76  
  77                  table.insert(self.snowBladeParticles, particleSystem);
  78              end;
  79          end;
  80          
  81          -- stop particle system to avoid emitted particles
  82          SnowcatParticles.setParticleSystemState(self.snowBladeParticles,    false);
  83      end;
  84      
  85      self.terrainMask                    = Utils.getLayerMask("Terrain");
  86      self.particleState                  = false;
  87  end;

SnowcatParticles:update()

Updates the particle systems according to the given values.

  91  function SnowcatParticles:update()
  92      if not self.isActive then
  93          if self.particleState then
  94              SnowcatParticles.setParticleSystemState(self.snowGroomerParticles,  false);
  95              SnowcatParticles.setParticleSystemState(self.snowBladeParticles,    false);
  96              self.particleState          = false;
  97          end;
  98          return;
  99      end;
 100  
 101      local offsetX, offsetY, offsetZ     = GameControl.getTerrainOffset();
 102  
 103      self.particleState                  = true;
 104  
 105      for k, particle in pairs(self.snowGroomerParticles) do
 106          local posX, posY, posZ          = getWorldPosition(particle.id);
 107          local hit, distance             = Utils.raycastGetDistance(Vector3:new(posX, posY, posZ), Vector3:new(0, -1, 0), 1, self.terrainMask);
 108          local x, y, z                   = getWorldPosition(self.id);
 109          local terrain1                  = TerrainEditor.getTerrainLayerIndexAtPosition(x-offsetX , z-offsetZ);
 110  
 111          if hit and math.abs(distance) < 0.2 and terrain1 == 0 and self.isActive and not particle.isActive then
 112              ParticleSystem.play(particle.id);
 113              particle.isActive           = true;
 114  
 115          elseif particle.isActive then
 116              ParticleSystem.stop(particle.id);
 117              particle.isActive           = false;
 118          end;
 119      end;
 120      
 121      for k, particle in pairs(self.snowBladeParticles) do
 122          local posX, posY, posZ          = getWorldPosition(particle.id);
 123          local hit, distance             = Utils.raycastGetDistance(Vector3:new(posX, posY, posZ), Vector3:new(0, -1, 0), 5, self.terrainMask);
 124          local x, y, z                   = getWorldPosition(self.id);
 125          local terrain1                  = TerrainEditor.getTerrainLayerIndexAtPosition(x-offsetX , z-offsetZ);
 126  
 127          if distance < 0.1 and terrain1 == 0 and not particle.isActive then
 128              local pressDownForce        = (0.1 - distance) * 10;
 129          
 130              local vx, vy, vz            = Rigidbody.getVelocity(self.id);
 131              local velocityVector        = VectorUtils.inverseTransformDirection(self.id, Vector3:new(vx, vy, vz));
 132              local realSpeed             = velocityVector.z;
 133              local speedFactor           = 0;
 134  
 135              if math.abs(self.hydraulicSpeed) > 1 and realSpeed > 1 then
 136                  speedFactor             = self.hydraulicSpeed / self.hydraulicMaxSpeed;
 137              end;
 138          
 139              ParticleSystem.play(particle.id);
 140              ParticleSystem.setEmissionRateOverTime(particle.id, speedFactor*pressDownForce*particle.amount)
 141              particle.isActive           = true;
 142  
 143          elseif particle.isActive then
 144              ParticleSystem.stop(particle.id);
 145              particle.isActive           = false;
 146          end;
 147      end;
 148  end;

SnowcatParticles.setParticleSystemState(particleSys, state)

Adjusts the particle system's state. Note that this is a static function and not an instance function (hence declared and called as SnowcatParticles.setParticleSystemState with a . and not a :).

 151  function SnowcatParticles.setParticleSystemState(particleSys, state)
 152      for k, particle in pairs(particleSys) do
 153          if state then
 154              ParticleSystem.play(particle.id);
 155          else
 156              ParticleSystem.stop(particle.id);
 157          end;
 158      end;
 159  end;

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

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