-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package ecs | ||
|
||
import "fmt" | ||
|
||
type Component interface{} | ||
|
||
type ComponentManager struct { | ||
components map[Entity]map[string]Component | ||
} | ||
|
||
func NewComponentManager() *ComponentManager { | ||
return &ComponentManager{ | ||
components: make(map[Entity]map[string]Component), | ||
} | ||
} | ||
|
||
func (manager *ComponentManager) AddComponent(entity Entity, component Component) { | ||
componentName := getType(component) | ||
if manager.components[entity] == nil { | ||
manager.components[entity] = make(map[string]Component) | ||
} | ||
manager.components[entity][componentName] = component | ||
} | ||
|
||
func (manager *ComponentManager) GetComponent(entity Entity, componentName string) (Component, bool) { | ||
if components, ok := manager.components[entity]; ok { | ||
comp, ok := components[componentName] | ||
return comp, ok | ||
} | ||
return nil, false | ||
} | ||
|
||
// getType returns a unique string identifier for the component's type. | ||
func getType(myvar interface{}) string { | ||
return fmt.Sprintf("%T", myvar) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package ecs | ||
|
||
type Entity uint32 | ||
|
||
type EntityManager struct { | ||
lastID Entity | ||
entities map[Entity]bool | ||
} | ||
|
||
func NewEntityManager() *EntityManager { | ||
return &EntityManager{ | ||
entities: make(map[Entity]bool), | ||
} | ||
} | ||
|
||
func (manager *EntityManager) NewEntity() Entity { | ||
manager.lastID++ | ||
manager.entities[manager.lastID] = true | ||
return manager.lastID | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ecs | ||
|
||
type System interface { | ||
Update(dt float32) | ||
Draw() // for renderer systems | ||
} | ||
|
||
type SystemManager struct { | ||
systems []System | ||
} | ||
|
||
func NewSystemManager() *SystemManager { | ||
return &SystemManager{} | ||
} | ||
|
||
func (manager *SystemManager) AddSystem(system System) { | ||
manager.systems = append(manager.systems, system) | ||
} | ||
|
||
func (manager *SystemManager) UpdateSystems(dt float32) { | ||
for _, system := range manager.systems { | ||
system.Update(dt) | ||
} | ||
} | ||
|
||
func (manager *SystemManager) DrawSystems() { | ||
for _, system := range manager.systems { | ||
system.Draw() | ||
} | ||
} |