import sys
import time
clock = time.clock

MWPYGameObjectType = {
				  'MWGOT_SIMPLE' : 0
				, 'MWGOT_ACTOR' : 1
				, 'MWGOT_BRIDGE' : 2
				, 'MWGOT_LIFT' : 3
				, 'MWGOT_PORTAL' : 4
				, 'MWGOT_TELEPORT' : 5
				, 'MWGOT_WALL' : 6
				, 'MWGOT_MONSTR' : 7
				, 'MWGOT_OUTTELEPORT' : 8
				, 'MWGOT_LETHAL' : 9
				, 'MWGOT_GROUP' : 10
				, 'MWGOT_COLLECT' : 11
				, 'MWGOT_WATER' : 12
				, 'MWGOT_PIPELINE' : 13
				, 'MWGOT_PIPELINEGROUP' : 14
				, 'MWGOT_WINDLEFT' : 15
				, 'MWGOT_WINDRIGHT' : 16
				, 'MWGOT_SPRING' : 17
				, 'MWGOT_SWITCH' : 18
				, 'MWGOT_STONE' : 19
				, 'MWGOT_GATE' : 20
				, 'MWGOT_FUNNEL' : 21
				, 'MWGOT_DOOR' : 22
				, 'MWGOT_BUTTON' : 23
				, 'MWGOT_FOUNTAIN' : 24                                  
				}

MWPYGameObjectTypeStr = {
				  0 : 'MWGOT_SIMPLE'
				, 1 : 'MWGOT_ACTOR'
				, 2 : 'MWGOT_BRIDGE'
				, 3 : 'MWGOT_LIFT'
				, 4 : 'MWGOT_PORTAL'
				, 5 : 'MWGOT_TELEPORT'
				, 6 : 'MWGOT_WALL'
				, 7 : 'MWGOT_MONSTR'
				, 8 : 'MWGOT_OUTTELEPORT'
				, 9 : 'MWGOT_LETHAL'
				, 10 : 'MWGOT_GROUP'
				, 11 : 'MWGOT_COLLECT'
				, 12 : 'MWGOT_WATER'
				, 13 : 'MWGOT_PIPELINE'
				, 14 : 'MWGOT_PIPELINEGROUP'
				, 15 : 'MWGOT_WINDLEFT'
				, 16 : 'MWGOT_WINDRIGHT'
				, 17 : 'MWGOT_SPRING'
				, 18 : 'MWGOT_SWITCH'
				, 19 : 'MWGOT_STONE'
				, 20 : 'MWGOT_GATE'
				, 21 : 'MWGOT_FUNNEL:'
				, 22 : 'MWGOT_DOOR'
				, 23 : 'MWGOT_BUTTON'
				, 24 : 'MWGOT_FOUNTAIN'                                  
				}

MWPYGameObjectState = {'AS_LEFT'  :  0
			  ,'AS_RIGHT' :  1
			  ,'AS_UP'	:  2
			  ,'AS_DOWN'  :  3
			  ,'AS_NONE'  :  4
			  ,'AS_PLAY'  :  5
			  ,'AS_OPEN'  :  6
			  ,'AS_CLOSE'  :  7
			  }


MWPYGameObjectStateStr = {0 : 'AS_LEFT'
			 ,1 : 'AS_RIGHT'
			 ,2 : 'AS_UP'
			 ,3 : 'AS_DOWN'
			 ,4 : 'AS_NONE'
			 ,5 : 'AS_PLAY'
			 ,6 : 'AS_OPEN'
			 ,7 : 'AS_CLOSE'                          
			 }


def getGameObjectStateStr(state):
	return MWPYGameObjectStateStr[state]

def getGameObjectTypeStr(gotype):
	return MWPYGameObjectTypeStr[gotype]

#constants
ScrW = 896.0
ScrH = 732.0
InvScrW = 1.0 / ScrW
InvScrH = 1.0 / ScrH
DblScrW = 2.0 * ScrW
DblScrH = 2.0 * ScrH
HalfInvScrW = 0.5 / ScrW
HalfInvScrH = 0.5 / ScrH


# main game object
# attributes:
#		x - x-position [double]
#		y - y-position [double]
#		w - half width [double]
#		h - half heigth [double]
#		state - game object state [int]
#		is_enable - processing and visualisation flag [Boolean]
#		gravity_action - apply gravity force flag [Boolean]
#		name - game object name [string]
#		vel_x - velocity X
#		vel_y - velocity Y
#		mass - body mass
#		states_info - store states and sprites for them
#		ScrW   - screen width
#		ScrH   - screen height
#		is_static - if static then not collision detection proceed with other static objects
#		time_delay - update and render delay
#		start_time - game object life time for delay effect
#		frc_x - X force accumulator
#		frc_y - y force accumulator
class MWPYGameObject:
	def __init__(self, gameObjectType = MWPYGameObjectType['MWGOT_SIMPLE']):
		self.gameObjectType = gameObjectType
		self.x = 0.0
		self.y = 0.0
		self.w = 0.0
		self.h = 0.0
		self.state = MWPYGameObjectState['AS_NONE']
		self.is_enable = True
		self.gravity_action = False
		self.name = ""
		self.vel_x = 0.0
		self.vel_y = 0.0
		self.mass = 0.0
		self.states_info = {}
		self.is_static = False
		self.time_delay = 0
		self.start_time = clock()
		self.frc_x = 0.0
		self.frc_y = 0.0
		self.is_actor = False
		self.is_monstr = False
		self.life_time = 0
		self.id = 0
		self.prev_state = []
		self.is_portal = False

	def reset_force_acc(self):
		self.frc_x = 0.0
		self.frc_y = 0.0

	def time_delay_done(self):
		return ((clock() - self.start_time) > self.time_delay)

	def enable(self):
		self.is_enable = True

	def disable(self):
		self.life_time = clock() - self.start_time
		self.is_enable = False

	def can_render(self):
		return self.time_delay_done()

	def can_update(self):
		return self.time_delay_done()

	def do_update(self, delta, currenttime, world_dict):
		return None

	def update(self, delta, currenttime, world_dict):
##		if(self.mass > 0):
##			if(self.gravity_action == True):
##				self.frc_y += 0.98 * self.mass  
##			self.vel_x += delta * self.frc_x / self.mass
##			self.vel_y += delta * self.frc_y / self.mass
##			self.x += delta * self.vel_x
##			self.y += delta * self.vel_y
		self.do_update(delta, currenttime, world_dict)
		life_time += delta


	def reset(self):
		return None

	def is_actor(self):
		return False

	def add_state_sprite(self, state, sprite_info):
		contains = state in self.states_info
		if (contains != True):
			self.states_info[state] = []
		self.states_info[state].append(sprite_info)

	def update_sprites_state(self, state, offset_x, offset_y):
		pos_x = round( (self.x + offset_x) * ScrW) * InvScrW
		pos_y = round( (self.y + offset_y) * ScrH) * InvScrH
		#bridge can change width
		width = round(self.w * DblScrW) * InvScrW
		height = round(self.h * DblScrH) * InvScrH
		if(state in self.states_info):
			sprite_list = self.states_info[state]
			for si in sprite_list:
				si.posx = pos_x + width * (1.0 - si.x_scale_factor)- HalfInvScrW
				si.posy = pos_y - HalfInvScrH
				si.width = width * si.x_scale_factor
				si.height = height
			return sprite_list
		return []




