Skip to content

Commit

Permalink
extract a function to eliminate duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
JiepengTan committed Jul 3, 2024
1 parent cd833f2 commit 283112a
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions spgdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ func (p *spriteDrawInfo) getUpdateRotateRect(x, y float64) *math32.RotatedRect {
c := p.sprite.costumes[p.sprite.costumeIndex_]

img, centerX, centerY := c.needImage(p.sprite.g.fs)
centerX += p.sprite.pivot.X * float64(c.bitmapResolution)
centerY -= p.sprite.pivot.Y * float64(c.bitmapResolution)
centerX, centerY = p.sprite.applyImagePivot(c, centerX, centerY)
rect := image.Rectangle{}
rect.Min.X = 0
rect.Min.Y = 0
Expand All @@ -136,8 +135,7 @@ func (p *spriteDrawInfo) updateMatrix() {
c := p.sprite.costumes[p.sprite.costumeIndex_]

img, centerX, centerY := c.needImage(p.sprite.g.fs)
centerX += p.sprite.pivot.X * float64(c.bitmapResolution)
centerY -= p.sprite.pivot.Y * float64(c.bitmapResolution)
centerX, centerY = p.sprite.applyImagePivot(c, centerX, centerY)
rect := image.Rectangle{}
rect.Min.X = 0
rect.Min.Y = 0
Expand Down Expand Up @@ -221,8 +219,7 @@ func (p *Sprite) touchPoint(x, y float64) bool {
}
c := p.costumes[p.costumeIndex_]
img, cx, cy := c.needImage(p.g.fs)
cx += p.pivot.X * float64(c.bitmapResolution)
cy -= p.pivot.Y * float64(c.bitmapResolution)
cx, cy = p.applyImagePivot(c, cx, cy)
geo := p.getDrawInfo().getPixelGeo(cx, cy)

pixel, _ := p.getDrawInfo().getPixel(pos, img, geo)
Expand Down Expand Up @@ -255,8 +252,7 @@ func (p *Sprite) touchRotatedRect(dstRect *math32.RotatedRect) bool {
}
c := p.costumes[p.costumeIndex_]
img, cx, cy := c.needImage(p.g.fs)
cx += p.pivot.X * float64(c.bitmapResolution)
cy -= p.pivot.Y * float64(c.bitmapResolution)
cx, cy = p.applyImagePivot(c, cx, cy)
geo := p.getDrawInfo().getPixelGeo(cx, cy)

//check boun rect pixel
Expand Down Expand Up @@ -293,14 +289,12 @@ func (p *Sprite) touchedColor_(dst *Sprite, color Color) bool {

c := p.costumes[p.costumeIndex_]
pimg, cx, cy := c.needImage(p.g.fs)
cx += p.pivot.X * float64(c.bitmapResolution)
cy -= p.pivot.Y * float64(c.bitmapResolution)
cx, cy = p.applyImagePivot(c, cx, cy)
geo := p.getDrawInfo().getPixelGeo(cx, cy)

c2 := dst.costumes[dst.costumeIndex_]
dstimg, cx2, cy2 := c2.needImage(p.g.fs)
cx2 += dst.pivot.X * float64(c2.bitmapResolution)
cy2 -= dst.pivot.Y * float64(c2.bitmapResolution)
cx2, cy2 = dst.applyImagePivot(c2, cx2, cy2)
geo2 := dst.getDrawInfo().getPixelGeo(cx2, cy2)

cr, cg, cb, ca := color.RGBA()
Expand Down Expand Up @@ -345,14 +339,12 @@ func (p *Sprite) touchingSprite(dst *Sprite) bool {

c := p.costumes[p.costumeIndex_]
pimg, cx, cy := c.needImage(p.g.fs)
cx += p.pivot.X * float64(c.bitmapResolution)
cy -= p.pivot.Y * float64(c.bitmapResolution)
cx, cy = p.applyImagePivot(c, cx, cy)
geo := p.getDrawInfo().getPixelGeo(cx, cy)

c2 := dst.costumes[dst.costumeIndex_]
dstimg, cx2, cy2 := c2.needImage(p.g.fs)
cx2 += dst.pivot.X * float64(c2.bitmapResolution)
cy2 -= dst.pivot.Y * float64(c2.bitmapResolution)
cx2, cy2 = dst.applyImagePivot(c2, cx2, cy2)
geo2 := dst.getDrawInfo().getPixelGeo(cx2, cy2)
//check boun rect pixel
for x := boundRect.X; x < boundRect.Width+boundRect.X; x++ {
Expand Down Expand Up @@ -425,10 +417,9 @@ func (p *Sprite) hit(hc hitContext) (hr hitResult, ok bool) {
if !rRect.Contains(pos) {
return
}
c2 := p.costumes[p.costumeIndex_]
img, cx, cy := c2.needImage(p.g.fs)
cx += p.pivot.X * float64(c2.bitmapResolution)
cy -= p.pivot.Y * float64(c2.bitmapResolution)
c := p.costumes[p.costumeIndex_]
img, cx, cy := c.needImage(p.g.fs)
cx, cy = p.applyImagePivot(c, cx, cy)
geo := p.getDrawInfo().getPixelGeo(cx, cy)
color1, pos := p.getDrawInfo().getPixel(pos, img, geo)
if debugInstr {
Expand All @@ -440,3 +431,9 @@ func (p *Sprite) hit(hc hitContext) (hr hitResult, ok bool) {
}
return hitResult{Target: p}, true
}

func (p *Sprite) applyImagePivot(c *costume, cx, cy float64) (float64, float64) {
cx += p.pivot.X * float64(c.bitmapResolution)
cy -= p.pivot.Y * float64(c.bitmapResolution)
return cx, cy
}

0 comments on commit 283112a

Please sign in to comment.