/
main.go
181 lines (159 loc) · 3.96 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"crypto/md5"
"errors"
"image"
"image/color"
"log"
"math/rand"
"net/url"
"os"
"strings"
"github.com/bmcszk/gptrts/pkg/comm"
"github.com/bmcszk/gptrts/pkg/game"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"github.com/hajimehoshi/ebiten/v2"
)
const (
screenWidth = 640
screenHeight = 480
)
var (
tilesImage *ebiten.Image
backgroundImages map[string]*ebiten.Image = make(map[string]*ebiten.Image)
)
type client struct {
*comm.Client
game *clientGame
}
func newClient(id game.PlayerIdType, ws *websocket.Conn) *client {
c := comm.NewClient(ws)
c.PlayerId = id
return &client{
Client: c,
}
}
func init() {
// Decode image from a byte slice instead of a file so that
// this example works in any working directory.
// If you want to use a file, there are some options:
// 1) Use os.Open and pass the file to the image decoder.
// This is a very regular way, but doesn't work on browsers.
// 2) Use ebitenutil.OpenFile and pass the file to the image decoder.
// This works even on browsers.
// 3) Use ebitenutil.NewImageFromFile to create an ebiten.Image directly from a file.
// This also works on browsers.
file, err := os.Open("tiles1.png")
if err != nil {
log.Fatal(err)
}
img, _, err := image.Decode(file)
// img, _, err := image.Decode(bytes.NewReader(images.Tiles_png))
if err != nil {
log.Fatal(err)
}
tilesImage = ebiten.NewImageFromImage(img)
}
func main() {
name := getName()
playerId := getPlayerId(name)
u := url.URL{Scheme: "ws", Host: "localhost:8000", Path: "/ws"}
log.Printf("connecting to %s", u.String())
ws, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
defer ws.Close()
c := newClient(playerId, ws)
g := newClientGame(playerId, game.NewStoreImpl(), c.processNewAction)
c.game = g
// Read messages from the server
go func() {
for c.Connected {
action, err := c.HandleInMessages()
if err != nil {
log.Println(err)
continue
}
g.HandleAction(action, c.route)
}
}()
if err = c.Send(game.PlayerJoinAction{
Type: game.PlayerJoinActionType,
Payload: game.Player{
Id: playerId,
Name: name,
Color: nameToColor(name),
},
}); err != nil {
log.Println(err)
}
// start ebiten on main thread
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
ebiten.SetWindowTitle(name)
if err := ebiten.RunGame(g); err != nil {
log.Fatal(err)
}
}
func nameToColor(name string) color.RGBA {
name = strings.TrimSpace(name)
name = strings.ToLower(name)
switch name {
case "red":
return color.RGBA{255, 0, 0, 255}
case "green":
return color.RGBA{0, 255, 0, 255}
case "blue":
return color.RGBA{0, 0, 255, 255}
case "yellow":
return color.RGBA{255, 255, 0, 255}
case "cyan":
return color.RGBA{0, 255, 255, 255}
case "purple":
return color.RGBA{255, 0, 255, 255}
default:
return randomRGBA()
}
}
// randomRGBA generates a random color in RGBA format
func randomRGBA() color.RGBA {
return color.RGBA{
R: uint8(rand.Intn(256)),
G: uint8(rand.Intn(256)),
B: uint8(rand.Intn(256)),
A: 255, // Set alpha to 255 for opaque color
}
}
func getName() string {
if len(os.Args) < 2 {
log.Fatal(errors.New("agrument missing"))
}
return os.Args[1]
}
func getPlayerId(name string) game.PlayerIdType {
hash := md5.Sum([]byte(name))
id, err := uuid.FromBytes(hash[:])
if err != nil {
log.Fatal(err)
}
return game.PlayerIdType(id)
}
// processNewAction - handler of new actions
func (c *client) processNewAction(action game.Action) {
if err := c.Send(action); err != nil {
log.Println("route %w", err)
}
c.game.HandleAction(action, c.route)
}
// route - handler of outgoing actions
func (c *client) route(action game.Action) {
if err := c.Send(action); err != nil {
log.Println("route %w", err)
}
switch a := action.(type) {
case game.MoveStartAction, game.MoveStepAction, game.MoveStopAction:
c.game.HandleAction(a, c.route)
}
}