Game Development Community

Chest Picklock

by MAY[] · in Game Design and Creative Issues · 06/05/2010 (9:06 am) · 0 replies

Hi everybody, today I`m finished my chest picklock system so I`m decited to share it with you. System is same as in Gothic / Risen rpg game...and its Beta version.

Here is code :
/// ----------------------------- ///
/// Chest System (c) MAY[] 2010 ///
/// -----------------------------///

// myhero - is name of player shape (%obj)
// hero - is datablock of shape "myhero" , should be renamed to playerData
// * function interaction(); have not key assignment

// Functions :


function pickLock(%item,%switch,%temp,%combination) // Base function
{
		if (%item.lockedd == false)
	      {
		%item.playThread(0,"open"); // Chest open animation ...
		/* Here will be code for showing items in chest... */
		   }		

	else 
		{   
	 	moveMap.unbind(mouse, button0);
	 	moveMap.unbind(mouse, button1);
	 	moveMap.bindCmd( mouse, button0, "pickLock($item,1,$pickLockTemp,$combination);","" );
   		moveMap.bindCmd( mouse, button1, "pickLock($item,2,$pickLockTemp,$combination);","" ); 
   		moveMap.bind( keyboard, e, returnToPlay);
   		myhero.setControlObject(%item); // disable player movement and binds mouse buttons for picklocking chest...
   			
	 if (%switch == 1) 
	 {
		 %temp++; 
		 $pickLockTemp =%temp;
		 %combination = %combination @"1 ";
		 $combination =%combination;
		 centerPrintAll("Left" ,0.5);
		 //sfxPlay(pickLock); 
		 %test = getword(%item.combination,%temp-1);
		 if (%test !$=1) %temp = %item.lockLevel ;
		 if (%temp == %item.lockLevel & %item.combination $= %combination)
				{ 
   				%item.lockedd=false; 
   				returnToPlay();
    			centerPrintAll("Chest unlocked.",2.5); 
    			pickLock($item);
    			}
    	if  (%temp == %item.lockLevel & %item.combination !$= %combination)
    			{
				centerPrintAll("Sorry, try again." ,0.5);
    			returnToPlay();
    			pickLock($item);
  				}	
  					
    }
    
    if (%switch == 2) 
    {
	    %temp++;
	    $pickLockTemp =%temp;
	    %combination = %combination@"2 ";
	    $combination =%combination;
	    centerPrintAll("Right" ,0.5);
	    //sfxPlay(pickLock);
    	  %test = getword(%item.combination,%temp-1);
		 if (%test !$=2) %temp = %item.lockLevel ;
    	 if (%temp == %item.lockLevel & %item.combination $= %combination)
			{		 
   			%item.lockedd=false; 
   			returnToPlay();
    		centerPrintAll("Chest unlocked.",2.5); 
    		pickLock($item);
    		}
     	if (%temp == %item.lockLevel & %item.combination !$= %combination)
     			{
	 			centerPrintAll("Sorry, try again." ,0.5);
	 			returnToPlay();
    			pickLock($item);
    			}
	}

	 }
} 
// This generating random combination by shape settings after game character collison...
function pickLockCombination(%level,%item,%temp)
{
 %temp++;
  if(%temp <= %level) {%random=getRandom(1,2); %item.combination=%item.combination@%random @ " "; pickLockCombination(%level,%item,%temp); }  
	 return %item.combination;
	}	
function returnToPlay()
{				
				myhero.setControlObject(myhero);
     			moveMap.unbind( mouse, button0);
   		        moveMap.unbind( mouse, button1); 
   		        moveMap.unbind(keyboard, e);
     			moveMap.bind( mouse, button0, mouseFire );
     			moveMap.bind( mouse, button1, altTrigger );
     			$pickLockTemp =0;
    			$combination ="";
	}	
			
// Chest shape :

datablock StaticShapeData(Chest)
{
   category = "Chests";
   shapeFile = "~/data/shapes/items/chest.dts"; 
   combination =""; // If blank - combination will be generated by pickLockCombination();
   lockLevel = 6; // Level of combination (6 means for example : L L L R R R) left, right...
   type = "Chest";
   pickUpName = "Simple chest";
   locked = true; // Chest is locked and it must be picklocked or opened by key.
      
};

// Item functions ...//

function Chest::onAdd(%this,%obj)
{
 %obj.combination = %this.combination; 
 %obj.lockLevel = %this.lockLevel;
 %obj.lockedd = %this.locked;
 // This add informations from datablock to shape...
}

// And interaction :

function hero::onCollision(%this,%obj,%col)
{
 // Chests ...
     if (%col.getDataBlock().type $= "Chest")
     { if (%col.combination $= "" &%col.lockedd == true ) // Combination is blank & chest is locked ...
     {pickLockCombination(%col.lockLevel,%col,0); // Random combination will be generated...
	     }
	  $interaction = "Chest"; // interaction is Chest type, so we can now call interacton(); function. (*)
      $item = %col;
	     } 
}  

function interaction()
{ switch$ ($interaction)
{	    
         
        case "Chest":
        pickLock($item);
	    
}
}