GOingTunneling/base.go

113 lines
3.7 KiB
Go
Raw Normal View History

2024-08-29 00:05:28 +02:00
package main
import "github.com/veandco/go-sdl2/sdl"
type Base struct {
2024-08-29 18:48:27 +02:00
owner *Player
openingWidth int32
gameObject *GameObject
2024-08-29 00:05:28 +02:00
}
func (base *Base) tick(players *[]*Player) {
for _, player := range *players {
2024-08-29 18:48:27 +02:00
if player.gameObject.baseRect.HasIntersection(&base.gameObject.baseRect) {
2024-08-29 00:05:28 +02:00
if player.rechargeCooldown == 0 && player.energy < MaxEnergy {
2024-08-29 18:48:27 +02:00
if MaxEnergy-player.energy < 4 {
player.energy++
} else {
player.energy += 4
}
2024-08-29 00:05:28 +02:00
if player == base.owner {
player.rechargeCooldown = RechargeCooldownOwn
} else {
player.rechargeCooldown = RechargeCooldownOpponent
}
}
2024-08-29 18:48:27 +02:00
if player == base.owner && player.repairCooldown == 0 && player.shields < MaxShields {
player.shields++
2024-08-29 00:05:28 +02:00
player.repairCooldown = RepairCooldown
}
}
}
}
func (base *Base) render(camera *sdl.Rect, surface *sdl.Surface) {
2024-08-29 18:48:27 +02:00
base.gameObject.render(camera, surface)
2024-08-29 00:05:28 +02:00
}
func (base *Base) build(gameMap *GameMap) {
2024-08-29 18:48:27 +02:00
borderWidth := base.gameObject.baseRect.W - base.openingWidth
2024-08-29 00:05:28 +02:00
borderWidthA := borderWidth / 2
if borderWidth < 0 {
panic("Bad border width")
}
2024-08-29 18:48:27 +02:00
if base.gameObject.baseRect.H < 9 {
2024-08-29 00:05:28 +02:00
panic("Bad border height")
}
2024-08-29 18:48:27 +02:00
if gameMap.width-base.gameObject.baseRect.X-base.gameObject.baseRect.W <= 0 {
2024-08-29 00:05:28 +02:00
panic("Bad base x location")
}
2024-08-29 18:48:27 +02:00
if gameMap.height-base.gameObject.baseRect.Y-base.gameObject.baseRect.H <= 0 {
2024-08-29 00:05:28 +02:00
panic("Bad base y location")
}
2024-08-29 18:48:27 +02:00
if base.gameObject.baseRect.X < 0 || base.gameObject.baseRect.Y < 0 {
2024-08-29 00:05:28 +02:00
panic("Bad base negative location")
}
2024-08-29 18:48:27 +02:00
for x := base.gameObject.baseRect.X; x < base.gameObject.baseRect.X+base.gameObject.baseRect.W+1; x++ {
for y := base.gameObject.baseRect.Y; y < base.gameObject.baseRect.Y+base.gameObject.baseRect.H+1; y++ {
2024-08-29 00:05:28 +02:00
gameMap.tiles[x][y] = 0
}
}
2024-08-29 18:48:27 +02:00
for y := base.gameObject.baseRect.Y; y < base.gameObject.baseRect.Y+base.gameObject.baseRect.H; y++ {
gameMap.tiles[base.gameObject.baseRect.X][y] = 4
gameMap.tiles[base.gameObject.baseRect.X+base.gameObject.baseRect.W][y] = 4
2024-08-29 00:05:28 +02:00
}
2024-08-29 18:48:27 +02:00
for x := base.gameObject.baseRect.X; x < base.gameObject.baseRect.X+borderWidthA; x++ {
gameMap.tiles[x][base.gameObject.baseRect.Y] = 4
gameMap.tiles[x][base.gameObject.baseRect.Y+base.gameObject.baseRect.H] = 4
2024-08-29 00:05:28 +02:00
}
2024-08-29 18:48:27 +02:00
for x := base.gameObject.baseRect.X + borderWidthA + base.openingWidth; x < base.gameObject.baseRect.X+base.gameObject.baseRect.W+1; x++ {
gameMap.tiles[x][base.gameObject.baseRect.Y] = 4
gameMap.tiles[x][base.gameObject.baseRect.Y+base.gameObject.baseRect.H] = 4
2024-08-29 00:05:28 +02:00
}
}
func createBases(players *[]*Player, gameMap *GameMap) *[]*Base {
bases := &[]*Base{}
2024-08-29 18:48:27 +02:00
2024-08-29 00:05:28 +02:00
for ownerID, player := range *players {
2024-08-29 18:48:27 +02:00
gameObject := &GameObject{}
gameObject.baseRect = sdl.Rect{
X: player.gameObject.baseRect.X - 14,
Y: player.gameObject.baseRect.Y - 14,
W: 35,
H: 35,
}
openingWidth := int32(float64(player.gameObject.baseRect.W) * 1.5)
if openingWidth%2 == 0 {
openingWidth++
}
borderWidth := gameObject.baseRect.W - openingWidth
borderWidthA := borderWidth / 2
borderWidthB := borderWidth - borderWidthA + 1
if borderWidth < 0 {
panic("Bad border width")
}
gameObject.addColor((*player).playerColors.body)
gameObject.addColoredRect(0, 0, 1, gameObject.baseRect.H, 0)
gameObject.addColoredRect(gameObject.baseRect.W, 0, 1, gameObject.baseRect.H, 0)
gameObject.addColoredRect(0, 0, borderWidthA, 1, 0)
gameObject.addColoredRect(borderWidthA+openingWidth, 0, borderWidthB, 1, 0)
gameObject.addColoredRect(0, gameObject.baseRect.H, borderWidthA, 1, 0)
gameObject.addColoredRect(borderWidthA+openingWidth, gameObject.baseRect.H, borderWidthB, 1, 0)
2024-08-29 00:05:28 +02:00
*bases = append(*bases, &Base{
2024-08-29 18:48:27 +02:00
gameObject: gameObject,
owner: (*players)[ownerID],
openingWidth: openingWidth,
2024-08-29 00:05:28 +02:00
})
(*bases)[ownerID].build(gameMap)
}
return bases
}