Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
} from "@interfaces";
import type { GridPosition, Position } from "@types";
import { Entity, GameMap } from "@world";
import { checkCollisions, convertPositionToGridPosition, getInPosition } from "@utils";
import { checkCollisions, convertPositionToGridPosition } from "@utils";
import { FactoryKeys } from "@enums";

export class EntityManager implements Manager {
Expand Down Expand Up @@ -165,7 +165,7 @@ export class EntityManager implements Manager {
const entity = this.get(id)

if (!entity) return false
if (!getInPosition(entity.position, Array.from(this.entities.values()))) return false
if (!(this.gameMap.getAllInPosition(entity.position, 'ENTITES').find(e => e.id === entity.id))) return false

return !checkCollisions(this.gameMap.getAllInPosition(entity.position), entity)
}
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/core/engine/classes-engine.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export interface IGameMap {
/**
* Get one object by id
* @param id - ID of object
* @returns { GameObject | undefined } - GameObject if founded, else false
* @returns { GameObject | undefined } - GameObject if founded, else undefined
*/
readonly getObject: (id: number) => GameObject | undefined;

Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/entity/pickup-guard.middleare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const PickUpGuard: MiddlewareFn = (cmd, next, game, ctx) => {
position
}

const item = getItemInPosition(position, game.options.map.getAllItems())
const item = getItemInPosition(game.options.map.getAllInPosition(position, 'OBJECTS'))

if (!item) anyErrorData = {
reason: ITERACTION_ERRORS.NOT_FOUND,
Expand Down
14 changes: 0 additions & 14 deletions src/utils/geometry/getters/getters.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
import type { Position, Quad } from "@types"
import { checkTwoPositions } from "@utils"
import type { Entity, GameObject } from "@world"

/**
* Find one Target in provided Position
* @param position - Position to find Target
* @param entities - Targets array to searching
* @returns {Target | undefined} - Target if founded, else undefined
*/
export function getInPosition(position: Position, objects: GameObject[]): GameObject | undefined
export function getInPosition(position: Position, entities: Entity[]): Entity | undefined
export function getInPosition(position: Position, entities: (Entity | GameObject)[]): (Entity | GameObject) | undefined {
return entities.find((entity) => checkTwoPositions(position, entity.position))
}

/**
* Get a central position of Quad
Expand Down
5 changes: 2 additions & 3 deletions src/utils/logic/getters/world-getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import type { GameObject } from "@world"

/**
* Return a Item in provided Position, else undefined if not found
* @param position - Position to search
* @param objects - GameObject to searching in
* @returns {IWorldItem | undefined} - Item if found, else undefined
*/
export function getItemInPosition(position: Position, objects: (IWorldItem & IGameObject)[]): IWorldItem | undefined {
const obj = objects.find((obj) => (checkTwoPositions(position, obj.position) && gameObjectIsItem(obj)))
export function getItemInPosition(objects: (IWorldItem & IGameObject)[]): IWorldItem | undefined {
const obj = objects.find((obj) => gameObjectIsItem(obj))

return obj ? convertGameObjectToInventoryItem(obj) : undefined
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AnyPosition, Position, Quad } from "@types"
import type { AnyPosition, GridPosition, Position, Quad } from "@types"

/**
* Check AnyPosition is Quad
Expand Down Expand Up @@ -26,4 +26,13 @@ export function positionIsPosition(position: AnyPosition): position is Position

if (x === undefined || y === undefined) return false
else return (!isNaN(x) && !isNaN(y) && position.length === 2) ? true : false
}

/**
* Checks given position is GridPosition
* @param position - Position to check
* @returns { boolean } - True if provided position is GridPosition, else false
*/
export function positionIsGridPosition(position: Position | GridPosition): position is GridPosition {
return typeof position === "string" && position.split(":").length > 0
}
2 changes: 1 addition & 1 deletion src/world/entities/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class Entity implements ITarget {
* @returns { IWorldItem } - IWorldItem
*/
public pickUp(position: Position): IWorldItem {
const item = getItemInPosition(position, this.map.getAllItems())!
const item = getItemInPosition(this.map.getAllInPosition(position, 'OBJECTS'))!

this.inventory.push(item)
this.map.deleteObject(item.id)
Expand Down
7 changes: 3 additions & 4 deletions src/world/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ import type {
} from "@types";
import {
convertAnyPositionToPosition,
gameObjectIsItem,
getInPosition,
gameObjectIsItem,
createQuadFromPosition,
checkCollisions,
convertPositionToGridPosition
convertPositionToGridPosition,
} from "@utils";
import { Entity, GameObject } from "@world";
import { BASE_HEARING_RADIUS } from "@const";
Expand Down Expand Up @@ -323,7 +322,7 @@ export class GameMap implements Map {
const object = this.getObject(id)

if (!object) return false
if (!getInPosition(object.position, Array.from(this.objects.values()))) return false
if (!(this.grid.get(convertPositionToGridPosition(object.position))?.has(object))) return false

return !checkCollisions(this.getAllInPosition(object.position), object)
}
Expand Down
Loading