4HKI6BGT2WLSYI77HSDDW4XOX7YH64E4X4PHGIFOFPAWIGABLGWQC
{
"$schema": "https://raw.githubusercontent.com/DanielGavin/ols/master/misc/ols.schema.json",
"enable_document_symbols": true,
"enable_hover": true,
"enable_snippets": true
}
package ecs
import "core:fmt"
Entity :: distinct u64
World :: struct {
next_id: u64,
components: map[typeid][dynamic]any,
}
NewWorld :: proc() -> ^World {
world := new(World)
world.next_id = 0
world.components = make(map[typeid][dynamic]any)
return world
}
CreateEntity :: proc(world: ^World) -> Entity {
current := world.next_id
world.next_id += 1
return Entity(current)
}
AddComponent :: proc(world: ^World, entity: Entity, component: $C) {
if _, exists := world^.components[C]; !exists {
world^.components[C] = make([dynamic]any, 0, 16)
}
m, _ := &world.components[C]
assign_at(m, int(entity), component)
fmt.println(2, world)
}
GetComponent :: proc(world: ^World, entity: Entity, $C: typeid) -> (C, bool) {
if components, exists := world.components[C]; exists {
if int(entity) < len(components) {
c := components[entity]
if c == nil {
return C{}, false
} else {
return components[entity].(C), true
}
}
}
return C{}, false
}
RemoveComponent :: proc(world: ^World, entity: Entity, $C: typeid) {
if components, exists := &world.components[C]; exists {
assign_at(components, int(entity), nil)
}
}
Position :: struct {
x: f32,
y: f32,
}
main :: proc() {
world := NewWorld()
defer {
for c in world.components {
delete(world.components[c])
}
}
defer delete(world.components)
defer free(world)
entity := CreateEntity(world)
fmt.println(1, world)
AddComponent(world, entity, Position{1, 2})
fmt.println(3, world)
if comp, found := GetComponent(world, entity, Position); found {
fmt.println("Component found: ", comp)
} else {
fmt.println("Component not found")
}
RemoveComponent(world, entity, Position)
}
{
"version": "2.0.0",
"tasks": [
{
"label": "Build Debug",
"type": "shell",
"command": "odin",
"args": [
"build",
"${workspaceFolder}", // Builds the currently open file. Change this to your specific project entry file if needed.
"-out:${workspaceFolder}/build/ecs",
"-debug"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "silent",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
},
"problemMatcher": []
}
]
}
{
"python.autoComplete.extraPaths": [
"${workspaceFolder}/sources/poky/bitbake/lib",
"${workspaceFolder}/sources/poky/meta/lib"
],
"python.analysis.extraPaths": [
"${workspaceFolder}/sources/poky/bitbake/lib",
"${workspaceFolder}/sources/poky/meta/lib"
],
"[python]": {
"diffEditor.ignoreTrimWhitespace": false,
"gitlens.codeLens.symbolScopes": [
"!Module"
],
"editor.formatOnType": true,
"editor.wordBasedSuggestions": "off",
"files.trimTrailingWhitespace": false
},
"[shellscript]": {
"files.eol": "\n",
"files.trimTrailingWhitespace": false
}
}
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Odin Project with LLDB",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/build/ecs",
"args": [],
"cwd": "${workspaceFolder}",
"preLaunchTask": "Build Debug", // Match the label of your build task in tasks.json
"terminal": "console"
}
]
}
.git
.DS_Store
build