
import curses, player, world, animate, inputtimer, moves, curses.ascii, color, getopt, sound

class Config:

	def __init__( s, args ):
		optlist, s.args = getopt.getopt( args, 'D:p:s:S:r:' )
		options = dict( optlist )
		s.daemonstring = options.get( '-D', '&&&=^#Oxe*' )
		s.daemonprob = int( options.get( '-p', 10 ))
		s.gridstep = int( options.get( '-s', 5 ))
		s.wallrank = int( options.get( '-r', 9 ))
		s.sampleprob = int( options.get( '-S', 0 ))

class EndGame:
	pass

class Main:

	statuswin_width = 6

	def __init__( s, config ):
		s.config = config

	def curses_init( s, stdscr ):
		curses.curs_set( 0 )
		stdscr.leaveok( 1 )
		color.init_colors()
		max_y, max_x = stdscr.getmaxyx()
		s.statuswin = stdscr.subwin( 0, max_x - s.statuswin_width )
		s.playwin = stdscr.subwin( max_y, max_x - s.statuswin_width, 0, 0 )

	def game_init( s ):
		s.wld = world.World( s.playwin )
		sound.init_sound()
		animate.ObjDaemon( s.wld, s.config.daemonstring,
				s.config.daemonprob )
		animate.WallDaemon( s.wld, s.wld.dim_x // 2, s.wld.dim_y // 2,
				moves.up, s.config.gridstep, s.config.wallrank )
		animate.SampleDaemon( s.wld, s.wld.dim_x - 1, s.wld.dim_y - 2,
				sound.find_samples( s.config.args ),
				s.config.sampleprob )
		s.pl = player.Player( s.wld, 0, 0, s.statuswin )
		s.inpt = inputtimer.InputTimer( 0.07 )
	
	def main( s, stdscr ):
		s.stdscr = stdscr
		s.curses_init( stdscr )
		s.game_init()
		s.inpt.eventloop( s.inputhandler, s.wld.proact, s.update )

	def inputhandler( s ):
		c = s.stdscr.getch()
		if c == curses.ascii.ESC: raise EndGame
		s.pl.handle_keypress( c ) or s.inpt.handle_keypress( c )

	def update( s ):
		s.playwin.refresh()
		s.statuswin.refresh()

