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 main
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"gitlab.com/kbr4/9heroja/collision"
"gitlab.com/kbr4/9heroja/configuration"
"gitlab.com/kbr4/9heroja/hero"
"gitlab.com/kbr4/9heroja/input"
@@ -51,6 +52,7 @@ type Game struct {
hero *hero.Hero
terrain *terrain.Terrain
zombies []*zombie.Zombie
world *collision.World
}
func (g *Game) Update() error {
@@ -63,17 +65,18 @@ func (g *Game) Update() error {
g.terrain.Move()
g.hero.Walk()
}
g.world.NotifyAboutCollisions()
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
g.terrain.DrawTerrain(screen)
g.hero.DrawHero(screen)
for _, z := range g.zombies {
z.OffsetX = g.terrain.PositionX
z.OffsetY = g.terrain.PositionY
z.DrawZombie(screen)
}
g.hero.DrawHero(screen)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
@@ -86,10 +89,12 @@ var GameInstance *Game
func init() {
GameInstance = &Game{}
GameInstance.world = collision.NewWorld()
GameInstance.control = &input.Keyboard{}
GameInstance.hero = hero.NewHero()
GameInstance.zombies = []*zombie.Zombie{zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie()}
GameInstance.world.AddEntity(GameInstance.hero)
// put zombies in random places on the screen but not too close to the hero or each other
for _, z := range GameInstance.zombies {
z.X = float64(configuration.Random(50, screenWidth-50))
@@ -99,6 +104,7 @@ func init() {
z.Y = float64(configuration.Random(0, screenHeight))
}
z.Walk()
GameInstance.world.AddEntity(z)
}
GameInstance.terrain = terrain.NewTerrain()