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}
// fill in background dots first
for x := 0; x < v.room.Width(); x++ {
for y := 0; y < v.room.Height(); y++ {
for x := 0; x < v.room.Width; x++ {
for y := 0; y < v.room.Height; y++ {
ui.screen.SetContent(x+offset.x, y+offset.y, '·', nil, tcell.StyleDefault)
}
}
// frame it
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-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)
for x := 0; x < v.room.Width(); x++ {
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+v.room.Width, offset.y+v.room.Height, '┘', nil, tcell.StyleDefault)
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+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+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 {

@ -1,35 +1,24 @@
package math
import "encoding/json"
type Bounds struct {
Min Vec `json:"min"`
Max Vec `json:"max"`
Origin Vec `json:"origin"`
Width int `json:"width"`
Height int `json:"height"`
}
func CreateBounds(width, height int) Bounds {
return Bounds{
Min: Vec{0, 0},
Max: Vec{width - 1, height - 1},
Origin: Vec{0, 0},
Width: width,
Height: height,
}
}
func (b Bounds) Width() int { return Abs(b.Max.X - b.Min.X) }
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) Area() int { return b.Width * b.Height }
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
}
func (b Bounds) MarshalJSON() ([]byte, error) { return json.Marshal([2]Vec{b.Min, b.Max}) }
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
return v.X >= b.Origin.X &&
v.X < b.Origin.X+b.Width &&
v.Y >= b.Origin.Y &&
v.Y < b.Height
}

@ -24,7 +24,7 @@ func (m *Move) exec(r *room, p *player, seq int) result {
pos := p.avatar.Position
target := pos.Add(math.Vec(*m))
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)}
}
@ -97,7 +97,7 @@ func (s *SpawnPlayer) exec(r *room, p *player, seq int) result {
}
welcome.Rooms[r.name] = wire.Room{
Name: r.name,
Bounds: r.bounds,
Bounds: r.Bounds,
Entities: ents,
}
for _, p := range r.players {

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

@ -24,8 +24,7 @@ func NewWorld(log *blammo.Log) *World {
foyer := room{
Log: log.Child("foyer"),
name: "foyer",
origin: point{0, 0},
bounds: bounds,
Bounds: math.CreateBounds(10, 10),
tiles: make([]tile, bounds.Area()),
players: make(map[string]*player),
}
@ -35,6 +34,7 @@ func NewWorld(log *blammo.Log) *World {
Glyph: 'd',
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{
Log: log,
rooms: []room{foyer},

@ -7,9 +7,6 @@ import (
// Room represents a 2-dimensional coordinate space.
type Room struct {
Name string `json:"name"`
Bounds math.Bounds `json:"bounds"`
math.Bounds `json:"bounds"`
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