Skip to content

Instantly share code, notes, and snippets.

@Noofbiz
Created December 11, 2018 00:29
Show Gist options
  • Save Noofbiz/af17111cc0cc197c6bd631134b9d02e0 to your computer and use it in GitHub Desktop.
Save Noofbiz/af17111cc0cc197c6bd631134b9d02e0 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"image"
"image/color"
"math/rand"
"sync"
"time"
"engo.io/ecs"
"engo.io/engo"
"engo.io/engo/common"
)
type DemoScene struct{}
func (*DemoScene) Preload() {}
func (*DemoScene) Setup(u engo.Updater) {
w, _ := u.(*ecs.World)
common.SetBackground(color.White)
w.AddSystem(&common.RenderSystem{})
w.AddSystem(&FPSLogSystem{})
w.AddSystem(&SpriteSystem{})
}
func (*DemoScene) Type() string {
return "demo"
}
type FPSLogSystem struct {
frames, drawCount int
ticker <-chan time.Time
lock sync.RWMutex
}
func (f *FPSLogSystem) New(*ecs.World) {
f.ticker = time.Tick(time.Second)
}
func (f *FPSLogSystem) Update(dt float32) {
f.frames++
select {
case <-f.ticker:
fmt.Printf("FPS is: %v\n", f.frames)
f.frames = 0
default:
}
}
func (*FPSLogSystem) Remove(e ecs.BasicEntity) {}
type SpriteSystem struct {
w *ecs.World
textures []common.Drawable
spritesheet *common.Spritesheet
drawCount, curCount int
}
func (s *SpriteSystem) New(w *ecs.World) {
s.w = w
rand.Seed(time.Now().UnixNano())
engo.Input.RegisterButton("spritesheet", engo.KeyS)
engo.Input.RegisterButton("lone sprite", engo.KeyA)
for imgs := 0; imgs < 10; imgs++ {
img := image.NewNRGBA(image.Rect(0, 0, 25, 25))
for i := 0; i < 25; i++ {
for j := 0; j < 25; j++ {
img.Set(i, j, color.NRGBA{R: uint8(rand.Intn(256)), G: uint8(rand.Intn(256)), B: uint8(rand.Intn(256)), A: 255})
}
}
imgobj := common.NewImageObject(img)
tex := common.NewTextureSingle(imgobj)
s.textures = append(s.textures, tex)
}
img := image.NewNRGBA(image.Rect(0, 0, 100, 100))
for i := 0; i < 100; i++ {
for j := 0; j < 100; j++ {
img.Set(i, j, color.NRGBA{R: uint8(rand.Intn(256)), G: uint8(rand.Intn(256)), B: uint8(rand.Intn(256)), A: 255})
}
}
imgobj := common.NewImageObject(img)
tex := common.NewTextureResource(imgobj)
s.spritesheet = common.NewSpritesheetFromTexture(&tex, 10, 10)
}
type sprite struct {
ecs.BasicEntity
common.SpaceComponent
common.RenderComponent
}
func (s *SpriteSystem) Update(float32) {
if engo.Input.Button("spritesheet").Down() {
s.drawCount++
sp := sprite{BasicEntity: ecs.NewBasic()}
sp.Position = engo.Point{
X: rand.Float32() * 800,
Y: rand.Float32() * 800,
}
sp.Rotation = rand.Float32() * 180
sp.Drawable = s.spritesheet.Drawable(rand.Intn(100))
for _, system := range s.w.Systems() {
switch sys := system.(type) {
case *common.RenderSystem:
sys.Add(&sp.BasicEntity, &sp.RenderComponent, &sp.SpaceComponent)
}
}
}
if engo.Input.Button("lone sprite").Down() {
s.drawCount++
sp := sprite{BasicEntity: ecs.NewBasic()}
sp.Position = engo.Point{
X: rand.Float32() * 800,
Y: rand.Float32() * 800,
}
sp.Rotation = rand.Float32() * 180
sp.Drawable = s.textures[rand.Intn(len(s.textures))]
for _, system := range s.w.Systems() {
switch sys := system.(type) {
case *common.RenderSystem:
sys.Add(&sp.BasicEntity, &sp.RenderComponent, &sp.SpaceComponent)
}
}
}
if s.drawCount != s.curCount {
s.curCount = s.drawCount
fmt.Printf("Drawable Count: %v\n", s.curCount)
}
}
func (*SpriteSystem) Remove(e ecs.BasicEntity) {}
func main() {
engo.Run(
engo.RunOptions{
Width: 800,
Height: 800,
FPSLimit: 60,
},
&DemoScene{},
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment