...

Source file src/gitlab.com/tslocum/venture/pkg/world/sprite.go

Documentation: gitlab.com/tslocum/venture/pkg/world

     1  package world
     2  
     3  import (
     4  	"image/color"
     5  	"log"
     6  
     7  	"github.com/hajimehoshi/ebiten"
     8  )
     9  
    10  // Sprite is a graphical node.
    11  type Sprite struct {
    12  	*Logic
    13  	Image  *ebiten.Image
    14  	Images []*ebiten.Image
    15  
    16  	Visible bool
    17  
    18  	w, h                 float64
    19  	x, y                 float64
    20  	scaleX, scaleY       float64
    21  	originalW, originalH float64
    22  	colorm               *ebiten.ColorM
    23  	opts                 *ebiten.DrawImageOptions
    24  	err                  error
    25  }
    26  
    27  // NewSprite returns a new Sprite node.
    28  func NewSprite(image *ebiten.Image, updateFunc func(screen *ebiten.Image) error) *Sprite {
    29  	w, h := image.Size()
    30  
    31  	s := &Sprite{Logic: NewLogic(updateFunc), Image: image, Images: []*ebiten.Image{image}, Visible: true, originalW: float64(w), originalH: float64(h), scaleX: 1, scaleY: 1, w: float64(w), h: float64(h), colorm: &ebiten.ColorM{}, opts: &ebiten.DrawImageOptions{}}
    32  	return s
    33  }
    34  
    35  // SetPosition sets the position of the node.
    36  func (s *Sprite) SetScale(x float64, y float64) {
    37  	s.Lock()
    38  	defer s.Unlock()
    39  
    40  	s.scaleX = x
    41  	s.scaleY = y
    42  
    43  	s.w = s.originalW * s.scaleX
    44  	s.h = s.originalH * s.scaleY
    45  
    46  	s.updateDrawOptions()
    47  }
    48  
    49  // SetPosition sets the position of the node.
    50  func (s *Sprite) GetScale() (float64, float64) {
    51  	s.Lock()
    52  	defer s.Unlock()
    53  
    54  	return s.scaleX, s.scaleY
    55  }
    56  
    57  // SetPosition sets the position of the node.
    58  func (s *Sprite) SetImage(image *ebiten.Image) {
    59  	s.Lock()
    60  	defer s.Unlock()
    61  
    62  	s.Image = image
    63  }
    64  
    65  // SetPosition sets the position of the node.
    66  func (s *Sprite) SetPosition(x float64, y float64) {
    67  	s.Lock()
    68  	defer s.Unlock()
    69  
    70  	s.x = x
    71  	s.y = y
    72  
    73  	s.updateDrawOptions()
    74  }
    75  
    76  // AddPosition adds to the position of the node.
    77  func (s *Sprite) AddPosition(x float64, y float64) {
    78  	s.Lock()
    79  	defer s.Unlock()
    80  
    81  	s.x += x
    82  	s.y += y
    83  
    84  	if s.opts != nil {
    85  		s.opts.GeoM.Translate(x, y)
    86  		return
    87  	}
    88  
    89  	s.updateDrawOptions()
    90  }
    91  
    92  // GetPosition returns the position of the node.
    93  func (s *Sprite) GetPosition() (x float64, y float64) {
    94  	s.Lock()
    95  	defer s.Unlock()
    96  
    97  	return s.x, s.y
    98  }
    99  
   100  // HitTest returns whether the the specified position is within the sprite bounds.
   101  func (s *Sprite) HitTest(x float64, y float64) bool {
   102  	s.Lock()
   103  	defer s.Unlock()
   104  
   105  	return x >= s.x && y >= s.y && x <= s.x+s.w && y <= s.y+s.h
   106  }
   107  
   108  // HitExact returns whether the the specified position is within a
   109  // non-transparent area of the sprite.
   110  func (s *Sprite) HitExact(x float64, y float64) bool {
   111  	s.Lock()
   112  	defer s.Unlock()
   113  
   114  	return s.Image.At(int(x-s.x), int(y-s.y)).(color.RGBA).A > 0
   115  }
   116  
   117  func (s *Sprite) GetSize() (float64, float64) {
   118  	s.Lock()
   119  	defer s.Unlock()
   120  
   121  	return s.w, s.h
   122  }
   123  
   124  func (s *Sprite) SetColorM(colorm *ebiten.ColorM) {
   125  	s.Lock()
   126  	defer s.Unlock()
   127  
   128  	s.colorm = colorm
   129  	s.updateDrawOptions()
   130  }
   131  
   132  func (s *Sprite) GetColorM() *ebiten.ColorM {
   133  	s.Lock()
   134  	defer s.Unlock()
   135  
   136  	return s.colorm
   137  }
   138  
   139  func (s *Sprite) updateDrawOptions() {
   140  	s.opts = &ebiten.DrawImageOptions{}
   141  	s.opts.GeoM.Scale(s.scaleX, s.scaleY)
   142  	s.opts.GeoM.Translate(s.x, s.y)
   143  	s.opts.ColorM = *s.colorm
   144  }
   145  
   146  // Draw draws the node.
   147  func (s *Sprite) Draw(screen *ebiten.Image) {
   148  	s.Lock()
   149  	defer s.Unlock()
   150  
   151  	if !s.Visible {
   152  		return
   153  	}
   154  
   155  	s.err = screen.DrawImage(s.Image, s.opts)
   156  	if s.err != nil {
   157  		log.Fatal(s.err)
   158  	}
   159  }
   160  

View as plain text