Action denied

Vehicles/VehicleSeat.lua

Every time you can take place on a seat, this seat will be powered by VehicleSeat. This is true for vehicles and ropeway carriers, but also for stationary objects (e.g. sun deck chairs or the chairs in some ropeway control rooms).

Regarding the implementation in vehicles, have a look at the Seats vehicle script. Some functions of it are also in use for ropeway carriers.

  26  g_currentVehicleSeat                = g_currentVehicleSeat or nil;
  27  
  28  VehicleSeat                         = VehicleSeat or {};
  29  local VehicleSeatClass              = Class(VehicleSeat);
  30  

VehicleSeat.controlElementActive()

Returns whether the seat's control element shall be active. Only active control elements can be clicked.

  34  function VehicleSeat.controlElementActive()
  35      return (PlayerController.isActive or SkierController.isActive) and g_currentVehicleSeat == nil;
  36  end;

VehicleSeat.leaveActiveSeat()

This will make the player leave any active seat.

  40  function VehicleSeat.leaveActiveSeat()
  41      if g_currentVehicleSeat == nil then return end;
  42  
  43      -- leave that seat
  44      g_currentVehicleSeat:leave();
  45  end;

VehicleSeat:onCreateStaticSeat(id, cameraId, leaveNode)

This function can be called for static objects (such as the chair in a command room) using following arguments:

  • id (int): id of the seat's control element.
  • cameraId (int): id of the transform which the camera shall be placed at.
  • leaveNode (int): id of the transform where the player will be placed at upon leaving the seat.
  52  function VehicleSeat:onCreateStaticSeat(id, cameraId, leaveNode)
  53      cameraId                        = cameraId  or getChildAt(id, 0);
  54      leaveNode                       = leaveNode or getChildAt(id, 1);
  55  
  56      -- create a new seat which handles all updateable stuff itself
  57      local seat                      = VehicleSeat:new(id, cameraId, leaveNode);
  58      seat.savegameKey                = string.format("seat:%s @ (%.4f, %.4f, %.4f)", getName(id), getWorldPosition(id));
  59  
  60      g_scenario:addObjectAndLoad(seat);
  61  end;

VehicleSeat:load(id, cameraId, leaveNode, ...)

Loads a newly created instance using the following parameters:

  • id (int): id of the seat's control element.
  • cameraId (int): id of the transform which the camera shall be placed at.
  • leaveNode (int): id of the transform where the player will be placed at upon leaving the seat.
  • : Arguments passed on to VehicleCamera.
  69  function VehicleSeat:load(id, cameraId, leaveNode, ...)
  70      self.id                         = id;
  71      
  72      ControlElement.new(self.id);
  73      ControlElement.setCallback(self.id, 1, function() self:buttonCallback() end);
  74      ControlElement.setActiveCallback(self.id, VehicleSeat.controlElementActive);
  75      ControlElement.setName(self.id, l10n.get("EnterSeat"));
  76  
  77      local firstCamera               = VehicleCamera:new(cameraId, nil,nil, ...);
  78      firstCamera.isInterieur         = true;
  79      
  80      self.cameraSlots                = {};
  81      self.cameraSlots[1]             = { firstCamera };
  82      self.activeCamera               = self.cameraSlots[1][1];
  83      
  84      self.selectedSlot               = 1;
  85      self.selectedIndex              = 1;
  86      
  87      self.seatActive                 = false;
  88      self.leaveNode                  = leaveNode;
  89      self.enteredOnSkis              = false;
  90      self.isInterieurCamera          = true;
  91  end;

VehicleSeat:saveToTable(parentTable, withoutCameras)

Saves all relevant variables to the savegames.

  94  function VehicleSeat:saveToTable(parentTable, withoutCameras)
  95      local tbl                       = {
  96          seatActive                  = self.seatActive,
  97          selectedSlot                = self.selectedSlot,
  98          selectedIndex               = self.selectedIndex,
  99          enteredOnSkis               = self.enteredOnSkis,
 100      };
 101      -- safety layer
 102      local ignoreCamera              = false;
 103      if self.cameraSlots ~= nil then
 104          for slotNo, slot in pairs(self.cameraSlots) do
 105              for cameraNo, v in pairs(slot) do
 106                  if not ignoreCamera then
 107                      tbl["camera" .. slotNo .. "_" .. cameraNo] = v:saveToTable();
 108                  end;
 109                  ignoreCamera        = withoutCameras;
 110              end;
 111          end;
 112      end;
 113  
 114      if self.savegameKey ~= nil and parentTable ~= nil then
 115          -- static instance
 116          parentTable[self.savegameKey]   = tbl;
 117      else
 118          return tbl;
 119      end;
 120  end;

VehicleSeat:loadFromTable(tbl)

Restore all relevant variables from the savegame, and if necessary, enter the seat.

 124  function VehicleSeat:loadFromTable(tbl)
 125      if tbl == nil then return end;
 126  
 127      if self.savegameKey ~= nil then
 128          tbl                         = tbl[self.savegameKey];
 129  
 130          if tbl == nil then return end;
 131      end;
 132  
 133      self.seatActive                 = getNoNil(tbl.seatActive,      self.seatActive);
 134  
 135      if self.seatActive then
 136          self:enter();
 137      end;
 138  
 139      self.selectedSlot               = getNoNil(tbl.selectedSlot,    self.selectedSlot);
 140      self.selectedIndex              = getNoNil(tbl.selectedIndex,   self.selectedIndex);
 141      self.enteredOnSkis              = getNoNil(tbl.enteredOnSkis,   self.enteredOnSkis);
 142  
 143      if self.cameraSlots ~= nil then
 144          for slotNo, slot in pairs(self.cameraSlots) do
 145              for cameraNo, v in pairs(slot) do
 146                  local cameraTbl     = tbl["camera" .. slotNo .. "_" .. cameraNo];
 147                  
 148                  if cameraTbl ~= nil then
 149                      v:loadFromTable(cameraTbl);
 150  
 151                      if cameraTbl.isActive then
 152                          self.activeCamera   = v;
 153                      end;
 154                  end;
 155              end;
 156          end;
 157      end;
 158  
 159      -- refresh active camera
 160      if self.seatActive then
 161          self.activeCamera:activate();
 162      end;
 163  end;

VehicleSeat:destroy()

 165  function VehicleSeat:destroy()
 166      ControlElement.destroy(self.id);
 167  end;

VehicleSeat:addCamera(slotNo, camera)

Adds the VehicleCamera camera to the slot number slotNo (int, range 1-4).

 171  function VehicleSeat:addCamera(slotNo, camera)
 172      local slot                      = self.cameraSlots[slotNo];
 173      if slot == nil then
 174          slot                        = {};
 175          self.cameraSlots[slotNo]    = slot;
 176      end;
 177      
 178      table.insert(slot, camera);
 179  end;

VehicleSeat:buttonCallback()

Is called if the player clicks the seat's control element.

 184  function VehicleSeat:buttonCallback()
 185      self:enter();
 186  end;

VehicleSeat:enter()

Makes the player enter this particular VehicleSeat.

 190  function VehicleSeat:enter()
 191      if g_currentVehicleSeat ~= nil then
 192          return;
 193      end;
 194  
 195      self.seatActive                 = true;
 196      self.enteredOnSkis              = SkierController.isActive;
 197      
 198      PlayerController:setIsActive(false);
 199      SkierController:setIsActive(false);
 200      EditorController:setIsActive(false);
 201      self.activeCamera:activate();
 202      
 203      g_currentVehicleSeat            = self;
 204  end;

VehicleSeat:selectCamera(slotNo)

Activates the specified camera slot slotNo (int, range 1-4).

 208  function VehicleSeat:selectCamera(slotNo)
 209      if self.selectedSlot == slotNo then
 210          -- turn one further
 211          local slot                  = self.cameraSlots[slotNo];
 212          self.selectedIndex          = self.selectedIndex + 1;
 213          self.selectedIndex          = self.selectedIndex > #slot and 1 or self.selectedIndex;
 214          self.activeCamera           = slot[self.selectedIndex];
 215          self.activeCamera:activate();
 216  
 217          
 218      elseif self.cameraSlots[slotNo] ~= nil then
 219          -- switch to that one
 220          local slot                  = self.cameraSlots[slotNo];
 221          self.selectedSlot           = slotNo;
 222          self.selectedIndex          = 1;
 223          self.activeCamera           = slot[self.selectedIndex];
 224          self.activeCamera:activate();
 225      end;
 226      self.isInterieurCamera          = self.activeCamera.isInterieur;
 227  end;

VehicleSeat:update(dt)

Called every frame to update the seat and the active camera.

 231  function VehicleSeat:update(dt)
 232      if self.seatActive and not GUI:getAnyGuiActive() then
 233          
 234          self.activeCamera:update(dt);
 235          self.isInterieurCamera      = self.activeCamera.isInterieur;
 236          
 237          if      InputMapper:getKeyDown(InputMapper.InterieurCamera)     then        self:selectCamera(1);
 238          elseif  InputMapper:getKeyDown(InputMapper.ExterieurCamera)     then        self:selectCamera(2);
 239          elseif  InputMapper:getKeyDown(InputMapper.CustomCamera1)       then        self:selectCamera(3);
 240          elseif  InputMapper:getKeyDown(InputMapper.CustomCamera2)       then        self:selectCamera(4);
 241          end;
 242          
 243          -- finally check whether we're going to leave
 244          if InputMapper:getKeyDown(InputMapper.EnterSeat) then
 245              self:leave();
 246          end;
 247  
 248          if self.updateCallback ~= nil then
 249              self.updateCallback(self.updateCallbackObject, dt);
 250          end;
 251          
 252      end;
 253  end;

VehicleSeat:fixedUpdate(dt)

Called upon physics update.

 257  function VehicleSeat:fixedUpdate(dt)
 258      if self.seatActive then
 259          self.activeCamera:fixedUpdate(dt);
 260      end;
 261  end;
 262  

VehicleSeat:leave()

Makes the player leave this particular seat. If you are not sure, which seat is active, always prefer calling VehicleSeat.leaveActiveSeat()!

 265  function VehicleSeat:leave()
 266      self.seatActive                 = false;
 267      
 268      if g_currentVehicleSeat ~= self then
 269          return;
 270      end;
 271      g_currentVehicleSeat            = nil;
 272      
 273      -- teleport player to our leave position
 274      local x,y,z                     = getWorldPosition(self.leaveNode);
 275  
 276      if self.enteredOnSkis then
 277          SkierController:moveToFeet(x,y,z);  
 278          SkierController:setIsActive(true);
 279      else
 280          PlayerController:moveToFeet(x,y,z); 
 281          PlayerController:setIsActive(true);
 282      end;
 283  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.