@rpgjs/physic
Version:
A deterministic 2D top-down physics library for RPG, sandbox and MMO games
151 lines (150 loc) • 3.21 kB
JavaScript
import { World } from "./index23.js";
class Region {
/**
* Creates a new region
*
* @param config - Region configuration
*/
constructor(config) {
this.entities = /* @__PURE__ */ new Set();
this.bounds = config.bounds.clone();
this.overlap = config.overlap ?? 0;
this.active = config.active ?? true;
this.world = new World({
spatialCellSize: 50,
spatialGridWidth: 50,
spatialGridHeight: 50
});
}
/**
* Gets the region bounds
*
* @returns AABB bounds
*/
getBounds() {
return this.bounds.clone();
}
/**
* Gets the expanded bounds including overlap
*
* @returns Expanded AABB
*/
getExpandedBounds() {
return this.bounds.expand(this.overlap);
}
/**
* Checks if a point is inside this region
*
* @param point - Point to check
* @returns True if point is inside
*/
contains(point) {
return this.bounds.contains(point);
}
/**
* Checks if an entity should belong to this region
*
* @param entity - Entity to check
* @returns True if entity should be in this region
*/
shouldContain(entity) {
return this.bounds.contains(entity.position);
}
/**
* Adds an entity to this region
*
* @param entity - Entity to add
*/
addEntity(entity) {
if (this.entities.has(entity)) {
return;
}
this.entities.add(entity);
this.world.addEntity(entity);
}
/**
* Removes an entity from this region
*
* @param entity - Entity to remove
*/
removeEntity(entity) {
if (this.entities.delete(entity)) {
this.world.removeEntity(entity);
}
}
/**
* Gets all entities in this region
*
* @returns Array of entities
*/
getEntities() {
return Array.from(this.entities);
}
/**
* Steps the region's physics simulation
*/
step() {
if (!this.active) {
return;
}
this.world.step();
}
/**
* Activates this region
*/
activate() {
this.active = true;
}
/**
* Deactivates this region
*/
deactivate() {
this.active = false;
}
/**
* Checks if this region is active
*
* @returns True if active
*/
isActive() {
return this.active;
}
/**
* Gets the world instance for this region
*
* @returns World instance
*/
getWorld() {
return this.world;
}
/**
* Checks if this region overlaps with another region
*
* @param other - Other region to check
* @returns True if regions overlap
*/
overlaps(other) {
const expandedA = this.getExpandedBounds();
const expandedB = other.getExpandedBounds();
return expandedA.intersects(expandedB);
}
/**
* Gets entities that might need migration to neighboring regions
*
* @returns Array of entities near region boundaries
*/
getBoundaryEntities() {
const boundaryEntities = [];
const expandedBounds = this.getExpandedBounds();
for (const entity of this.entities) {
if (expandedBounds.contains(entity.position) && !this.bounds.contains(entity.position)) {
boundaryEntities.push(entity);
}
}
return boundaryEntities;
}
}
export {
Region
};
//# sourceMappingURL=index25.js.map