Guest User

Untitled

a guest
Dec 10th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "image"
  6. "image/color"
  7. "math/rand"
  8. "sync"
  9. "time"
  10.  
  11. "engo.io/ecs"
  12. "engo.io/engo"
  13. "engo.io/engo/common"
  14. )
  15.  
  16. type DemoScene struct{}
  17.  
  18. func (*DemoScene) Preload() {}
  19.  
  20. func (*DemoScene) Setup(u engo.Updater) {
  21. w, _ := u.(*ecs.World)
  22. common.SetBackground(color.White)
  23. w.AddSystem(&common.RenderSystem{})
  24. w.AddSystem(&FPSLogSystem{})
  25. w.AddSystem(&SpriteSystem{})
  26. }
  27.  
  28. func (*DemoScene) Type() string {
  29. return "demo"
  30. }
  31.  
  32. type FPSLogSystem struct {
  33. frames, drawCount int
  34. ticker <-chan time.Time
  35. lock sync.RWMutex
  36. }
  37.  
  38. func (f *FPSLogSystem) New(*ecs.World) {
  39. f.ticker = time.Tick(time.Second)
  40. }
  41.  
  42. func (f *FPSLogSystem) Update(dt float32) {
  43. f.frames++
  44. select {
  45. case <-f.ticker:
  46. fmt.Printf("FPS is: %v\n", f.frames)
  47. f.frames = 0
  48. default:
  49. }
  50. }
  51.  
  52. func (*FPSLogSystem) Remove(e ecs.BasicEntity) {}
  53.  
  54. type SpriteSystem struct {
  55. w *ecs.World
  56. textures []common.Drawable
  57. spritesheet *common.Spritesheet
  58. drawCount, curCount int
  59. }
  60.  
  61. func (s *SpriteSystem) New(w *ecs.World) {
  62. s.w = w
  63. rand.Seed(time.Now().UnixNano())
  64. engo.Input.RegisterButton("spritesheet", engo.KeyS)
  65. engo.Input.RegisterButton("lone sprite", engo.KeyA)
  66.  
  67. for imgs := 0; imgs < 10; imgs++ {
  68. img := image.NewNRGBA(image.Rect(0, 0, 25, 25))
  69. for i := 0; i < 25; i++ {
  70. for j := 0; j < 25; j++ {
  71. img.Set(i, j, color.NRGBA{R: uint8(rand.Intn(256)), G: uint8(rand.Intn(256)), B: uint8(rand.Intn(256)), A: 255})
  72. }
  73. }
  74. imgobj := common.NewImageObject(img)
  75. tex := common.NewTextureSingle(imgobj)
  76. s.textures = append(s.textures, tex)
  77. }
  78.  
  79. img := image.NewNRGBA(image.Rect(0, 0, 100, 100))
  80. for i := 0; i < 100; i++ {
  81. for j := 0; j < 100; j++ {
  82. img.Set(i, j, color.NRGBA{R: uint8(rand.Intn(256)), G: uint8(rand.Intn(256)), B: uint8(rand.Intn(256)), A: 255})
  83. }
  84. }
  85. imgobj := common.NewImageObject(img)
  86. tex := common.NewTextureResource(imgobj)
  87. s.spritesheet = common.NewSpritesheetFromTexture(&tex, 10, 10)
  88. }
  89.  
  90. type sprite struct {
  91. ecs.BasicEntity
  92. common.SpaceComponent
  93. common.RenderComponent
  94. }
  95.  
  96. func (s *SpriteSystem) Update(float32) {
  97. if engo.Input.Button("spritesheet").Down() {
  98. s.drawCount++
  99. sp := sprite{BasicEntity: ecs.NewBasic()}
  100. sp.Position = engo.Point{
  101. X: rand.Float32() * 800,
  102. Y: rand.Float32() * 800,
  103. }
  104. sp.Rotation = rand.Float32() * 180
  105. sp.Drawable = s.spritesheet.Drawable(rand.Intn(100))
  106. for _, system := range s.w.Systems() {
  107. switch sys := system.(type) {
  108. case *common.RenderSystem:
  109. sys.Add(&sp.BasicEntity, &sp.RenderComponent, &sp.SpaceComponent)
  110. }
  111. }
  112. }
  113. if engo.Input.Button("lone sprite").Down() {
  114. s.drawCount++
  115. sp := sprite{BasicEntity: ecs.NewBasic()}
  116. sp.Position = engo.Point{
  117. X: rand.Float32() * 800,
  118. Y: rand.Float32() * 800,
  119. }
  120. sp.Rotation = rand.Float32() * 180
  121. sp.Drawable = s.textures[rand.Intn(len(s.textures))]
  122. for _, system := range s.w.Systems() {
  123. switch sys := system.(type) {
  124. case *common.RenderSystem:
  125. sys.Add(&sp.BasicEntity, &sp.RenderComponent, &sp.SpaceComponent)
  126. }
  127. }
  128. }
  129. if s.drawCount != s.curCount {
  130. s.curCount = s.drawCount
  131. fmt.Printf("Drawable Count: %v\n", s.curCount)
  132. }
  133. }
  134.  
  135. func (*SpriteSystem) Remove(e ecs.BasicEntity) {}
  136.  
  137. func main() {
  138. engo.Run(
  139. engo.RunOptions{
  140. Width: 800,
  141. Height: 800,
  142. FPSLimit: 60,
  143. },
  144. &DemoScene{},
  145. )
  146. }
Add Comment
Please, Sign In to add comment