2024-05-28 11:24:12 +03:00
|
|
|
package ox
|
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
|
|
|
)
|
|
|
|
|
2024-05-28 11:24:12 +03:00
|
|
|
import "surdeus.su/core/gg/mx"
|
2024-06-01 16:07:28 +03:00
|
|
|
import "surdeus.su/core/gg"
|
2024-05-28 11:24:12 +03:00
|
|
|
|
2024-06-01 16:07:28 +03:00
|
|
|
var _ = gg.Drawer(&Sprite{})
|
2023-02-17 16:56:15 +03:00
|
|
|
type Sprite struct {
|
2024-06-01 16:07:28 +03:00
|
|
|
Transform
|
|
|
|
Drawity
|
2023-02-17 16:56:15 +03:00
|
|
|
}
|
2023-02-17 23:51:40 +03:00
|
|
|
|
2024-01-11 04:45:28 +03:00
|
|
|
var (
|
|
|
|
//spritesOp DrawImageOptions
|
|
|
|
)
|
|
|
|
|
2024-06-01 16:07:28 +03:00
|
|
|
func (s *Sprite) Draw(c gg.Context) *gg.Drawing {
|
2023-05-26 18:31:04 +03:00
|
|
|
// Nothing to draw.
|
|
|
|
if s.Images[0] == nil {
|
2024-01-11 06:39:57 +03:00
|
|
|
return nil
|
2023-05-22 23:39:01 +03:00
|
|
|
}
|
2023-05-26 18:31:04 +03:00
|
|
|
|
2024-05-28 11:24:12 +03:00
|
|
|
t := s.GetRectangleToDraw().Transform
|
|
|
|
m := t.GetMatrice()
|
2024-06-01 16:07:28 +03:00
|
|
|
if !s.IsFloating() {
|
|
|
|
m.Concat(c.Camera().GetRealMatrice(c))
|
2023-02-18 03:51:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
// Drawing without shader.
|
|
|
|
if s.Shader == nil {
|
2024-06-01 16:07:28 +03:00
|
|
|
c.Image().DrawImage(s.Images[0], &ebiten.DrawImageOptions{
|
2024-01-08 02:53:00 +03:00
|
|
|
GeoM: m,
|
|
|
|
})
|
2024-01-11 06:39:57 +03:00
|
|
|
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()
|
2023-06-03 10:39:15 +03:00
|
|
|
// Drawing with shader.
|
2024-06-01 16:07:28 +03:00
|
|
|
c.Image().DrawRectShader(
|
2024-05-28 11:24:12 +03:00
|
|
|
w, h,
|
|
|
|
s.Shader,
|
|
|
|
&ebiten.DrawRectShaderOptions{
|
|
|
|
Images: s.Images,
|
|
|
|
Uniforms: s.Uniforms,
|
|
|
|
GeoM: m,
|
|
|
|
},
|
|
|
|
)
|
2024-01-11 06:39:57 +03:00
|
|
|
return nil
|
2023-02-17 23:51:40 +03:00
|
|
|
}
|
|
|
|
|
2024-01-16 04:11:42 +03:00
|
|
|
// Return the rectangle that contains the sprite to draw.
|
2024-05-28 11:24:12 +03:00
|
|
|
func (s *Sprite) GetRectangleToDraw() Rectangle {
|
2023-06-03 10:39:15 +03:00
|
|
|
if s.Images[0] == nil {
|
2024-01-16 04:11:42 +03:00
|
|
|
return Rectangle{}
|
2023-06-03 10:39:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
w, h := s.Images[0].Size()
|
|
|
|
t := s.Transform
|
2024-01-18 06:06:27 +03:00
|
|
|
t.SetAround(
|
2024-06-01 16:07:28 +03:00
|
|
|
t.GetAround().Mul(
|
|
|
|
mx.Vector{mx.Float(w), mx.Float(h)},
|
2024-01-18 06:06:27 +03:00
|
|
|
),
|
|
|
|
)
|
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
|
|
|
}
|
|
|
|
|
2024-05-28 11:24:12 +03:00
|
|
|
// Return the rectangle that contains
|
|
|
|
// the sprite in the engine.
|
|
|
|
func (s *Sprite) GetRectangle() Rectangle {
|
2024-01-16 04:11:42 +03:00
|
|
|
if s.Images[0] == nil {
|
|
|
|
return Rectangle{
|
|
|
|
Transform: s.Transform,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w, h := s.Images[0].Size()
|
|
|
|
t := s.Transform
|
|
|
|
|
|
|
|
return Rectangle{
|
|
|
|
Transform: t,
|
2024-06-01 16:07:28 +03:00
|
|
|
Width: mx.Float(w),
|
|
|
|
Height: mx.Float(h),
|
2024-01-16 04:11:42 +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.
|
2024-06-01 16:07:28 +03:00
|
|
|
func (s *Sprite) MakeTriangles() mx.Triangles {
|
|
|
|
return s.GetRectangle().MakeTriangles()
|
2023-06-03 10:39:15 +03:00
|
|
|
}
|
|
|
|
|
2024-05-28 11:24:12 +03:00
|
|
|
func (s *Sprite) Vertices() mx.Vectors {
|
|
|
|
return s.GetRectangle().Vertices()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) Edges() mx.Lines {
|
|
|
|
return s.GetRectangle().Edges()
|
2024-01-16 04:11:42 +03:00
|
|
|
}
|
|
|
|
|
2024-06-01 16:07:28 +03:00
|
|
|
func (s *Sprite) ContainsPoint(
|
|
|
|
pt mx.Vector,
|
|
|
|
) bool {
|
2024-05-28 11:24:12 +03:00
|
|
|
return s.GetRectangle().ContainsPoint(pt)
|
2024-01-16 04:11:42 +03:00
|
|
|
}
|
|
|
|
|
2024-06-01 16:07:28 +03:00
|
|
|
func (s *Sprite) GetContainedPoints(
|
|
|
|
pts mx.Vectors,
|
|
|
|
) mx.Vectors {
|
|
|
|
return s.GetRectangle().GetContainedPoints(pts)
|
2024-01-16 04:11:42 +03:00
|
|
|
}
|