mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-25 01:20:04 +00:00
Fix indentations
This commit is contained in:
parent
74ac6e1821
commit
b3d9ffd0bd
1 changed files with 449 additions and 480 deletions
111
src/init.luau
111
src/init.luau
|
@ -11,14 +11,14 @@ type ArchetypeId = number
|
||||||
|
|
||||||
type Column = { any }
|
type Column = { any }
|
||||||
|
|
||||||
type Map<K, V> = {[K]: V}
|
type Map<K, V> = { [K]: V }
|
||||||
|
|
||||||
type GraphEdge = {
|
type GraphEdge = {
|
||||||
from: Archetype,
|
from: Archetype,
|
||||||
to: Archetype?,
|
to: Archetype?,
|
||||||
prev: GraphEdge?,
|
prev: GraphEdge?,
|
||||||
next: GraphEdge?,
|
next: GraphEdge?,
|
||||||
id: number
|
id: number,
|
||||||
}
|
}
|
||||||
|
|
||||||
type GraphEdges = Map<i53, GraphEdge>
|
type GraphEdges = Map<i53, GraphEdge>
|
||||||
|
@ -27,7 +27,7 @@ type GraphNode = {
|
||||||
add: GraphEdges,
|
add: GraphEdges,
|
||||||
remove: GraphEdges,
|
remove: GraphEdges,
|
||||||
add_ref: GraphEdge?,
|
add_ref: GraphEdge?,
|
||||||
remove_ref: GraphEdge?
|
remove_ref: GraphEdge?,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Archetype = {
|
export type Archetype = {
|
||||||
|
@ -42,12 +42,12 @@ export type Archetype = {
|
||||||
type Record = {
|
type Record = {
|
||||||
archetype: Archetype,
|
archetype: Archetype,
|
||||||
row: number,
|
row: number,
|
||||||
dense: i24
|
dense: i24,
|
||||||
}
|
}
|
||||||
|
|
||||||
type EntityIndex = {
|
type EntityIndex = {
|
||||||
dense: Map<i24, i53>,
|
dense: Map<i24, i53>,
|
||||||
sparse: Map<i53, Record>
|
sparse: Map<i53, Record>,
|
||||||
}
|
}
|
||||||
|
|
||||||
type ArchetypeRecord = {
|
type ArchetypeRecord = {
|
||||||
|
@ -205,9 +205,7 @@ local function entity_index_new_id(entityIndex: EntityIndex, index: i24): i53
|
||||||
return id
|
return id
|
||||||
end
|
end
|
||||||
|
|
||||||
local function archetype_move(entity_index: EntityIndex, to: Archetype,
|
local function archetype_move(entity_index: EntityIndex, to: Archetype, dst_row: i24, from: Archetype, src_row: i24)
|
||||||
dst_row: i24, from: Archetype, src_row: i24)
|
|
||||||
|
|
||||||
local src_columns = from.columns
|
local src_columns = from.columns
|
||||||
local dst_columns = to.columns
|
local dst_columns = to.columns
|
||||||
local dst_entities = to.entities
|
local dst_entities = to.entities
|
||||||
|
@ -287,8 +285,7 @@ local function hash(arr: { number }): string
|
||||||
return table.concat(arr, "_")
|
return table.concat(arr, "_")
|
||||||
end
|
end
|
||||||
|
|
||||||
local world_get: (world: World, entityId: i53,
|
local world_get: (world: World, entityId: i53, a: i53, b: i53?, c: i53?, d: i53?, e: i53?) -> ...any
|
||||||
a: i53, b: i53?, c: i53?, d: i53?, e: i53?) -> ...any
|
|
||||||
do
|
do
|
||||||
-- Keeping the function as small as possible to enable inlining
|
-- Keeping the function as small as possible to enable inlining
|
||||||
local records
|
local records
|
||||||
|
@ -305,9 +302,7 @@ do
|
||||||
return columns[tr.column][row]
|
return columns[tr.column][row]
|
||||||
end
|
end
|
||||||
|
|
||||||
function world_get(world: World, entity: i53,
|
function world_get(world: World, entity: i53, a: i53, b: i53?, c: i53?, d: i53?, e: i53?): ...any
|
||||||
a: i53, b: i53?, c: i53?, d: i53?, e: i53?): ...any
|
|
||||||
|
|
||||||
local record = world.entityIndex.sparse[entity]
|
local record = world.entityIndex.sparse[entity]
|
||||||
if not record then
|
if not record then
|
||||||
return nil
|
return nil
|
||||||
|
@ -394,9 +389,7 @@ local function world_has(world: World, entity: number, ...: i53): boolean
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_target(world: World, entity: i53,
|
local function world_target(world: World, entity: i53, relation: i24, index: number): i24?
|
||||||
relation: i24, index: number): i24?
|
|
||||||
|
|
||||||
local record = world.entityIndex.sparse[entity]
|
local record = world.entityIndex.sparse[entity]
|
||||||
local archetype = record.archetype
|
local archetype = record.archetype
|
||||||
if not archetype then
|
if not archetype then
|
||||||
|
@ -454,7 +447,8 @@ local function id_record_ensure(world: World, id: number): IdRecord
|
||||||
|
|
||||||
local is_tag = not world_has_one_inline(world, relation, EcsComponent)
|
local is_tag = not world_has_one_inline(world, relation, EcsComponent)
|
||||||
|
|
||||||
flags = bit32.bor(flags,
|
flags = bit32.bor(
|
||||||
|
flags,
|
||||||
if on_add then ECS_ID_HAS_ON_ADD else 0,
|
if on_add then ECS_ID_HAS_ON_ADD else 0,
|
||||||
if on_remove then ECS_ID_HAS_ON_REMOVE else 0,
|
if on_remove then ECS_ID_HAS_ON_REMOVE else 0,
|
||||||
if on_set then ECS_ID_HAS_ON_SET else 0,
|
if on_set then ECS_ID_HAS_ON_SET else 0,
|
||||||
|
@ -482,7 +476,7 @@ local function archetype_append_to_records(
|
||||||
)
|
)
|
||||||
local tr = idr.cache[archetype_id]
|
local tr = idr.cache[archetype_id]
|
||||||
if not tr then
|
if not tr then
|
||||||
tr = { column = index, count = 1}
|
tr = { column = index, count = 1 }
|
||||||
idr.cache[archetype_id] = tr
|
idr.cache[archetype_id] = tr
|
||||||
idr.size += 1
|
idr.size += 1
|
||||||
records[id] = tr
|
records[id] = tr
|
||||||
|
@ -607,8 +601,7 @@ local function find_archetype_without(world: World, node: Archetype, id: i53): A
|
||||||
return archetype_ensure(world, dst)
|
return archetype_ensure(world, dst)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function archetype_init_edge(archetype: Archetype,
|
local function archetype_init_edge(archetype: Archetype, edge: GraphEdge, id: i53, to: Archetype)
|
||||||
edge: GraphEdge, id: i53, to: Archetype)
|
|
||||||
edge.from = archetype
|
edge.from = archetype
|
||||||
edge.to = to
|
edge.to = to
|
||||||
edge.id = id
|
edge.id = id
|
||||||
|
@ -666,29 +659,21 @@ local function init_edge_for_remove(world, archetype, edge, id, to)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function create_edge_for_add(world: World, node: Archetype,
|
local function create_edge_for_add(world: World, node: Archetype, edge: GraphEdge, id: i53): Archetype
|
||||||
edge: GraphEdge, id: i53): Archetype
|
|
||||||
|
|
||||||
local to = find_archetype_with(world, node, id)
|
local to = find_archetype_with(world, node, id)
|
||||||
init_edge_for_add(world, node, edge, id, to)
|
init_edge_for_add(world, node, edge, id, to)
|
||||||
return to
|
return to
|
||||||
end
|
end
|
||||||
|
|
||||||
local function create_edge_for_remove(world: World, node: Archetype,
|
local function create_edge_for_remove(world: World, node: Archetype, edge: GraphEdge, id: i53): Archetype
|
||||||
edge: GraphEdge, id: i53): Archetype
|
|
||||||
|
|
||||||
local to = find_archetype_without(world, node, id)
|
local to = find_archetype_without(world, node, id)
|
||||||
init_edge_for_remove(world, node, edge, id, to)
|
init_edge_for_remove(world, node, edge, id, to)
|
||||||
return to
|
return to
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function archetype_traverse_add(world: World, id: i53, from: Archetype): Archetype
|
||||||
local function archetype_traverse_add(world: World, id: i53,
|
|
||||||
from: Archetype): Archetype
|
|
||||||
|
|
||||||
from = from or world.ROOT_ARCHETYPE
|
from = from or world.ROOT_ARCHETYPE
|
||||||
local edge = archetype_ensure_edge(
|
local edge = archetype_ensure_edge(world, from.node.add, id)
|
||||||
world, from.node.add, id)
|
|
||||||
|
|
||||||
local to = edge.to
|
local to = edge.to
|
||||||
if not to then
|
if not to then
|
||||||
|
@ -701,8 +686,7 @@ end
|
||||||
local function archetype_traverse_remove(world: World, id: i53, from: Archetype): Archetype
|
local function archetype_traverse_remove(world: World, id: i53, from: Archetype): Archetype
|
||||||
from = from or world.ROOT_ARCHETYPE
|
from = from or world.ROOT_ARCHETYPE
|
||||||
|
|
||||||
local edge = archetype_ensure_edge(
|
local edge = archetype_ensure_edge(world, from.node.add, id)
|
||||||
world, from.node.add, id)
|
|
||||||
|
|
||||||
local to = edge.to
|
local to = edge.to
|
||||||
if not to then
|
if not to then
|
||||||
|
@ -850,9 +834,7 @@ local function world_clear(world: World, entity: i53)
|
||||||
entity_move(world.entityIndex, entity, record, ROOT_ARCHETYPE)
|
entity_move(world.entityIndex, entity, record, ROOT_ARCHETYPE)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function archetype_fast_delete_last(columns: { Column },
|
local function archetype_fast_delete_last(columns: { Column }, column_count: number, types: { i53 }, entity: i53)
|
||||||
column_count: number, types: { i53 }, entity: i53)
|
|
||||||
|
|
||||||
for i, column in columns do
|
for i, column in columns do
|
||||||
if column ~= NULL_ARRAY then
|
if column ~= NULL_ARRAY then
|
||||||
column[column_count] = nil
|
column[column_count] = nil
|
||||||
|
@ -860,9 +842,7 @@ local function archetype_fast_delete_last(columns: { Column },
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function archetype_fast_delete(columns: { Column },
|
local function archetype_fast_delete(columns: { Column }, column_count: number, row, types, entity)
|
||||||
column_count: number, row, types, entity)
|
|
||||||
|
|
||||||
for i, column in columns do
|
for i, column in columns do
|
||||||
if column ~= NULL_ARRAY then
|
if column ~= NULL_ARRAY then
|
||||||
column[row] = column[column_count]
|
column[row] = column[column_count]
|
||||||
|
@ -882,9 +862,7 @@ local function archetype_disconnect_edge(edge: GraphEdge)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function archetype_remove_edge(edges: Map<i53, GraphEdge>,
|
local function archetype_remove_edge(edges: Map<i53, GraphEdge>, id: i53, edge: GraphEdge)
|
||||||
id: i53, edge: GraphEdge)
|
|
||||||
|
|
||||||
archetype_disconnect_edge(edge)
|
archetype_disconnect_edge(edge)
|
||||||
edges[id] = nil
|
edges[id] = nil
|
||||||
end
|
end
|
||||||
|
@ -960,9 +938,7 @@ end
|
||||||
|
|
||||||
local world_delete: (world: World, entity: i53, destruct: boolean?) -> ()
|
local world_delete: (world: World, entity: i53, destruct: boolean?) -> ()
|
||||||
do
|
do
|
||||||
local function archetype_delete(world: World,
|
local function archetype_delete(world: World, archetype: Archetype, row: number, destruct: boolean?)
|
||||||
archetype: Archetype, row: number, destruct: boolean?)
|
|
||||||
|
|
||||||
local entityIndex = world.entityIndex
|
local entityIndex = world.entityIndex
|
||||||
local columns = archetype.columns
|
local columns = archetype.columns
|
||||||
local types = archetype.types
|
local types = archetype.types
|
||||||
|
@ -1044,15 +1020,15 @@ do
|
||||||
if idr_t then
|
if idr_t then
|
||||||
for archetype_id in idr_t.cache do
|
for archetype_id in idr_t.cache do
|
||||||
local children = {}
|
local children = {}
|
||||||
local idr_o_archetype = archetypes[archetype_id]
|
local idr_t_archetype = archetypes[archetype_id]
|
||||||
|
|
||||||
local idr_o_types = idr_o_archetype.types
|
local idr_t_types = idr_t_archetype.types
|
||||||
|
|
||||||
for _, child in idr_o_archetype.entities do
|
for _, child in idr_t_archetype.entities do
|
||||||
table.insert(children, child)
|
table.insert(children, child)
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, id in idr_o_types do
|
for _, id in idr_t_types do
|
||||||
if not ECS_IS_PAIR(id) then
|
if not ECS_IS_PAIR(id) then
|
||||||
continue
|
continue
|
||||||
end
|
end
|
||||||
|
@ -1081,7 +1057,6 @@ do
|
||||||
|
|
||||||
record.archetype = nil :: any
|
record.archetype = nil :: any
|
||||||
entityIndex.sparse[entity] = nil
|
entityIndex.sparse[entity] = nil
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1089,7 +1064,6 @@ local function world_contains(world: World, entity): boolean
|
||||||
return world.entityIndex.sparse[entity] ~= nil
|
return world.entityIndex.sparse[entity] ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function NOOP() end
|
local function NOOP() end
|
||||||
|
|
||||||
local function ARM(query, ...)
|
local function ARM(query, ...)
|
||||||
|
@ -1598,7 +1572,9 @@ if _G.__JECS_DEBUG then
|
||||||
-- error but stack trace always starts at first callsite outside of this file
|
-- error but stack trace always starts at first callsite outside of this file
|
||||||
local function throw(msg: string)
|
local function throw(msg: string)
|
||||||
local s = 1
|
local s = 1
|
||||||
repeat s += 1 until debug.info(s, "s") ~= debug.info(1, "s")
|
repeat
|
||||||
|
s += 1
|
||||||
|
until debug.info(s, "s") ~= debug.info(1, "s")
|
||||||
if warn then
|
if warn then
|
||||||
error(msg, s)
|
error(msg, s)
|
||||||
else
|
else
|
||||||
|
@ -1649,14 +1625,14 @@ if _G.__JECS_DEBUG then
|
||||||
|
|
||||||
World.set = function(world: World, entity: i53, id: i53, value: any): ()
|
World.set = function(world: World, entity: i53, id: i53, value: any): ()
|
||||||
local is_tag = ID_IS_TAG(world, id)
|
local is_tag = ID_IS_TAG(world, id)
|
||||||
if (is_tag and value == nil) then
|
if is_tag and value == nil then
|
||||||
world_add(world, entity, id)
|
world_add(world, entity, id)
|
||||||
local _1 = get_name(world, entity)
|
local _1 = get_name(world, entity)
|
||||||
local _2 = get_name(world, id)
|
local _2 = get_name(world, id)
|
||||||
local why = "cannot set component value to nil"
|
local why = "cannot set component value to nil"
|
||||||
throw(why)
|
throw(why)
|
||||||
return
|
return
|
||||||
elseif (value ~= nil and is_tag) then
|
elseif value ~= nil and is_tag then
|
||||||
world_add(world, entity, id)
|
world_add(world, entity, id)
|
||||||
local _1 = get_name(world, entity)
|
local _1 = get_name(world, entity)
|
||||||
local _2 = get_name(world, id)
|
local _2 = get_name(world, id)
|
||||||
|
@ -1684,8 +1660,7 @@ if _G.__JECS_DEBUG then
|
||||||
if value ~= nil then
|
if value ~= nil then
|
||||||
local _1 = get_name(world, entity)
|
local _1 = get_name(world, entity)
|
||||||
local _2 = get_name(world, id)
|
local _2 = get_name(world, id)
|
||||||
throw("You provided a value when none was expected. "
|
throw("You provided a value when none was expected. " .. `Did you mean to use "world:add({_1}, {_2})"`)
|
||||||
..`Did you mean to use "world:add({_1}, {_2})"`)
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1710,7 +1685,8 @@ if _G.__JECS_DEBUG then
|
||||||
end
|
end
|
||||||
throw(
|
throw(
|
||||||
`cannot get (#{i}) component {name} value because it is a tag.`
|
`cannot get (#{i}) component {name} value because it is a tag.`
|
||||||
..`\n[jecs] note: If this was intentional, use "world:has({_1}, {name}) instead"`)
|
.. `\n[jecs] note: If this was intentional, use "world:has({_1}, {name}) instead"`
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1722,15 +1698,15 @@ if _G.__JECS_DEBUG then
|
||||||
local _1 = get_name(world, entity)
|
local _1 = get_name(world, entity)
|
||||||
local _2 = get_name(world, relation)
|
local _2 = get_name(world, relation)
|
||||||
|
|
||||||
throw("We have changed the function call to require an index parameter,"
|
throw(
|
||||||
..` please use world:target({_1}, {_2}, 0)`)
|
"We have changed the function call to require an index parameter,"
|
||||||
|
.. ` please use world:target({_1}, {_2}, 0)`
|
||||||
|
)
|
||||||
end
|
end
|
||||||
return world_target(world, entity, relation, index)
|
return world_target(world, entity, relation, index)
|
||||||
end
|
end
|
||||||
|
|
||||||
World.remove = function()
|
World.remove = function() end
|
||||||
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function World.new()
|
function World.new()
|
||||||
|
@ -1853,14 +1829,7 @@ export type World = {
|
||||||
& (<A, B>(self: World, Id<A>, Id<B>) -> Query<A, B>)
|
& (<A, B>(self: World, Id<A>, Id<B>) -> Query<A, B>)
|
||||||
& (<A, B, C>(self: World, Id<A>, Id<B>, Id<C>) -> Query<A, B, C>)
|
& (<A, B, C>(self: World, Id<A>, Id<B>, Id<C>) -> Query<A, B, C>)
|
||||||
& (<A, B, C, D>(self: World, Id<A>, Id<B>, Id<C>, Id<D>) -> Query<A, B, C, D>)
|
& (<A, B, C, D>(self: World, Id<A>, Id<B>, Id<C>, Id<D>) -> Query<A, B, C, D>)
|
||||||
& (<A, B, C, D, E>(
|
& (<A, B, C, D, E>(self: World, Id<A>, Id<B>, Id<C>, Id<D>, Id<E>) -> Query<A, B, C, D, E>)
|
||||||
self: World,
|
|
||||||
Id<A>,
|
|
||||||
Id<B>,
|
|
||||||
Id<C>,
|
|
||||||
Id<D>,
|
|
||||||
Id<E>
|
|
||||||
) -> Query<A, B, C, D, E>)
|
|
||||||
& (<A, B, C, D, E, F>(
|
& (<A, B, C, D, E, F>(
|
||||||
self: World,
|
self: World,
|
||||||
Id<A>,
|
Id<A>,
|
||||||
|
|
Loading…
Reference in a new issue