ebiten/examples/particles/main.go

181 lines
3.7 KiB
Go
Raw Normal View History

2019-02-23 17:20:03 +01:00
// Copyright 2019 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2021-06-24 14:49:37 +02:00
//go:build example
2020-10-06 17:45:54 +02:00
// +build example
2019-02-23 17:20:03 +01:00
package main
import (
"bytes"
"container/list"
"fmt"
"image"
"image/color"
_ "image/png"
"log"
"math"
"math/rand"
"time"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
2019-02-23 17:20:03 +01:00
)
func init() {
rand.Seed(time.Now().UnixNano())
}
const (
screenWidth = 640
screenHeight = 480
)
var smokeImage *ebiten.Image
func init() {
// Decode an image from the image file's byte slice.
// Now the byte slice is generated with //go:generate for Go 1.15 or older.
2021-09-20 08:16:09 +02:00
// If you use Go 1.16 or newer, it is strongly recommended to use //go:embed to embed the image file.
// See https://pkg.go.dev/embed for more details.
2019-02-23 17:20:03 +01:00
img, _, err := image.Decode(bytes.NewReader(images.Smoke_png))
if err != nil {
log.Fatal(err)
}
smokeImage = ebiten.NewImageFromImage(img)
2019-02-23 17:20:03 +01:00
}
type sprite struct {
count int
maxCount int
2019-02-23 18:09:38 +01:00
dir float64
2019-02-23 17:20:03 +01:00
img *ebiten.Image
scale float64
2019-02-23 18:09:38 +01:00
angle float64
2019-02-23 17:20:03 +01:00
alpha float64
}
func (s *sprite) update() {
if s.count == 0 {
return
}
s.count--
}
func (s *sprite) terminated() bool {
return s.count == 0
}
func (s *sprite) draw(screen *ebiten.Image) {
if s.count == 0 {
return
}
const (
ox = screenWidth / 2
oy = screenHeight / 2
)
2019-02-23 18:09:38 +01:00
x := math.Cos(s.dir) * float64(s.maxCount-s.count)
y := math.Sin(s.dir) * float64(s.maxCount-s.count)
2019-02-23 17:20:03 +01:00
op := &ebiten.DrawImageOptions{}
sx, sy := s.img.Size()
op.GeoM.Translate(-float64(sx)/2, -float64(sy)/2)
2019-02-23 18:09:38 +01:00
op.GeoM.Rotate(s.angle)
2019-02-23 17:20:03 +01:00
op.GeoM.Scale(s.scale, s.scale)
op.GeoM.Translate(x, y)
op.GeoM.Translate(ox, oy)
rate := float64(s.count) / float64(s.maxCount)
alpha := 0.0
if rate < 0.2 {
alpha = rate / 0.2
} else if rate > 0.8 {
alpha = (1 - rate) / 0.2
} else {
alpha = 1
}
alpha *= s.alpha
op.ColorM.Scale(1, 1, 1, alpha)
screen.DrawImage(s.img, op)
}
func newSprite(img *ebiten.Image) *sprite {
c := rand.Intn(50) + 300
2019-02-23 18:09:38 +01:00
dir := rand.Float64() * 2 * math.Pi
2019-02-23 17:20:03 +01:00
a := rand.Float64() * 2 * math.Pi
s := rand.Float64()*0.1 + 0.4
return &sprite{
img: img,
maxCount: c,
count: c,
2019-02-23 18:09:38 +01:00
dir: dir,
angle: a,
scale: s,
alpha: 0.5,
2019-02-23 17:20:03 +01:00
}
}
2020-05-14 20:51:40 +02:00
type Game struct {
sprites *list.List
}
func (g *Game) Update() error {
2020-05-14 20:51:40 +02:00
if g.sprites == nil {
g.sprites = list.New()
}
2019-02-23 17:20:03 +01:00
2020-05-14 20:51:40 +02:00
if g.sprites.Len() < 500 && rand.Intn(4) < 3 {
2019-02-23 17:20:03 +01:00
// Emit
2020-05-14 20:51:40 +02:00
g.sprites.PushBack(newSprite(smokeImage))
2019-02-23 17:20:03 +01:00
}
2020-05-14 20:51:40 +02:00
for e := g.sprites.Front(); e != nil; e = e.Next() {
2019-02-23 17:20:03 +01:00
s := e.Value.(*sprite)
s.update()
if s.terminated() {
2020-05-14 20:51:40 +02:00
defer g.sprites.Remove(e)
2019-02-23 17:20:03 +01:00
}
}
2020-05-14 20:51:40 +02:00
return nil
}
2019-02-23 17:20:03 +01:00
2020-05-14 20:51:40 +02:00
func (g *Game) Draw(screen *ebiten.Image) {
2019-02-23 17:20:03 +01:00
screen.Fill(color.RGBA{0x99, 0xcc, 0xff, 0xff})
2020-05-14 20:51:40 +02:00
for e := g.sprites.Front(); e != nil; e = e.Next() {
2019-02-23 17:20:03 +01:00
s := e.Value.(*sprite)
s.draw(screen)
}
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nSprites: %d", ebiten.ActualTPS(), g.sprites.Len()))
2020-05-14 20:51:40 +02:00
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
2019-02-23 17:20:03 +01:00
}
func main() {
2020-05-14 20:51:40 +02:00
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Particles (Ebitengine Demo)")
2020-05-14 20:51:40 +02:00
if err := ebiten.RunGame(&Game{}); err != nil {
2019-02-23 17:20:03 +01:00
log.Fatal(err)
}
}