 Systems = require './systems'
 
 C = require './entity/components'
+
+clamp = (x,min,max) ->
+  return min if x < min
+  return max if x > max
+  x
+
+keepWithin = (x,target,minDist,maxDist) ->
+  if target - x < minDist
+    # move left to preserve min dist:
+    return target - minDist
+  else if target - x > maxDist
+    # move right to stay within max dist:
+    return target - maxDist
+  x # x is already at comfortable distance to target
+
 
 Systems.register 'viewport-track-samus', class ViewportTrackSamus
   constructor: ({@container,@tileGrid}) ->

    @minY = 0
    @maxY = (@tileGrid.length - 15) * 16

  run: (estore, dt, input) ->
    for samus in estore.getComponentsOfType('samus')
      position = estore.getComponent(samus.eid, 'position')

      viewport =
        x: -@container.x
        y: -@container.y

      if position.x - viewport.x < 7*16
        viewport.x = position.x - 7*16
      else if position.x - viewport.x > 9*16
        viewport.x = position.x - 9*16

      viewport.y = position.y - 7*16

      if viewport.x < @minX
        viewport.x = @minX
      else if viewport.x > @maxX
        viewport.x = @maxX

      if viewport.y < @minY
        viewport.y = @minY
      else if viewport.y > @maxY
        viewport.y = @maxY

      @container.x = -viewport.x
      @container.y = -viewport.y


Samus = require './entity/samus'


    divRem = (numer,denom) -> [Math.floor(numer/denom), numer % denom]

    map = areaB

    mapRowCount = map.length * 15
    mapColCount = map[0].length * 16
