Commits

Samuel Jackson  committed 90040ba

Block physics now fully working corretly. Continuing to work on rotation.

  • Participants
  • Parent commits c82160c

Comments (0)

Files changed (3)

 	self.x = (coll - 1) * 32
 	self.y = (row - 1) * 32
 end
+
+function Block:setCoordinates(x, y)
+	self.coll = (x * 32) + 1
+	self.row = (y * 32) + 1
+	self.x = x
+	self.y = y
+end
 	end
 	
 	if e == "down" then
-		local diff = checkHighestBlock()
-		currentBlock:moveToBottom(diff)
-		makeNewBlock()
+		local flag = false
+		while flag == false do
+			if currentBlock:checkHitFloor() == false and checkHitBlock() == false then
+				currentBlock:down()
+			else
+				makeNewBlock()
+				flag = true
+			end
+		end
 	end
 	
 	if e == "up" then
 	return false
 end
 
-function checkHighestBlock()
-	local highestBlock = 20
-	local lowestShapeBlock = 0
-	local hBlock
-	local diff
-	
-	for i,v in ipairs(DrawList) do
-		for j,w in ipairs(currentBlock.blockArray) do
-			if w.coll == v.coll and v.row >= w.row then
-				highestBlock = math.min(v.row, highestBlock)
-				hBlock = v
-			end
-		end
-	end
-	
-	if highestBlock < 20 then
-		for i,v in ipairs(currentBlock.blockArray) do
-			if v.coll == hBlock.coll and hBlock.row >= v.row  then
-				lowestShapeBlock = math.max(v.row, lowestShapeBlock)
-			end
-		end
-	else
-		for i,v in ipairs(currentBlock.blockArray) do
-			lowestShapeBlock = math.max(v.row, lowestShapeBlock)
-		end
-	end
-	
-	diff = highestBlock - lowestShapeBlock
-	
-	if highestBlock < 20 then
-		diff = diff -1
-	end
-	
-	return diff
-end
 
 function makeNewBlock()
 	for i,v in ipairs(currentBlock.blockArray) do
 	o = {
 			blockArray = {},
 			angle = 1,
-			shapeType = math.random(7)
+			shapeType = 2,--math.random(7)
+			px = 0,
+			py = 0
 		}
 	setmetatable(o, self)
     self.__index = self
 	end
 	
 	buildShapeType(self.blockArray, self.shapeType)
-	
+
+	local c = updateCentrePoint(self.blockArray)
+	self.px = c[1]
+	self.py = c[2]
 end
 
 function Shape:draw()
 		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
-end
-
-function Shape:moveToBottom(diff)
-	for i,v in ipairs(self.blockArray) do
-		v:setTableVal(v.coll, v.row + diff)
-	end
+	local c = updateCentrePoint(self.blockArray)
+	self.px = c[1]
+	self.py = c[2]
 end
 
 function Shape:rotate()
-	local t = self.shapeType
-	local a = self.angle
-	--note there is no code for rotating a cube as it would be the same all the time.
-	if t == 2 then
-		if a == 1 or a == 3 then
-			--shape is vertical
-			for i,v in ipairs(self.blockArray) do
-				v:setTableVal(v.row, v.coll)
-			end
-		elseif a == 2 or a == 4 then
-			--shape is horizontal
+		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)
 		end
-	elseif t == 3 then
-	end
+		
+		--opposite direction
+		-- x2 = (px + py - y1 - q)
+		-- y2 = (x1 + py - px)
 end
 
 function buildShapeType(blocks, t)
-	blocks[1]:setTableVal(5, -4)
+	blocks[1]:setTableVal(5, -3)
+	local coll = blocks[1].coll
+	local row = blocks[1].row
 	if t == 1 then
 		--shape is a cube
-		coll = blocks[1].coll
-		row = blocks[1].row
-		
 		blocks[2]:setTableVal(coll + 1, row)
 		blocks[3]:setTableVal(coll, row + 1)
 		blocks[4]:setTableVal(coll + 1, row + 1)
 
 	elseif t == 2 then
 		--shape is a straight line
-		coll = blocks[1].coll
-		row = blocks[1].row
-		
 		blocks[2]:setTableVal(coll, row + 1)
 		blocks[3]:setTableVal(coll, row + 2)
 		blocks[4]:setTableVal(coll, row + 3)
+		
 	elseif t == 3 then
 		--shape is a t shape
-		coll = blocks[1].coll
-		row = blocks[1].row
-		
 		blocks[2]:setTableVal(coll + 1, row)
 		blocks[3]:setTableVal(coll + 2, row)
 		blocks[4]:setTableVal(coll + 1, row - 1)
+		
 	elseif t == 4 then
 		--shape is a forward L
-		coll = blocks[1].coll
-		row = blocks[1].row
-		
 		blocks[2]:setTableVal(coll, row + 1)
 		blocks[3]:setTableVal(coll, row + 2)
 		blocks[4]:setTableVal(coll + 1, row + 2)
+		
 	elseif t == 5 then
 		--shape is a backwards L
-		coll = blocks[1].coll
-		row = blocks[1].row
-		
 		blocks[2]:setTableVal(coll, row + 1)
 		blocks[3]:setTableVal(coll, row + 2)
 		blocks[4]:setTableVal(coll + 1, row)
+		
 	elseif t == 6 then
 		--shape is a forward Z
-		coll = blocks[1].coll
-		row = blocks[1].row
-		
 		blocks[2]:setTableVal(coll, row + 1)
 		blocks[3]:setTableVal(coll + 1, row + 1)
 		blocks[4]:setTableVal(coll + 1, row + 2)
+		
 	elseif t == 7 then
 		--shape is a backwards Z
-		coll = blocks[1].coll
-		row = blocks[1].row
-		
 		blocks[2]:setTableVal(coll, row + 1)
 		blocks[3]:setTableVal(coll - 1, row + 1)
 		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