1package server 2 3import ( 4 "errors" 5 6 "tinyadventure/internal/model" 7 "tinyadventure/internal/world" 8) 9 10func (g *game) near(x, y int) bool { 11 return world.Distance(g.state.Party.X, g.state.Party.Y, x, y) <= 1 12} 13 14func (g *game) interact(id string) error { 15 g.begin() 16 if p := g.poi(id); p != nil { 17 if !g.near(p.X, p.Y) { 18 return errors.New("too far away") 19 } 20 if !p.Cleared { 21 if !g.bossGate(p) { 22 return errors.New("the way is barred: finish the other main steps first") 23 } 24 g.state.Party.X, g.state.Party.Y = p.X, p.Y 25 g.fight(p.ID, p.X, p.Y, func() { p.Cleared = true }) 26 } 27 return nil 28 } 29 if b := g.band(id); b != nil && !b.Cleared { 30 if !g.near(b.X, b.Y) { 31 return errors.New("too far away") 32 } 33 g.state.Party.X, g.state.Party.Y = b.X, b.Y 34 g.fight(b.ID, b.X, b.Y, func() { b.Cleared = true }) 35 return nil 36 } 37 for i := range g.state.Map.Loot { 38 l := &g.state.Map.Loot[i] 39 if l.ID != id { 40 continue 41 } 42 if l.Collected { 43 return errors.New("already collected") 44 } 45 if !g.near(l.X, l.Y) { 46 return errors.New("too far away") 47 } 48 l.Collected = true 49 g.take(l.Item) 50 g.advance() 51 return nil 52 } 53 for _, s := range g.state.Map.Spawns { 54 if s.ID == id { 55 if !g.near(s.X, s.Y) { 56 return errors.New("too far away") 57 } 58 return nil 59 } 60 } 61 return errors.New("unknown id") 62} 63 64func (g *game) unit(id string) *model.Unit { 65 for i := range g.state.Party.Units { 66 if g.state.Party.Units[i].ID == id { 67 return &g.state.Party.Units[i] 68 } 69 } 70 return nil 71}