GOingTunneling/base.go

145 lines
3.2 KiB
Go
Raw Normal View History

2024-08-29 00:05:28 +02:00
package main
import "github.com/veandco/go-sdl2/sdl"
const (
openingWidth = 7
)
type Base struct {
rect sdl.Rect
owner *Player
}
func (base *Base) tick(players *[]*Player) {
for _, player := range *players {
if player.cullingRect.HasIntersection(&base.rect) {
if player.rechargeCooldown == 0 && player.energy < MaxEnergy {
player.energy++
if player == base.owner {
player.rechargeCooldown = RechargeCooldownOwn
} else {
player.rechargeCooldown = RechargeCooldownOpponent
}
}
if player == base.owner && player.repairCooldown == 0 && player.health < MaxHealth {
player.health++
player.repairCooldown = RepairCooldown
}
}
}
}
func (base *Base) render(camera *sdl.Rect, surface *sdl.Surface) {
borderWidth := base.rect.W - openingWidth
borderWidthA := borderWidth / 2
borderWidthB := borderWidth - borderWidthA + 1
if borderWidth < 0 {
panic("Bad border width")
}
rects := []sdl.Rect{
{
X: base.rect.X,
Y: base.rect.Y,
W: 1,
H: base.rect.H,
},
{
X: base.rect.X + base.rect.W,
Y: base.rect.Y,
W: 1,
H: base.rect.H,
},
{
X: base.rect.X,
Y: base.rect.Y,
W: borderWidthA,
H: 1,
},
{
X: base.rect.X + borderWidthA + openingWidth,
Y: base.rect.Y,
W: borderWidthB,
H: 1,
},
{
X: base.rect.X,
Y: base.rect.Y + base.rect.H,
W: borderWidthA,
H: 1,
},
{
X: base.rect.X + borderWidthA + openingWidth,
Y: base.rect.Y + base.rect.H,
W: borderWidthB,
H: 1,
},
}
for _, rect := range rects {
if camera.HasIntersection(&rect) {
cameraCompensatedRect := sdl.Rect{
X: rect.X - camera.X,
Y: rect.Y - camera.Y,
W: rect.W,
H: rect.H,
}
surface.FillRect(&cameraCompensatedRect, (*base.owner).color2)
}
}
}
func (base *Base) build(gameMap *GameMap) {
borderWidth := base.rect.W - openingWidth
borderWidthA := borderWidth / 2
if borderWidth < 0 {
panic("Bad border width")
}
if base.rect.H < 9 {
panic("Bad border height")
}
if gameMap.width-base.rect.X-base.rect.W <= 0 {
panic("Bad base x location")
}
if gameMap.height-base.rect.Y-base.rect.H <= 0 {
panic("Bad base y location")
}
if base.rect.X < 0 || base.rect.Y < 0 {
panic("Bad base negative location")
}
for x := base.rect.X; x < base.rect.X+base.rect.W+1; x++ {
for y := base.rect.Y; y < base.rect.Y+base.rect.H+1; y++ {
gameMap.tiles[x][y] = 0
}
}
for y := base.rect.Y; y < base.rect.Y+base.rect.H; y++ {
gameMap.tiles[base.rect.X][y] = 4
gameMap.tiles[base.rect.X+base.rect.W][y] = 4
}
for x := base.rect.X; x < base.rect.X+borderWidthA; x++ {
gameMap.tiles[x][base.rect.Y] = 4
gameMap.tiles[x][base.rect.Y+base.rect.H] = 4
}
for x := base.rect.X + borderWidthA + openingWidth; x < base.rect.X+base.rect.W+1; x++ {
gameMap.tiles[x][base.rect.Y] = 4
gameMap.tiles[x][base.rect.Y+base.rect.H] = 4
}
}
func createBases(players *[]*Player, gameMap *GameMap) *[]*Base {
bases := &[]*Base{}
for ownerID, player := range *players {
*bases = append(*bases, &Base{
rect: sdl.Rect{
X: player.posX - 14,
Y: player.posY - 14,
W: 35,
H: 35,
},
owner: (*players)[ownerID],
})
(*bases)[ownerID].build(gameMap)
}
return bases
}