1
0

main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2019 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bytes"
  17. "container/list"
  18. "fmt"
  19. "image"
  20. "image/color"
  21. _ "image/png"
  22. "log"
  23. "math"
  24. "math/rand/v2"
  25. "github.com/hajimehoshi/ebiten/v2"
  26. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  27. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  28. )
  29. const (
  30. screenWidth = 640
  31. screenHeight = 480
  32. )
  33. var smokeImage *ebiten.Image
  34. func init() {
  35. // Decode an image from the image file's byte slice.
  36. img, _, err := image.Decode(bytes.NewReader(images.Smoke_png))
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. smokeImage = ebiten.NewImageFromImage(img)
  41. }
  42. type sprite struct {
  43. count int
  44. maxCount int
  45. dir float64
  46. img *ebiten.Image
  47. scale float64
  48. angle float64
  49. alpha float32
  50. }
  51. func (s *sprite) update() {
  52. if s.count == 0 {
  53. return
  54. }
  55. s.count--
  56. }
  57. func (s *sprite) terminated() bool {
  58. return s.count == 0
  59. }
  60. func (s *sprite) draw(screen *ebiten.Image) {
  61. if s.count == 0 {
  62. return
  63. }
  64. const (
  65. ox = screenWidth / 2
  66. oy = screenHeight / 2
  67. )
  68. x := math.Cos(s.dir) * float64(s.maxCount-s.count)
  69. y := math.Sin(s.dir) * float64(s.maxCount-s.count)
  70. op := &ebiten.DrawImageOptions{}
  71. sx, sy := s.img.Bounds().Dx(), s.img.Bounds().Dy()
  72. op.GeoM.Translate(-float64(sx)/2, -float64(sy)/2)
  73. op.GeoM.Rotate(s.angle)
  74. op.GeoM.Scale(s.scale, s.scale)
  75. op.GeoM.Translate(x, y)
  76. op.GeoM.Translate(ox, oy)
  77. rate := float32(s.count) / float32(s.maxCount)
  78. var alpha float32
  79. if rate < 0.2 {
  80. alpha = rate / 0.2
  81. } else if rate > 0.8 {
  82. alpha = (1 - rate) / 0.2
  83. } else {
  84. alpha = 1
  85. }
  86. alpha *= s.alpha
  87. op.ColorScale.ScaleAlpha(alpha)
  88. screen.DrawImage(s.img, op)
  89. }
  90. func newSprite(img *ebiten.Image) *sprite {
  91. c := rand.IntN(50) + 300
  92. dir := rand.Float64() * 2 * math.Pi
  93. a := rand.Float64() * 2 * math.Pi
  94. s := rand.Float64()*0.1 + 0.4
  95. return &sprite{
  96. img: img,
  97. maxCount: c,
  98. count: c,
  99. dir: dir,
  100. angle: a,
  101. scale: s,
  102. alpha: 0.5,
  103. }
  104. }
  105. type Game struct {
  106. sprites *list.List
  107. }
  108. func (g *Game) Update() error {
  109. if g.sprites == nil {
  110. g.sprites = list.New()
  111. }
  112. if g.sprites.Len() < 500 && rand.IntN(4) < 3 {
  113. // Emit
  114. g.sprites.PushBack(newSprite(smokeImage))
  115. }
  116. for e := g.sprites.Front(); e != nil; e = e.Next() {
  117. s := e.Value.(*sprite)
  118. s.update()
  119. if s.terminated() {
  120. defer g.sprites.Remove(e)
  121. }
  122. }
  123. return nil
  124. }
  125. func (g *Game) Draw(screen *ebiten.Image) {
  126. screen.Fill(color.RGBA{0x99, 0xcc, 0xff, 0xff})
  127. for e := g.sprites.Front(); e != nil; e = e.Next() {
  128. s := e.Value.(*sprite)
  129. s.draw(screen)
  130. }
  131. ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nSprites: %d", ebiten.ActualTPS(), g.sprites.Len()))
  132. }
  133. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  134. return screenWidth, screenHeight
  135. }
  136. func main() {
  137. ebiten.SetWindowSize(screenWidth, screenHeight)
  138. ebiten.SetWindowTitle("Particles (Ebitengine Demo)")
  139. if err := ebiten.RunGame(&Game{}); err != nil {
  140. log.Fatal(err)
  141. }
  142. }