Skip to content

Commit

Permalink
simple suport for ecs
Browse files Browse the repository at this point in the history
  • Loading branch information
jossse69 committed Mar 21, 2024
1 parent 79b23f8 commit c5cae88
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
12 changes: 9 additions & 3 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package core

import (
rl "github.com/gen2brain/raylib-go/raylib"
"github.com/jossse69/PUZZ/ecs"
"github.com/jossse69/PUZZ/renderer"
)

// Game represents the core engine structure.
type Game struct {
Renderer *renderer.Renderer
// Add other core components here, such as Audio, Input, etc.
Renderer *renderer.Renderer
Systems *ecs.SystemManager
Entities *ecs.EntityManager
Components *ecs.ComponentManager

// Load calls once when the game starts, useful for loading resources.
Load func()
Expand All @@ -25,7 +28,10 @@ type Game struct {
func NewGame(gameWidth, gameHeight int, title string) *Game {
// Initialize the Raylib window and other settings here if not already done in Renderer.
game := &Game{
Renderer: renderer.NewRenderer(gameWidth, gameHeight, title),
Renderer: renderer.NewRenderer(gameWidth, gameHeight, title),
Systems: ecs.NewSystemManager(),
Entities: ecs.NewEntityManager(),
Components: ecs.NewComponentManager(),
}

return game
Expand Down
36 changes: 36 additions & 0 deletions ecs/component.go
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)
}
20 changes: 20 additions & 0 deletions ecs/entity.go
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
}
30 changes: 30 additions & 0 deletions ecs/system.go
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()
}
}

0 comments on commit c5cae88

Please sign in to comment.