ecs attempt
This commit is contained in:
@ -12,9 +12,9 @@ type Controller struct {
|
||||
systems map[reflect.Type]System
|
||||
sortedSystems map[int][]System
|
||||
priorityKeys []int
|
||||
nextEntityID int
|
||||
components map[reflect.Type][]int
|
||||
entities map[int]map[reflect.Type]Component
|
||||
nextEntityID Entity
|
||||
components map[reflect.Type][]Entity
|
||||
entities map[Entity]map[reflect.Type]Component
|
||||
deadEntities []int
|
||||
|
||||
// The component map will keep track of what components are available
|
||||
@ -28,8 +28,8 @@ func NewController() *Controller {
|
||||
controller.sortedSystems = make(map[int][]System)
|
||||
controller.priorityKeys = []int{}
|
||||
controller.nextEntityID = 0
|
||||
controller.components = make(map[reflect.Type][]int)
|
||||
controller.entities = make(map[int]map[reflect.Type]Component)
|
||||
controller.components = make(map[reflect.Type][]Entity)
|
||||
controller.entities = make(map[Entity]map[reflect.Type]Component)
|
||||
controller.deadEntities = []int{}
|
||||
controller.componentMap = make(map[string]Component)
|
||||
|
||||
@ -38,7 +38,7 @@ func NewController() *Controller {
|
||||
|
||||
// Create a new entity in the world. An entity is simply a unique integer.
|
||||
// If any components are provided, they will be associated with the created entity
|
||||
func (c *Controller) CreateEntity(components []Component) int {
|
||||
func (c *Controller) CreateEntity(components []Component) Entity {
|
||||
c.nextEntityID += 1
|
||||
|
||||
if len(components) > 0 {
|
||||
@ -54,7 +54,7 @@ func (c *Controller) CreateEntity(components []Component) int {
|
||||
|
||||
// DeleteEntity removes an entity, all component instances attached to that entity, and any components associations with
|
||||
// that entity
|
||||
func (c *Controller) DeleteEntity(entity int) {
|
||||
func (c *Controller) DeleteEntity(entity Entity) {
|
||||
// First, delete all the component associations for the entity to be removed
|
||||
for k, _ := range c.entities[entity] {
|
||||
c.RemoveComponent(entity, k)
|
||||
@ -87,7 +87,7 @@ func (c *Controller) GetMappedComponentClass(componentName string) Component {
|
||||
// AddComponent adds a component to an entity. The component is added to the global list of components for the
|
||||
// processor, and also directly associated with the entity itself. This allows for flexible checking of components,
|
||||
// as you can check which entites are associated with a component, and vice versa.
|
||||
func (c *Controller) AddComponent(entity int, component Component) {
|
||||
func (c *Controller) AddComponent(entity Entity, component Component) {
|
||||
// First, get the type of the component
|
||||
componentType := reflect.TypeOf(component)
|
||||
|
||||
@ -104,7 +104,7 @@ func (c *Controller) AddComponent(entity int, component Component) {
|
||||
}
|
||||
|
||||
// HasComponent checks a given entity to see if it has a given component associated with it
|
||||
func (c *Controller) HasComponent(entity int, componentType reflect.Type) bool {
|
||||
func (c *Controller) HasComponent(entity Entity, componentType reflect.Type) bool {
|
||||
if _, ok := c.entities[entity][componentType]; ok {
|
||||
return true
|
||||
} else {
|
||||
@ -113,7 +113,7 @@ func (c *Controller) HasComponent(entity int, componentType reflect.Type) bool {
|
||||
}
|
||||
|
||||
// GetComponent returns the component instance for a component type, if one exists for the provided entity
|
||||
func (c *Controller) GetComponent(entity int, componentType reflect.Type) Component {
|
||||
func (c *Controller) GetComponent(entity Entity, componentType reflect.Type) Component {
|
||||
// Check the given entity has the provided component
|
||||
if c.HasComponent(entity, componentType) {
|
||||
return c.entities[entity][componentType]
|
||||
@ -123,7 +123,7 @@ func (c *Controller) GetComponent(entity int, componentType reflect.Type) Compon
|
||||
}
|
||||
|
||||
// GetEntity gets a specific entity, and all of its component instances
|
||||
func (c *Controller) GetEntity(entity int) map[reflect.Type]Component {
|
||||
func (c *Controller) GetEntity(entity Entity) map[reflect.Type]Component {
|
||||
for i, _ := range c.entities {
|
||||
if i == entity {
|
||||
return c.entities[entity]
|
||||
@ -134,14 +134,14 @@ func (c *Controller) GetEntity(entity int) map[reflect.Type]Component {
|
||||
}
|
||||
|
||||
// GetEntities returns a map of all entities and their component instances
|
||||
func (c *Controller) GetEntities() map[int]map[reflect.Type]Component {
|
||||
func (c *Controller) GetEntities() map[Entity]map[reflect.Type]Component {
|
||||
return c.entities
|
||||
}
|
||||
|
||||
// GetEntitiesWithComponent returns a list of all entities with a given component attached
|
||||
// TODO: Allow for passing a list of components
|
||||
func (c *Controller) GetEntitiesWithComponent(componentType reflect.Type) []int {
|
||||
entitiesWithComponent := make([]int, 0)
|
||||
func (c *Controller) GetEntitiesWithComponent(componentType reflect.Type) []Entity {
|
||||
entitiesWithComponent := make([]Entity, 0)
|
||||
for entity := range c.entities {
|
||||
if c.HasComponent(entity, componentType) {
|
||||
entitiesWithComponent = append(entitiesWithComponent, entity)
|
||||
@ -152,7 +152,7 @@ func (c *Controller) GetEntitiesWithComponent(componentType reflect.Type) []int
|
||||
}
|
||||
|
||||
// UpdateComponent updates a component on an entity with a new version of the same component
|
||||
func (c *Controller) UpdateComponent(entity int, componentType reflect.Type, newComponent Component) int {
|
||||
func (c *Controller) UpdateComponent(entity Entity, componentType reflect.Type, newComponent Component) Entity {
|
||||
// First, remove the component in question (Don't actually update things, but rather remove and replace)
|
||||
c.RemoveComponent(entity, componentType)
|
||||
|
||||
@ -165,7 +165,7 @@ func (c *Controller) UpdateComponent(entity int, componentType reflect.Type, new
|
||||
// DeleteComponent will delete a component instance from an entity, based on component type. It will also remove the
|
||||
// association between the component and the entity, and remove the component from the processor completely if no
|
||||
// other entities are using it.
|
||||
func (c *Controller) RemoveComponent(entity int, componentType reflect.Type) int {
|
||||
func (c *Controller) RemoveComponent(entity Entity, componentType reflect.Type) Entity {
|
||||
// Find the index of the entity to operate on in the components slice
|
||||
index := -1
|
||||
for i, v := range c.components[componentType] {
|
||||
|
33
engine/ecs/systems/mob_render_system.go
Normal file
33
engine/ecs/systems/mob_render_system.go
Normal file
@ -0,0 +1,33 @@
|
||||
package systems
|
||||
|
||||
import (
|
||||
"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
|
||||
"lab.zaar.be/thefish/alchemyst-go/engine/types"
|
||||
"lab.zaar.be/thefish/alchemyst-go/ui/mainwindow"
|
||||
)
|
||||
|
||||
type MobRenderSystem struct {
|
||||
EntityController *ecs.Controller
|
||||
Layer *mainwindow.Layer
|
||||
}
|
||||
|
||||
func (mrs MobRenderSystem) Process(){
|
||||
for e := range mrs.EntityController.GetEntities() {
|
||||
if mrs.EntityController.HasComponent(e, types.Coords{}.TypeOf()) &&
|
||||
mrs.EntityController.HasComponent(e, types.Appearance{}.TypeOf()) {
|
||||
|
||||
pos := mrs.EntityController.GetComponent(e, types.Coords{}.TypeOf()).(types.Coords)
|
||||
appearance := mrs.EntityController.GetComponent(e, types.Appearance{}.TypeOf()).(types.Appearance)
|
||||
|
||||
//fixme
|
||||
// Clear the cell this entity occupies, so it is the only glyph drawn there
|
||||
for i := 0; i <= 2; i++ {
|
||||
mrs.Layer.ClearArea(pos.X, pos.Y,1,1)
|
||||
//gogue.ClearArea(pos.X, pos.Y, 1, 1, i)
|
||||
}
|
||||
//fixme
|
||||
mrs.Layer.WithRawColor(appearance.ColorSet.Fg.GetColor()).Put(pos.X, pos.Y, appearance.Glyph)
|
||||
//gogue.PrintGlyph(pos.X, pos.Y, appearance.Glyph, "", appearance.Layer)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user