-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Flag interfaces #7
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,12 @@ import * as ecs from "@rbxts/jecs"; | |
|
||
import { createSignal, type Signal } from "./signal"; | ||
|
||
export interface Wildcard {} | ||
export interface ChildOf {} | ||
export interface Flag { | ||
readonly __flamecs_flag: unique symbol; | ||
} | ||
|
||
export interface Wildcard extends Flag {} | ||
export interface ChildOf extends Flag {} | ||
|
||
export type Entity<T = unknown> = ecs.Entity<T>; | ||
export type Id<T = unknown> = ecs.Id<T>; | ||
|
@@ -27,6 +31,15 @@ export type FilterPairs<T> = { | |
[K in keyof T]: FilterPair<T[K]>; | ||
}; | ||
|
||
export type OmitFlag<T> = T extends Flag ? undefined : T; | ||
export type OmitFlags<T> = { | ||
[K in keyof T]: OmitFlag<T[K]>; | ||
}; | ||
|
||
type WithTrailingUndefined<T extends Array<unknown>> = T extends [...infer Rest, undefined] | ||
? [...WithTrailingUndefined<Rest>, undefined?] | ||
: T; | ||
|
||
const components = new Map<string, Entity>(); | ||
export const registry = new ecs.World(); | ||
|
||
|
@@ -75,7 +88,7 @@ function hookListeners<T>(id: Entity<T>): void { | |
* @param key - Flamework autogenerated key. | ||
* @metadata macro | ||
*/ | ||
export function reserve<T>(runtimeId: Entity<T>, key?: Modding.Generic<T, "id">): void { | ||
export function reserve<T>(runtimeId: Entity<T>, key?: ComponentKey<T>): void { | ||
assert(key); | ||
assert(!components.has(key), `A component with the key "${key}" already exists`); | ||
components.set(key, runtimeId); | ||
|
@@ -94,12 +107,12 @@ export function reserve<T>(runtimeId: Entity<T>, key?: Modding.Generic<T, "id">) | |
* @returns The component entity ID. | ||
* @metadata macro | ||
*/ | ||
export function component<T>(key?: ComponentKey<T>): Entity<T> { | ||
export function component<T>(key?: ComponentKey<T>): Entity<OmitFlag<T>> { | ||
assert(key); | ||
let id = components.get(key) as Entity<T> | undefined; | ||
let id = components.get(key) as Entity<OmitFlag<T>> | undefined; | ||
|
||
if (id === undefined) { | ||
id = registry.component<T>(); | ||
id = registry.component<OmitFlag<T>>(); | ||
components.set(key, id); | ||
hookListeners(id); | ||
} | ||
|
@@ -115,7 +128,7 @@ export function component<T>(key?: ComponentKey<T>): Entity<T> { | |
* @returns The component or pair ID. | ||
* @metadata macro. | ||
*/ | ||
export function getId<T>(key?: ResolveKey<T>): Id<FilterPair<T>> { | ||
export function getId<T>(key?: ResolveKey<T>): Id<OmitFlag<FilterPair<T>>> { | ||
assert(key); | ||
|
||
if (typeIs(key, "table")) { | ||
|
@@ -139,15 +152,20 @@ export function getId<T>(key?: ResolveKey<T>): Id<FilterPair<T>> { | |
* @metadata macro | ||
*/ | ||
export function spawn<T extends Array<unknown>>( | ||
bundle?: FilterPairs<T>, | ||
bundle?: WithTrailingUndefined<OmitFlags<FilterPairs<T>>>, | ||
keys?: ResolveKeys<T>, | ||
): Tag { | ||
const entity = registry.entity(); | ||
|
||
if (bundle && keys) { | ||
if (keys) { | ||
for (let index = 0; index < keys.size(); index++) { | ||
const id = getId(keys[index]); | ||
registry.set(entity, id, bundle[index]); | ||
const value = bundle?.[index]; | ||
if (value !== undefined) { | ||
registry.set(entity, id, value); | ||
} else { | ||
registry.add(entity, id); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -174,13 +192,19 @@ export function despawn(entity: Entity): void { | |
*/ | ||
export function insert<T extends Array<unknown>>( | ||
entity: Entity, | ||
values: FilterPairs<T>, | ||
values?: WithTrailingUndefined<OmitFlags<FilterPairs<T>>>, | ||
keys?: ResolveKeys<T>, | ||
): void { | ||
assert(keys); | ||
|
||
for (let index = 0; index < keys.size(); index++) { | ||
const id = getId(keys[index]); | ||
registry.set(entity, id, values[index]); | ||
const value = values?.[index]; | ||
if (value !== undefined) { | ||
registry.set(entity, id, value); | ||
} else { | ||
registry.add(entity, id); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -193,7 +217,7 @@ export function insert<T extends Array<unknown>>( | |
* @param key - Flamework autogenerated key. | ||
* @metadata macro | ||
*/ | ||
export function add<T extends Pair<defined>>( | ||
export function add<T extends Pair<defined & Flag>>( | ||
entity: Entity, | ||
object: Entity, | ||
key?: ComponentKey<FilterPair<T>>, | ||
|
@@ -208,7 +232,10 @@ export function add<T extends Pair<defined>>( | |
* @param key - Flamework autogenerated key. | ||
* @metadata macro | ||
*/ | ||
export function add<T extends Pair<defined, defined>>(entity: Entity, key?: ResolveKey<T>): void; | ||
export function add<T extends Pair<defined & Flag, defined>>( | ||
entity: Entity, | ||
key?: ResolveKey<T>, | ||
): void; | ||
|
||
/** | ||
* Adds a component to an entity. | ||
|
@@ -218,7 +245,7 @@ export function add<T extends Pair<defined, defined>>(entity: Entity, key?: Reso | |
* @param key - Flamework autogenerated key. | ||
* @metadata macro | ||
*/ | ||
export function add<T>(entity: Entity, key?: ComponentKey<T>): void; | ||
export function add<T extends Flag>(entity: Entity, key?: ComponentKey<T>): void; | ||
|
||
export function add(entity: Entity, argument1?: unknown, argument2?: unknown): void { | ||
if (argument2 !== undefined) { | ||
|
@@ -295,7 +322,7 @@ export function remove(entity: Entity, argument1?: unknown, argument2?: unknown) | |
export function set<T extends Pair<defined>>( | ||
entity: Entity, | ||
object: Entity, | ||
value: FilterPair<T>, | ||
value: OmitFlag<FilterPair<T>>, | ||
key?: ComponentKey<FilterPair<T>>, | ||
): void; | ||
|
||
|
@@ -311,7 +338,7 @@ export function set<T extends Pair<defined>>( | |
*/ | ||
export function set<T extends Pair<defined, defined>>( | ||
entity: Entity, | ||
value: FilterPair<T>, | ||
value: OmitFlag<FilterPair<T>>, | ||
key?: ResolveKey<T>, | ||
): void; | ||
|
||
|
@@ -324,7 +351,7 @@ export function set<T extends Pair<defined, defined>>( | |
* @param key - Flamework autogenerated key. | ||
* @metadata macro | ||
*/ | ||
export function set<T>(entity: Entity, value: T, key?: ComponentKey<T>): void; | ||
export function set<T>(entity: Entity, value: OmitFlag<T>, key?: ComponentKey<T>): void; | ||
|
||
export function set( | ||
entity: Entity, | ||
|
@@ -360,7 +387,7 @@ export function get<T extends Pair<defined>>( | |
entity: Entity, | ||
object: Entity, | ||
key?: ComponentKey<FilterPair<T>>, | ||
): FilterPair<T> | undefined; | ||
): OmitFlag<FilterPair<T>> | undefined; | ||
|
||
/** | ||
* Retrieves the value of a component or pair for a specific entity. | ||
|
@@ -371,7 +398,7 @@ export function get<T extends Pair<defined>>( | |
* @returns The value associated with the component or pair. | ||
* @metadata macro | ||
*/ | ||
export function get<T>(entity: Entity, key?: ResolveKey<T>): FilterPair<T> | undefined; | ||
export function get<T>(entity: Entity, key?: ResolveKey<T>): OmitFlag<FilterPair<T>> | undefined; | ||
|
||
export function get(entity: Entity, argument1?: unknown, argument2?: unknown): unknown { | ||
if (argument2 !== undefined) { | ||
|
@@ -462,7 +489,7 @@ export function parent(entity: Entity): Entity | undefined { | |
* @returns The pair ID. | ||
* @metadata macro | ||
*/ | ||
export function pair<P>(object: Entity, predicate?: ComponentKey<P>): Pair<P, unknown> { | ||
export function pair<P>(object: Entity, predicate?: ComponentKey<P>): Pair<OmitFlag<P>, unknown> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just saw thiis but the first element is the predicate not the object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The predicate here is a flamework generated key. |
||
const predicateId = component(predicate); | ||
return ecs.pair(predicateId, object); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change the name of this interface and every subsequent dependents on this interface to use "Tag"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tag is already used for Entity<.undefined>. Should I just get rid of that then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that sounds reasonable.