- commit
- 61c7c761cf74e646e94f5ff1e50c3644fd46a454
- parent
- 8e6449ed1bc0db46e95cdbfa4fa0261a7cd4c5af
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-09-25 05:49
refactor: use more Point
Diffstat
| M | game.go | 21 | ++++++++------------- |
| M | geo.go | 16 | ++++++++++++++-- |
2 files changed, 22 insertions, 15 deletions
diff --git a/game.go b/game.go
@@ -164,9 +164,9 @@ func (game *Game) generateMap() {
164 164 }
165 165 }
166 166
167 -1 func (game *Game) IsFree(x int, y int) bool {
-1 167 func (game *Game) IsFree(p Point) bool {
168 168 for _, rect := range game.Rects {
169 -1 if rect.Contains(x, y) {
-1 169 if rect.Contains(p) {
170 170 return true
171 171 }
172 172 }
@@ -285,17 +285,12 @@ func (game *Game) run() {
285 285 msg := cmsg.Msg
286 286
287 287 if msg["action"] == "move" {
288 -1 pos := player.Pos
289 -1 if msg["dir"] == "up" {
290 -1 pos.Y -= 1
291 -1 } else if msg["dir"] == "right" {
292 -1 pos.X += 1
293 -1 } else if msg["dir"] == "down" {
294 -1 pos.Y += 1
295 -1 } else if msg["dir"] == "left" {
296 -1 pos.X -= 1
-1 288 dir, ok := msg["dir"].(string)
-1 289 if !ok {
-1 290 continue
297 291 }
298 -1 if game.IsFree(pos.X, pos.Y) {
-1 292 pos := player.Pos.Move(dir)
-1 293 if game.IsFree(pos) {
299 294 player.Pos = pos
300 295 game.broadcast([]Message{
301 296 Message{
@@ -325,7 +320,7 @@ func (game *Game) run() {
325 320 } else if msg["dir"] == "left" {
326 321 pos.X -= 1
327 322 }
328 -1 if game.IsFree(pos.X, pos.Y) {
-1 323 if game.IsFree(pos) {
329 324 monster.Pos = pos
330 325 game.broadcast([]Message{
331 326 Message{
diff --git a/geo.go b/geo.go
@@ -16,6 +16,18 @@ type Rect struct {
16 16
17 17 var dirs = []string{"up", "right", "down", "left"}
18 18
-1 19 func (point *Point) Move(dir string) Point {
-1 20 if dir == "up" {
-1 21 return Point{point.X, point.Y - 1}
-1 22 } else if dir == "right" {
-1 23 return Point{point.X + 1, point.Y}
-1 24 } else if dir == "down" {
-1 25 return Point{point.X, point.Y + 1}
-1 26 } else {
-1 27 return Point{point.X - 1, point.Y}
-1 28 }
-1 29 }
-1 30
19 31 func makeRect(x1 int, y1 int, x2 int, y2 int) Rect {
20 32 if x1 > x2 {
21 33 x1, x2 = x2, x1
@@ -42,8 +54,8 @@ func (rect *Rect) Perimeter() int {
42 54 return ((rect.X2 - rect.X1) + (rect.Y2 - rect.Y1)) * 2
43 55 }
44 56
45 -1 func (rect *Rect) Contains(x int, y int) bool {
46 -1 return x >= rect.X1 && x <= rect.X2 && y >= rect.Y1 && y <= rect.Y2
-1 57 func (rect *Rect) Contains(p Point) bool {
-1 58 return p.X >= rect.X1 && p.X <= rect.X2 && p.Y >= rect.Y1 && p.Y <= rect.Y2
47 59 }
48 60
49 61 func (rect *Rect) Center() Point {