Sperrbare Lenkachse V2

User avatar
desperados93
GIANTS Software | Vehicle Integration
Posts: 295
Joined: Wed Jul 08, 2009 3:50 pm
Location: Rotenburg an der Fulda
Contact:

Sperrbare Lenkachse V2

Post by desperados93 » Thu May 15, 2014 12:00 pm

Tach zusammen,

da meine LUA Kenntnisse schon einige Tage zurück liegen, muss ich euch mal fragen, vielleicht kann mir jemand weiter helfen ;-)

Da Script was ich verwende ist unten im Anhang.

In den Zeilen 85-87 wird festgelegt, welchen Wert die Achse bekommt, wenn sie gesperrt wird. Ich möchte aber, dass die Achse nicht sofort voll gesperrt ist, sondern sich langsam beizieht, wie in echt halt.

Ich habe schon folgende option ausprobiert..

Code: Select all

while wheel.steeringAxleRotMax > 0 do
				wheel.steeringAxleRotMax = wheel.steeringAxleRotMax - self.updateTime;     				
			end
self.updateTime ist dabei: self.updateTime = dt*0.0002;

Wenn ich die lenkachse nun sperre, hängt das spiel für dauer X und danach geht es weiter und die achse ist zu.. ich möchte halt dass man sieht wie sie zu geht, und ebenso, dass wenn sie zu ist und ich sie auf mache der Wert nicht dem entsprechenden radius entspricht, sondern 0 und sie dann erst aufgeht wenn ich fahre.

Wäre super wenn mir da jemand helfen könnte.

Code: Select all

--
-- LockSteeringaxles - V1.1
-- allows to lock/unlock the steeringaxles
--
-- @Author  FIAT80-90DT
-- @date  	29.08.2011
-- @Edit	04.12.2013 - bugfixes | better network-support | add steeringaxle for the reversetrip
--
-- www.modding-stage.com -- 

LockSteeringaxles = {};

function LockSteeringaxles.prerequisitesPresent(specializations)
    return true;
end;

function LockSteeringaxles:load(xmlFile)
	self.lockAxles = SpecializationUtil.callSpecializationsFunction("lockAxles");
	
	self.enableIfReverse = Utils.getNoNil(getXMLBool(xmlFile, "vehicle.wheels.enableSteeringaxlesIfReverse"), true);
	
	for i=1, table.getn(self.wheels) do 
		self.wheels[i].backupScale = self.wheels[i].steeringAxleScale;
		self.wheels[i].backupMax = self.wheels[i].steeringAxleRotMax;
		self.wheels[i].backupMin = self.wheels[i].steeringAxleRotMin;
    end;
	
	if InputBinding.LOCK_AXLES == nil then
		InputBinding.LOCK_AXLES = InputBinding.IMPLEMENT_EXTRA3;
	end;
end;

function LockSteeringaxles:readStream(streamId, connection)
	self:lockAxles(streamReadBool(streamId), true);
end;

function LockSteeringaxles:writeStream(streamId, connection)
	streamWriteBool(streamId, self.lock);
end;

function LockSteeringaxles:delete()
end;

function LockSteeringaxles:mouseEvent(posX, posY, isDown, isUp, button)
end;

function LockSteeringaxles:keyEvent(unicode, sym, modifier, isDown)
end;

function LockSteeringaxles:update(dt)
	if self:getIsActiveForInput() then
		if InputBinding.hasEvent(InputBinding.TEBBE_LOCK_AXLES) then
			self:lockAxles(not self.lock);			
		end;
	end;
	
	-- # Section from sven18koehler # --
	if self.updateSteeringAxleAngle and self.enableIfReverse == "true" then
		if self.attacherVehicle ~= nil and self.movingDirection < 0 then
			local x,y,z = worldDirectionToLocal(self.steeringAxleNode, localDirectionToWorld(self.attacherVehicle.steeringAxleNode, 0, 0, 1));
			local dot = z;
			dot = dot / Utils.vector2Length(x,z);
			local angle = math.acos(dot);
			if x < 0 then
				angle = -angle;
			end;
			local startSpeed = self.steeringAxleAngleScaleStart;
			local endSpeed = self.steeringAxleAngleScaleEnd;
			local scale = Utils.clamp(1 + (self.lastSpeed*-3600-startSpeed) * 1.0/(startSpeed-endSpeed), 0, 1);
			self.steeringAxleAngle = angle*scale;
		end;
	end;
	--#--
end;

function LockSteeringaxles:updateTick(dt)
	self.updateTime = dt*0.0002;
end;

function LockSteeringaxles:lockAxles(lock, noEventSend)
	for i=1, table.getn(self.wheels) do 
		local wheel = self.wheels[i];
		if lock then 
		
			wheel.steeringAxleScale = 0;
			wheel.steeringAxleRotMax = 0;
			wheel.steeringAxleRotMin = 0;
		else
		
		
		
		
			wheel.steeringAxleScale = wheel.backupScale;
			wheel.steeringAxleRotMax = wheel.backupMax;
			wheel.steeringAxleRotMin = wheel.backupMin;
		end;
	end;
	self.lock = lock;
	LockSteeringaxlesEvent.sendEvent(self, self.lock, noEventSend);
end;

function LockSteeringaxles:draw()
	if self.lock then
		g_currentMission:addHelpButtonText(g_i18n:getText("unlockSteeringaxles"), InputBinding.TEBBE_LOCK_AXLES);
	else
		g_currentMission:addHelpButtonText(g_i18n:getText("lockSteeringaxles"), InputBinding.TEBBE_LOCK_AXLES);
	end;
end;
						
						
						
--####	EVENT	####-- 
	
--@author	-	FIAT80-90DT																
--@date		-	28.08.11											

LockSteeringaxlesEvent = {};
LockSteeringaxlesEvent_mt = Class(LockSteeringaxlesEvent, Event);

InitEventClass(LockSteeringaxlesEvent, "LockSteeringaxlesEvent");

function LockSteeringaxlesEvent:emptyNew()
    local self = Event:new(LockSteeringaxlesEvent_mt);
    self.className="LockSteeringaxlesEvent";
    return self;
end;

function LockSteeringaxlesEvent:new(vehicle, lock)
    local self = LockSteeringaxlesEvent:emptyNew()
    self.vehicle = vehicle;
	self.lock = lock;
    return self;
end;

function LockSteeringaxlesEvent:readStream(streamId, connection)
    local id = streamReadInt32(streamId);
    self.vehicle = networkGetObject(id);
	
	self.lock = streamReadBool(streamId);
	self:run(connection);
end;

function LockSteeringaxlesEvent:run(connection)
	self.vehicle:lockAxles(self.lock, true);
	if not connection:getIsServer() then
        g_server:broadcastEvent(LockSteeringaxlesEvent:new(self.vehicle, self.lock), nil, connection, self.vehicle);
    end;
end;

function LockSteeringaxlesEvent:writeStream(streamId, connection)
    streamWriteInt32(streamId, networkGetObjectId(self.vehicle));
	streamWriteBool(streamId, self.lock);
end;


function LockSteeringaxlesEvent.sendEvent(vehicle, lock, noEventSend)
	if noEventSend == nil or noEventSend == false then
		if g_server ~= nil then
			g_server:broadcastEvent(LockSteeringaxlesEvent:new(vehicle, lock), nil, nil, vehicle);
		else
			g_client:getServerConnection():sendEvent(LockSteeringaxlesEvent:new(vehicle, lock));
		end;
	end;
end;

User avatar
Sven777b
Posts: 55
Joined: Thu Aug 14, 2008 4:28 am
Location: Ost-Thüringen

Re: Sperrbare Lenkachse V2

Post by Sven777b » Thu May 15, 2014 12:52 pm

prinzipiell niemals in update oder draw solche Schleifen verwenden...
das Spiel arbeitet alle Update() routinen aller mods nacheinander ab - das dauert normalerweise nur zehntel Sekunden.
Wenn du Dinge einbauen willst, die sich über einen gewissen Zeitraum hin verändern, dann musst du das über eine Variable machen, deren Wert sich in jedem Update-Durchlauf ändert.

das heisst...
als erstes muss der eigentliche Part zum locken aus der funktion lockAxles() raus und in die Update Routine.

Code: Select all

function LockSteeringaxles:lockAxles(lock, noEventSend)
   self.lock = lock;
   LockSteeringaxlesEvent.sendEvent(self, self.lock, noEventSend);
end;
desweiteren solltest du bedenken das die Variable dt natürlich in jeder Funktion einen anderen Wert hat. updateTick wird seltener aufgerufen wie update() - deine Berechnung der self.updateTime() sollte daher in der selben funktion liegen wie auch die reduzierung des locks.

Wenn du jetzt eine gleichförmige sperrung willst, sollte das ganze in update() oder updateTick() geschehen. Du musst selber mal checken ob dir updateTick() reicht:

Code: Select all

function LockSteeringaxles:updateTick(dt)
   local updateTime = dt*0.0002;
   for i=1, table.getn(self.wheels) do
      local wheel = self.wheels[i];
      if self.lock == true then
      	if wheel.steeringAxleRotMax > 0 then
	      	wheel.steeringAxleRotMax = Math.max(0,  wheel.steeringAxleRotMax - updateTime);
                -- pro Durchlauf den Wert updateTime abziehen bis das ganze auf 0 ist
	      	wheel.steeringAxleRotMin = Math.min(0,  wheel.steeringAxleRotMax + updateTime);
                -- rotMin ist negativ - daher muss die Formel hier umgekehrt sein
	      	wheel.steeringAxleScale = Math.max(0, wheel.steeringAxleScale - updateTime);
	      end;
      else
      	if wheel.steeringAxleRotMax < wheel.backupMax then
	      	-- optional kann man die hier auch wieder weich aufmachen - die oberen Formeln - nur jeweils umgekehrt
	         wheel.steeringAxleScale = wheel.backupScale;
	         wheel.steeringAxleRotMax = wheel.backupMax;
	         wheel.steeringAxleRotMin = wheel.backupMin;
	    end;
      end;
   end;
end;

User avatar
Bluebaby210
Posts: 359
Joined: Sun Jun 26, 2011 6:48 pm

Re: Sperrbare Lenkachse V2

Post by Bluebaby210 » Thu May 15, 2014 9:06 pm

Sven777b wrote:prinzipiell niemals in update oder draw solche Schleifen verwenden...
Aus Neugier, was meinst du jetzt mit solche Schleifen? Meinst du damit jetzt die while Schleife wie sie desperados in seinem ersten Code gepostet hat?

User avatar
desperados93
GIANTS Software | Vehicle Integration
Posts: 295
Joined: Wed Jul 08, 2009 3:50 pm
Location: Rotenburg an der Fulda
Contact:

Re: Sperrbare Lenkachse V2

Post by desperados93 » Mon May 19, 2014 4:20 pm

ich komme immer noch nicht weiter... dass sie langsam zu geht funktioniert zwar, aber langsam auf, nicht..

Code: Select all

---- LockSteeringaxles - V1.1
-- allows to lock/unlock the steeringaxles
--
-- @Author  FIAT80-90DT
-- @date      29.08.2011
-- @Edit    04.12.2013 - bugfixes | better network-support | add steeringaxle for the reversetrip
--
-- http://www.modding-stage.com -- 

LockSteeringaxles = {};

function LockSteeringaxles.prerequisitesPresent(specializations)
    return true;
end;

function LockSteeringaxles:load(xmlFile)
    self.lockAxles = SpecializationUtil.callSpecializationsFunction("lockAxles");
    
    self.enableIfReverse = Utils.getNoNil(getXMLBool(xmlFile, "vehicle.wheels.enableSteeringaxlesIfReverse"), true);
    
    for i=1, table.getn(self.wheels) do 
        self.wheels[i].backupScale = self.wheels[i].steeringAxleScale;
        self.wheels[i].backupMax = self.wheels[i].steeringAxleRotMax;
        self.wheels[i].backupMin = self.wheels[i].steeringAxleRotMin;
    end;
    
    if InputBinding.LOCK_AXLES == nil then
        InputBinding.LOCK_AXLES = InputBinding.IMPLEMENT_EXTRA3;
    end;
end;

function LockSteeringaxles:readStream(streamId, connection)
    self:lockAxles(streamReadBool(streamId), true);
end;

function LockSteeringaxles:writeStream(streamId, connection)
    streamWriteBool(streamId, self.lock);
end;

function LockSteeringaxles:delete()
end;

function LockSteeringaxles:mouseEvent(posX, posY, isDown, isUp, button)
end;

function LockSteeringaxles:keyEvent(unicode, sym, modifier, isDown)
end;

function LockSteeringaxles:update(dt)
    if self:getIsActiveForInput() then
        if InputBinding.hasEvent(InputBinding.TEBBE_LOCK_AXLES) then
            self:lockAxles(not self.lock);            
        end;
    end;
    
    -- # Section from sven18koehler # --
    if self.updateSteeringAxleAngle and self.enableIfReverse == "true" then
        if self.attacherVehicle ~= nil and self.movingDirection < 0 then
            local x,y,z = worldDirectionToLocal(self.steeringAxleNode, localDirectionToWorld(self.attacherVehicle.steeringAxleNode, 0, 0, 1));
            local dot = z;
            dot = dot / Utils.vector2Length(x,z);
            local angle = math.acos(dot);
            if x < 0 then
                angle = -angle;
            end;
            local startSpeed = self.steeringAxleAngleScaleStart;
            local endSpeed = self.steeringAxleAngleScaleEnd;
            local scale = Utils.clamp(1 + (self.lastSpeed*-3600-startSpeed) * 1.0/(startSpeed-endSpeed), 0, 1);
            self.steeringAxleAngle = angle*scale;
        end;
    end;
    --#--
end;

function LockSteeringaxles:updateTick(dt)

self.updateTime = dt*0.0002;
if self.lock and not self.steeringAxleLocked then

for i=1, table.getn(self.wheels) do 
	local wheel = self.wheels[i];
    local locked = true;
    for j=1,#self.wheels do
        if wheel.steeringAxleRotMax > 0.1 then
            wheel.steeringAxleRotMax = wheel.steeringAxleRotMax - self.updateTime;
            wheel.steeringAxleRotMin = wheel.steeringAxleRotMin + self.updateTime;
					
            locked = false;
			--print("steeringAxleRotMax", wheel.steeringAxleRotMax);
			--print("steeringAxleRotMin", wheel.steeringAxleRotMin);
        else
			--print("else wird durchlaufen, alles auf 0");
			wheel.steeringAxleScale = 0;
            wheel.steeringAxleRotMax = 0;
            wheel.steeringAxleRotMin = 0;
			self.ok = true;
		end;
		
    end;
			
end;

if self.ok then
	--print("self.ok");
	self.steeringAxleLocked = locked;
end;

end;



	
			

end;

function LockSteeringaxles:lockAxles(lock, noEventSend)
    for i=1, table.getn(self.wheels) do 
        local wheel = self.wheels[i];
        if lock then 
        

         
        else
        
        
         self.steeringAxleLocked = false;
        
            wheel.steeringAxleScale = wheel.backupScale;
            wheel.steeringAxleRotMax = wheel.backupMax;
            wheel.steeringAxleRotMin = wheel.backupMin;
        end;
    end;
    self.lock = lock;
    LockSteeringaxlesEvent.sendEvent(self, self.lock, noEventSend);
end;

function LockSteeringaxles:draw()
    if self.lock then
        g_currentMission:addHelpButtonText(g_i18n:getText("unlockSteeringaxles"), InputBinding.TEBBE_LOCK_AXLES);
    else
        g_currentMission:addHelpButtonText(g_i18n:getText("lockSteeringaxles"), InputBinding.TEBBE_LOCK_AXLES);
    end;
end;
                        
                        
                        
--####    EVENT    ####-- 
    
--@author    -    FIAT80-90DT                                                                
--@date        -    28.08.11                                            

LockSteeringaxlesEvent = {};
LockSteeringaxlesEvent_mt = Class(LockSteeringaxlesEvent, Event);

InitEventClass(LockSteeringaxlesEvent, "LockSteeringaxlesEvent");

function LockSteeringaxlesEvent:emptyNew()
    local self = Event:new(LockSteeringaxlesEvent_mt);
    self.className="LockSteeringaxlesEvent";
    return self;
end;

function LockSteeringaxlesEvent:new(vehicle, lock)
    local self = LockSteeringaxlesEvent:emptyNew()
    self.vehicle = vehicle;
    self.lock = lock;
    return self;
end;

function LockSteeringaxlesEvent:readStream(streamId, connection)
    local id = streamReadInt32(streamId);
    self.vehicle = networkGetObject(id);
    
    self.lock = streamReadBool(streamId);
    self:run(connection);
end;

function LockSteeringaxlesEvent:run(connection)
    self.vehicle:lockAxles(self.lock, true);
    if not connection:getIsServer() then
        g_server:broadcastEvent(LockSteeringaxlesEvent:new(self.vehicle, self.lock), nil, connection, self.vehicle);
    end;
end;

function LockSteeringaxlesEvent:writeStream(streamId, connection)
    streamWriteInt32(streamId, networkGetObjectId(self.vehicle));
    streamWriteBool(streamId, self.lock);
end;

function LockSteeringaxlesEvent.sendEvent(vehicle, lock, noEventSend)
    if noEventSend == nil or noEventSend == false then
        if g_server ~= nil then
            g_server:broadcastEvent(LockSteeringaxlesEvent:new(vehicle, lock), nil, nil, vehicle);
        else
            g_client:getServerConnection():sendEvent(LockSteeringaxlesEvent:new(vehicle, lock));
        end;
    end;
end;

hab auchmal versucht das ganze neu zu schreiben, aber iwie gehts auch nicht..

Code: Select all

--
-- LockSteeringaxles - V1.1
-- allows to locked/unlock the steeringaxles
--
-- @Author  FIAT80-90DT
-- @date      29.08.2011
-- @Edit    04.12.2013 - bugfixes | better network-support | add steeringaxle for the reversetrip
--
-- www.modding-stage.com -- 

LockSteeringaxles = {};

function LockSteeringaxles.prerequisitesPresent(specializations)
    return true;
end;

function LockSteeringaxles:load(xmlFile)
    self.lockAxles = SpecializationUtil.callSpecializationsFunction("lockAxles");
    
    for i=1, table.getn(self.wheels) do 
        self.wheels[i].oldScale = self.wheels[i].steeringAxleScale;
        self.wheels[i].oldMax = self.wheels[i].steeringAxleRotMax;
        self.wheels[i].oldMin = self.wheels[i].steeringAxleRotMin;
    end;
    	
	self.locked = false;
	print("Achse offen");
end;


function LockSteeringaxles:delete()
end;

function LockSteeringaxles:mouseEvent(posX, posY, isDown, isUp, button)
end;

function LockSteeringaxles:keyEvent(unicode, sym, modifier, isDown)
end;

function LockSteeringaxles:update(dt)

	self.updateTime = dt*0.0002;

	if InputBinding.hasEvent(InputBinding.Lenkachse) and not self.locked then
		print("Tastendruck fuer Achse zu: ");
			for i=1, table.getn(self.wheels) do 
			local wheel = self.wheels[i];
				for j=1,#self.wheels do
					if wheel.steeringAxleRotMax > 0.1 then
						wheel.steeringAxleRotMax = wheel.steeringAxleRotMax - self.updateTime;
						wheel.steeringAxleRotMin = wheel.steeringAxleRotMin + self.updateTime;
						print("Achse schliessen, durchlauf: ", j, "Winkel", wheel.steeringAxleRotMax);
					else
						wheel.steeringAxleScale = 0;
						wheel.steeringAxleRotMax = 0;
						wheel.steeringAxleRotMin = 0;
						self.locked = true;
						print("achse auf 0 gesetzt: ");
					end;
				end;
			end;
		end;

		if InputBinding.hasEvent(InputBinding.Lenkachse) and self.locked then
			print("Tastendruck fueür Achse auf: ");
			for i=1, table.getn(self.wheels) do 
			local wheel = self.wheels[i];
				for j=1,#self.wheels do
					if wheel.steeringAxleRotMax < wheel.oldMax then
						wheel.steeringAxleRotMax = wheel.steeringAxleRotMax + self.updateTime;
						wheel.steeringAxleRotMin = wheel.steeringAxleRotMin - self.updateTime;
						print("Achse oeffnen, durchlauf: ", j);
					else
						wheel.steeringAxleScale = wheel.oldScale;
						wheel.steeringAxleRotMax = wheel.oldMax;
						wheel.steeringAxleRotMin = wheel.oldMin;
						self.locked = false;
						print("achse auf alten Wert gesetzt: ");
					end;
				end;
			end;
		end;
end;



function LockSteeringaxles:draw()
    if self.locked then
        g_currentMission:addHelpButtonText(g_i18n:getText("Lenkachse_auf"), InputBinding.Lenkachse);
    else
        g_currentMission:addHelpButtonText(g_i18n:getText("Lenkachse_zu"), InputBinding.Lenkachse);
    end;
end;
                        
     

Face
GIANTS Software | Lead Gameplay Programmer
Posts: 128
Joined: Sun Jun 17, 2007 8:21 pm

Re: Sperrbare Lenkachse V2

Post by Face » Mon May 19, 2014 5:24 pm

Bluebaby210 wrote:
Sven777b wrote:prinzipiell niemals in update oder draw solche Schleifen verwenden...
Aus Neugier, was meinst du jetzt mit solche Schleifen? Meinst du damit jetzt die while Schleife wie sie desperados in seinem ersten Code gepostet hat?
update selbst ist bereits eine Art while-schleife. Das was despo macht sollte verhindert werden, weil das enorm viele Iterationen sind und somit das Game freezed bis die while-Schleife durchgelaufen ist. Im Endeffekt ist das aber nichts anderes als ein Aufruf... weil mehr sieht man nicht.
Damit eine Bewegung wirklich sichtbar ist muss man sie über mehrer update-Aufrufe verteilen.

User avatar
Bluebaby210
Posts: 359
Joined: Sun Jun 26, 2011 6:48 pm

Re: Sperrbare Lenkachse V2

Post by Bluebaby210 » Mon May 19, 2014 10:42 pm

Danke für die Erklärung Face. *thumbsup*

despo, was hat den Sven777b geschrieben? Optional wieder weich aufmachen, wenn du die oberen Formeln jeweils umkehrst...!
Ersetz deinen updateTick() mal durch folgenden:

Code: Select all

function LockSteeringaxles:updateTick(dt)
   self.updateTime = dt*0.0002;
   for i=1, table.getn(self.wheels) do
      local wheel = self.wheels[i];
      if self.lock == true then
         if wheel.steeringAxleRotMax > 0 then
            wheel.steeringAxleRotMax = Math.max(0,  wheel.steeringAxleRotMax - self.updateTime );
            wheel.steeringAxleRotMin = Math.min(0,  wheel.steeringAxleRotMax + self.updateTime );
            wheel.steeringAxleScale = Math.max(0, wheel.steeringAxleScale - self.updateTime );
         --print("langsam zumachen: steeringAxleRotMax", wheel.steeringAxleRotMax);
         --print("langsam zumachen: steeringAxleRotMin", wheel.steeringAxleRotMin);
         end;
      else
         if wheel.steeringAxleRotMax < wheel.backupMax then
            wheel.steeringAxleRotMax = Math.max(0,  wheel.steeringAxleRotMax + self.updateTime );
            wheel.steeringAxleRotMin = Math.min(0,  wheel.steeringAxleRotMax - self.updateTime );
            wheel.steeringAxleScale = Math.max(0, wheel.steeringAxleScale + self.updateTime );
			--print("langsam aufmachen: steeringAxleRotMax", wheel.steeringAxleRotMax);
			--print("langsam aufmachen: steeringAxleRotMin", wheel.steeringAxleRotMin);
      end;
      end;
   end;
end;
Dann noch deine function:lockAxles() ändern, bzw alles löschen bis auf:

Code: Select all

    self.lock = lock;
    LockSteeringaxlesEvent.sendEvent(self, self.lock, noEventSend);

und dann hast du in deiner schleife im updateTick() auch einen Schreibfehler: "#self.wheels", sollte wohl wenn dann "self.wheels" heißen...!


Oder kopierst einfach alles aus dem Spoiler: :wink3:

Code: Select all

---- LockSteeringaxles - V1.1
-- allows to lock/unlock the steeringaxles
--
-- @Author  FIAT80-90DT
-- @date      29.08.2011
-- @Edit    04.12.2013 - bugfixes | better network-support | add steeringaxle for the reversetrip
--
-- http://www.modding-stage.com -- 

LockSteeringaxles = {};

function LockSteeringaxles.prerequisitesPresent(specializations)
    return true;
end;

function LockSteeringaxles:load(xmlFile)
    self.lockAxles = SpecializationUtil.callSpecializationsFunction("lockAxles");
    
    self.enableIfReverse = Utils.getNoNil(getXMLBool(xmlFile, "vehicle.wheels.enableSteeringaxlesIfReverse"), true);
    
    for i=1, table.getn(self.wheels) do 
        self.wheels[i].backupScale = self.wheels[i].steeringAxleScale;
        self.wheels[i].backupMax = self.wheels[i].steeringAxleRotMax;
        self.wheels[i].backupMin = self.wheels[i].steeringAxleRotMin;
    end;
    
    if InputBinding.LOCK_AXLES == nil then
        InputBinding.LOCK_AXLES = InputBinding.IMPLEMENT_EXTRA3;
    end;
end;

function LockSteeringaxles:readStream(streamId, connection)
    self:lockAxles(streamReadBool(streamId), true);
end;

function LockSteeringaxles:writeStream(streamId, connection)
    streamWriteBool(streamId, self.lock);
end;

function LockSteeringaxles:delete()
end;

function LockSteeringaxles:mouseEvent(posX, posY, isDown, isUp, button)
end;

function LockSteeringaxles:keyEvent(unicode, sym, modifier, isDown)
end;

function LockSteeringaxles:update(dt)
    if self:getIsActiveForInput() then
        if InputBinding.hasEvent(InputBinding.TEBBE_LOCK_AXLES) then
            self:lockAxles(not self.lock);            
        end;
    end;
    
    -- # Section from sven18koehler # --
    if self.updateSteeringAxleAngle and self.enableIfReverse == "true" then
        if self.attacherVehicle ~= nil and self.movingDirection < 0 then
            local x,y,z = worldDirectionToLocal(self.steeringAxleNode, localDirectionToWorld(self.attacherVehicle.steeringAxleNode, 0, 0, 1));
            local dot = z;
            dot = dot / Utils.vector2Length(x,z);
            local angle = math.acos(dot);
            if x < 0 then
                angle = -angle;
            end;
            local startSpeed = self.steeringAxleAngleScaleStart;
            local endSpeed = self.steeringAxleAngleScaleEnd;
            local scale = Utils.clamp(1 + (self.lastSpeed*-3600-startSpeed) * 1.0/(startSpeed-endSpeed), 0, 1);
            self.steeringAxleAngle = angle*scale;
        end;
    end;
    --#--
end;

function LockSteeringaxles:updateTick(dt)
   self.updateTime = dt*0.0002;
	for i=1, table.getn(self.wheels) do
		local wheel = self.wheels[i];
		if self.lock == true then
			if wheel.steeringAxleRotMax > 0 then
				wheel.steeringAxleRotMax = Math.max(0,  wheel.steeringAxleRotMax - self.updateTime );
				wheel.steeringAxleRotMin = Math.min(0,  wheel.steeringAxleRotMax + self.updateTime );
				wheel.steeringAxleScale = Math.max(0, wheel.steeringAxleScale - self.updateTime );
				--print("langsam zumachen: steeringAxleRotMax", wheel.steeringAxleRotMax);
				--print("langsam zumachen: steeringAxleRotMin", wheel.steeringAxleRotMin);
			end;
		else
			if wheel.steeringAxleRotMax < wheel.backupMax then
				wheel.steeringAxleRotMax = Math.max(0,  wheel.steeringAxleRotMax + self.updateTime );
				wheel.steeringAxleRotMin = Math.min(0,  wheel.steeringAxleRotMax - self.updateTime );
				wheel.steeringAxleScale = Math.max(0, wheel.steeringAxleScale + self.updateTime );
				--print("langsam aufmachen: steeringAxleRotMax", wheel.steeringAxleRotMax);
				--print("langsam aufmachen: steeringAxleRotMin", wheel.steeringAxleRotMin);
			end;
		end;
   end;
end;

function LockSteeringaxles:lockAxles(lock, noEventSend)
    self.lock = lock;
    LockSteeringaxlesEvent.sendEvent(self, self.lock, noEventSend);
end;

function LockSteeringaxles:draw()
    if self.lock then
        g_currentMission:addHelpButtonText(g_i18n:getText("unlockSteeringaxles"), InputBinding.TEBBE_LOCK_AXLES);
    else
        g_currentMission:addHelpButtonText(g_i18n:getText("lockSteeringaxles"), InputBinding.TEBBE_LOCK_AXLES);
    end;
end;
                        
                        
                        
--####    EVENT    ####-- 
    
--@author    -    FIAT80-90DT                                                                
--@date        -    28.08.11                                            

LockSteeringaxlesEvent = {};
LockSteeringaxlesEvent_mt = Class(LockSteeringaxlesEvent, Event);

InitEventClass(LockSteeringaxlesEvent, "LockSteeringaxlesEvent");

function LockSteeringaxlesEvent:emptyNew()
    local self = Event:new(LockSteeringaxlesEvent_mt);
    self.className="LockSteeringaxlesEvent";
    return self;
end;

function LockSteeringaxlesEvent:new(vehicle, lock)
    local self = LockSteeringaxlesEvent:emptyNew()
    self.vehicle = vehicle;
    self.lock = lock;
    return self;
end;

function LockSteeringaxlesEvent:readStream(streamId, connection)
    local id = streamReadInt32(streamId);
    self.vehicle = networkGetObject(id);
    
    self.lock = streamReadBool(streamId);
    self:run(connection);
end;

function LockSteeringaxlesEvent:run(connection)
    self.vehicle:lockAxles(self.lock, true);
    if not connection:getIsServer() then
        g_server:broadcastEvent(LockSteeringaxlesEvent:new(self.vehicle, self.lock), nil, connection, self.vehicle);
    end;
end;

function LockSteeringaxlesEvent:writeStream(streamId, connection)
    streamWriteInt32(streamId, networkGetObjectId(self.vehicle));
    streamWriteBool(streamId, self.lock);
end;

function LockSteeringaxlesEvent.sendEvent(vehicle, lock, noEventSend)
    if noEventSend == nil or noEventSend == false then
        if g_server ~= nil then
            g_server:broadcastEvent(LockSteeringaxlesEvent:new(vehicle, lock), nil, nil, vehicle);
        else
            g_client:getServerConnection():sendEvent(LockSteeringaxlesEvent:new(vehicle, lock));
        end;
    end;
end;

Face
GIANTS Software | Lead Gameplay Programmer
Posts: 128
Joined: Sun Jun 17, 2007 8:21 pm

Re: Sperrbare Lenkachse V2

Post by Face » Tue May 20, 2014 9:32 am

#self.wheels ist das gleiche wie table.getn(self.wheels) sofern die wheels per table.insert eingefügt wurden

Post Reply