2023-02-17 12:47:17 +03:00
|
|
|
package gx
|
|
|
|
|
2023-02-17 23:51:40 +03:00
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
|
|
|
|
2023-02-17 16:56:15 +03:00
|
|
|
type Sprite struct {
|
2023-05-04 19:31:33 +03:00
|
|
|
T Transform
|
2023-05-26 18:31:04 +03:00
|
|
|
Shader *Shader
|
|
|
|
Images [4]*Image
|
|
|
|
Uniforms map[string] any
|
2023-05-23 16:19:55 +03:00
|
|
|
Floating, Visible bool
|
2023-02-17 16:56:15 +03:00
|
|
|
}
|
2023-02-17 23:51:40 +03:00
|
|
|
|
|
|
|
func (s *Sprite) Draw(
|
|
|
|
e *Engine,
|
|
|
|
i *Image,
|
|
|
|
) {
|
2023-05-26 18:31:04 +03:00
|
|
|
// Nothing to draw.
|
|
|
|
if s.Images[0] == nil {
|
|
|
|
return
|
2023-05-22 23:39:01 +03:00
|
|
|
}
|
2023-05-26 18:31:04 +03:00
|
|
|
|
2023-02-18 19:35:38 +03:00
|
|
|
m := &Matrix{}
|
2023-02-17 23:51:40 +03:00
|
|
|
|
2023-05-04 19:31:33 +03:00
|
|
|
m.Concat(s.T.Matrix(e))
|
2023-05-26 18:31:04 +03:00
|
|
|
if !s.Floating {
|
|
|
|
m.Concat(e.Camera().RealMatrix(
|
2023-02-18 15:38:28 +03:00
|
|
|
e,
|
|
|
|
true,
|
|
|
|
))
|
2023-02-18 03:51:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
// Drawing without shader.
|
|
|
|
if s.Shader == nil {
|
|
|
|
opts := &ebiten.DrawImageOptions{}
|
|
|
|
opts.GeoM = *m
|
|
|
|
i.DrawImage(s.Images[0], opts)
|
|
|
|
return
|
|
|
|
}
|
2023-05-22 23:39:01 +03:00
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
// Drawing with shader.
|
|
|
|
w, h := s.Images[0].Size()
|
|
|
|
opts := &ebiten.DrawRectShaderOptions{
|
|
|
|
Images: s.Images,
|
|
|
|
Uniforms: s.Uniforms,
|
|
|
|
GeoM: *m,
|
|
|
|
}
|
|
|
|
i.DrawRectShader(w, h, s.Shader, opts)
|
2023-02-17 23:51:40 +03:00
|
|
|
}
|
|
|
|
|
2023-05-23 16:19:55 +03:00
|
|
|
func (s *Sprite) IsVisible() bool {
|
|
|
|
return s.Visible
|
|
|
|
}
|
|
|
|
|