Fix a few network issues
This commit is contained in:
@@ -3,8 +3,12 @@ package main
|
||||
import (
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"image/color"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var baseMutex sync.RWMutex
|
||||
|
||||
type Base struct {
|
||||
ownerID uint32
|
||||
openingWidth int32
|
||||
@@ -12,8 +16,9 @@ type Base struct {
|
||||
}
|
||||
|
||||
func (base *Base) tick(players map[uint32]*Player) {
|
||||
playersMutex.RLock()
|
||||
for playerID, player := range players {
|
||||
if player.gameObject.baseRect.HasIntersection(&base.gameObject.baseRect) {
|
||||
if player.gameObject.baseRect.HasIntersection(base.gameObject.baseRect) {
|
||||
if player.rechargeCooldown == 0 && player.energy < serverConfig.MaxEnergy {
|
||||
if serverConfig.MaxEnergy-player.energy < 4 {
|
||||
player.energy++
|
||||
@@ -32,6 +37,7 @@ func (base *Base) tick(players map[uint32]*Player) {
|
||||
}
|
||||
}
|
||||
}
|
||||
playersMutex.RUnlock()
|
||||
}
|
||||
|
||||
func (base *Base) render(camera *sdl.Rect, surface *sdl.Surface, bases map[uint32]*Base) {
|
||||
@@ -42,14 +48,6 @@ func (base *Base) render(camera *sdl.Rect, surface *sdl.Surface, bases map[uint3
|
||||
}
|
||||
|
||||
func (base *Base) build(gameMap *GameMap) {
|
||||
borderWidth := base.gameObject.baseRect.W - base.openingWidth
|
||||
borderWidthA := borderWidth / 2
|
||||
if borderWidth < 0 {
|
||||
panic("Bad border width")
|
||||
}
|
||||
if base.gameObject.baseRect.H < 9 {
|
||||
panic("Bad border height")
|
||||
}
|
||||
if gameMap.width-uint32(base.gameObject.baseRect.X)-uint32(base.gameObject.baseRect.W) <= 0 {
|
||||
panic("Bad base x location")
|
||||
}
|
||||
@@ -57,30 +55,32 @@ func (base *Base) build(gameMap *GameMap) {
|
||||
panic("Bad base y location")
|
||||
}
|
||||
if base.gameObject.baseRect.X < 0 || base.gameObject.baseRect.Y < 0 {
|
||||
panic("Bad base negative location")
|
||||
panic("Bad base negative location " + strconv.Itoa(int(base.gameObject.baseRect.X)) + " - " + strconv.Itoa(int(base.gameObject.baseRect.Y)))
|
||||
}
|
||||
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++ {
|
||||
base.gameObject.adjustBaseRect()
|
||||
for x := base.gameObject.baseRect.X; x < base.gameObject.baseRect.X+base.gameObject.baseRect.W; x++ {
|
||||
for y := base.gameObject.baseRect.Y; y < base.gameObject.baseRect.Y+base.gameObject.baseRect.H; y++ {
|
||||
gameMap.tiles[x][y] = 0
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
for _, rectT := range base.gameObject.getCurrentRects() {
|
||||
rect := sdl.Rect{
|
||||
X: rectT.rect.X + base.gameObject.baseRect.X,
|
||||
Y: rectT.rect.Y + base.gameObject.baseRect.Y,
|
||||
W: rectT.rect.W,
|
||||
H: rectT.rect.H,
|
||||
}
|
||||
for x := rect.X; x < rect.X+rect.W; x++ {
|
||||
for y := rect.Y; y < rect.Y+rect.H; y++ {
|
||||
gameMap.tiles[x][y] = 4
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func createBase(gameMap *GameMap, baseColor color.Color, posX, posY, ownerID, openingWidth uint32) *Base {
|
||||
gameObject := &GameObject{}
|
||||
gameObject.baseRect = sdl.Rect{
|
||||
gameObject.baseRect = &sdl.Rect{
|
||||
X: int32(posX),
|
||||
Y: int32(posY),
|
||||
W: 35,
|
||||
@@ -97,12 +97,12 @@ func createBase(gameMap *GameMap, baseColor color.Color, posX, posY, ownerID, op
|
||||
panic("Bad border width")
|
||||
}
|
||||
gameObject.addColor(baseColor)
|
||||
gameObject.addColoredRect(0, 0, 1, gameObject.baseRect.H, 0)
|
||||
gameObject.addColoredRect(gameObject.baseRect.W, 0, 1, gameObject.baseRect.H, 0)
|
||||
gameObject.addColoredRect(0, 0, 1, gameObject.baseRect.H-1, 0)
|
||||
gameObject.addColoredRect(gameObject.baseRect.W, 0, 1, gameObject.baseRect.H-1, 0)
|
||||
gameObject.addColoredRect(0, 0, borderWidthA, 1, 0)
|
||||
gameObject.addColoredRect(borderWidthA+int32(openingWidth), 0, borderWidthB, 1, 0)
|
||||
gameObject.addColoredRect(0, gameObject.baseRect.H, borderWidthA, 1, 0)
|
||||
gameObject.addColoredRect(borderWidthA+int32(openingWidth), gameObject.baseRect.H, borderWidthB, 1, 0)
|
||||
gameObject.addColoredRect(0, gameObject.baseRect.H-1, borderWidthA, 1, 0)
|
||||
gameObject.addColoredRect(borderWidthA+int32(openingWidth), gameObject.baseRect.H-1, borderWidthB, 1, 0)
|
||||
base := &Base{
|
||||
gameObject: gameObject,
|
||||
ownerID: ownerID,
|
||||
@@ -118,12 +118,16 @@ func (base *Base) delete(bases map[uint32]*Base) {
|
||||
gameMap.tiles[x][y] = 0
|
||||
}
|
||||
}
|
||||
baseMutex.Lock()
|
||||
delete(bases, base.ownerID)
|
||||
baseMutex.Unlock()
|
||||
}
|
||||
|
||||
func createBases(players map[uint32]*Player, gameMap *GameMap) map[uint32]*Base {
|
||||
bases := map[uint32]*Base{}
|
||||
playersMutex.RLock()
|
||||
for ownerID, player := range players {
|
||||
baseMutex.Lock()
|
||||
bases[ownerID] = createBase(gameMap,
|
||||
player.playerColors.body,
|
||||
uint32(player.gameObject.baseRect.X-14),
|
||||
@@ -131,6 +135,8 @@ func createBases(players map[uint32]*Player, gameMap *GameMap) map[uint32]*Base
|
||||
ownerID,
|
||||
uint32(float64(player.gameObject.baseRect.W)*1.5),
|
||||
)
|
||||
baseMutex.Unlock()
|
||||
}
|
||||
playersMutex.RUnlock()
|
||||
return bases
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user