-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from yohamta/develop
ECS layer system
- Loading branch information
Showing
9 changed files
with
201 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,50 @@ | ||
package ecs | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hajimehoshi/ebiten/v2" | ||
"github.com/yohamta/donburi" | ||
) | ||
|
||
type layer struct { | ||
type Layer struct { | ||
*layer | ||
systems []DrawSystem | ||
image *ebiten.Image | ||
} | ||
|
||
func newLayer() *layer { | ||
return &layer{ | ||
systems: []DrawSystem{}, | ||
} | ||
func newLayer(l *layer) *Layer { | ||
return &Layer{l, []DrawSystem{}} | ||
} | ||
|
||
func (l *layer) Draw(e *ECS, i *ebiten.Image) { | ||
func (l *Layer) draw(e *ECS, i *ebiten.Image) { | ||
screen := i | ||
if l.image != nil { | ||
screen = l.image | ||
} | ||
for _, s := range l.systems { | ||
s(e, screen) | ||
} | ||
} | ||
|
||
func (l *layer) addDrawSystem(s DrawSystem) { | ||
func (l *Layer) addDrawSystem(s DrawSystem) { | ||
l.systems = append(l.systems, s) | ||
} | ||
|
||
var ( | ||
layers []*layer | ||
) | ||
|
||
type layer struct { | ||
id LayerID | ||
tag *donburi.ComponentType | ||
} | ||
|
||
func getLayer(layerID LayerID) *layer { | ||
if int(layerID) >= len(layers) { | ||
layers = append(layers, make([]*layer, int(layerID)-len(layers)+1)...) | ||
} | ||
if layers[layerID] == nil { | ||
layers[layerID] = &layer{ | ||
id: layerID, | ||
tag: donburi.NewTag().SetName(fmt.Sprintf("Layer%d", layerID)), | ||
} | ||
} | ||
return layers[layerID] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ecs | ||
|
||
import "testing" | ||
|
||
func TestLayer(t *testing.T) { | ||
l := getLayer(0) | ||
ll := getLayer(1) | ||
|
||
if l.id != 0 { | ||
t.Errorf("layer id is not 0") | ||
} | ||
if ll.id != 1 { | ||
t.Errorf("layer id is not 1") | ||
} | ||
if l.tag == ll.tag { | ||
t.Errorf("layer tag is same") | ||
} | ||
if l.tag.Name() != "Layer0" { | ||
t.Errorf("layer tag name is not Layer0") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package layers | ||
|
||
import "github.com/yohamta/donburi/ecs" | ||
|
||
const ( | ||
LayerBackground ecs.LayerID = iota | ||
LayerBunnies | ||
LayerMetrics | ||
) |
Oops, something went wrong.