Collision detection system

This commit is contained in:
2023-11-30 21:01:49 +01:00
parent c5b8fc40cb
commit b6776fa9f3
5 changed files with 216 additions and 22 deletions

View File

@@ -3,6 +3,7 @@ package zombie
import (
"bytes"
"github.com/hajimehoshi/ebiten/v2"
"gitlab.com/kbr4/9heroja/collision"
"gitlab.com/kbr4/9heroja/configuration"
"gitlab.com/kbr4/9heroja/resources"
"image"
@@ -25,6 +26,7 @@ type Zombie struct {
Y float64
OffsetX float64
OffsetY float64
IsDead bool
}
type spritePosition struct {
@@ -57,7 +59,10 @@ func (z *Zombie) DrawZombie(screen *ebiten.Image) {
op.GeoM.Reset()
op.GeoM.Translate(z.X+z.OffsetX, z.Y+z.OffsetY)
op.GeoM.Scale(1, 1)
// Apply red tint if the zombie is dead
if z.IsDead {
op.ColorScale.Scale(1, 0, 0, 1) // Scale the red channel up, green and blue down.
}
screen.DrawImage(zombieImage.SubImage(image.Rect(p.x+1, p.y, p.x+resources.ZombieTileSize, p.y+resources.HeroTileSize)).(*ebiten.Image), op)
}
@@ -73,23 +78,26 @@ func init() {
}
func NewZombie() *Zombie {
hero := &Zombie{
zombie := &Zombie{
step: 0,
IsWalking: false,
IsDead: false,
}
go func() {
for {
select {
case <-walkTicker.C:
hero.step++
if hero.step > 3 {
hero.step = 0
if zombie.IsWalking {
zombie.step++
if zombie.step > 3 {
zombie.step = 0
}
}
}
}
}()
return hero
return zombie
}
func (z *Zombie) Walk() {
@@ -109,6 +117,36 @@ func (z *Zombie) ChangeDirection(d configuration.Direction) {
func (z *Zombie) Stop() {
z.step = 2
walkTicker.Stop()
z.IsWalking = false
}
func (z *Zombie) CollisionShape() collision.Polygon {
if !z.IsDead {
return collision.Polygon{
Points: []collision.Point{
{X: z.X + z.OffsetX, Y: z.Y + z.OffsetY},
{X: z.X + z.OffsetX + 32, Y: z.Y + z.OffsetY},
{X: z.X + z.OffsetX + 32, Y: z.Y + z.OffsetY + 32},
{X: z.X + z.OffsetX, Y: z.Y + z.OffsetY + 32},
},
}
} else {
return collision.Polygon{
Points: []collision.Point{},
}
}
}
func (z *Zombie) CollisionObjectType() collision.ObjectType {
return collision.Zombie
}
func (z *Zombie) HandleCollisionEvent(other collision.Collidable) {
coll := other.CollisionObjectType()
switch coll {
case collision.Hero:
z.Stop()
z.IsDead = true
}
}