var is_open = false;
var is_locked = false;
var is_prevented = false;
var initial_rotation_state = Vector3(0, 0, 0);
var current_tween = null;
var start_sound = null;
var stop_sound = null;
var start_close_sound = null;
var stop_close_sound = null;
func Toggle(_param = null):
if is_open:
Close(_param);
else:
Open(_param);
The code you've provided defines a script for a door entity in a game, likely using a game engine like Godot or similar. It allows the door to be opened, closed, locked, and unlocked, while also incorporating sound effects and tween animations for smooth movement. Let’s break down the key components of the code:
1. Flags and Initialization
gdscript
const FLAG_USE_OPENS = 256;
const FLAG_REVERSE = 2;
const FLAG_X_AXIS = 64;
const FLAG_Z_AXIS = 128;
const FLAG_STARTS_LOCKED = 2048;
const FLAG_ONE_WAY = 16;
const FLAG_TOUCH_OPENS = 1024;
const FLAG_TOGGLE = 32;
var is_open = false;
var is_locked = false;
var is_prevented = false;
var initial_rotation_state = Vector3(0, 0, 0);
var current_tween = null;
var start_sound = null;
var stop_sound = null;
var start_close_sound = null;
var stop_close_sound = null;
This section declares various constants that represent different states or behaviors of the door, such as whether it starts locked or if it opens with a toggle. The door's state is tracked with boolean variables (e.g., is_open, is_locked), and it has variables for sound effects and tween animations.
This function preloads sound effects used for opening and closing the door. It retrieves sound file names via an entity object and caches them for performance.
3. Interactions
gdscript
func _interact(_player: Player):
if not has_flag(FLAG_USE_OPENS): return;
if not has_flag(FLAG_TOGGLE) and is_open: return;
if is_locked:
trigger_output("OnLockedUse");
return;
Toggle();
This function manages player interactions with the door. It checks if the door can be opened (e.g., whether it starts locked or is already open) and triggers the toggling function.
4. Movement Logic
gdscript
func move_door(progress: float = 0.0):
var speed = float(entity.get("speed", 0));
var rotation_distance = float(entity.get("distance", 90.0));
var target_rotation = get_target_rotation(progress);
// Handle current tween
if current_tween:
is_prevented = true;
current_tween.stop();
// Log warning if speed is zero
if speed == 0.0:
Debugger.log("Door speed is 0, please set a speed value. Entity id " + str(entity.id));
return;
var move_time = rotation_distance / speed;
current_tween = create_tween();
current_tween.set_process_mode(Tween.TWEEN_PROCESS_PHYSICS);
current_tween.tween_property(self, "rotation", target_rotation, move_time);
await current_tween.finished;
current_tween = null;
This function is responsible for the actual movement of the door. It calculates how much the door should rotate based on the current speed and distance parameters set on the entity. A tween animation is created for smooth motion, and it handles stopping any existing animation if a new one is triggered.
5. Opening and Closing
gdscript
func Open(_param):
if is_open: return;
is_open = true;
trigger_output("OnOpen");
if start_sound: SoundManager.play_sound(global_position, start_sound);
await move_door(1.0);
if stop_sound: SoundManager.play_sound(global_position, stop_sound);
if not is_prevented:
trigger_output("OnFullyOpen");
else:
is_prevented = false;
func Close(_param):
if not is_open: return;
is_open = false;
trigger_output("OnClose");
if start_close_sound: SoundManager.play_sound(global_position, start_close_sound);
elif start_sound: SoundManager.play_sound(global_position, start_sound);
await move_door(0.0);
if stop_close_sound: SoundManager.play_sound(global_position, stop_close_sound);
elif stop_sound: SoundManager.play_sound(global_position, stop_sound);
if not is_prevented:
trigger_output("OnFullyClosed");
else:
is_prevented = false;
These functions handle the opening and closing of the door, respectively. They play appropriate sound effects, invoke the movement logic, and trigger output events upon completion.
These methods toggle the door's locked state and trigger corresponding outputs.
7. Toggle Functionality
gdscript
func Toggle(_param = null):
if is_open:
Close(_param);
else:
Open(_param);
The Toggle function allows the player to switch the door state between open and closed.
Overall, this script is designed to work with a game engine and provides a robust framework for managing door behavior, sound, and animations, suitable for interactive environments in video games.