ichigo/engine/sprite.go

130 lines
2.9 KiB
Go
Raw Normal View History

2021-08-04 15:08:26 +10:00
package engine
import (
2021-08-05 12:33:23 +10:00
"encoding/gob"
2021-09-17 11:13:39 +10:00
"fmt"
2021-08-04 15:08:26 +10:00
"image"
2021-09-08 20:08:57 +10:00
"drjosh.dev/gurgle/geom"
2021-08-04 15:08:26 +10:00
"github.com/hajimehoshi/ebiten/v2"
)
2021-08-18 16:34:51 +10:00
// Ensure Sprite satisfies interfaces.
2021-08-27 11:49:11 +10:00
var _ interface {
2021-09-10 17:18:20 +10:00
BoundingBoxer
2021-08-27 11:49:11 +10:00
Drawer
Scanner
2021-09-01 09:34:49 +10:00
Transformer
2021-08-27 11:49:11 +10:00
Updater
} = &Sprite{}
2021-08-18 16:34:51 +10:00
2021-08-05 12:33:23 +10:00
func init() {
2021-08-25 15:04:38 +10:00
gob.Register(&Sprite{})
2021-08-05 12:33:23 +10:00
}
2021-08-05 12:26:41 +10:00
// Sprite combines an Actor with the ability to Draw from a single spritesheet.
2021-08-04 15:08:26 +10:00
type Sprite struct {
2021-09-03 10:24:16 +10:00
Actor Actor
DrawOffset image.Point
2021-09-13 16:50:35 +10:00
Hides
2021-08-26 13:57:19 +10:00
Sheet Sheet
2021-08-04 15:08:26 +10:00
2021-08-05 12:26:41 +10:00
anim *Anim
2021-08-04 15:08:26 +10:00
}
2021-09-10 17:18:20 +10:00
// BoundingBox forwards the call to Actor.
func (s *Sprite) BoundingBox() geom.Box { return s.Actor.BoundingBox() }
2021-09-02 11:53:04 +10:00
// Draw draws the current cell to the screen.
func (s *Sprite) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
2021-09-02 11:53:04 +10:00
screen.DrawImage(s.Sheet.SubImage(s.anim.Cell()), opts)
2021-08-04 15:08:26 +10:00
}
2021-09-11 13:00:23 +10:00
// DrawAfter reports if the sprite should be drawn after x.
2021-09-10 17:18:20 +10:00
func (s *Sprite) DrawAfter(x Drawer) bool {
2021-09-16 11:18:24 +10:00
if !commonDrawerComparisons {
2021-09-15 17:41:23 +10:00
sb := s.BoundingBox()
switch x := x.(type) {
case BoundingBoxer:
xb := x.BoundingBox()
if sb.Max.Z <= xb.Min.Z { // s is behind x
return false
}
if sb.Min.Z >= xb.Max.Z { // s is in front of x
return true
}
if sb.Min.Y >= xb.Max.Y { // s is below x
return false
}
if sb.Max.Y <= xb.Min.Y { // s is above x
return true
}
case ZPositioner:
return sb.Min.Z > x.ZPos() // s is after
2021-09-11 13:00:23 +10:00
}
}
return false
}
// DrawBefore reports if the sprite should be drawn before x.
func (s *Sprite) DrawBefore(x Drawer) bool {
2021-09-16 11:18:24 +10:00
if !commonDrawerComparisons {
2021-09-15 17:41:23 +10:00
sb := s.BoundingBox()
switch x := x.(type) {
case BoundingBoxer:
xb := x.BoundingBox()
if sb.Min.Z >= xb.Max.Z { // s is in front of x
return false
}
if sb.Max.Z <= xb.Min.Z { // s is behind x
return true
}
if sb.Max.Y <= xb.Min.Y { // s is above x
return false
}
if sb.Min.Y >= xb.Max.Y { // s is below x
return true
}
case ZPositioner:
return sb.Max.Z < x.ZPos() // s is before
2021-09-11 13:00:23 +10:00
}
2021-09-10 17:18:20 +10:00
}
return false
2021-09-02 16:55:12 +10:00
}
2021-09-02 11:53:04 +10:00
// Scan returns the Actor and the Sheet.
2021-08-20 17:05:47 +10:00
func (s *Sprite) Scan() []interface{} {
return []interface{}{
&s.Actor,
2021-08-26 13:57:19 +10:00
&s.Sheet,
2021-08-20 17:05:47 +10:00
}
}
2021-08-04 15:08:26 +10:00
2021-09-02 11:53:04 +10:00
// SetAnim sets the Anim to use for the sprite. If it is not the same as the
// one currently set, it resets the new anim.
2021-08-05 15:24:16 +10:00
func (s *Sprite) SetAnim(a *Anim) {
if s.anim != a {
a.Reset()
}
s.anim = a
}
2021-08-04 17:19:17 +10:00
2021-09-17 11:13:39 +10:00
func (s *Sprite) String() string {
return fmt.Sprintf("Sprite@%v", s.Actor.Pos)
}
2021-09-07 14:00:50 +10:00
// Transform returns a translation by the DrawOffset and Actor.Pos projected
func (s *Sprite) Transform() (opts ebiten.DrawImageOptions) {
2021-09-08 20:08:57 +10:00
opts.GeoM.Translate(geom.CFloat(
2021-09-07 14:00:50 +10:00
// Reaching into Actor for a reference to Game so I don't have to
// implement Prepare in this file, but writing this long comment
// providing exposition...
s.Actor.game.Projection.Project(s.Actor.Pos).Add(s.DrawOffset),
2021-09-03 10:24:16 +10:00
))
2021-09-07 14:00:50 +10:00
return opts
2021-09-01 09:34:49 +10:00
}
2021-09-02 11:53:04 +10:00
// Update updates the Sprite's anim. anim can change a bit so we don't tell Game
// about it, but that means it must be updated manually.
2021-08-05 12:26:41 +10:00
func (s *Sprite) Update() error { return s.anim.Update() }