-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
192 lines (158 loc) · 3.4 KB
/
map.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
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"encoding/json"
"fmt"
"sync"
"golang.org/x/net/websocket"
)
// Map generic struct
type Map struct {
mu *sync.Mutex
cmdChan chan string
id string
name string
size [2]int64
mobs map[string]*Monster
sockets map[string]*websocket.Conn
players map[string]*Player
}
// NewMap instance for server
func NewMap(id, name string, size [2]int64) *Map {
return &Map{
mu: &sync.Mutex{},
id: id,
name: name,
size: size,
sockets: make(map[string]*websocket.Conn),
mobs: make(map[string]*Monster),
players: map[string]*Player{},
}
}
// Run map functionability
func (m *Map) Run(loadedMap chan<- bool) {
m.loadPlayers()
m.loadMobs()
loadedMap <- true
go m.Socket()
}
// ----------------- Monsters ------------------ //
func (m *Map) loadMobs() {
var tMobs int
var loadedMobs int
loadedMob := make(chan bool)
//TODO: Loop to load mobs from db or json file
for i := 0; i < 3; i++ {
mob := NewMonster(1002, m)
mob.walkSpeed = 1200
m.mobs[mob.memID] = mob
}
for i := 0; i < 2; i++ {
mob := NewMonster(1007, m)
mob.aggresive = true
m.mobs[mob.memID] = mob
}
for i := 0; i < 2; i++ {
mob := NewMonster(1049, m)
mob.walkSpeed = 900
m.mobs[mob.memID] = mob
}
tMobs = len(m.mobs)
for _, mb := range m.mobs {
go mb.Run(loadedMob)
}
mobCheckLoop:
for {
select {
case <-loadedMob:
loadedMobs++
totalMobs++
if loadedMobs == tMobs {
break mobCheckLoop
}
}
}
}
// fake func
func (m *Map) loadPlayers() {
fmt.Println("Player at X:", 10, "Y:", 10)
player := &Player{
currentMap: m,
mu: &sync.Mutex{},
positionX: 10,
positionY: 10,
walkSpeed: 1200,
hp: 50,
maxHP: 50,
sockets: map[string]*websocket.Conn{},
cmdChan: make(chan string),
status: playerStatusIdle,
}
player.memID = fmt.Sprintf("%p", player)
m.players[player.memID] = player
for _, p := range m.players {
go p.Run()
}
}
// ----------------- Basic Info ------------------ //
func (m *Map) getBasicInfo() []byte {
var mobs []string
for _, mb := range m.mobs {
mobs = append(mobs, mb.memID)
}
var players []string
for i := range m.players {
players = append(players, i)
}
tmap := map[string]interface{}{
"id": m.id,
"name": m.name,
"size": m.size,
"mobs": mobs,
"players": players,
}
b, _ := json.Marshal(tmap)
return []byte(fmt.Sprintf("info:%s\n", string(b)))
}
func (m *Map) getMob(mobID string) (*Monster, bool) {
mob, ok := m.mobs[mobID]
return mob, ok
}
func (m *Map) getPlayer(playerID string) (*Player, bool) {
player, ok := m.players[playerID]
return player, ok
}
// ----------------- Socket Functions ------------------ //
//Socket for map output data about the map commands and changes
// for JS browser to handle
func (m *Map) Socket() {
for {
select {
case cmd := <-m.cmdChan:
jsonData := []byte{}
switch cmd {
case "something":
}
m.rangeSockets(cmd, string(jsonData))
}
}
}
func (m *Map) rangeSockets(cmd, data string) {
data = fmt.Sprintf("%s:%s\n", cmd, data)
m.mu.Lock()
for _, sock := range m.sockets {
go func(sock *websocket.Conn, data string) {
sock.Write([]byte(data))
}(sock, data)
}
m.mu.Unlock()
}
func (m *Map) addSocket(address string, ws *websocket.Conn) {
m.mu.Lock()
m.sockets[address] = ws
m.mu.Unlock()
}
func (m *Map) delSocket(address string) {
m.mu.Lock()
delete(m.sockets, address)
m.mu.Unlock()
}