Here's the way I did it. My building has 11 floors that the elevators go to. I have eleven triggers for my elevator, one per floor. I gave the triggers a script_noteworthy value of the floor it pointed to.
For example, the trigger that takes you to the third floor has the key of "script_worthy", and a value of "3".
The triggers all had targets of the script_brushmodels I used for the buttons. I'm sure there was a better way to do it, but it works. =)
My elevator entity has a variable called "moving", and one called "atfloor". Those are pretty self explanatory as to what they are for.
Here's the core of the code
Code:
towerInit()
{
eleva.cab.cab = getent("acab", "targetname");
eleva.atfloor = 1; //elevator starts out at level 1
eleva.moving = false;
eleva.buttons = getentarray("buttonatrig", "targetname");
for(i=0; i < eleva.buttons.size; i++)
{
ent = getent(eleva.buttons[i].target, "targetname");
eleva.buttons[i] enablelinkto();
eleva.buttons[i] linkto(ent);
switch (eleva.buttons[i].script_noteworthy)
{
case "1": eleva.buttons[i].script_noteworthy = 1;
break;
case "2": eleva.buttons[i].script_noteworthy = 2;
break;
case "3": eleva.buttons[i].script_noteworthy = 3;
break;
case "4": eleva.buttons[i].script_noteworthy = 4;
break;
case "5": eleva.buttons[i].script_noteworthy = 5;
break;
case "6": eleva.buttons[i].script_noteworthy = 6;
break;
case "7": eleva.buttons[i].script_noteworthy = 7;
break;
case "8": eleva.buttons[i].script_noteworthy = 8;
break;
case "9": eleva.buttons[i].script_noteworthy = 9;
break;
case "10": eleva.buttons[i].script_noteworthy = 10;
break;
default: eleva.buttons[i].script_noteworthy = 0;
break;
}
eleva.buttons[i] thread buttonThink(eleva);
}
}
buttonThink(elevator)
{
while(1)
{
self waittill("trigger");
if ((elevator.atfloor != self.script_noteworthy) && (!elevator.moving))
{
elevator.moving = true;
closeDoor(elevator);
direction = self.script_noteworthy - elevator.atfloor;
moveCab(elevator, direction);
elevator.atfloor = elevator.atfloor + direction;
openDoor(elevator);
elevator.moving = false;
}
else
wait .1;
}
}
moveCab(elevator, direction)
{
if (direction < 0)
{
speed = direction * -2;
}
else
{
speed = direction * 2;
}
elevator.cab.cab movez((136*direction), speed, 1, 1);
for(i=0; i < elevator.buttons.size; i++)
{
ent = getent(elevator.buttons[i].target, "targetname");
ent movez((136*direction), speed, 1, 1);
}
elevator.cab.cab waittill("movedone");
}
I left out the openDoor() and closeDoor() functions because those really only apply to my elevator, and are more confusing to explain than they're probably worth in this situation.
The thing about this code is it's pretty easily modified to add or remove floors. In fact, my tower started out as 9 floors, but I added roof access and a basement. Changing the code to add in the two floors only took a few seconds.
Basically, what it does is it knows the floor the elevator is at. If you shoot the button to go to the third floor, it takes the floor number that you want to go to, and subtracts from that the floor that you're at.
IE, elevator at floor 4, you want to go to 1.
eleva.atfloor = 4;
self.script_noteworthy = 1;
direction = self.script_noteworthy - eleva.atfloor;
direction = -3;
It now knows it has to move down three floors. There are 136 units between each floor, so it moves -3*136, or 408 units down.
Hopefully that made sense and is actually helpful.
edited on Aug. 4, 2007 04:55 am by (VM)Monkey