- commit
- 8e6449ed1bc0db46e95cdbfa4fa0261a7cd4c5af
- parent
- 5ee1fc0199f99bd37e00607ea234e32e2949764e
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-09-25 05:13
make monsters move
Diffstat
| M | game.go | 59 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | geo.go | 6 | ++++++ |
2 files changed, 65 insertions, 0 deletions
diff --git a/game.go b/game.go
@@ -3,6 +3,7 @@ package main 3 3 import ( 4 4 "log" 5 5 "sync" -1 6 "time" 6 7 7 8 "github.com/gorilla/websocket" 8 9 ) @@ -21,6 +22,7 @@ type Player struct { 21 22 22 23 type Monster struct { 23 24 Game *Game -1 25 quit chan bool 24 26 Id int 25 27 Rune rune 26 28 Pos Point @@ -32,11 +34,17 @@ type PlayerMessage struct { 32 34 Msg Message 33 35 } 34 36 -1 37 type MonsterMessage struct { -1 38 Monster *Monster -1 39 Msg Message -1 40 } -1 41 35 42 type Game struct { 36 43 Id string 37 44 Players map[*Player]bool 38 45 Monsters map[*Monster]bool 39 46 Msg chan PlayerMessage -1 47 MMsg chan MonsterMessage 40 48 register chan *Player 41 49 unregister chan *Player 42 50 lastId int @@ -64,6 +72,7 @@ func getGame(id string) *Game { 64 72 Players: make(map[*Player]bool), 65 73 Monsters: make(map[*Monster]bool), 66 74 Msg: make(chan PlayerMessage), -1 75 MMsg: make(chan MonsterMessage), 67 76 register: make(chan *Player), 68 77 unregister: make(chan *Player), 69 78 lastId: 0, @@ -79,6 +88,27 @@ func getGame(id string) *Game { 79 88 return game 80 89 } 81 90 -1 91 func (monster *Monster) run() { -1 92 timeout := time.Duration(float32(time.Second) / monster.Speed) -1 93 ticker := time.NewTicker(timeout) -1 94 defer ticker.Stop() -1 95 -1 96 for { -1 97 select { -1 98 case <-monster.quit: -1 99 return -1 100 case <-ticker.C: -1 101 monster.Game.MMsg <- MonsterMessage{ -1 102 monster, -1 103 Message{ -1 104 "action": "move", -1 105 "dir": RandomDir(), -1 106 }, -1 107 } -1 108 } -1 109 } -1 110 } -1 111 82 112 func (game *Game) broadcast(msgs []Message) { 83 113 for player, _ := range game.Players { 84 114 player.Send <- msgs @@ -92,6 +122,7 @@ func (game *Game) createId() int { 92 122 93 123 func (game *Game) generateMap() { 94 124 for monster := range game.Monsters { -1 125 monster.quit <- true 95 126 delete(game.Monsters, monster) 96 127 } 97 128 @@ -113,12 +144,14 @@ func (game *Game) generateMap() { 113 144 114 145 monster := Monster{ 115 146 game, -1 147 make(chan bool), 116 148 game.createId(), 117 149 'm', 118 150 rect.RandomPoint(), 119 151 2, 120 152 } 121 153 game.Monsters[&monster] = true -1 154 go monster.run() 122 155 123 156 prev = rect 124 157 } @@ -277,6 +310,32 @@ func (game *Game) run() { 277 310 } else if verbose { 278 311 log.Println("unknown action", msg) 279 312 } -1 313 case mmsg := <-game.MMsg: -1 314 monster := mmsg.Monster -1 315 msg := mmsg.Msg -1 316 -1 317 if msg["action"] == "move" { -1 318 pos := monster.Pos -1 319 if msg["dir"] == "up" { -1 320 pos.Y -= 1 -1 321 } else if msg["dir"] == "right" { -1 322 pos.X += 1 -1 323 } else if msg["dir"] == "down" { -1 324 pos.Y += 1 -1 325 } else if msg["dir"] == "left" { -1 326 pos.X -= 1 -1 327 } -1 328 if game.IsFree(pos.X, pos.Y) { -1 329 monster.Pos = pos -1 330 game.broadcast([]Message{ -1 331 Message{ -1 332 "action": "setPosition", -1 333 "id": monster.Id, -1 334 "pos": monster.Pos, -1 335 }, -1 336 }) -1 337 } -1 338 } 280 339 } 281 340 } 282 341 }
diff --git a/geo.go b/geo.go
@@ -14,6 +14,8 @@ type Rect struct {
14 14 Y2 int `json:"y2"`
15 15 }
16 16
-1 17 var dirs = []string{"up", "right", "down", "left"}
-1 18
17 19 func makeRect(x1 int, y1 int, x2 int, y2 int) Rect {
18 20 if x1 > x2 {
19 21 x1, x2 = x2, x1
@@ -57,3 +59,7 @@ func (rect *Rect) RandomPoint() Point {
57 59 rect.Y1 + rand.Intn(rect.Y2-rect.Y1+1),
58 60 }
59 61 }
-1 62
-1 63 func RandomDir() string {
-1 64 return dirs[rand.Intn(4)]
-1 65 }