Skip to content

Instantly share code, notes, and snippets.

@LeonMrBonnie
Created July 21, 2019 16:31
Show Gist options
  • Save LeonMrBonnie/a9336e0be7eaed0cffdc030cd2925494 to your computer and use it in GitHub Desktop.
Save LeonMrBonnie/a9336e0be7eaed0cffdc030cd2925494 to your computer and use it in GitHub Desktop.
RageMP Door Class by LeonMrBonnie
//Made by LeonMrBonnie
let _Doors = [];
/**
* Door class for handling game doors
* @author LeonMrBonnie
*/
class Door
{
/**
* Creates a new door
* @author LeonMrBonnie
* @param {String} name Door display name
* @param {String} identifier Door identifier (must be unique)
* @param {Number} hash Door hash
* @param {Number} x Door X position
* @param {Number} y Door Y position
* @param {Number} z Door Z position
* @param {Boolean} locked Initial door locked state
*/
constructor(name, identifier, hash, x, y, z, locked = false)
{
if(getDoorFromIdentifier(identifier) !== undefined) throw Error(`The door identifier '${identifier}' is already in use! (${name})`);
this.DoorIdentifier = identifier;
this.DoorDisplayName = name;
this.DoorHash = hash;
this.DoorX = x;
this.DoorY = y;
this.DoorZ = z;
this.DoorLocked = locked;
this.ColShape = mp.colshapes.newSphere(x, y, z, 1.0);
this.EventFunction = (shape) =>
{
if(shape === this.ColShape) return mp.events.call("Doors_DoorOpened", this);
}
mp.events.add("playerExitColshape", this.EventFunction);
if(!mp.game.object.doesDoorExist(hash)) mp.game.object.addDoorToSystem(hash, hash, x, y, z, true, true, true);
mp.game.object.doorControl(hash, x, y, z, locked, 0.0, 50.0, 0.0);
_Doors.push(this);
}
get Name()
{
return this.DoorDisplayName;
}
set Name(name)
{
this.DoorDisplayName = name;
}
get ID()
{
return this.DoorIdentifier;
}
get Position()
{
return new mp.Vector3(this.DoorX, this.DoorY, this.DoorZ);
}
/**
* Sets the door lock status for the local player
* @author LeonMrBonnie
* @param {Boolean} state Door lock status
*/
setLocked(state)
{
this.DoorLocked = state;
mp.game.object.doorControl(this.DoorHash, this.DoorX, this.DoorY, this.DoorZ, state, 0.0, 50.0, 0.0);
}
/**
* Returns the current lock status of the door
* @author LeonMrBonnie
* @returns {Boolean} Door lock status
*/
isLocked()
{
return this.DoorLocked;
}
/**
* Deletes the door
* @author LeonMrBonnie
*/
destroy()
{
let idx = _Doors.indexOf(this);
if(idx === -1) throw Error(`This door object doesn't exist anymore!`);
_Doors.splice(idx, 1);
mp.game.object.removeDoorFromSystem(this.DoorHash);
mp.events.remove("playerExitColshape", this.EventFunction);
}
}
/**
* Gets a door from the identifier
* @author LeonMrBonnie
* @param {String} id Door identifier
* @returns {Door|undefined} Door object, or if none found undefined
*/
function getDoorFromIdentifier(id)
{
for(let i = 0; i < _Doors.length; i++)
{
if(_Doors[i].ID === id) return _Doors[i];
}
return undefined;
}
/**
* Gets a door from a position
* @author LeonMrBonnie
* @param {Number} x X position
* @param {Number} y Y position
* @param {Number} z Z position
* @param {Number} range Search range
* @returns {Door|undefined} Door object, or if none found undefined
*/
function getDoorFromPosition(x, y, z, range = 5.0)
{
let found = undefined, dist, lastDist = 99, pos;
for(let i = 0; i < _Doors.length; i++)
{
pos = _Doors[i].Position;
dist = mp.game.system.vdist(x, y, z, pos.x, pos.y, pos.z);
if(dist < range && dist < lastDist) found = _Doors[i], lastDist = dist;
}
return found;
}
mp.events.add("Doors_DoorOpened", (door) =>
{
mp.game.graphics.notify(`Du hast die Tür ${door.Name} betreten!`);
});
exports.Door = Door;
exports.getDoorFromID = getDoorFromIdentifier;
exports.getDoorFromPosition = getDoorFromPosition;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment