Doors and Keys

Posted By: mschoenhals

Doors and Keys - 08/26/15 16:15

Hi all,

I'm trying to create locking doors with keys. I can get a rotating door working just fine; I can create the lock and even the variable for unlocking but I can't get the key action to work correctly. I'm uncertain on how to switch my variable during the game. If I switch it in code, it works. Below is the variables, locking function and the door function. Can someone suggest a key action that would work with my code?

Code:
var have_key1 = false;  //beginning of game you don't have the key
var have_key2 = false;

STRING* got_key_wav = "gotkey.wav";  //sets the file name for getting a key
STRING* door_opens_wav = "dooropens.wav";  //sets the file name for opening the door

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function lock_check(){
	if(////////////////////////////////////
	(my.skill1 == 0) ||  //if the door is set to 0, it has no lock. The || means OR.
	(my.skill1 == 1 && have_key1) ||  //you have key one or....
	(my.skill1 == 2 && have_key2)  //my.skill1 is defined in the first skill of the key model.
	){return (false);}  //you either have the key or there is no lock
	else{
		return (true);
	}
	
}


action door(){  //this will appear as the behavior in WED
	
	set(me, POLYGON | SHADOW);  //polygon sets it for collision with player.
	
	var door_swing_speed = 5;  //variable speed for door swinging
	var closed_pan = my.pan;   //variable for closed door
	var OPEN_OR_CLOSED = 0; // -- 0 is closed
	if(is(my,FLAG1)) {OPEN_OR_CLOSED = 1;my.pan+=90;closed_pan = my.pan-90;} //if you check flag1 in WED, 1 is Open.
	
	VECTOR door_handle; //vector spot on the model, that's where the player needs to be closest to activate the door.
	vec_for_vertex(door_handle, me, 8);  //check what vertex to use for the door handle. This is defined in MED
	
	while(1){wait(1);
		
		if(lock_check() == false){  //lock check is defined above. If it returns false, you can open the door.
			
			
			if(vec_dist(door_handle.x, player.x) < 100 && key_enter == 1 && my.pan == closed_pan){  //this for a closed door being opened.
				set(me, PASSABLE);
				while(my.pan < closed_pan+90){wait(1);my.pan+=door_swing_speed;}
				SND_CREATE_STATIC(door_opens, door_opens_wav); // play the "door opens" sound
				snd_play(door_opens, 100, 0);
				reset(me, PASSABLE);  //when it is opening, it won't get hung up on things
				while(key_enter){wait(1);}
			}
			
			if(vec_dist(door_handle.x, player.x) < 100 && key_enter == 1 && my.pan == closed_pan+90){  //this for an open door being closed.
				set(me, PASSABLE);
				while(my.pan > closed_pan){wait(1);my.pan-=door_swing_speed;}
				SND_CREATE_STATIC(door_opens, door_opens_wav); // play the "door opens" sound
				snd_play(door_opens, 100, 0);
				reset(me, PASSABLE);
				while(key_enter){wait(1);}
			}
		}

	}
}

Posted By: Aku_Aku

Re: Doors and Keys - 08/26/15 20:38

What should do the key function?
Posted By: mschoenhals

Re: Doors and Keys - 08/26/15 20:46

change the variable have_key1 to true. If it's key 2, it should change the variable have_key2 to true.
Posted By: Aku_Aku

Re: Doors and Keys - 08/26/15 20:53

When should change that variable? What activity does trigger change?
Posted By: mschoenhals

Re: Doors and Keys - 08/26/15 21:37

When the player gets the key. So, if he comes within 50 quints, he picks up the key and can unlock the door.
Posted By: Aku_Aku

Re: Doors and Keys - 08/26/15 21:44

I think that is a state (possessing the key). Is there a variable that hold that state?
OK i can see, the 50 quants proximity is that state.
Will be get the key automatically?
Next day i can continue this conversion. Stay tuned...
Posted By: mschoenhals

Re: Doors and Keys - 08/27/15 00:42

Yup. Key should be taken automatically when the player runs over it.
Posted By: Aku_Aku

Re: Doors and Keys - 08/27/15 19:11

I assume, there is a function that controls the movement of the player.
I think, this function should check the status of player in each moment. When the function recognizes the proximity between the player and the key is 50 quants or less, then it have to set the relevant variable to store this state, perhaps in the my.skill1. From this point the action door will notice the change and will open the door, in my opinion.
I can imagine, may there are other solutions, so you don't need to accept this at the first sight. You can look up for other solutions, if it does not fit to your requirements. Or you can find a flaw in this logic...
Anyway, i hope this post helps you in your work.
Posted By: rayp

Re: Doors and Keys - 08/27/15 21:39

Did not read, but wanna leave this. Maybe it helps...
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=439108

Greets
© 2024 lite-C Forums