Files

158 lines
3.8 KiB
Go
Raw Permalink Normal View History

2023-11-17 22:02:40 +01:00
package main
import (
2023-12-28 14:42:20 +01:00
"fmt"
2023-11-17 22:02:40 +01:00
"github.com/hajimehoshi/ebiten/v2"
2023-12-28 14:42:20 +01:00
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
2023-11-28 07:29:17 +01:00
"github.com/hajimehoshi/ebiten/v2/inpututil"
2023-11-30 21:01:49 +01:00
"gitlab.com/kbr4/9heroja/collision"
2023-11-21 20:12:56 +01:00
"gitlab.com/kbr4/9heroja/configuration"
2023-11-19 08:06:40 +01:00
"gitlab.com/kbr4/9heroja/hero"
2023-11-28 07:29:17 +01:00
"gitlab.com/kbr4/9heroja/input"
2023-11-19 08:06:40 +01:00
"gitlab.com/kbr4/9heroja/terrain"
2023-12-28 14:42:20 +01:00
"gitlab.com/kbr4/9heroja/weapons"
2023-11-30 14:49:01 +01:00
"gitlab.com/kbr4/9heroja/zombie"
2023-11-17 22:02:40 +01:00
_ "image/png"
"log"
)
const (
screenWidth = 640
screenHeight = 480
tileSize = 33
titleFontSize = fontSize * 1.5
fontSize = 24
smallFontSize = fontSize / 2
pipeWidth = tileSize * 2
pipeStartOffsetX = 8
pipeIntervalX = 8
pipeGapY = 5
)
func floorDiv(x, y int) int {
d := x / y
if d*y == x || x >= 0 {
return d
}
return d - 1
}
func floorMod(x, y int) int {
return x - floorDiv(x, y)*y
}
type Game struct {
// The gopher's position
x16 int
y16 int
vy16 int
2023-11-28 07:29:17 +01:00
keys []ebiten.Key
control *input.Keyboard
2023-11-19 08:06:40 +01:00
hero *hero.Hero
terrain *terrain.Terrain
2023-11-30 14:49:01 +01:00
zombies []*zombie.Zombie
2023-11-30 21:01:49 +01:00
world *collision.World
2023-12-28 14:42:20 +01:00
bullets []*weapons.Handgun
2023-11-17 22:02:40 +01:00
}
func (g *Game) Update() error {
2023-11-28 07:29:17 +01:00
g.keys = inpututil.AppendPressedKeys(g.keys[:0])
GameInstance.hero.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
GameInstance.terrain.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
2024-01-04 05:57:21 +01:00
bullet := GameInstance.hero.Fire(g.terrain.PositionX, g.terrain.PositionY)
2023-12-28 14:42:20 +01:00
if bullet != nil {
GameInstance.world.AddEntity(bullet)
g.bullets = append(g.bullets, bullet)
}
2023-11-28 07:29:17 +01:00
if len(g.keys) <= 0 {
g.hero.Stop()
} else {
2024-01-08 06:19:39 +01:00
g.hero.Move()
2023-11-28 07:29:17 +01:00
g.hero.Walk()
}
2023-12-28 14:42:20 +01:00
for _, b := range g.bullets {
if b.IsFlying {
b.Move()
}
}
2024-01-08 06:19:39 +01:00
for _, z := range g.zombies {
z.TargetX = g.hero.X
z.TargetY = g.hero.Y
}
2023-12-28 14:42:20 +01:00
2023-11-30 21:01:49 +01:00
g.world.NotifyAboutCollisions()
2023-11-17 22:02:40 +01:00
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
2023-11-19 08:06:40 +01:00
g.terrain.DrawTerrain(screen)
2023-11-30 14:49:01 +01:00
for _, z := range g.zombies {
z.DrawZombie(screen)
}
2023-12-28 14:42:20 +01:00
for _, b := range g.bullets {
b.OffsetX = g.terrain.PositionX
b.OffsetY = g.terrain.PositionY
if b.IsFlying {
b.DrawHandgunBullet(screen)
}
}
2023-11-30 21:01:49 +01:00
g.hero.DrawHero(screen)
2023-12-28 14:42:20 +01:00
msg := fmt.Sprintf(`TPS: %0.2f
FPS: %0.2f`, ebiten.ActualTPS(), ebiten.ActualFPS())
ebitenutil.DebugPrint(screen, msg)
2023-11-17 22:02:40 +01:00
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
s := ebiten.DeviceScaleFactor()
return int(float64(outsideWidth) * s), int(float64(outsideHeight) * s)
}
2023-11-19 08:06:40 +01:00
var GameInstance *Game
2023-11-17 22:02:40 +01:00
func init() {
2023-11-19 08:06:40 +01:00
GameInstance = &Game{}
2023-11-30 21:01:49 +01:00
GameInstance.world = collision.NewWorld()
2023-11-28 07:29:17 +01:00
GameInstance.control = &input.Keyboard{}
2023-11-19 14:19:34 +01:00
GameInstance.hero = hero.NewHero()
2023-11-30 14:49:01 +01:00
GameInstance.zombies = []*zombie.Zombie{zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie()}
2023-11-30 21:01:49 +01:00
GameInstance.world.AddEntity(GameInstance.hero)
2023-11-30 14:49:01 +01:00
// put zombies in random places on the screen but not too close to the hero or each other
for _, z := range GameInstance.zombies {
2024-01-08 06:19:39 +01:00
z.TargetX = GameInstance.hero.X
z.TargetY = GameInstance.hero.Y
2023-11-30 14:49:01 +01:00
z.X = float64(configuration.Random(50, screenWidth-50))
z.Y = float64(configuration.Random(50, screenHeight-50))
for z.X > float64(screenWidth/2-64) && z.X < float64(screenWidth/2+64) && z.Y > float64(screenHeight/2-64) && z.Y < float64(screenHeight/2+64) {
z.X = float64(configuration.Random(0, screenWidth))
z.Y = float64(configuration.Random(0, screenHeight))
}
z.Walk()
2023-11-30 21:01:49 +01:00
GameInstance.world.AddEntity(z)
2023-11-30 14:49:01 +01:00
}
2023-11-19 14:19:34 +01:00
GameInstance.terrain = terrain.NewTerrain()
2023-11-30 14:49:01 +01:00
2023-11-21 20:12:56 +01:00
GameInstance.hero.ChangeDirection(configuration.North)
GameInstance.terrain.ChangeDirection(configuration.North)
2023-11-19 14:19:34 +01:00
GameInstance.hero.Walk()
2023-11-17 22:02:40 +01:00
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Bosanski Zombicid")
2023-11-19 08:06:40 +01:00
if err := ebiten.RunGame(GameInstance); err != nil {
2023-11-17 22:02:40 +01:00
log.Fatal(err)
}
}