gg/sprite.go

104 lines
1.7 KiB
Go
Raw Normal View History

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
)
type Sprite struct {
Object
Transform
ShaderOptions
2023-12-23 00:09:07 +03:00
Floating bool
Visibility
}
2023-02-17 23:51:40 +03:00
var (
//spritesOp DrawImageOptions
)
func (s *Sprite) Draw(c *Context) []EVertex {
2023-05-26 18:31:04 +03:00
// Nothing to draw.
if s.Images[0] == nil {
return nil
2023-05-22 23:39:01 +03:00
}
2023-05-26 18:31:04 +03:00
t := s.RectangleToDraw().Transform
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 {
c.DrawImage(s.Images[0], &ebiten.DrawImageOptions{
GeoM: m,
})
return nil
2023-05-26 18:31:04 +03:00
}
2023-05-22 23:39:01 +03:00
2023-05-26 18:31:04 +03:00
w, h := s.Images[0].Size()
// Drawing with shader.
c.DrawRectShader(w, h, s.Shader, &ebiten.DrawRectShaderOptions{
2023-05-26 18:31:04 +03:00
Images: s.Images,
Uniforms: s.Uniforms,
GeoM: m,
})
return nil
2023-02-17 23:51:40 +03:00
}
// 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
2024-01-18 06:06:27 +03:00
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),
}
}
2023-12-23 00:09:07 +03:00
// 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)
}