cleaning up bounds check

master
Jordan Orelli 4 years ago
parent bd46b57315
commit 5a15c3c9bc

@ -51,24 +51,24 @@ func (v *gameView) draw(ui *UI) {
offset := point{1, 1} offset := point{1, 1}
// fill in background dots first // fill in background dots first
for x := 0; x < v.room.Width(); x++ { for x := 0; x < v.room.Width; x++ {
for y := 0; y < v.room.Height(); y++ { for y := 0; y < v.room.Height; y++ {
ui.screen.SetContent(x+offset.x, y+offset.y, '·', nil, tcell.StyleDefault) ui.screen.SetContent(x+offset.x, y+offset.y, '·', nil, tcell.StyleDefault)
} }
} }
// frame it // frame it
ui.screen.SetContent(offset.x-1, offset.y-1, '┌', nil, tcell.StyleDefault) ui.screen.SetContent(offset.x-1, offset.y-1, '┌', nil, tcell.StyleDefault)
ui.screen.SetContent(offset.x+v.room.Width(), offset.y-1, '┐', nil, tcell.StyleDefault) ui.screen.SetContent(offset.x+v.room.Width, offset.y-1, '┐', nil, tcell.StyleDefault)
ui.screen.SetContent(offset.x-1, offset.y+v.room.Height(), '└', nil, tcell.StyleDefault) ui.screen.SetContent(offset.x-1, offset.y+v.room.Height, '└', nil, tcell.StyleDefault)
ui.screen.SetContent(offset.x+v.room.Width(), offset.y+v.room.Height(), '┘', nil, tcell.StyleDefault) ui.screen.SetContent(offset.x+v.room.Width, offset.y+v.room.Height, '┘', nil, tcell.StyleDefault)
for x := 0; x < v.room.Width(); x++ { for x := 0; x < v.room.Width; x++ {
ui.screen.SetContent(x+offset.x, offset.y-1, '─', nil, tcell.StyleDefault) ui.screen.SetContent(x+offset.x, offset.y-1, '─', nil, tcell.StyleDefault)
ui.screen.SetContent(x+offset.x, offset.y+v.room.Height(), '─', nil, tcell.StyleDefault) ui.screen.SetContent(x+offset.x, offset.y+v.room.Height, '─', nil, tcell.StyleDefault)
} }
for y := 0; y < v.room.Height(); y++ { for y := 0; y < v.room.Height; y++ {
ui.screen.SetContent(offset.x-1, y+offset.y, '│', nil, tcell.StyleDefault) ui.screen.SetContent(offset.x-1, y+offset.y, '│', nil, tcell.StyleDefault)
ui.screen.SetContent(offset.x+v.room.Width(), y+offset.y, '│', nil, tcell.StyleDefault) ui.screen.SetContent(offset.x+v.room.Width, y+offset.y, '│', nil, tcell.StyleDefault)
} }
for _, e := range v.room.Entities { for _, e := range v.room.Entities {

@ -1,35 +1,24 @@
package math package math
import "encoding/json"
type Bounds struct { type Bounds struct {
Min Vec `json:"min"` Origin Vec `json:"origin"`
Max Vec `json:"max"` Width int `json:"width"`
Height int `json:"height"`
} }
func CreateBounds(width, height int) Bounds { func CreateBounds(width, height int) Bounds {
return Bounds{ return Bounds{
Min: Vec{0, 0}, Origin: Vec{0, 0},
Max: Vec{width - 1, height - 1}, Width: width,
Height: height,
} }
} }
func (b Bounds) Width() int { return Abs(b.Max.X - b.Min.X) } func (b Bounds) Area() int { return b.Width * b.Height }
func (b Bounds) Height() int { return Abs(b.Max.Y - b.Min.Y) }
func (b Bounds) Area() int { return b.Width() * b.Height() }
func (b Bounds) Contains(v Vec) bool { func (b Bounds) Contains(v Vec) bool {
return v.X >= b.Min.X && v.X <= b.Max.X && v.Y >= b.Min.Y && v.Y <= b.Max.Y return v.X >= b.Origin.X &&
} v.X < b.Origin.X+b.Width &&
v.Y >= b.Origin.Y &&
func (b Bounds) MarshalJSON() ([]byte, error) { return json.Marshal([2]Vec{b.Min, b.Max}) } v.Y < b.Height
func (b *Bounds) UnmarshalJSON(buf []byte) error {
var raw [2]Vec
if err := json.Unmarshal(buf, &raw); err != nil {
return err
}
b.Min = raw[0]
b.Max = raw[1]
return nil
} }

@ -24,7 +24,7 @@ func (m *Move) exec(r *room, p *player, seq int) result {
pos := p.avatar.Position pos := p.avatar.Position
target := pos.Add(math.Vec(*m)) target := pos.Add(math.Vec(*m))
p.Info("running move for player %s from %v to %v", p.name, *m, target) p.Info("running move for player %s from %v to %v", p.name, *m, target)
if !p.room.bounds.Contains(target) { if !p.room.Contains(target) {
return result{reply: wire.Errorf("target cell (%d, %d) is out of bounds", target.X, target.Y)} return result{reply: wire.Errorf("target cell (%d, %d) is out of bounds", target.X, target.Y)}
} }
@ -97,7 +97,7 @@ func (s *SpawnPlayer) exec(r *room, p *player, seq int) result {
} }
welcome.Rooms[r.name] = wire.Room{ welcome.Rooms[r.name] = wire.Room{
Name: r.name, Name: r.name,
Bounds: r.bounds, Bounds: r.Bounds,
Entities: ents, Entities: ents,
} }
for _, p := range r.players { for _, p := range r.players {

@ -11,8 +11,7 @@ import (
type room struct { type room struct {
*blammo.Log *blammo.Log
name string name string
origin point math.Bounds
bounds math.Bounds
tiles []tile tiles []tile
players map[string]*player players map[string]*player
} }
@ -68,9 +67,9 @@ func (r *room) removePlayer(name string) bool {
} }
func (r *room) getTile(pos math.Vec) *tile { func (r *room) getTile(pos math.Vec) *tile {
if !r.bounds.Contains(pos) { if !r.Contains(pos) {
return nil return nil
} }
n := pos.X*r.bounds.Width() + pos.Y n := pos.X*r.Width + pos.Y
return &r.tiles[n] return &r.tiles[n]
} }

@ -24,8 +24,7 @@ func NewWorld(log *blammo.Log) *World {
foyer := room{ foyer := room{
Log: log.Child("foyer"), Log: log.Child("foyer"),
name: "foyer", name: "foyer",
origin: point{0, 0}, Bounds: math.CreateBounds(10, 10),
bounds: bounds,
tiles: make([]tile, bounds.Area()), tiles: make([]tile, bounds.Area()),
players: make(map[string]*player), players: make(map[string]*player),
} }
@ -35,6 +34,7 @@ func NewWorld(log *blammo.Log) *World {
Glyph: 'd', Glyph: 'd',
behavior: doNothing{}, behavior: doNothing{},
} }
log.Info("created foyer with bounds: %#v having width: %d height: %d area: %d", foyer.Bounds, foyer.Width, foyer.Height, foyer.Area())
return &World{ return &World{
Log: log, Log: log,
rooms: []room{foyer}, rooms: []room{foyer},

@ -7,9 +7,6 @@ import (
// Room represents a 2-dimensional coordinate space. // Room represents a 2-dimensional coordinate space.
type Room struct { type Room struct {
Name string `json:"name"` Name string `json:"name"`
Bounds math.Bounds `json:"bounds"` math.Bounds `json:"bounds"`
Entities map[int]*Entity `json:"entities"` Entities map[int]*Entity `json:"entities"`
} }
func (r Room) Width() int { return r.Bounds.Width() }
func (r Room) Height() int { return r.Bounds.Height() }

Loading…
Cancel
Save