Skip to content

Instantly share code, notes, and snippets.

@DeadSuperHero
Last active June 6, 2020 05:48
Show Gist options
  • Save DeadSuperHero/65e3a1880f9f3cf3ed22e076ac0bfca0 to your computer and use it in GitHub Desktop.
Save DeadSuperHero/65e3a1880f9f3cf3ed22e076ac0bfca0 to your computer and use it in GitHub Desktop.
Door Switch / State function for AGS
// Note: this example assumes that you have a door object in your room with the ID of 0, or name of oDoor.
// Another assumption is that the WalkableArea value that connects the indoors and outdoors has an ID of 3
// Effectively, all this script does is checks and sets states based on when the player steps on a given region.
// Keep in mind that the Object Properties are prepared outside of this script. The properties are:
// "open", and "inside_building", both boolean values.
// These properties are set based on which region the player steps on.
// It's assumed that region 1 is inside the doorway, region 2 is outside it.
// Each region, upon being stepped on, switches itself off and turns on the other one
// while setting the "inside_building" state.
// One last assumption: Currently, the "animations" only consist of a single initial frame, so no actual animation
// is done in this example. I may update this later as I add animation into my own game to better demonstrate the
// "correct" way to animate door objects.
function openDoor()
// Sets the Door object to a specific loop in a set view, then sets an object propery of Open to True, and adjusts the baseline for the new door height
// Turns on a Walkable area that bridges indoors and outdoors
{
object[0].SetView(6, 1, 0);
oDoor.SetProperty("open", true);
object[0].Baseline = 160;
RestoreWalkableArea(3);
}
function closeDoor()
// Same thing as last function, but in reverse
// This time, we set the view to a closed door picture, set the value to false, adjust the baseline, and turn the walkable area off.
{
object[0].SetView(6, 0, 0);
oDoor.SetProperty("open", false);
object[0].Baseline = 145;
RemoveWalkableArea(3);
}
function oDoor_Interact()
// Checks the state of the Door object. If the "open" value is false, we'll set a view for an open door, set the "open" property to "true",
// Then we run the openDoor function!
{
if (object[0].GetProperty("open") == false) {
// The inside_building check is to determine whether or not the player character is outside or indoors, so that we know where to correctly move him to.
if (object[0].GetProperty("inside_building") == false) {
cEgo.Walk(49, 157, eBlock, eWalkableAreas);
cEgo.FaceObject(oDoor, eBlock);
openDoor();
cEgo.Walk(68, 138, eBlock, eWalkableAreas);
}
else {
cEgo.FaceObject(oDoor, eBlock);
openDoor();
cEgo.Walk(52, 145, eBlock, eWalkableAreas);
}
}
// Do the same thing as before, but backwards.
// Run the closeDoor function!
else {
closeDoor();
}
}
function region1_WalksOnto()
// We use a region to act as a switch; when the player steps onto it, the game registers the player as being inside the building
{
closeDoor();
oDoor.SetProperty("inside_building", true);
region[1].Enabled = false;
region[2].Enabled = true;
}
function region2_WalksOnto()
// Same idea, this time we denote the player as being outside instead.
{
oDoor.SetProperty("inside_building", false);
region[1].Enabled = true;
region[2].Enabled = false;
closeDoor();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment