-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (146 loc) · 3.18 KB
/
main.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"bufio"
"fmt"
"image"
"image/color"
"log"
"os"
"strconv"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
)
const (
winW = 640
winH = 640
)
//Env Game's environment
type Env struct {
sqList [][]*square
grid *ebiten.Image
size int
autoMode string
player image.Point
score int
over bool
sqW int
offset int
start image.Point
end image.Point
}
type square struct {
pos image.Point
state int
}
func parseArgs() (string, int, []int, []int, [][]int) {
//Parse Args
if len(os.Args[1:]) < 1 {
fmt.Printf("Missing Argument\nUsage : Astar_go map_file [-m algo]\nwith algo : 'Astar', WIP")
os.Exit(1)
}
// Get args
mode := ""
for i, arg := range os.Args {
if arg == "-m" && i+1 < len(os.Args) {
mode = os.Args[i+1]
}
}
// Read map file
file, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer file.Close()
reader := bufio.NewReader(file)
firstLine, _, err := reader.ReadLine()
if err != nil {
fmt.Println("Error Reading map file")
}
// Read size
size, err := strconv.Atoi(string(firstLine))
if err != nil || size < 8 {
fmt.Println("Invalid Argument")
os.Exit(1)
}
// Read map
startTab := make([]int, 2)
endTab := make([]int, 2)
readenMap := make([][]int, size)
for y := 0; y < size; y++ {
line, _, err := reader.ReadLine()
if err != nil {
fmt.Println("Error Reading map file")
}
readenMap[y] = make([]int, size)
for x := 0; x < size; x++ {
switch {
case line[x] == '.':
readenMap[y][x] = 0
case line[x] == 's':
readenMap[y][x] = 1
startTab[0] = x
startTab[1] = y
case line[x] == 'e':
readenMap[y][x] = 2
endTab[0] = x
endTab[1] = y
case line[x] == '#':
readenMap[y][x] = 3
}
}
}
return mode, size, startTab, endTab, readenMap
}
// Update screen 60 time / s
func (env *Env) update(screen *ebiten.Image) error {
if ebiten.IsDrawingSkipped() {
return nil
}
// Print map
gridOp := &ebiten.DrawImageOptions{}
gridOp.GeoM.Translate(0, 0)
screen.DrawImage(env.grid, gridOp)
// Move
if env.over == false {
// Print player
playerSq := env.buildSquare(color.RGBA{255, 153, 0, 200})
playerOp := &ebiten.DrawImageOptions{}
playerOp.GeoM.Translate(float64(env.player.X*env.sqW), float64(env.player.Y*env.sqW))
screen.DrawImage(playerSq, playerOp)
// Check if finished
if env.checkEnd(env.player.X, env.player.Y) {
env.over = true
}
} else {
// Print Score
scoreStr := strconv.Itoa(env.score)
scoreMsg := fmt.Sprintf("GAME OVER\nScore : %s", scoreStr)
ebitenutil.DebugPrint(screen, scoreMsg)
}
return nil
}
func main() {
mode, size, start, end, readenMap := parseArgs()
env := Env{
sqList: make([][]*square, size),
size: size,
autoMode: mode,
player: image.Point{start[0], start[1]},
score: 0,
over: false,
sqW: int(winW / size),
offset: 1,
start: image.Point{start[0], start[1]},
end: image.Point{end[0], end[1]},
}
env.buildMap(size, readenMap)
// Creates main window
if len(env.autoMode) > 0 {
go env.botPlayer(env.autoMode)
} else {
go env.movePlayer()
}
if err := ebiten.Run(env.update, winW, winH, 1, "Astar Go"); err != nil {
log.Fatal(err)
}
}