Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This package manages most functions for the board, tile, and ring structs
- package main
- import(
- "github.com/gorilla/websocket"
- )
- //Helper func to create new tile
- func newTile(gb *GameBoard, x, y, z uint8) *HexTile{
- return &HexTile{[]uint8{x, y, z}, []bool{false, false, false, false, false, false}, "", gb}
- }
- //Acts as the center tile for any board because the first ring is always 6 tiles
- var Center HexTile = HexTile{[]uint8{0, 0, 0}, []bool{true, true, true, true, true, true}, "NULL", nil}
- /*
- Helper to create new board
- NOTE: d is the diamater of
- the board i.e. the number of rings
- however, this does not include the
- center piece, so gb.rings[0] will
- have six tiles
- */
- func newBoard(d uint8) *GameBoard{
- var gb *GameBoard = &GameBoard{make([]*BoardRing, d), 0, make([]Attacker, 3), 0, make([]Defender, 3), true}
- for i := uint8(0); i < d; i++{
- gb.rings[i] = &BoardRing{make([]*HexTile, (i+1)*6), false}
- for j := uint8(0); j < (i+1)*6; j++{
- gb.rings[i].tiles[j] = newTile(gb, i+1, j/(i+1), j%(i+1))
- }
- if i > d-1{
- gb.rings[i].edge = true
- }
- }
- return gb
- }
- //Tile funcs
- //Gets a tile adjacent to the given tile
- //the next tile is 0, and it goes clockwise from there
- func (t *HexTile) getAdjacent(side uint8) *HexTile{
- if t.root == nil {
- return t
- }
- x, y, z := t.pos[0], t.pos[1], t.pos[2]
- switch side%6{
- case 0:
- if z < x-1 {
- z++
- }else{
- y++
- z = 0
- }
- break
- case 1:
- if t.isOnEdge() == false {
- x++
- z++
- }
- break
- case 2:
- if t.isOnEdge() == false {
- x++
- }
- break
- case 3:
- if z > 0 {
- z--
- }else if t.isOnEdge() == false{
- if y > 0 {
- y--
- }else {
- y = 5
- }
- z = x
- x++
- }
- break
- case 4:
- if z > 0 && x > 0 {
- x--
- z--
- }else{
- if y > 0 {
- y--
- }else {
- y = 5
- }
- z = x-1
- }
- break
- case 5:
- if x > 0 {
- x--
- }
- break
- }
- return t.root.getTile(uint(x), uint(y), uint(z))
- }
- //Determines if the tile is a corner (vertex)
- func (t *HexTile) isCorner() bool{
- if t.pos[2] == 0 {
- return true
- }
- return false
- }
- //Determines if the tile is on the outer ring of the board
- func (t *HexTile) isOnEdge() bool{
- return t.root.rings[t.pos[0]-1].edge
- }
- /*
- Gets a tile from a ring, vertex, and offset
- ring 0 to d where d is the value used in NewBoard
- vert 0 to 5 where 0 is the start of the ring and each addition of one donates d tiles
- oset 0 to (d-1)
- */
- func (gb *GameBoard) getTile(ring, vert, oset uint) *HexTile{
- if ring < 1{
- return &Center
- }else if ring <= uint(len(gb.rings)) && ring > oset {
- t := oset+(ring*(vert%6))
- return gb.rings[ring-1].tiles[t]
- }
- return &HexTile{[]uint8{0, 0, 0}, []bool{false, false, false, false, false, false}, "FALSE_TILE", nil}
- }
- func (gb *GameBoard) addPlayer(socket *websocket.Conn, t bool) int8{
- var i int8 = -1
- if t && gb.numa < 3 {
- i = gb.numa
- gb.numa++
- gb.pla[i] = Attacker{[]uint8{uint8(len(gb.rings)), uint8(3*i-1), 0}, socket, false}
- }else if gb.numd < 3 {
- i = gb.numd
- gb.numd++
- gb.pld[i] = Defender{uint8(i+1), socket, false}
- i+=3
- }
- return i
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement