import GameLogic as G
import Rasterizer as R

###########################	Logic Bricks

# Get controller
cont = G.getCurrentController()

# Get sensor named Mouse
mouse = cont.getSensor("Mouse")
		 
# Get the actuators
rotLeftRight = cont.getActuator("LookLeftRight")
rotUpDown = cont.getActuator("LookUpDown")	


############################## Need the size of the game window


width = R.getWindowWidth()
height = R.getWindowHeight()
			 

############################### Get the mouse movement

def mouseMove():
			
		# distance moved from screen center			 
		x = width/2 - mouse.getXPosition() 
		y = height/2 - mouse.getYPosition()
		 
		# intialize mouse so it doesn't jerk first time
		if hasattr(G, 'init') == False:
				x = 0
				y = 0
				G.init = True
	 
		return (x, y)

pos = mouseMove()

		 
	######## Figure out how much to rotate camera and player ########
	#

scene = G.getCurrentScene()
camera = scene.active_camera
camPos = camera.getPosition()


	# Mouse sensitivity
sensitivity = 0.005

	# Amount, direction and sensitivity
leftRight = pos[0] * sensitivity
upDown = pos[1] * sensitivity

oldLeftRight = 0
oldUpDown = 0

	#smooth out movement
leftRight = (oldLeftRight*0.9 + leftRight*0.1)
upDown = (oldUpDown*0.9 + upDown*0.1)


oldUpDown=upDown
oldLeftRight=leftRight

	######### Use actuators to rotate camera and player #############

# Set the rotation values
rotLeftRight.setDRot( 0.0, 0.0, leftRight, False)	 
rotUpDown.setDRot( upDown, 0.0, 0.0, True)

# Use them


G.addActiveActuator(rotLeftRight, True)
G.addActiveActuator(rotUpDown, True)


############# Center mouse pointer in game window ###############

# Center mouse in game window
# when mouselook is active, always want to hide mouse
R.setMousePosition(width/2, height/2)
R.showMouse(0)


camera.setPosition( [camPos[0], camPos[1], camPos[2] ] )