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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface IMovedData {
/**
* Entity position after move
*/
readonly newPosition: AnyPosition;
readonly position: AnyPosition;
}

export interface IPlaySoundData {
Expand Down
5 changes: 5 additions & 0 deletions src/interfaces/hooks/use-link.hook.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ export interface ILinkOptions {
* Auto delete link when
*/
readonly autoUnlinkOn?: UnlinkWhen[];

/**
* If true, register useLink as middleware
*/
readonly enableMiddleware?: boolean;
}
44 changes: 30 additions & 14 deletions src/utils/hooks/use-link.hook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { anyWorldObjectIsGameObject, convertAnyPositionToPosition } from "@utils";
import type { Linkable } from "@types";
import type { Linkable, MiddlewareFn, Position } from "@types";
import type { IDeadData, ILink, ILinkOptions, IMovedData, IUseValidationContext } from "@interfaces";
import { USE_VALIDATION_EVENT_PREFIX } from "@const";
import { CommandType } from "@enums";
Expand Down Expand Up @@ -35,32 +35,48 @@ export function useLink(from: Linkable, to: Linkable, options?: ILinkOptions): I
link: (options?: ILinkOptions) => link.isActive ? false : useLink(from, to, options)
} as ILink

if (childIsEntity && options?.maxDistance) move = game.registerCustomEvent<IMovedData & IUseValidationContext>(`${USE_VALIDATION_EVENT_PREFIX}:${CommandType.MOVE}`, (opt, event, data) => {
function checkMaximum(before: Position, after: Position): boolean {
const [x1, y1] = before
const [x2, y2] = after

return (x2-x1 > options?.maxDistance! || y2-y1 > options?.maxDistance!)
}

if (options?.enableMiddleware) game.use((cmd, next) => {
if (cmd.type !== CommandType.MOVE || typeof options.maxDistance === 'undefined') return next()
else return checkMaximum(from.position, cmd.data.position || cmd.data.newPosition) ? null : next()
})
else {
if (childIsEntity && options?.maxDistance) move = game.registerCustomEvent<IMovedData & IUseValidationContext>(`${USE_VALIDATION_EVENT_PREFIX}:${CommandType.MOVE}`, (opt, event, data) => {
if (
data.entity
&& !anyWorldObjectIsGameObject(data.entity)
&& data.entity.id === to.id
) {

const [x1, y1] = from.position
const [x2, y2] = convertAnyPositionToPosition(data.eventData.newPosition || (data.eventData as any).position)

if (x2-x1 > options!.maxDistance! || y2-y1 > options!.maxDistance!) {
if (checkMaximum(from.position, convertAnyPositionToPosition(data.eventData.position))) {
data.eventData.isAllowed = false
data.eventData.errors.push(`[useLink]: Max distance occured (${options!.maxDistance})`)

if (options.autoUnlinkOn?.includes("childOutOfRange")) link.unLink()
}
if (options.autoUnlinkOn?.includes("childOutOfRange")) link.unLink()
}
})
if (parentIsEntity && childIsEntity) {
})
if (parentIsEntity && childIsEntity) {
if (options?.killChild) kill = game.on<IDeadData>("entityDead", (opt, event, data) => {
if (data.eventData.entity.id === from.id) game.options.manager.kill(to.id)
if (options.autoUnlinkOn?.includes("parentKilled")) link.unLink()
if (data.eventData.entity.id === from.id) {
game.options.manager.kill(to.id)

if (options.autoUnlinkOn?.includes("parentKilled")) link.unLink()
}
})
if (options?.deleteChild) deleting = game.on<{}>("entityDeleted", (opt, event, data) => {
if (data.entity!.id === from.id) game.options.manager.delete(to.id)
if (options.autoUnlinkOn?.includes("parentDeleted")) link.unLink()
if (data.entity!.id === from.id) {
game.options.manager.delete(to.id)

if (options.autoUnlinkOn?.includes("parentDeleted")) link.unLink()
}
})
}
}

return link
Expand Down
2 changes: 1 addition & 1 deletion src/world/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class GameMap implements Map {
eventData: {
entity,
startPosition: entity.position,
newPosition: to
position: to
}
})

Expand Down
6 changes: 3 additions & 3 deletions test/collision.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ describe('Collisions Tests', () => {
else if (name === 'sword3') collisionByChestWeightTest = true
})
game.on<IMovedData>('entityMoved', (o, e, d) => {
const { newPosition } = d.eventData
const { position } = d.eventData

if (positionIsPosition(newPosition)) {
if (checkTwoPositions(newPosition, [2, 0])) moveToDoesntCollisionObjectTest = true
if (positionIsPosition(position)) {
if (checkTwoPositions(position, [2, 0])) moveToDoesntCollisionObjectTest = true
}
})

Expand Down
4 changes: 2 additions & 2 deletions test/gametest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const SUPER_ZOMBIE = blueprintsFactory.register({
const killQuest = questsFactory.create({
name: "Move to 5,6",
injectEvents: ['entityMoved'],
onEvent: (options, event, data, self) => checkTwoPositions([5, 6], (data.eventData as IMovedData).newPosition as Position),
onEvent: (options, event, data, self) => checkTwoPositions([5, 6], (data.eventData as IMovedData).position as Position),
onComplete: (e) => {
console.log('QUEST COMPLETED FOR', e.name)
}
Expand Down Expand Up @@ -314,7 +314,7 @@ game.dispatch({
}
})

useLink(player, player_second, { maxDistance: 1, autoUnlinkOn: ["childOutOfRange", "parentDeleted", "parentKilled"], killChild: false, deleteChild: false })
useLink(player, player_second, { maxDistance: 1, autoUnlinkOn: ["childOutOfRange", "parentDeleted", "parentKilled"], killChild: false, deleteChild: false, enableMiddleware: true })

console.log(useValidation(player_second, CommandType.MOVE, {
newPosition: [7, 7]
Expand Down
Loading