Skip to content

Commit

Permalink
little refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
zikolach committed Dec 25, 2013
1 parent 1bf2294 commit 6e7080f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
25 changes: 8 additions & 17 deletions app/service/GameServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,8 @@ object Game {
}

def persist(rectOpt: Option[Rect]): (Space, Option[Rect]) = {
rectOpt match {
case Some(rect) =>
val table: Set[(Int, Int)] = (for {
y <- rect.y1 - 1 to rect.y2 + 1
x <- rect.x1 - 1 to rect.x2 + 1 if state(x, y)
} yield (x, y)).toSet
val (xs, ys) = (table.map(_._1), table.map(_._2))
val newRect: Rect = Rect(xs.reduceOption(_ min _).getOrElse(0), ys.reduceOption(_ min _).getOrElse(0), xs.reduceOption(_ max _).getOrElse(0), ys.reduceOption(_ max _).getOrElse(0))
((x: Int, y: Int) => table.contains((x, y)), Some(newRect))
case None =>
((x: Int, y: Int) => false, None)
}
val table = state.toSet(rectOpt.map(_.zoom(1)))
((x: Int, y: Int) => table.contains((x, y)), table.bounds)
}

def ++(other: Space): Space =
Expand All @@ -262,13 +252,14 @@ object Game {
def --(other: Space): Space =
(x: Int, y: Int) => state(x, y) && !other(x, y)

def toSet(rect: Rect): Set[Point] = (for {
y <- rect.y1 to rect.y2
x <- rect.x1 to rect.x2 if state(x, y)
} yield (x, y).toPoint).toSet

def toSet(rectOpt: Option[Rect]): Set[Point] = rectOpt match {
case None => Set.empty
case Some(rect) =>
(for {
y <- rect.y1 to rect.y2
x <- rect.x1 to rect.x2 if state(x, y)
} yield (x, y).toPoint).toSet
case Some(rect) => state.toSet(rect)
}

}
Expand Down
5 changes: 5 additions & 0 deletions app/service/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ package object service {
if (p.y > y2) p.y else y2
)

def zoom(left: Int, up: Int, right: Int, down: Int): Rect =
Rect(x1 - left, y1 - up, x2 + right, y2 + down)

def zoom(inc: Int): Rect = zoom(inc, inc, inc, inc)

def intersect(other: Rect): Option[Rect] =
if (other.x2 < x1 || other.x1 > x2 || other.y2 < y1 || other.y1 > y2) None
else Some(Rect(
Expand Down

0 comments on commit 6e7080f

Please sign in to comment.