gg/sprite.go
2024-01-18 06:06:27 +03:00

103 lines
1.7 KiB
Go

package gg
import (
"github.com/hajimehoshi/ebiten/v2"
//"fmt"
)
type Sprite struct {
Object
Transform
ShaderOptions
Floating bool
Visibility
}
var (
//spritesOp DrawImageOptions
)
func (s *Sprite) Draw(c *Context) []EVertex {
// Nothing to draw.
if s.Images[0] == nil {
return nil
}
t := s.RectangleToDraw().Transform
m := t.Matrix()
if !s.Floating {
m.Concat(c.Camera.RealMatrix())
}
// Drawing without shader.
if s.Shader == nil {
c.DrawImage(s.Images[0], &ebiten.DrawImageOptions{
GeoM: m,
})
return nil
}
w, h := s.Images[0].Size()
// Drawing with shader.
c.DrawRectShader(w, h, s.Shader, &ebiten.DrawRectShaderOptions{
Images: s.Images,
Uniforms: s.Uniforms,
GeoM: m,
})
return nil
}
// Return the rectangle that contains the sprite to draw.
func (s *Sprite) RectangleToDraw() Rectangle {
if s.Images[0] == nil {
return Rectangle{}
}
w, h := s.Images[0].Size()
t := s.Transform
t.SetAround(
t.Around().Mul(
V(Float(w), Float(h)),
),
)
return Rectangle{
Transform: t,
}
}
// Return the rectangle that contains the sprite in the engine.
func (s *Sprite) Rectangle() Rectangle {
if s.Images[0] == nil {
return Rectangle{
Transform: s.Transform,
}
}
w, h := s.Images[0].Size()
t := s.Transform
return Rectangle{
Transform: t,
Width: Float(w),
Height: Float(h),
}
}
// Get triangles of the rectangle that contains the sprite
// and has the same widght and height.
func (s *Sprite) MakeTriangles() Triangles {
return s.Rectangle().MakeTriangles()
}
func (s *Sprite) Vertices() Vertices {
return s.Rectangle().Vertices()
}
func (s *Sprite) Edges() Edges {
return s.Rectangle().Edges()
}
func (s *Sprite) ContainedPoints(pts Points) Points {
return s.Rectangle().ContainedPoints(pts)
}