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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "litecanvas",
"version": "0.201.0",
"version": "0.202.0",
"description": "Lightweight HTML5 canvas 2D game engine suitable for small projects and creative coding. Inspired by PICO-8 and p5.js/Processing.",
"license": "MIT",
"author": "Luiz Bills <luizbills@pm.me>",
Expand Down
19 changes: 9 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1257,14 +1257,11 @@ export default function litecanvas(settings = {}) {
/**
* Returns information about the engine instance.
*
* @param {number|string} index
* @param {number} index
* @returns {any}
*/
stat(index) {
DEV: assert(
isNumber(index) || 'string' === typeof index,
loggerPrefix + 'stat() 1st param must be a number or string'
)
DEV: assert(isNumber(index), loggerPrefix + 'stat() 1st param must be a number')

const internals = [
// 0
Expand Down Expand Up @@ -1297,12 +1294,14 @@ export default function litecanvas(settings = {}) {
_fontLineHeight,
]

const data = { index, value: internals[index] }

// plugins can modify or create new stats
instance.emit('stat', data)
DEV: assert(
index >= 0 && index < internals.length,
loggerPrefix +
'stat() 1st param must be a number between 0 and ' +
(internals.length - 1)
)

return data.value
return internals[index]
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 11 additions & 40 deletions tests/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,52 +233,23 @@ test('stat(12) returns the current color palette state modified by palc()', (t)
}
})

test('stat modified via event', async (t) => {
const expected = 'BAR'
test('stat(13) returns the current font line height factor', (t) => {
const expected = 2

await onLitecanvas(local, 'init', () => {
local.listen('stat', (data) => {
// modify only the stat(0)
if (0 === data.index) {
data.value.foo = expected
}
})
local.textgap(expected)

const actual = local.stat(0).foo
t.is(actual, expected)
})
const actual = local.stat(13)
t.deepEqual(actual, expected)
})

test('number stat created via event', async (t) => {
await onLitecanvas(local, 'init', () => {
const customIndex = 42
const expected = 'The answer to the Ultimate Question of Life, the Universe, and Everything'

local.listen('stat', (data) => {
// create a value for stat(42)
if (customIndex === data.index) {
data.value = expected
}
})
test('stat with invalid indexes must throw error in dev mode', (t) => {
const last = 13

const actual = local.stat(customIndex)
t.is(actual, expected)
t.throws(() => {
local.stat(last + 1)
})
})

test('string stat created via event', async (t) => {
await onLitecanvas(local, 'init', () => {
const customIndex = 'forty-two'
const expected = 'The answer to the Ultimate Question of Life, the Universe, and Everything'

local.listen('stat', (data) => {
// create a value for stat(42)
if (customIndex === data.index) {
data.value = expected
}
})

const actual = local.stat(customIndex)
t.is(actual, expected)
t.throws(() => {
local.stat(-1)
})
})
3 changes: 1 addition & 2 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,10 @@ declare global {
* - n = 11: the current font family
* - n = 12: the current state of the color palette
* - n = 13: the current font gap
* - n = *any other value*: probably returns undefined
*
* @param index
*/
function stat(index: number | string): any
function stat(index: number): any
/**
* Pauses the engine loop (update & draw).
*/
Expand Down
3 changes: 1 addition & 2 deletions types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,11 +618,10 @@ type LitecanvasInstance = {
* - n = 11: the current font family
* - n = 12: the current state of the color palette
* - n = 13: the current font gap
* - n = *any other value*: probably returns undefined
*
* @param index
*/
stat(index: number | string): any
stat(index: number): any
/**
* Pauses the engine loop (update & draw).
*/
Expand Down
Loading