diff --git a/cmd/game/main.go b/cmd/game/main.go index 4f1435a..d18fb8d 100644 --- a/cmd/game/main.go +++ b/cmd/game/main.go @@ -4,6 +4,7 @@ import ( "github.com/rs/zerolog" "github.com/rs/zerolog/log" "lab.zaar.be/thefish/alchemyst-go/engine/ecs" + "lab.zaar.be/thefish/alchemyst-go/engine/ecs/systems" "lab.zaar.be/thefish/alchemyst-go/engine/gamemap" "lab.zaar.be/thefish/alchemyst-go/engine/gamemap/mapgens" "lab.zaar.be/thefish/alchemyst-go/engine/gamestate" @@ -112,12 +113,11 @@ func main() { controller.AddComponent(player, rooms[0].Center) //implicit Coords - render := mob.MobRenderSystem{EntityController: controller} + render := systems.MobRenderSystem{EntityController: controller} controller.AddSystem(render, 1) - - + level.Player = player //but every call to bearlibterminal must be wrapped to closure and passed to mainfunc var exit = false diff --git a/engine/ecs/controller.go b/engine/ecs/controller.go index 1f9c8dd..b948fc6 100644 --- a/engine/ecs/controller.go +++ b/engine/ecs/controller.go @@ -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] { diff --git a/engine/mob/mob_render_system.go b/engine/ecs/systems/mob_render_system.go similarity index 66% rename from engine/mob/mob_render_system.go rename to engine/ecs/systems/mob_render_system.go index 47a4918..91f3daa 100644 --- a/engine/mob/mob_render_system.go +++ b/engine/ecs/systems/mob_render_system.go @@ -1,12 +1,14 @@ -package mob +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(){ @@ -17,13 +19,15 @@ func (mrs MobRenderSystem) Process(){ 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++ { - //fixme - gogue.ClearArea(pos.X, pos.Y, 1, 1, i) + mrs.Layer.ClearArea(pos.X, pos.Y,1,1) + //gogue.ClearArea(pos.X, pos.Y, 1, 1, i) } //fixme - gogue.PrintGlyph(pos.X, pos.Y, appearance.Glyph, "", appearance.Layer) + mrs.Layer.WithRawColor(appearance.ColorSet.Fg.GetColor()).Put(pos.X, pos.Y, appearance.Glyph) + //gogue.PrintGlyph(pos.X, pos.Y, appearance.Glyph, "", appearance.Layer) } } } \ No newline at end of file diff --git a/engine/gamestate/gamestate.go b/engine/gamestate/gamestate.go index 6fe2dc4..4f75a2d 100644 --- a/engine/gamestate/gamestate.go +++ b/engine/gamestate/gamestate.go @@ -2,7 +2,6 @@ package gamestate import ( "lab.zaar.be/thefish/alchemyst-go/engine/gamemap" - "lab.zaar.be/thefish/alchemyst-go/engine/mob" ) type GameState struct { @@ -13,7 +12,6 @@ type GameState struct { FovRecompute chan struct{} Redraw chan struct{} Level *gamemap.Level - Player *mob.Player } // do runs f on the main thread. diff --git a/engine/mob/mob.go b/engine/mob/mob.go index 8a93082..ba70a70 100644 --- a/engine/mob/mob.go +++ b/engine/mob/mob.go @@ -18,9 +18,10 @@ func (m *Mob) Walk(level *gamemap.Level, dx, dy int) { if level.GetTile(newCoords).BlocksPass { return } - if level.Objects.At(newCoords).HasComponent("block_pass") { - - } + //fixme + //if level.Objects.At(newCoords).HasComponent("block_pass") { + // + //} fmt.Printf("new coords: %d, %d\n", m.Coords.X, m.Coords.Y) } diff --git a/ui/mainwindow/viewport.go b/ui/mainwindow/viewport.go index 0c77ca3..afe4ab7 100644 --- a/ui/mainwindow/viewport.go +++ b/ui/mainwindow/viewport.go @@ -6,6 +6,7 @@ import ( "lab.zaar.be/thefish/alchemyst-go/engine/fov" "lab.zaar.be/thefish/alchemyst-go/engine/fov/precomputed_shade" "lab.zaar.be/thefish/alchemyst-go/engine/gamestate" + "lab.zaar.be/thefish/alchemyst-go/engine/mob" "lab.zaar.be/thefish/alchemyst-go/engine/types" "time" ) @@ -14,12 +15,12 @@ var NotInViewError = errors.New("not in ViewPort") type ViewPort struct { *types.Rect - cameraCoords types.Coords - layer *Layer - Fov fov.Fov - PlayerCoords types.Coords - PlayerTorchRadius int - animateTiles *time.Ticker + cameraCoords types.Coords + layer *Layer + Fov fov.Fov + Player mob.Player + TorchRadius int + animateTiles *time.Ticker } func NewViewPort(x, y, w, h int, layer *Layer) *ViewPort { @@ -32,7 +33,7 @@ func NewViewPort(x, y, w, h int, layer *Layer) *ViewPort { Fov: computedFov, } - vp.PlayerTorchRadius = 11 + vp.TorchRadius = 12 vp.animateTiles = time.NewTicker(time.Second / 12) return &vp @@ -45,7 +46,7 @@ func (vp *ViewPort) Close() { func (vp *ViewPort) Move(state *gamestate.GameState) { - c := &state.Player.Coords + c := &state.Level.Player.HasComponent(types.Coords{}).Coords x := c.X - vp.Rect.W/2 y := c.Y - vp.Rect.H/2 @@ -103,7 +104,7 @@ func (vp *ViewPort) Render(state *gamestate.GameState) { vp.layer.ClearRect(vp.Rect) fovRecompute = true redraw = true - vp.Fov.ComputeFov(state.Level, state.Player.Coords, vp.PlayerTorchRadius) + vp.Fov.ComputeFov(state.Level, state.Level.Player.Coords, vp.TorchRadius) } //if redraw { @@ -121,7 +122,7 @@ func (vp *ViewPort) Render(state *gamestate.GameState) { } } //mobs - pc, err := vp.ToVPCoords(state.Player.Coords) + pc, err := vp.ToVPCoords(state.Level.Player.Coords) _ = pc if err != nil { fmt.Println("error on getting player position")