meta data for this page
Objects/AnchorPoint.lua
The AnchorPoint class stores information on anchor points, which any vehicles equipped with a RopeWinch can be attached to. The script is mainly used by RopeWinch and VehicleAnchor.
25 AnchorPoint = AnchorPoint or {}; 26 local AnchorPointClass = Class(AnchorPoint); 27 28 -- set this variable for coupling to nil 29 --AnchorPoint.activeVehicle = nil;
AnchorPoint.onCreate(id)
Creates a new AnchorPoint instance for a static object with id id
(int).
33 function AnchorPoint.onCreate(id) 34 AnchorPoint:new(getChildAt(id, 0), id); 35 end;
AnchorPoint.onCreatePlayer(id)
Internally called by an onCreate script attached to the player.
Do not call this from any mod.
40 function AnchorPoint.onCreatePlayer(id) 41 AnchorPoint:new(id, nil, true); 42 end;
AnchorPoint:load(id, btnId, isPlayer)
Loads a newly created AnchorPoint instance using following arguments:
id
(int) is the transform id of the rope target, i.e. the rope will be connected to here.btnId
(int) is the transform id of the control element. The player can click this control element to attach any active rope to the anchor point.isPlayer
(bool) should usually befalse
.
49 function AnchorPoint:load(id, btnId, isPlayer) 50 self.ropeTargetId = id; 51 self.controlButtonId = btnId; 52 53 self.connectedVehicle = nil; 54 55 if self.controlButtonId == nil and not isPlayer then 56 print("Error while loading AnchorPoint: no control button id specified, rope target id is " .. tostring(id) .. ". Loading skipped"); 57 return; 58 end; 59 if self.controlButtonId ~= nil then 60 ControlElement.new(self.controlButtonId); 61 62 ControlElement.setCallback(self.controlButtonId, 1, 63 function() 64 self:buttonCallback(); 65 end 66 ); 67 ControlElement.setActiveCallback(self.controlButtonId, 68 function() 69 return self:getControlElementActive(); 70 end 71 ); 72 73 ControlElement.setName(self.controlButtonId, l10n.get("WinchAnchor_Attach")); 74 75 -- register anchor point 76 local x,y,z = getWorldPosition(self.ropeTargetId); 77 78 self.anchorPointName = ("anchor" .. string.format(":%s @ (%.4f, %.4f, %.4f)", getName(self.ropeTargetId), x,y,z)); 79 80 local origName = self.anchorPointName; 81 82 -- find a name that is not yet blocked 83 for i=0, 100, 1 do 84 if RopeWinch.anchorPoints[self.anchorPointName] == nil then 85 break; 86 else 87 self.anchorPointName = origName .. "(" .. i .. ")"; 88 end; 89 end; 90 91 else 92 -- only player is allowed not to pass a controlButton 93 self.anchorPointName = "player"; 94 end; 95 96 -- ropewinch does not call delete function, therefore we add it manually 97 RopeWinch.registerAnchorPoint(self.anchorPointName, self); 98 addDestroyListener(id, self); 99 end;
AnchorPoint:getControlElementActive()
Returns whether the anchor point's control element shall be activated. The element cannot be clicked if this returns false
.
102 function AnchorPoint:getControlElementActive() 103 if self.connectedVehicle ~= nil then 104 if AnchorPoint.activeVehicle ~= nil then 105 -- we have a vehicle attached, but the player is also trying to attach a vehicle 106 -- => block this 107 return false; 108 end; 109 return self.connectedVehicle.getMayRopeWinchDisconnect == nil or self.connectedVehicle:getMayRopeWinchDisconnect(); 110 end; 111 -- otherwise we're prepared for attaching 112 return AnchorPoint.activeVehicle ~= nil; 113 end;
AnchorPoint:buttonCallback()
Is called if the player has clicked the control element (i.e. we can assume that AnchorPoint:getControlElementActive()
has returned true
).
116 function AnchorPoint:buttonCallback() 117 if self.connectedVehicle ~= nil then 118 self:disconnect(); 119 120 elseif AnchorPoint.activeVehicle ~= nil then 121 self:attach(AnchorPoint.activeVehicle); 122 AnchorPoint.activeVehicle = nil; 123 end; 124 end;
AnchorPoint:attach(vehicle)
Attaches the vehicle vehicle
(table of vehicle instance) to this anchor point.
127 function AnchorPoint:attach(vehicle) 128 if vehicle == nil or vehicle.attachWinchToAnchorPoint == nil then return end; 129 130 self.connectedVehicle = vehicle; 131 self.connectedVehicle:attachWinchToAnchorPoint(self.anchorPointName); 132 133 if self.controlButtonId ~= nil then 134 ControlElement.setName(self.controlButtonId, l10n.get("WinchAnchor_Disconnect")); 135 end; 136 end;
AnchorPoint:disconnect(noPlayerAttaching)
Disconnects/detaches any active vehicle from this anchor point. If noPlayerAttaching
is nil
or false
, the rope will be attached to the player controller. Otherwise (in case of true
) the rope will not be connected to the player.
139 function AnchorPoint:disconnect(noPlayerAttaching) 140 if self.connectedVehicle == nil then return end; 141 142 -- decouple that vehicle 143 if not noPlayerAttaching then 144 self.connectedVehicle:attachWinchToAnchorPoint("player"); 145 else 146 self.connectedVehicle:attachWinchToAnchorPoint(nil); 147 end; 148 149 -- and set it as active vehicle 150 AnchorPoint.activeVehicle = self.connectedVehicle; 151 152 self.connectedVehicle = nil; 153 if self.controlButtonId ~= nil then 154 ControlElement.setName(self.controlButtonId, l10n.get("WinchAnchor_Attach")); 155 end; 156 end;
AnchorPoint:destroy()
Is called if the anchor point is destroyed, e.g. if the prefab is deleted in ingame editor.
159 function AnchorPoint:destroy() 160 self:disconnect(true); 161 RopeWinch.unregisterAnchorPoint(self.anchorPointName); 162 end;
Copyright
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.