move to terrain render system

This commit is contained in:
thefish 2019-11-16 03:40:26 +03:00
parent 7b4a3c3200
commit 7e9f7686a9
17 changed files with 158 additions and 62 deletions

View File

@ -165,10 +165,10 @@ func main() {
//fixme set up (load / generate) player - move to game / enter or title / exit
player := controller.CreateEntity([]ecs.Component{})
controller.AddComponent(player, &types.Appearance{
Glyph: &types.PlainGlyphHolder{"@"},
controller.AddComponent(player, types.Appearance{
Glyph: types.PlainGlyphHolder{"@"},
ColorSet: types.TileColorSet{
Fg: &types.PlainColorHolder{255, 255, 255, 255},
Fg: types.PlainColorHolder{255, 255, 255, 255},
},
})

View File

@ -2,19 +2,33 @@ package ecs
// ECS system by jcerise, github.com/jcerise/gogue
const AmmoComponent = "ammo"
const AppearanceComponent = "appearance"
const ArmorComponent = "armor"
const ArmsComponent = "arms"
const BackpackComponent = "backpack"
const CarriedComponent = "carried"
const ConsumableComponent = "consumable"
const CoordsComponent = "coords"
const MobComponent = "mob"
const MoveableComponent = "movable"
const CarriedComponent = "carried"
const NamedComponent = "named"
const RangedComponent = "ranged"
const UsableComponent = "usable"
const WearableComponent = "usable"
const ArmsComponent = "arms"
const RangedComponent = "ranged"
const AmmoComponent = "ammo"
const ArmorComponent = "armor"
const BackpackComponent = "backpack"
type Component interface {
Type() string
}
}
type Named struct {
Name string
}
func (n Named) Type() string {
return NamedComponent
}
func (n Named) GetName() string {
return n.Name
}

View File

@ -3,6 +3,7 @@ package ecs
// ECS system by jcerise, github.com/jcerise/gogue
const MobRenderSystem = "mobrender"
const TerrainRenderSystem = "terrainrender"
type System interface {
Process()

View File

@ -7,27 +7,34 @@ import (
)
type MobRenderSystem struct {
EntityController *ecs.Controller
Layer *mainwindow.Layer
Controller *ecs.Controller
Layer *mainwindow.Layer
Viewport *mainwindow.ViewPort
}
func (mrs MobRenderSystem) Process(){
for e := range mrs.EntityController.GetEntities() {
if mrs.EntityController.HasComponent(e, ecs.CoordsComponent) &&
mrs.EntityController.HasComponent(e, ecs.AppearanceComponent) {
for e := range mrs.Controller.GetEntities() {
if mrs.Controller.HasComponent(e, ecs.CoordsComponent) &&
mrs.Controller.HasComponent(e, ecs.AppearanceComponent) {
pos := mrs.EntityController.GetComponent(e, ecs.CoordsComponent).(types.Coords)
appearance := mrs.EntityController.GetComponent(e, ecs.AppearanceComponent).(types.Appearance)
pos := mrs.Controller.GetComponent(e, ecs.CoordsComponent).(types.Coords)
appearance := mrs.Controller.GetComponent(e, ecs.AppearanceComponent).(types.Appearance)
//fixme
// if vp.Rect.InBounds(pos) {
// 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)
//for i := 0; i <= 2; i++ {
//mrs.Layer.ClearArea(pos.X, pos.Y,1,1) //WHY?!
//gogue.ClearArea(pos.X, pos.Y, 1, 1, i)
}
//}
//fixme
mrs.Layer.WithRawColor(appearance.ColorSet.Fg.GetColor()).Put(pos.X, pos.Y, appearance.Glyph)
mrs.Layer.WithRawColor(appearance.ColorSet.Fg.GetColor()).Put(pos.X, pos.Y, appearance.Glyph.GetGlyph())
mrs.Layer.WithRawColor(appearance.ColorSet.Fg.GetColor()).Put(pos.X, pos.Y, appearance.Glyph.GetGlyph())
//gogue.PrintGlyph(pos.X, pos.Y, appearance.Glyph, "", appearance.Layer)
//}
}
}
}

View File

@ -0,0 +1,65 @@
package systems
import (
"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"lab.zaar.be/thefish/alchemyst-go/ui/mainwindow"
)
type TerrainRenderSystem struct {
Viewport mainwindow.ViewPort
Level gamemap.Level
}
func (trs TerrainRenderSystem) Process() {
playerCoords := state.Controller.GetComponent(state.Player, ecs.CoordsComponent).(types.Coords)
vp.Move(state, playerCoords)
if fovRecompute {
vp.layer.ClearRect(vp.Rect)
fovRecompute = false
redraw = true
vp.Fov.ComputeFov(state.Level, playerCoords, vp.TorchRadius)
}
//vp.layer.ClearArea(0, 7, 20, 1)
//vp.layer.Print(0,7, fmt.Sprintf("pcds: %v", playerCoords))
if redraw {
//terrain
for y := 0; y < vp.H; y++ {
for x := 0; x < vp.W; x++ {
mapCoords := types.Coords{vp.cameraCoords.X + x, vp.cameraCoords.Y + y}
if state.Level.InBounds(mapCoords) {
tile := state.Level.GetTile(mapCoords)
if tile.Explored || tile.Visible {
vp.layer.PutToBase(x+vp.X, y+vp.Y, tile.GetChar(), tile.GetRawColor(), tile.GetRawBgColor())
}
}
}
}
/*
//mobs
pc, err := vp.ToVPCoords(playerCoords)
_ = pc
if err != nil {
fmt.Println("error on getting player position")
} else {
vp.layer.WithColor("white").Put(pc.X+vp.X, pc.Y+vp.Y, "@")
//mw.GetLayer("base").WithColor("white").Put(42, 10, "B")
//mw.GetLayer("overlay").WithColor("white").Put(59, 10, "O")
}
*/
//redraw = true
redraw = false
}
}
func (trs TerrainRenderSystem) Type() string {
return ecs.TerrainRenderSystem
}

View File

@ -5,6 +5,7 @@ import (
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"lab.zaar.be/thefish/alchemyst-go/util"
"lab.zaar.be/thefish/alchemyst-go/util/appctx"
)
//fixme move to config
@ -101,6 +102,15 @@ func GetRandomRoomList(ctx context.Context, rng *util.RNG, l *gamemap.Level, max
return rooms
}
func BlitToLevel (ctx context.Context, l *gamemap.Level, rooms[]gamemap.Room) {
for _, room := range rooms {
err := room.BlitToLevel(ctx, l)
if err != nil {
appctx.Logger(ctx).Err(err)
}
}
}
//delaunay helper funcs
func MedianStraight(rng *util.RNG, l *gamemap.Level, rooms []gamemap.Room, centers []types.Coords, edge types.Edge) {
//find connected rooms

View File

@ -1,7 +1,6 @@
package mapgens
import (
"fmt"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
"lab.zaar.be/thefish/alchemyst-go/util"
"lab.zaar.be/thefish/alchemyst-go/util/appctx"
@ -20,12 +19,7 @@ func DefaultGen(ctx appctx.ClientCtx,l *gamemap.Level) (*gamemap.Level, []gamema
rooms := GetRandomRoomList(ctx, rng, l, maxrooms, minRoomSize, maxRoomSize)
for _, room := range rooms {
err := room.BlitToLevel(l)
if err != nil {
fmt.Printf("err: %v", err)
}
}
BlitToLevel(ctx, l, rooms)
for idx, room := range rooms {
if idx > 0 {

View File

@ -2,7 +2,6 @@ package mapgens
import (
"context"
"fmt"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"lab.zaar.be/thefish/alchemyst-go/util"
@ -22,12 +21,7 @@ func DelaunayMstGen(ctx context.Context, l *gamemap.Level) (*gamemap.Level, []ga
rooms := GetRandomRoomList(ctx, rng, l, maxrooms, minRoomSize, maxRoomSize)
for _, room := range rooms {
err := room.BlitToLevel(l)
if err != nil {
fmt.Printf("err: %v", err)
}
}
BlitToLevel(ctx, l, rooms)
centers := make([]types.Coords, 0)
for _, room := range rooms {

View File

@ -1,7 +1,6 @@
package mapgens
import (
"fmt"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"lab.zaar.be/thefish/alchemyst-go/util"
@ -21,13 +20,7 @@ func DelaunayMstExtGen(ctx appctx.ClientCtx, l *gamemap.Level) (*gamemap.Level,
}
rooms := GetRandomRoomList(ctx, rng, l, maxrooms, minRoomSize, maxRoomSize)
for _, room := range rooms {
err := room.BlitToLevel(l)
if err != nil {
fmt.Printf("err: %v", err)
}
}
BlitToLevel(ctx, l, rooms)
centers := make([]types.Coords, 0)
for _, room := range rooms {

View File

@ -2,7 +2,6 @@ package mapgens
import (
"context"
"fmt"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"lab.zaar.be/thefish/alchemyst-go/util"
@ -21,13 +20,7 @@ func DelaunayPureGen(ctx context.Context, l *gamemap.Level) (*gamemap.Level, []g
}
rooms := GetRandomRoomList(ctx, rng, l, maxrooms, minRoomSize, maxRoomSize)
for _, room := range rooms {
err := room.BlitToLevel(l)
if err != nil {
fmt.Printf("err: %v", err)
}
}
BlitToLevel(ctx, l, rooms)
centers := make([]types.Coords, 0)
for _, room := range rooms {

View File

@ -1,12 +1,14 @@
package gamemap
import (
"context"
"errors"
"fmt"
"lab.zaar.be/thefish/alchemyst-go/engine/items"
"lab.zaar.be/thefish/alchemyst-go/engine/mob"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"lab.zaar.be/thefish/alchemyst-go/util"
"lab.zaar.be/thefish/alchemyst-go/util/appctx"
"strings"
)
@ -31,7 +33,7 @@ func (r *Room) Put (x, y int, tileFunc interface{}) {
}
}
func (room *Room) BlitToLevel(l *Level) error {
func (room *Room) BlitToLevel(ctx context.Context, l *Level) error {
//copy tiles like this:
//https://stackoverflow.com/questions/21011023/copy-pointer-values-a-b-in-golang
@ -49,7 +51,7 @@ func (room *Room) BlitToLevel(l *Level) error {
//check underlying tile
if underlyingTile == nil ||
underlyingTile.Name != "Wall" {
fmt.Println("Invalid blit!")
appctx.Logger(ctx).Warn().Msg("Invalid blit!")
return invalidBlit
}
l.Put(mapCoords.X, mapCoords.Y, tileFunc)

View File

@ -85,4 +85,16 @@ func (c Carried) GetMass(what ecs.Entity) int {
}
func (c *Carried) GetBulk(what ecs.Entity) int {
return c.Bulk
}
}
func FindCarriedOnTile(coords types.Coords) []ecs.Entity {
carrieds := Controller.GetEntitiesWithComponent(ecs.CarriedComponent)
result := make([]ecs.Entity, 0)
for _, ent := range carrieds {
car := Controller.GetComponent(ent, ecs.CarriedComponent)
if car == coords {
result = append(result, ent)
}
}
return result
}

View File

@ -0,0 +1,10 @@
package items
import "lab.zaar.be/thefish/alchemyst-go/engine/ecs"
type Consumable struct {
}
func (c Consumable) Type() string {
return ecs.ConsumableComponent
}

View File

@ -3,5 +3,5 @@ package itemprops
type DamageProfile struct {
Pierce int
Bash int
Cleave int
Crush int
}

View File

@ -6,8 +6,9 @@ import (
)
type Mob struct {
*types.Appearance
types.Appearance
types.Coords
ecs.Named
BlocksPass bool
}

View File

@ -19,7 +19,7 @@ type GameScreen struct {
func NewGameScreen(mw *mainwindow.MainWindow, state *gamestate.GameState, viewPort *mainwindow.ViewPort, controller *ecs.Controller, scm *types.ScreenManager) *GameScreen {
ts := &GameScreen{mw: mw, state: state, vp: viewPort, controller: controller, scm: scm}
renderMobs := systems.MobRenderSystem{EntityController: ts.controller}
renderMobs := systems.MobRenderSystem{Controller: ts.controller, Layer: ts.mw.GetLayer("base"), Viewport: ts.vp}
ts.controller.AddSystem(renderMobs, 1)
return ts
}
@ -82,6 +82,5 @@ func (ts *GameScreen) HandleInput(input string) {
func (ts *GameScreen) Render() {
ts.vp.Render(ts.state)
}
ts.controller.Process([]string{})
}

View File

@ -2,7 +2,6 @@ package mainwindow
import (
"errors"
"fmt"
"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
"lab.zaar.be/thefish/alchemyst-go/engine/fov"
"lab.zaar.be/thefish/alchemyst-go/engine/fov/precomputed_shade"
@ -40,7 +39,7 @@ func NewViewPort(x, y, w, h int, layer *Layer) *ViewPort {
func (vp *ViewPort) Close() {
vp.animateTiles.Stop()
vp.animateTiles = nil //free pointer to ticker
vp.animateTiles = nil //zero pointer to ticker
}
func (vp *ViewPort) Move(state *gamestate.GameState, newCoords types.Coords) {
@ -124,6 +123,7 @@ func (vp *ViewPort) Render(state *gamestate.GameState) {
}
}
}
/*
//mobs
pc, err := vp.ToVPCoords(playerCoords)
_ = pc
@ -134,6 +134,7 @@ func (vp *ViewPort) Render(state *gamestate.GameState) {
//mw.GetLayer("base").WithColor("white").Put(42, 10, "B")
//mw.GetLayer("overlay").WithColor("white").Put(59, 10, "O")
}
*/
//redraw = true
redraw = false