
import pimobj

class OutOfBounds:
	pass

class World:

	def __init__( s, scr ):
		s.scr = scr
		s.dim_y, s.dim_x = scr.getmaxyx()
		s.mp = []
		for x in range( s.dim_x ):
			col = [ [] for i in range( s.dim_y ) ]
			s.mp.append( col )
		s.empty = pimobj.Empty()
		s.wall = pimobj.Wall()
		s.animate = []
		s.roundnr = 0

	def outbound( s, x, y ):
		if x == s.dim_x - 1 and y == s.dim_y - 1: return True
		if 0 <= x < s.dim_x and 0 <= y < s.dim_y: return False
		return True

	def objs_at( s, x, y ):
		if s.outbound( x, y ): return [ s.wall ]
		return s.mp[x][y]

	def add_obj( s, o, x, y ):
		if s.outbound( x, y ): raise OutOfBounds
		s.mp[x][y].append( o )
		o.draw( s.scr, x, y )

	def rm_obj( s, o, x, y ):
		if s.outbound( x, y ): raise OutOfBounds
		s.mp[x][y].remove( o )
		s.draw( x, y )

	def draw( s, x, y ):
		if s.mp[x][y]: s.mp[x][y][-1].draw( s.scr, x, y )
		else: s.empty.draw( s.scr, x, y )

	def redraw( s ):
		for x in range( dim_x ):
			for y in range( dim_y ):
				s.draw( x, y )

	def add_anim( s, o ):
		if o not in s.animate: s.animate.append( o )

	def rm_anim( s, o ):
		s.animate.remove( o )

	def proact( s ):
		for o in s.animate: o.proact( s.roundnr )
		s.roundnr = s.roundnr + 1

