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
26 changes: 26 additions & 0 deletions packages/core/src/commands/addresource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Command } from '@wimaengine/command'
import { World } from '@wimaengine/ecs'

export class AddResourceCommand extends Command {

/**
* @readonly
* @type {object}
*/
resource

/**
* @param {object} resource
*/
constructor(resource) {
super()
this.resource = resource
}

/**
* @param {World} world
*/
execute(world) {
world.setResource(this.resource)
}
}
5 changes: 4 additions & 1 deletion packages/core/src/commands/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from './spawn'
export * from './addresource'
export * from './despawn'
export * from './removeresource'
export * from './setresourcealias'
export * from './spawn'
27 changes: 27 additions & 0 deletions packages/core/src/commands/removeresource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/** @import {Constructor} from '@wimaengine/type' */
import { Command } from '@wimaengine/command'
import { World } from '@wimaengine/ecs'

export class RemoveResourceCommand extends Command {

/**
* @readonly
* @type {Constructor}
*/
resourceType

/**
* @param {Constructor} resourceType
*/
constructor(resourceType) {
super()
this.resourceType = resourceType
}

/**
* @param {World} world
*/
execute(world) {
world.removeResource(this.resourceType)
}
}
35 changes: 35 additions & 0 deletions packages/core/src/commands/setresourcealias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/** @import {Constructor, TypeId} from '@wimaengine/type' */
import { Command } from '@wimaengine/command'
import { World } from '@wimaengine/ecs'

export class SetResourceAliasCommand extends Command {

/**
* @readonly
* @type {TypeId}
*/
id

/**
* @readonly
* @type {Constructor}
*/
alias

/**
* @param {TypeId} id
* @param {Constructor} alias
*/
constructor(id, alias) {
super()
this.id = id
this.alias = alias
}

/**
* @param {World} world
*/
execute(world) {
world.setResourceAlias(this.id, this.alias)
}
}
1 change: 1 addition & 0 deletions packages/core/src/core/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './schedules'
export * from './runner'
export * from './entity'
export * from './resourcecommands'
export * from './systemgroups'
export * from './snapshot'
export * from './worlds'
51 changes: 51 additions & 0 deletions packages/core/src/core/resourcecommands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/** @import {Constructor, TypeId} from '@wimaengine/type' */
import { CommandQueue } from '@wimaengine/command'
import { World } from '@wimaengine/ecs'
import { AddResourceCommand, RemoveResourceCommand, SetResourceAliasCommand } from '../commands'

export class ResourceCommands {

/**
* @private
* @type {CommandQueue}
*/
buffer

/**
* @param {World} world
*/
constructor(world) {
this.buffer = world.getResource(CommandQueue)
}

/**
* @param {object} resource
* @returns {this}
*/
add(resource) {
this.buffer.add(new AddResourceCommand(resource))

return this
}

/**
* @param {Constructor} resourceType
* @returns {this}
*/
remove(resourceType) {
this.buffer.add(new RemoveResourceCommand(resourceType))

return this
}

/**
* @param {TypeId} id
* @param {Constructor} alias
* @returns {this}
*/
setAlias(id, alias) {
this.buffer.add(new SetResourceAliasCommand(id, alias))

return this
}
}
53 changes: 53 additions & 0 deletions packages/core/tests/resourcecommands.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { strictEqual } from 'node:assert'
import { describe, test } from 'vitest'
import { CommandQueue } from '@wimaengine/command'
import { World } from '@wimaengine/ecs'
import { typeid } from '@wimaengine/type'
import { ResourceCommands } from '../src/index.js'
import { executeCommands } from '../src/systems/index.js'

class TestResource { }
class TestAlias extends TestResource { }

describe('Testing `ResourceCommands`', () => {
test('`add()` adds a resource when commands execute', () => {
const world = new World()
const resource = new TestResource()

world.setResource(new CommandQueue())
const commands = new ResourceCommands(world)

commands.add(resource)
executeCommands(world)

strictEqual(world.getResource(TestResource), resource)
})

test('`remove()` removes a resource when commands execute', () => {
const world = new World()

world.setResource(new CommandQueue())
world.setResource(new TestResource())
const commands = new ResourceCommands(world)

commands.remove(TestResource)
executeCommands(world)

strictEqual(world.hasResource(TestResource), false)
})

test('`setAlias()` resolves a resource through an alias when commands execute', () => {
const world = new World()
const resource = new TestResource()
const id = typeid(TestResource)

world.setResource(resource)
world.setResource(new CommandQueue())
const commands = new ResourceCommands(world)

commands.setAlias(id, TestAlias)
executeCommands(world)

strictEqual(world.getResource(TestAlias), resource)
})
})