Skip to content

Commit

Permalink
Buzzards collide with each other
Browse files Browse the repository at this point in the history
  • Loading branch information
depsypher committed Jun 26, 2024
1 parent 9cab182 commit c9e9af4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
48 changes: 26 additions & 22 deletions entity/buzzard.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,32 @@ func (b *Buzzard) mounted(gs *GameState) {
b.FacingRight = true
}
})

b.Collisions(gs.BuzzardsAsSprites(), func(c *Sprite) {
b.bounce(gs, c)
})
}

func (b *Buzzard) unmounted(gs *GameState) {
if b.X < app.ScreenWidth/2 {
b.FacingRight = false
b.xSpeed = -3
} else {
b.FacingRight = true
b.xSpeed = 3
}
b.doFlap()
b.image = b.buildMount()
b.velocity()
if b.X < -float64(b.Width) || b.X > app.ScreenWidth+float64(b.Width/2) {
for i, buzz := range gs.Buzzards {
if buzz == b {
b.state = DEAD
gs.Buzzards = remove(gs.Buzzards, i)
break
}
}
}
}

func (b *Buzzard) bounce(gs *GameState, collider *Sprite) bool {
Expand All @@ -136,28 +162,6 @@ func (b *Buzzard) bounce(gs *GameState, collider *Sprite) bool {
return above
}

func (b *Buzzard) unmounted(gs *GameState) {
if b.X < app.ScreenWidth/2 {
b.FacingRight = false
b.xSpeed = -3
} else {
b.FacingRight = true
b.xSpeed = 3
}
b.doFlap()
b.image = b.buildMount()
b.velocity()
if b.X < -float64(b.Width) || b.X > app.ScreenWidth+float64(b.Width/2) {
for i, buzz := range gs.Buzzards {
if buzz == b {
b.state = DEAD
gs.Buzzards = remove(gs.Buzzards, i)
break
}
}
}
}

func remove(s []*Buzzard, i int) []*Buzzard {
s[i] = s[len(s)-1]
return s[:len(s)-1]
Expand Down
5 changes: 4 additions & 1 deletion entity/sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ func (s *Sprite) Collides(c *Sprite) bool {

for y := hitBoxMinY; y < hitBoxMaxY; y++ {
for x := hitBoxMinX; x < hitBoxMaxX; x++ {
if s.image == nil || c.image == nil {
return true
}
alpha1 := s.image.RGBA64At(x-s.rect().Min.X, y-s.rect().Min.Y).A
if alpha1 != 0 {
alpha2 := c.image.RGBA64At(x-c.rect().Min.X, y-c.rect().Min.Y).A
Expand All @@ -243,7 +246,7 @@ type doOnCollide func(c *Sprite)
func (s *Sprite) Collisions(group []*Sprite, onCollide doOnCollide) []*Sprite {
var result []*Sprite
for _, c := range group {
if s.Collides(c) {
if s != c && s.Collides(c) {
onCollide(c)
result = append(result, c)
}
Expand Down
8 changes: 8 additions & 0 deletions entity/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ func (gs *GameState) CliffAsSprites() []*Sprite {
}
return r
}

func (gs *GameState) BuzzardsAsSprites() []*Sprite {
r := make([]*Sprite, len(gs.Buzzards))
for i := range gs.Buzzards {
r[i] = gs.Buzzards[i].MountSprite.Sprite
}
return r
}

0 comments on commit c9e9af4

Please sign in to comment.