2023-10-23 15:45:18 +03:00
|
|
|
package gg
|
2023-02-17 12:47:17 +03:00
|
|
|
|
2023-02-17 23:51:40 +03:00
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2023-12-23 00:09:07 +03:00
|
|
|
//"fmt"
|
2023-02-17 23:51:40 +03:00
|
|
|
)
|
|
|
|
|
2023-02-17 16:56:15 +03:00
|
|
|
type Sprite struct {
|
2024-01-08 06:09:36 +03:00
|
|
|
Object
|
2023-05-30 13:24:14 +03:00
|
|
|
Transform
|
|
|
|
ShaderOptions
|
2023-12-23 00:09:07 +03:00
|
|
|
Floating bool
|
|
|
|
Visibility
|
2023-02-17 16:56:15 +03:00
|
|
|
}
|
2023-02-17 23:51:40 +03:00
|
|
|
|
2023-12-20 22:39:33 +03:00
|
|
|
func (s *Sprite) Draw(c *Context) {
|
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-06-03 10:39:15 +03:00
|
|
|
t := s.Rectangle().Transform
|
2024-01-08 02:53:00 +03:00
|
|
|
m := t.Matrix()
|
2023-05-26 18:31:04 +03:00
|
|
|
if !s.Floating {
|
2023-12-23 00:09:07 +03:00
|
|
|
m.Concat(c.Camera.RealMatrix())
|
2023-02-18 03:51:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
// Drawing without shader.
|
|
|
|
if s.Shader == nil {
|
2024-01-08 02:53:00 +03:00
|
|
|
c.DrawImage(s.Images[0], &ebiten.DrawImageOptions{
|
|
|
|
GeoM: m,
|
|
|
|
})
|
2023-05-26 18:31:04 +03:00
|
|
|
return
|
|
|
|
}
|
2023-05-22 23:39:01 +03:00
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
w, h := s.Images[0].Size()
|
2023-06-03 10:39:15 +03:00
|
|
|
// Drawing with shader.
|
2024-01-08 02:53:00 +03:00
|
|
|
c.DrawRectShader(w, h, s.Shader, &ebiten.DrawRectShaderOptions{
|
2023-05-26 18:31:04 +03:00
|
|
|
Images: s.Images,
|
|
|
|
Uniforms: s.Uniforms,
|
2024-01-08 02:53:00 +03:00
|
|
|
GeoM: m,
|
|
|
|
})
|
2023-02-17 23:51:40 +03:00
|
|
|
}
|
|
|
|
|
2023-06-03 10:39:15 +03:00
|
|
|
// Return the rectangle that contains the sprite.
|
|
|
|
func (s *Sprite) Rectangle() Rectangle {
|
|
|
|
if s.Images[0] == nil {
|
|
|
|
panic("trying to get rectangle for nil image pointer")
|
|
|
|
}
|
|
|
|
|
|
|
|
w, h := s.Images[0].Size()
|
|
|
|
t := s.Transform
|
2023-12-23 00:09:07 +03:00
|
|
|
t.Around.X *= Float(w)
|
|
|
|
t.Around.Y *= Float(h)
|
2023-06-03 10:39:15 +03:00
|
|
|
|
2024-01-08 06:09:36 +03:00
|
|
|
return Rectangle{
|
|
|
|
Transform: t,
|
|
|
|
}
|
2023-06-03 10:39:15 +03:00
|
|
|
}
|
|
|
|
|
2023-12-23 00:09:07 +03:00
|
|
|
// Get triangles of the rectangle that contains the sprite
|
|
|
|
// and has the same widght and height.
|
2023-06-03 10:39:15 +03:00
|
|
|
func (s *Sprite) Triangles() Triangles {
|
|
|
|
return s.Rectangle().Triangles()
|
|
|
|
}
|
|
|
|
|