working prototype with ecs

This commit is contained in:
anton.gurov
2019-11-05 17:55:38 +03:00
parent 096ff2b182
commit a195e335eb
7 changed files with 120 additions and 52 deletions

View File

@ -1,6 +1,7 @@
package gamestate
import (
"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
)
@ -10,8 +11,10 @@ type GameState struct {
Input chan string
RawInput chan int
FovRecompute chan struct{}
Redraw chan struct{}
Redraw chan struct{}
Player ecs.Entity
Level *gamemap.Level
Controller *ecs.Controller
}
// do runs f on the main thread.

View File

@ -1,8 +1,6 @@
package mob
import (
"fmt"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"reflect"
)
@ -13,26 +11,10 @@ type Mob struct {
BlocksPass bool
}
func (m *Mob) Walk(level *gamemap.Level, dx, dy int) {
newCoords := types.Coords{m.X + dx, m.Y + dy}
if level.GetTile(newCoords).BlocksPass {
return
}
//fixme
//if level.Objects.At(newCoords).HasComponent("block_pass") {
//
//}
fmt.Printf("new coords: %d, %d\n", m.Coords.X, m.Coords.Y)
}
func (m *Mob) Render() {
}
func (m *Mob) MoveToCoords(c types.Coords) {
}
func (mob Mob) TypeOf() reflect.Type {
return reflect.TypeOf(mob)
}

View File

@ -0,0 +1,50 @@
package movement
import (
"fmt"
"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
"lab.zaar.be/thefish/alchemyst-go/engine/mob"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"reflect"
)
type Moveable struct {
Controller *ecs.Controller
Level *gamemap.Level
}
func (mov Moveable) Walk() {
}
func (mov Moveable) IsBlocked(c types.Coords) bool {
if mov.Level.GetTile(c).BlocksPass == true {
return true
}
list := mov.Controller.GetEntitiesWithComponent(mob.Mob{}.TypeOf())
for idx, _ := range list {
coords := mov.Controller.GetComponent(list[idx], types.Coords{}.TypeOf())
if coords == nil {
continue
}
coords = coords.(types.Coords)
if coords != c {
continue
}
m := mov.Controller.GetComponent(list[idx], mob.Mob{}.TypeOf())
if m == nil {
continue
}
if m.(mob.Mob).BlocksPass {
return true
}
}
fmt.Printf("\nCoords %v do not block pass!", c)
return false
}
func (mov Moveable) TypeOf() reflect.Type {
return reflect.TypeOf(mov)
}

View File

@ -1,5 +0,0 @@
package mob
type Player struct {
Mob
}

View File

@ -2,6 +2,8 @@ package screens
import (
"lab.zaar.be/thefish/alchemyst-go/engine/gamestate"
"lab.zaar.be/thefish/alchemyst-go/engine/mob/movement"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"lab.zaar.be/thefish/alchemyst-go/ui/mainwindow"
)
@ -21,28 +23,28 @@ func (ts *GameScreen) HandleInput(input string) {
//ts.state.Do(func(){
switch input {
case "Up", "k", "8":
ts.state.Player.Walk(ts.state.Level, 0, -1)
ts.walk(ts.state, 0, -1)
break
case "Down", "j", "2":
ts.state.Player.Walk(ts.state.Level,0, 1)
ts.walk(ts.state,0, 1)
break
case "Left", "h", "4":
ts.state.Player.Walk(ts.state.Level,-1, 0)
ts.walk(ts.state,-1, 0)
break
case "Right", "l", "6":
ts.state.Player.Walk(ts.state.Level,1, 0)
ts.walk(ts.state,1, 0)
break
case "y", "7":
ts.state.Player.Walk(ts.state.Level,-1, -1)
ts.walk(ts.state,-1, -1)
break
case "u", "9":
ts.state.Player.Walk(ts.state.Level,1, -1)
ts.walk(ts.state,1, -1)
break
case "b", "1":
ts.state.Player.Walk(ts.state.Level,-1, 1)
ts.walk(ts.state,-1, 1)
break
case "n", "3":
ts.state.Player.Walk(ts.state.Level,1, 1)
ts.walk(ts.state,1, 1)
break
default:
ts.mw.GetLayer("base").ClearArea(0, 3, 40, 1)
@ -56,3 +58,18 @@ func (ts *GameScreen) Exit() {}
func (ts *GameScreen) Render() {
ts.vp.Render(ts.state)
}
func (ts *GameScreen) walk(state *gamestate.GameState, dx, dy int) {
controller := state.Controller
coords := controller.GetComponent(state.Player, types.Coords{}.TypeOf()).(types.Coords)
newCoords := types.Coords{coords.X + dx, coords.Y + dy}
movable := controller.GetComponent(state.Player, movement.Moveable{}.TypeOf()).(movement.Moveable)
if !movable.IsBlocked(newCoords) {
controller.UpdateComponent(state.Player, types.Coords{}.TypeOf(), newCoords)
}
state.Redraw <- struct{}{}
state.FovRecompute <- struct{}{}
}