Door (class)
A class is a blueprint for creating objects. Every door shares the same properties (x, y, isOpen) but with different values. When you write 'new Door(...)' you create a new instance with its own data.
class Door {
constructor(x, y, w, h, id, isKeyDoor) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.id = id; // Unique ID for each door
this.isKeyDoor = isKeyDoor; // True if this is the "right key" door
this.isOpen = false; // Current state of the door
this.statusMessage = ""; // Message to display on the door (e.g., "Wrong key!")
this.messageTimer = 0; // Timer to make the message disappear
}
}
Line-by-line explanation (5 lines)
constructor(x, y, w, h, id, isKeyDoor) {- The constructor method runs once when a new Door is created, setting up all its properties.
this.x = x;- Stores the door's horizontal position on the canvas - 'this' means "this specific door object".
this.isKeyDoor = isKeyDoor;- Stores whether this particular door is the winning door (true) or a decoy (false).
this.isOpen = false;- Every door starts closed; this flag will be set to true when clicked correctly.
this.messageTimer = 0;- Countdown timer for displaying messages - when 0, no message shows; when > 0, decrement each frame.