gg/sprite.go

73 lines
1.2 KiB
Go

package gg
import (
"github.com/hajimehoshi/ebiten/v2"
//"fmt"
)
type Sprite struct {
Object
Transform
ShaderOptions
Floating bool
Visibility
Collidability
Resolvability
}
var (
//spritesOp DrawImageOptions
)
func (s *Sprite) Draw(c *Context) []EVertex {
// Nothing to draw.
if s.Images[0] == nil {
return nil
}
t := s.Rectangle().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.
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
t.Around.X *= Float(w)
t.Around.Y *= Float(h)
return Rectangle{
Transform: t,
}
}
// 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()
}