All pastes #1418491 Raw Edit

Tiller

public text v1 · immutable
#1418491 ·published 2009-05-10 19:00 UTC
rendered paste body
--
-- Tiller
-- Class for all Tillers
--
-- @author  Stefan Geiger (mailto:sgeiger@giants.ch)
-- @modified Sam Cappelle (mailto:sam@samcappelle.be)
-- @date  14/08/2008

Tiller = {};

function Tiller.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Cultivator,specializations);
    end;
	function Tiller:load(xmlFile)
    
	
	self.drumNode = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.drum#index"));
	self.threshingParticleSystems1 = {};
    local psName = "vehicle.threshingParticleSystem1";
    Utils.loadParticleSystem(xmlFile, self.threshingParticleSystems1, psName, self.rootNode, false)
	self.threshingParticleSystems2 = {};
    local psName = "vehicle.threshingParticleSystem2";
    Utils.loadParticleSystem(xmlFile, self.threshingParticleSystems2, psName, self.rootNode, false)
	self.threshingParticleSystems3 = {};
    local psName = "vehicle.threshingParticleSystem3";
    Utils.loadParticleSystem(xmlFile, self.threshingParticleSystems3, psName, self.rootNode, false)
	self.threshingParticleSystems4 = {};
    local psName = "vehicle.threshingParticleSystem4";
    Utils.loadParticleSystem(xmlFile, self.threshingParticleSystems4, psName, self.rootNode, false)
	
	local workSound = getXMLString(xmlFile, "vehicle.workSound#file");
    if workSound ~= nil and workSound ~= "" then
        self.workSound = createSample("workSound");
        loadSample(self.workSound, workSound, false);
        self.workSoundPitchOffset = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.workSound#pitchOffset"), 0);
        self.workSoundPitchScale = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.workSound#pitchScale"), 0.05);
        self.workSoundPitchMax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.workSound#pitchMax"), 2.0);
    end;
	
    delete(xmlFile);

    self.hasGroundContact = false;
	self.isActive = false;
    self.speedViolationMaxTime = 2500;
    self.speedViolationTimer = self.speedViolationMaxTime;
	
    return self;
end;

function Tiller:delete()
    removeContactReport(self.rootNode);
    Implement.delete(self);
end;

function Tiller:mouseEvent(posX, posY, isDown, isUp, button)
    Implement.mouseEvent(self, posX, posY, isDown, isUp, button);
end;

function Tiller:keyEvent(unicode, sym, modifier, isDown)
    Implement.keyEvent(self, unicode, sym, modifier, isDown);
end;

function Tiller:update(dt)

    if self.isAttached and self.isSelected then
        if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA) then
           self.isActive = not self.isActive;
        end;
    end;
		
	if self.isAttached and self.isActive and self.drumNode ~= nil then
        rotate(self.drumNode, 0.3 + (50 * self.attacherVehicle.lastSpeed), 0, 0);  
    end;
			
	if self.hasGroundContact and self.isActive and table.getn(self.cuttingAreas) then
            local x,y,z = getWorldTranslation(self.cuttingAreas[1].start);
            local x1,y1,z1 = getWorldTranslation(self.cuttingAreas[1].width);
            local x2,y2,z2 = getWorldTranslation(self.cuttingAreas[1].height);
            Utils.updateCultivatorArea(x, z, x1, z1, x2, z2);
			Utils.destroyCompleteArea(x,z,x1,z1,x2,z2);
	end;		
	
	if self.isActive then
		if not self.workSoundRunning then
   	 	playSample(self.workSound, 0, 1.6, 0);
    	self.workSoundRunning = true;
		end;
	end;	
	
	if not self.isActive then
		if self.workSoundRunning then
        stopSample(self.workSound, 0, 1.6, 0);
        self.workSoundRunning = false;
		end;
	end;
				
	if self.hasGroundContact and self.isActive then
 			Utils.setEmittingState(self.threshingParticleSystems1, true);
			Utils.setEmittingState(self.threshingParticleSystems2, true);
			Utils.setEmittingState(self.threshingParticleSystems3, true);
			Utils.setEmittingState(self.threshingParticleSystems4, true);
    end;
	
	if not self.hasGroundContact or not self.isActive then
 			Utils.setEmittingState(self.threshingParticleSystems1, false);
			Utils.setEmittingState(self.threshingParticleSystems2, false);
			Utils.setEmittingState(self.threshingParticleSystems3, false);
			Utils.setEmittingState(self.threshingParticleSystems4, false);
    end;

    if self.isAttached and self.hasGroundContact then
        if self.attacherVehicle.lastSpeed*3600 > 20 then
            self.speedViolationTimer = self.speedViolationTimer - dt;
            if self.speedViolationTimer < 0 then
                self.attacherVehicle:detachImplementByObject(self);
            end;
        else
            self.speedViolationTimer = self.speedViolationMaxTime;
        end;
    else
        self.speedViolationTimer = self.speedViolationMaxTime;
    end;
    Implement.update(self, dt);
	
end;

function Tiller:draw()

    Implement.draw(self);
 
    if self.isActive then
        g_currentMission:addHelpButtonText("Kuhn Frse ausschalten", InputBinding.IMPLEMENT_EXTRA);
    else
        g_currentMission:addHelpButtonText("Kuhn Frse einschalten", InputBinding.IMPLEMENT_EXTRA);
    end;

    if math.abs(self.speedViolationTimer - self.speedViolationMaxTime) > 2 then
        g_currentMission:addWarning("Fahren Sie nicht zu schnell\nTempomat Stufe 1: Taste 1", 0.07+0.022, 0.019+0.029);
    end;
	
end;

function Tiller:onAttach(attacherVehicle)

    Implement.onAttach(self, attacherVehicle);
    addContactReport(self.rootNode, "groundContactReport", self);
	
end;

function Tiller:onDetach()

    Implement.onDetach(self);

    self.speedViolationTimer = self.speedViolationMaxTime;

    removeContactReport(self.rootNode);
	
	Utils.setEmittingState(self.threshingParticleSystems1, false);
	Utils.setEmittingState(self.threshingParticleSystems2, false);
	Utils.setEmittingState(self.threshingParticleSystems3, false);
	Utils.setEmittingState(self.threshingParticleSystems4, false);
	
	if self.workSoundRunning then
    stopSample(self.workSound, 0, 1.6, 0);
    self.workSoundRunning = false;
	end;
	
end;

function Tiller:groundContactReport(objectId, otherObjectId, isStart, normalForce, tangentialForce)

    if otherObjectId == g_currentMission.terrainRootNode then
        self.hasGroundContact = normalForce > 0 or tangentialForce > 0;
    end;
	
end;

function Utils.destroyCompleteArea(startWorldX, startWorldZ, widthWorldX, widthWorldZ, heightWorldX, heightWorldZ)

	local meadowId 		 = g_currentMission.meadowId;
	local cuttedMeadowId = g_currentMission.cuttedMeadowId;
	Utils.updateDensity(meadowId, startWorldX, startWorldZ, widthWorldX, widthWorldZ, heightWorldX, heightWorldZ, 0, 0);
	Utils.updateDensity(cuttedMeadowId, startWorldX, startWorldZ, widthWorldX, widthWorldZ, heightWorldX, heightWorldZ, 0, 0);
	
end;