#Twisted Shootout
#Copyright (C) 2007  Han Dao
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see .

#You can contact the author at wikipediankiba@gmail.com
import pygame

class MapLaw:
	def __init__(self,maposition):
		self.position = maposition
		self.p = self.position.p
		self.w = self.position.e
		self.i = self.position.i
		self.action = False
		self.xprove = False
		self.yprove = False
		self.ystate = False
		self.xstate = False
	def load(self,collide):
		self.collide = collide
		self.collide.add(self.position.mapobj,self.position.i)
	def compute(self):
		self.bullet()
		self.items()
		self.xy_move()
		self.down()
		self.action = False
		self.xprove = False
		self.yprove = False
	#Player + Map Collision
	def check_x(self,x):
		#RIGHT
		if self.p.rect.x > (x + 40):
			self.xstate = 1	
			self.xprove = True
		#LEFT
		elif self.p.rect.x < (x + 40):
			self.xstate = 2
			self.xprove = True
	def check_y(self,y):
		#UP
		if self.p.rect.y - y < 0:
			self.ystate = 1	
			self.yprove = True
		#DOWN
		elif self.p.rect.y > (y + 10):
			self.ystate = 2
			self.yprove = True
	def xy_move(self):
		#player & map object collided if x and y does not return False
		#x and y are the location of the map object the player collided with. x is the most left and y is the most upper positions of the map object.
		x , y = self.collide.map_collide()
		if y != False:
			self.check_x(x)
			self.check_y(y)
			if self.xprove == True:
				if self.xstate == 1:
					self.right(x)
				elif self.xstate == 2:
					self.left(x)
			if self.yprove == True:
				if self.ystate == 1:
					self.over(y)
				elif self.ystate == 2:
					self.under(y)
					
	#MOVE LEFT EVENT
	#50, the length of the player is used to move the player backward instead of 80.
	#50 and 80 are used so the player is right next to the object.
	def left(self,x):
		self.p.rect.x = x - 50
	#MOVE RIGHT EVENT
	#40 is the value that is halfway from beginning of x to the end of x
	#80 is the length of the box used in moving the player forward 
	def right(self,x):
		self.p.rect.x = x + 80
	#MOVE UP EVENT
	#This part deal with player object on top of the box. Remember that y start at top with lower number and then end at bottom with a greater number.
	#50 is the length of the player used in moving the player up
	def over(self,y):
		self.p.rect.y = y - 50
	#MOVE DOWN EVENT
	#This part deal with the event that the player is under the box.
	#60 is the length of the box used in moving the player down.
	def under(self,y):
		self.p.rect.y = y + 60
		self.p.status = False
		self.p.trust = 0
	def down(self):
		if self.p.rect.y < 550:
			if self.p.status == False:
				self.p.rect.y = self.p.rect.y + 5
				x, y = self.collide.map_collide()
				if y != False:
					self.p.rect.y = self.p.rect.y - 5
		if self.p.rect.y > 550:
			self.p.rect.y = 550
	def bullet(self):
		self.collide.bulletobj()
	def items(self):
		n = self.collide.itemsobj()
		if n == -1:
			return
		self.i.sprites.remove(self.i.items[n])
		self.i.items.pop(n)