GOingTunneling/main.go
2024-08-29 00:05:28 +02:00

159 lines
4.0 KiB
Go

package main
import (
"github.com/veandco/go-sdl2/sdl"
)
const (
MapWidth = 1000 // Width of the map
MapHeight = 1000 // Height of the map
MaxEnergy = 1760
MaxHealth = 88
NormalShotCost = 3
SuperShotCost = 20
MovementCost = 1
DiggingCost = 2
ShootDiggingCostBonus = 1
ShootCooldown = 8
RechargeCooldownOwn = 0
DiggingCooldown = 4
RechargeCooldownOpponent = 6
RepairCooldown = 4
MovementCooldown = 2
MovementCooldownNoEnergy = 4
DiggingCooldownNoEnergy = 8
)
func main() {
initializeSDL()
defer sdl.Quit()
window, logicalSurface := setupWindowAndSurface()
window.SetTitle("GOing to tunnel")
defer window.Destroy()
defer logicalSurface.Free()
playSurface, playSurfaceRect, playSurfaceTargetRect := setupPlaySurface()
defer playSurface.Free()
HUDSurface, HUDSurfaceRect, HUDSurfaceTargetRect := setupHUDSurface()
defer HUDSurface.Free()
pixelBG := sdl.MapRGBA(logicalSurface.Format, 80, 20, 10, 255)
gameMap := GameMap{}
gameMap.createGameMap(MapWidth, MapHeight)
players := &[]*Player{}
createPlayer(
sdl.MapRGBA(playSurface.Format, 0, 0, 182, 255),
sdl.MapRGBA(playSurface.Format, 44, 44, 255, 255),
sdl.MapRGBA(playSurface.Format, 243, 235, 28, 255),
&gameMap,
players)
createPlayer(
sdl.MapRGBA(playSurface.Format, 44, 184, 44, 255),
sdl.MapRGBA(playSurface.Format, 0, 249, 0, 255),
sdl.MapRGBA(playSurface.Format, 243, 235, 28, 255),
&gameMap,
players)
bases := createBases(players, &gameMap)
bulletMap := &[]*Bullet{}
bulletParticleMap := &[]*BulletParticle{}
camera := sdl.Rect{X: 0, Y: 0, W: 76, H: 76}
running := true
for running {
currentTime := sdl.GetTicks64()
running = handleEvents(window, logicalSurface)
keyboard := sdl.GetKeyboardState()
running = running && handleInput(keyboard, bulletMap, (*players)[0], &gameMap, playSurface.Format, players)
renderScene(logicalSurface, playSurface, HUDSurface, &gameMap, players, bases, bulletMap, bulletParticleMap, &camera, playSurfaceRect, playSurfaceTargetRect, HUDSurfaceRect, HUDSurfaceTargetRect)
adjustWindow(window, logicalSurface, pixelBG)
// Calculate delay to achieve roughly 60 FPS
frameDuration := 1000 / 60
elapsed := sdl.GetTicks64() - currentTime
if delay := frameDuration - int(elapsed); delay > 0 {
sdl.Delay(uint32(delay))
}
}
}
func handleInput(keyboard []uint8, bullets *[]*Bullet, player *Player, gameMap *GameMap, format *sdl.PixelFormat, players *[]*Player) bool {
shoot := false
super := false
// Flags to track movement in each direction
moveUp := false
moveDown := false
moveLeft := false
moveRight := false
// Process keyboard input
for key, value := range keyboard {
if value == 0 {
continue
}
if key == sdl.SCANCODE_ESCAPE {
return false
} else if key == sdl.SCANCODE_SPACE {
shoot = true
} else if key == sdl.SCANCODE_LCTRL {
super = true
} else if key == sdl.SCANCODE_W {
moveUp = true
} else if key == sdl.SCANCODE_S {
moveDown = true
} else if key == sdl.SCANCODE_A {
moveLeft = true
} else if key == sdl.SCANCODE_D {
moveRight = true
}
}
// Handle shooting after the loop
if shoot {
player.shoot(super, bullets, format)
}
// Determine player orientation for diagonal movement
if moveUp && moveRight {
player.orientation = 4 // Up-Right
} else if moveUp && moveLeft {
player.orientation = 5 // Up-Left
} else if moveDown && moveRight {
player.orientation = 6 // Down-Right
} else if moveDown && moveLeft {
player.orientation = 7 // Down-Left
} else if moveUp {
player.orientation = 0 // Up
} else if moveRight {
player.orientation = 1 // Right
} else if moveDown {
player.orientation = 2 // Down
} else if moveLeft {
player.orientation = 3 // Left
}
// Handle movement after the loop
if moveUp || moveDown || moveLeft || moveRight {
if player.tryMove(gameMap, shoot, players) {
player.energy -= MovementCost
}
}
return true
}