-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevels.go
More file actions
101 lines (86 loc) · 2.17 KB
/
Copy pathlevels.go
File metadata and controls
101 lines (86 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"bufio"
"fmt"
"strconv"
"strings"
)
func (g *GameState) loadLevel(levelNum int) error {
data, exists := levelData[levelNum]
if !exists {
return fmt.Errorf("level %d not found", levelNum)
}
return g.parseLevel(data)
}
func (g *GameState) parseLevel(levelData string) error {
reader := strings.NewReader(levelData)
scanner := bufio.NewScanner(reader)
// Reset game state for new level
g.Food = 0
g.PendingDir = Direction{}
// Initialize Pacman
g.Pacman.Char = 'C'
g.Pacman.Color = int16(ColorPacman)
g.Pacman.Dir = Direction{0, -1}
// Initialize ghosts
ghostChars := []rune{'&', '&', '&', '&'}
ghostColors := []int16{
int16(ColorGhost1), int16(ColorGhost2),
int16(ColorGhost3), int16(ColorGhost4),
}
for i := range g.Ghosts {
g.Ghosts[i].Char = ghostChars[i]
g.Ghosts[i].Color = ghostColors[i]
}
// Parse level data
for y := 0; y < LevelHeight && scanner.Scan(); y++ {
line := scanner.Text()
fields := strings.Fields(line)
for x := 0; x < LevelWidth && x < len(fields); x++ {
val, err := strconv.Atoi(fields[x])
if err != nil {
continue
}
g.Level[y][x] = val
switch CellType(val) {
case CellPellet:
g.Food++
case CellBlinky:
g.Ghosts[0].Pos = Position{y, x}
g.Ghosts[0].Dir = Direction{1, 0}
g.Level[y][x] = int(CellBlank)
case CellInkey:
g.Ghosts[1].Pos = Position{y, x}
g.Ghosts[1].Dir = Direction{-1, 0}
g.Level[y][x] = int(CellBlank)
case CellClyde:
g.Ghosts[2].Pos = Position{y, x}
g.Ghosts[2].Dir = Direction{0, -1}
g.Level[y][x] = int(CellBlank)
case CellPinky:
g.Ghosts[3].Pos = Position{y, x}
g.Ghosts[3].Dir = Direction{0, 1}
g.Level[y][x] = int(CellBlank)
case CellPacmanStart:
g.Pacman.Pos = Position{y, x}
g.Level[y][x] = int(CellBlank)
}
}
}
// Read level number
if scanner.Scan() {
if num, err := strconv.Atoi(strings.TrimSpace(scanner.Text())); err == nil {
g.LevelNumber = num
} else {
g.LevelNumber = g.CurrentLevel
}
} else {
g.LevelNumber = g.CurrentLevel
}
// Save starting positions
g.Pacman.StartPos = g.Pacman.Pos
for i := range g.Ghosts {
g.Ghosts[i].StartPos = g.Ghosts[i].Pos
}
return nil
}