Commits

Samuel Jackson committed 258ac66

Rotations are now working.

Comments (0)

Files changed (1)

 	o = {
 			blockArray = {},
 			angle = 1,
-			shapeType = 2,--math.random(7)
+			shapeType = math.random(7),
 			px = 0,
 			py = 0
 		}
 	end
 	
 	buildShapeType(self.blockArray, self.shapeType)
-
-	local c = updateCentrePoint(self.blockArray)
-	self.px = c[1]
-	self.py = c[2]
 end
 
 function Shape:draw()
 	local flag = false
 	for i, v in ipairs (self.blockArray) do
 		if (v.row + 1) > 20 then
-			flag = true
+			return true
 		else
 			flag = false
 		end
 			v:setTableVal(v.coll + 1, v.row)
 		end
 	end
-	
-	local c = updateCentrePoint(self.blockArray)
-	self.px = c[1]
-	self.py = c[2]
 end
 
 function Shape:down()
 	for i,v in ipairs (self.blockArray) do
 		v:setTableVal(v.coll, v.row + 1)
 	end
-	local c = updateCentrePoint(self.blockArray)
-	self.px = c[1]
-	self.py = c[2]
 end
 
 function Shape:rotate()
-		for i,v in ipairs(self.blockArray) do
-			local x2 = (v.y + self.px - self.py)
-			local y2 = (self.px + self.py - v.x - 32)
-			print(x2)
-			print(y2)
-			v:setCoordinates(x2, y2)
+	blocks = self.blockArray
+	t = self.shapeType
+	a = self.angle
+	
+	coll = blocks[1].coll
+	row = blocks[1].row
+	--no need to rotate a square... it has 4 planes of symmetry
+	if t == 2 then
+		--shape is a straight line
+		
+		if a == 1 then
+			--make it go horizontal
+			coll = coll - 2
+			row = row + 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll - 2) and v.row == (blocks[1].row + 2)) or
+				(v.coll == (blocks[2].coll - 2 + 1) and v.row == (blocks[2].row + 2)) or
+				(v.coll == (blocks[3].coll - 2 + 2) and v.row == (blocks[3].row + 2)) or
+				(v.coll == (blocks[4].coll - 2 + 3) and v.row == (blocks[4].row + 2))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll + 3 <= 10) and row + 3 <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll + 1, row)
+				blocks[3]:setTableVal(coll + 2, row)
+				blocks[4]:setTableVal(coll + 3, row)
+				self.angle = 2
+			end
+		elseif a == 2 then
+			--make it go vertical
+			coll = coll + 2
+			row = row - 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll + 2) and v.row == (blocks[1].row - 2)) or
+				(v.coll == (blocks[2].coll + 2) and v.row == (blocks[2].row - 2 + 1)) or
+				(v.coll == (blocks[3].coll + 2) and v.row == (blocks[3].row - 2 + 2)) or
+				(v.coll == (blocks[4].coll + 2) and v.row == (blocks[4].row - 2 + 3))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 or coll + 3 <= 10) and row + 3 <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll, row + 1)
+				blocks[3]:setTableVal(coll, row + 2)
+				blocks[4]:setTableVal(coll, row + 3)
+				self.angle = 1
+			end
+		end
+	elseif t == 3 then
+		--shape is a t shape
+		
+		if a == 1 then
+			row = row - 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll) and v.row == (blocks[1].row - 2)) or
+				(v.coll == (blocks[2].coll) and v.row == (blocks[2].row - 2 + 1)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row - 2 + 2)) or
+				(v.coll == (blocks[4].coll) and v.row == (blocks[4].row))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll + 2 <= 10) and row + 2 <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll, row + 1)
+				blocks[3]:setTableVal(coll, row + 2)
+				blocks[4]:setTableVal(blocks[4].coll, blocks[4].row)
+				self.angle = 2
+			end
+		elseif a == 2 then
+			coll = coll + 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll + 2) and v.row == (blocks[1].row)) or
+				(v.coll == (blocks[2].coll + 2 - 1) and v.row == (blocks[2].row)) or
+				(v.coll == (blocks[3].coll + 2 - 2) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll) and v.row == (blocks[4].row))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll <= 10) and row <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll - 1, row)
+				blocks[3]:setTableVal(coll - 2, row)
+				blocks[4]:setTableVal(blocks[4].coll, blocks[4].row)
+				self.angle = 3
+			end
+		elseif a == 3 then
+			row = row + 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll) and v.row == (blocks[1].row + 2)) or
+				(v.coll == (blocks[2].coll) and v.row == (blocks[2].row + 2 - 1)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row + 2 - 2)) or
+				(v.coll == (blocks[4].coll) and v.row == (blocks[4].row))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll <= 10) and row <= 20 then	
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll, row - 1)
+				blocks[3]:setTableVal(coll, row - 2)
+				blocks[4]:setTableVal(blocks[4].coll, blocks[4].row)
+				self.angle = 4
+			end
+		elseif a == 4 then
+			coll =  coll - 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll - 2) and v.row == (blocks[1].row)) or
+				(v.coll == (blocks[2].coll - 2 + 1) and v.row == (blocks[2].row)) or
+				(v.coll == (blocks[3].coll - 2 + 2) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll) and v.row == (blocks[4].row))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll <= 10) and row <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll + 1, row)
+				blocks[3]:setTableVal(coll + 2, row)
+				blocks[4]:setTableVal(blocks[4].coll, blocks[4].row)
+				self.angle = 1
+			end
 		end
 		
-		--opposite direction
-		-- x2 = (px + py - y1 - q)
-		-- y2 = (x1 + py - px)
+	elseif t == 4 then
+	
+		--shape is a forward L
+		if a == 1 then
+			coll = coll + 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll + 2) and v.row == (blocks[1].row)) or
+				(v.coll == (blocks[2].coll + 2 - 1) and v.row == (blocks[2].row)) or
+				(v.coll == (blocks[3].coll + 2 - 2) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll + 2 - 2) and v.row == (blocks[4].row + 1))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll <= 10) and row <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll - 1, row)
+				blocks[3]:setTableVal(coll - 2, row)
+				blocks[4]:setTableVal(coll - 2, row + 1)
+				self.angle = 2
+			end
+		elseif a == 2 then
+			row = row + 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll) and v.row == (blocks[1].row + 2)) or
+				(v.coll == (blocks[2].coll) and v.row == (blocks[2].row + 2 - 1)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row + 2 - 2)) or
+				(v.coll == (blocks[4].coll - 1) and v.row == (blocks[4].row + 2 - 2))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll <= 10) and row <= 20 then	
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll, row - 1)
+				blocks[3]:setTableVal(coll, row - 2)
+				blocks[4]:setTableVal(coll - 1, row - 2)
+				self.angle = 3
+			end
+		elseif a == 3 then
+			coll =  coll - 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll - 2) and v.row == (blocks[1].row)) or
+				(v.coll == (blocks[2].coll - 2 + 1) and v.row == (blocks[2].row)) or
+				(v.coll == (blocks[3].coll - 2 + 2) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll - 2 + 2) and v.row == (blocks[4].row - 1))
+				then
+					return false
+				end
+			end
+			if (coll >= 1 and coll <= 10) and row <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll + 1, row)
+				blocks[3]:setTableVal(coll + 2, row)
+				blocks[4]:setTableVal(coll + 2, row - 1)
+				self.angle = 4
+			end
+		elseif a == 4 then
+			row = row - 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll) and v.row == (blocks[1].row - 2)) or
+				(v.coll == (blocks[2].coll) and v.row == (blocks[2].row - 2 + 1)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row - 2 + 2)) or
+				(v.coll == (blocks[4].coll + 1) and v.row == (blocks[4].row - 2 + 2))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll + 2 <= 10) and row + 2 <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll, row + 1)
+				blocks[3]:setTableVal(coll, row + 2)
+				blocks[4]:setTableVal(coll + 1, row + 2)
+				self.angle = 1
+			end
+		end
+	elseif t == 5 then
+		--shape is a backward L
+		if a == 1 then
+			coll = coll + 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll + 2) and v.row == (blocks[1].row)) or
+				(v.coll == (blocks[2].coll + 2 - 1) and v.row == (blocks[2].row)) or
+				(v.coll == (blocks[3].coll + 2 - 2) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll + 2) and v.row == (blocks[4].row + 1))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll <= 10) and row <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll - 1, row)
+				blocks[3]:setTableVal(coll - 2, row)
+				blocks[4]:setTableVal(coll, row + 1)
+				self.angle = 2
+			end
+		elseif a == 2 then
+			row = row + 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll) and v.row == (blocks[1].row + 2)) or
+				(v.coll == (blocks[2].coll) and v.row == (blocks[2].row + 2 - 1)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row + 2 - 2)) or
+				(v.coll == (blocks[4].coll - 1) and v.row == (blocks[4].row + 2))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll <= 10) and row <= 20 then	
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll, row - 1)
+				blocks[3]:setTableVal(coll, row - 2)
+				blocks[4]:setTableVal(coll - 1, row)
+				self.angle = 3
+			end
+		elseif a == 3 then
+			coll =  coll - 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll - 2) and v.row == (blocks[1].row)) or
+				(v.coll == (blocks[2].coll - 2 + 1) and v.row == (blocks[2].row)) or
+				(v.coll == (blocks[3].coll - 2 + 2) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll - 2) and v.row == (blocks[4].row - 1))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll <= 10) and row <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll + 1, row)
+				blocks[3]:setTableVal(coll + 2, row)
+				blocks[4]:setTableVal(coll, row - 1)
+				self.angle = 4
+			end
+		elseif a == 4 then
+			row = row - 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll) and v.row == (blocks[1].row - 2)) or
+				(v.coll == (blocks[2].coll) and v.row == (blocks[2].row - 2 + 1)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row - 2 + 2)) or
+				(v.coll == (blocks[4].coll + 1) and v.row == (blocks[4].row))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll + 2 <= 10) and row + 2 <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll, row + 1)
+				blocks[3]:setTableVal(coll, row + 2)
+				blocks[4]:setTableVal(coll + 1, row)
+				self.angle = 1
+			end
+		end
+	elseif t == 6 then
+		if a == 1 then
+			coll = coll + 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll + 2) and v.row == (blocks[1].row)) or
+				(v.coll == (blocks[2].coll + 2 - 1) and v.row == (blocks[2].row)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll + 2 - 2) and v.row == (blocks[4].row + 1))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll <= 10) and row <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll - 1, row)
+				blocks[3]:setTableVal(blocks[3].coll, blocks[3].row)
+				blocks[4]:setTableVal(coll - 2, row + 1)
+				self.angle = 2
+			end
+		elseif a == 2 then
+			row = row + 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll) and v.row == (blocks[1].row + 2)) or
+				(v.coll == (blocks[2].coll) and v.row == (blocks[2].row + 2 - 1)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll - 1) and v.row == (blocks[4].row + 2 - 2))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll <= 10) and row <= 20 then	
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll, row - 1)
+				blocks[3]:setTableVal(blocks[3].coll, blocks[3].row)
+				blocks[4]:setTableVal(coll - 1, row - 2)
+				self.angle = 3
+			end
+		elseif a == 3 then
+			coll =  coll - 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll - 2) and v.row == (blocks[1].row)) or
+				(v.coll == (blocks[2].coll - 2 + 1) and v.row == (blocks[2].row)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll - 2 + 2) and v.row == (blocks[4].row - 1))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll <= 10) and row <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll + 1, row)
+				blocks[3]:setTableVal(blocks[3].coll, blocks[3].row)
+				blocks[4]:setTableVal(coll + 2, row - 1)
+				self.angle = 4
+			end
+		elseif a == 4 then
+			row = row - 2
+			
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll) and v.row == (blocks[1].row - 2)) or
+				(v.coll == (blocks[2].coll) and v.row == (blocks[2].row - 2 + 1)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll + 1) and v.row == (blocks[4].row - 2 + 2))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll + 2 <= 10) and row + 2 <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll, row + 1)
+				blocks[3]:setTableVal(blocks[3].coll, blocks[3].row)
+				blocks[4]:setTableVal(coll + 1, row + 2)
+				self.angle = 1
+			end
+		end
+	elseif t == 7 then
+		if a == 1 then
+			row = row + 2
+						
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll) and v.row == (blocks[1].row + 2)) or
+				(v.coll == (blocks[2].coll - 1) and v.row == (blocks[2].row + 2)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll - 2) and v.row == (blocks[4].row + 2 - 1))
+				then
+					return false
+				end
+			end
+			
+			if (coll >= 1 and coll + 2 <= 10) and row + 2 <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll - 1, row)
+				blocks[3]:setTableVal(blocks[3].coll, blocks[3].row)
+				blocks[4]:setTableVal(coll - 2, row - 1)
+				self.angle = 2
+			end
+		elseif a == 2 then
+			coll = coll - 2
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll - 2) and v.row == (blocks[1].row)) or
+				(v.coll == (blocks[2].coll - 2) and v.row == (blocks[2].row - 1)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll - 2 + 1) and v.row == (blocks[4].row - 2))
+				then
+					return false
+				end
+			end
+			if (coll >= 1 and coll + 2 <= 10) and row + 2 <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll, row - 1)
+				blocks[3]:setTableVal(blocks[3].coll, blocks[3].row)
+				blocks[4]:setTableVal(coll + 1, row - 2)
+				self.angle = 3
+			end
+		elseif a == 3 then
+			row = row - 2
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll) and v.row == (blocks[1].row - 2)) or
+				(v.coll == (blocks[2].coll + 1) and v.row == (blocks[2].row - 2)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll + 2) and v.row == (blocks[4].row - 2 + 1))
+				then
+					return false
+				end
+			end
+			if (coll >= 1 and coll + 2 <= 10) and row + 2 <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll + 1, row)
+				blocks[3]:setTableVal(blocks[3].coll, blocks[3].row)
+				blocks[4]:setTableVal(coll + 2, row + 1)
+				self.angle = 4
+			end
+		elseif a == 4 then
+			coll = coll + 2
+			for i,v in ipairs (DrawList) do
+				if 
+				(v.coll == (blocks[1].coll + 2) and v.row == (blocks[1].row)) or
+				(v.coll == (blocks[2].coll + 2) and v.row == (blocks[2].row + 1)) or
+				(v.coll == (blocks[3].coll) and v.row == (blocks[3].row)) or
+				(v.coll == (blocks[4].coll + 2 - 1) and v.row == (blocks[4].row + 2))
+				then
+					return false
+				end
+			end
+			if (coll >= 1 and coll + 2 <= 10) and row + 2 <= 20 then
+				blocks[1]:setTableVal(coll, row)
+				blocks[2]:setTableVal(coll, row + 1)
+				blocks[3]:setTableVal(blocks[3].coll, blocks[3].row)
+				blocks[4]:setTableVal(coll - 1, row + 2)
+				self.angle = 1
+			end
+		end
+	end
 end
 
 function buildShapeType(blocks, t)
 		blocks[4]:setTableVal(coll - 1, row + 2)
 	end
 
-end
-
-function updateCentrePoint(blocks)
-	local bigX = 0
-	local	bigY = 0
-	local smallX = 0
-	local smallY = 0
-	
-	--calc x and y
-	for i, v in ipairs(blocks) do
-		bigX = math.max(bigX, v.x + 32)
-		bigY = math.max(bigY, v.y + 32)
-		smallX = math.min(smallX, v.x)
-		smallY = math.min(smallY, v.y)
-	end
-	
-	local px = ((bigX - smallX) / 2) + smallX
-	local py = ((bigY - smallY) / 2) + smallY
-	return {px, py}
 end