Define maze struct
This commit is contained in:
parent
ecb6379d3c
commit
a4875fc79b
1 changed files with 52 additions and 0 deletions
52
world/maze.go
Normal file
52
world/maze.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package world
|
||||
|
||||
import (
|
||||
"pacman/assets"
|
||||
)
|
||||
|
||||
const (
|
||||
MAZE_WIDTH = assets.MAZE_WIDTH
|
||||
MAZE_HEIGHT = assets.MAZE_HEIGHT
|
||||
)
|
||||
|
||||
type Maze [MAZE_WIDTH * MAZE_HEIGHT]assets.Tile
|
||||
|
||||
// Create a new maze and load data.
|
||||
func NewMaze(data []byte) Maze {
|
||||
m := Maze{}
|
||||
m.Load(data)
|
||||
return m
|
||||
}
|
||||
|
||||
// Load data into maze.
|
||||
func (m *Maze) Load(data []byte) {
|
||||
n_bytes := m.readMaze(data)
|
||||
|
||||
for i := n_bytes; i < len(m); i++ {
|
||||
m[i] = 0
|
||||
}
|
||||
}
|
||||
|
||||
// Read maze data.
|
||||
func (m *Maze) readMaze(data []byte) int {
|
||||
i := 0
|
||||
for _, x := range data {
|
||||
if x == '\n' {
|
||||
continue
|
||||
}
|
||||
m[i] = assets.Tile(x)
|
||||
i++
|
||||
if i >= len(m) {
|
||||
break
|
||||
}
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func (m Maze) At(x, y uint8) assets.Tile {
|
||||
return m[uint16(x)+(uint16(y)*MAZE_WIDTH)]
|
||||
}
|
||||
|
||||
func (m *Maze) Set(x, y uint8, value assets.Tile) {
|
||||
m[uint16(x)+(uint16(y)*MAZE_WIDTH)] = value
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue