Przeglądaj źródła

Game is done apparently;

bjorn 8 lat temu
rodzic
commit
0460a7da50
2 zmienionych plików z 84 dodań i 16 usunięć
  1. 32 12
      client.lua
  2. 52 4
      server.lua

+ 32 - 12
client.lua

@@ -102,16 +102,18 @@ function client:update(dt)
 
 		if self.dueling > 0 and self.duelTimer > 0 then
 			self.duelTimer = math.max(self.duelTimer - dt, 0)
-			local tx, ty, tz, angle, slotX, slotY, slotZ = self:getDuelZones()
-			local x, y, z = self.controllers[2]:getPosition()
-			print(tx, ty, tz, x, y, z, math.sqrt((slotX - x) ^ 2 + (slotY - y) ^ 2 + (slotZ - z) ^ 2), .1)
-			if math.sqrt((slotX - x) ^ 2 + (slotY - y) ^ 2 + (slotZ - z) ^ 2) < .1 then
-				if not self.duelHover then
-					self.duelHover = true
-					self.controllers[2]:vibrate(.002)
+			if self.cardGrab.active then
+				local tx, ty, tz, angle, slotX, slotY, slotZ = self:getDuelZones()
+				local x, y, z = self.controllers[2]:getPosition()
+				print(tx, ty, tz, x, y, z, math.sqrt((slotX - x) ^ 2 + (slotY - y) ^ 2 + (slotZ - z) ^ 2), .1)
+				if math.sqrt((slotX - x) ^ 2 + (slotY - y) ^ 2 + (slotZ - z) ^ 2) < .1 then
+					if not self.duelHover then
+						self.duelHover = true
+						self.controllers[2]:vibrate(.002)
+					end
+				else
+					self.duelHover = false
 				end
-			else
-				self.duelHover = false
 			end
 		end
 
@@ -328,6 +330,7 @@ function client:draw()
 						lovr.graphics.push()
 						lovr.graphics.translate(x, y, z)
 						lovr.graphics.rotate(-math.pi / 2, 1, 0, 0)
+						lovr.graphics.rotate(angle, 0, 1, 0)
 						self:drawCard(player, i, 0, 0, 0, .5)
 						lovr.graphics.pop()
 					elseif (player.id == self.id and self.cardGrab.active and self.cardGrab.card == i) or (player.grabbedCard == i) then
@@ -437,7 +440,7 @@ function client:drawCard(player, cardIndex, ...)
 	local card = player.cards[cardIndex]
 	if card.position <= 0 then return end
 
-	if player.id == self.id or cardIndex == player.grabbedCard then
+	if player.id == self.id or cardIndex == player.grabbedCard or (self.dueling == player.id and self.duelChoice > 0 and player.duelChoice > 0) then
 		lovr.graphics.setColor(255, 255, 255)
 	else
 		lovr.graphics.setColor(0, 0, 0)
@@ -480,7 +483,7 @@ function client:controllerpressed(controller, button)
 		self.emoji.transform:origin()
 		self.emoji.transform:translate(self.emoji.position:unpack())
 		self.emoji.transform:rotate(unpack(self.emoji.orientation))
-	elseif controller == self.controllers[2] and button == 'trigger' then
+	elseif controller == self.controllers[2] and button == 'trigger' and not (self.dueling > 0 and self.duelChoice > 0) then
 		local minCard, minDis, x, y, z, angle, ax, ay, az = self:getClosestCard()
 		if minCard and minDis < .075 then
 			self.cardGrab.active = true
@@ -743,7 +746,24 @@ function client.messages.server.duel(self, data)
 end
 
 function client.messages.server.outcome(self, data)
-	--
+	local p1 = self.players[data.first]
+	local p2 = self.players[data.second]
+
+	if p1 then
+		p1.stars = data.firstStars
+		p1.cards = data.firstCards
+	end
+
+	if p2 then
+		p2.stars = data.secondStars
+		p2.cards = data.secondCards
+	end
+
+	if p1.id == self.id or p2.id == self.id then
+		self.dueling = 0
+		self.duelChoice = 0
+		self.duelTimer = 0
+	end
 end
 
 return client

+ 52 - 4
server.lua

@@ -58,6 +58,8 @@ function server:update(dt)
 							p2.dueling = i
 							p1.duelTimer = 30
 							p2.duelTimer = 30
+							p1.duelOutcomeTimer = 1
+							p2.duelOutcomeTimer = 1
 							p1.proposition = 0
 							p2.proposition = 0
 							p1.duelChoice = 0
@@ -68,10 +70,55 @@ function server:update(dt)
 				end
 			end
 
-			if self.players[i].duelTimer > 0 then
-				self.players[i].duelTimer = math.max(self.players[i].duelTimer - dt, 0)
-				if self.players[i].duelTimer == 0 then
-					-- Timeout, send outcome message
+			if self.players[i].dueling > 0 then
+				local p1 = self.players[i]
+				local p2 = self.players[p1.dueling]
+
+				if p1.duelChoice > 0 and p2.duelChoice > 0 then
+					p1.duelTimer = 0
+					p2.duelTimer = 0
+					p1.duelOutcomeTimer = math.max(p1.duelOutcomeTimer - dt, 0)
+
+					if p1.duelOutcomeTimer == 0 then
+						p1.dueling = 0
+						p2.dueling = 0
+						p1.cards[p1.duelChoice].position = 0
+						p2.cards[p2.duelChoice].position = 0
+
+						-- Figure out if someone won
+						local p1Type = p1.cards[p1.duelChoice].type
+						local p2Type = p2.cards[p2.duelChoice].type
+						if (p1Type == 2 and p2Type == 1) or (p1Type == 1 and p2Type == 3) or (p1Type == 3 and p2Type == 2) then
+							p1.stars = p1.stars + 1
+							p2.stars = p2.stars - 1
+						elseif (p2Type == 2 and p1Type == 1) or (p2Type == 1 and p1Type == 3) or (p2Type == 3 and p1Type == 2) then
+							p1.stars = p1.stars - 1
+							p2.stars = p2.stars + 1
+						end
+
+						-- Tell everyone about the changes
+						self:broadcast('outcome', { first = i, second = p1.dueling, firstCards = p1.cards, secondCards = p2.cards, firstStars = p1.stars, secondStars = p2.stars })
+					end
+				end
+
+				if p1.duelTimer > 0 then
+					p1.duelTimer = math.max(p1.duelTimer - dt, 0)
+					if p1.duelTimer == 0 then
+						p1.dueling = 0
+						p2.dueling = 0
+
+						if p1.duelChoice > 0 and p2.duelChoice == 0 then
+							p1.cards[p1.duelChoice].position = 0
+							p1.stars = p1.stars + 1
+							p2.stars = p2.stars - 1
+						elseif p2.duelChoice > 0 and p1.duelChoice == 0 then
+							p2.cards[p2.duelChoice].position = 0
+							p2.stars = p2.stars + 1
+							p1.stars = p1.stars - 1
+						end
+
+						self:broadcast('outcome', { first = i, second = p1.dueling, firstCards = p1.cards, secondCards = p2.cards, firstStars = p1.stars, secondStars = p2.stars })
+					end
 				end
 			end
 		end
@@ -191,6 +238,7 @@ function server:createPlayer(peer)
 		proposition = 0,
 		dueling = 0,
 		duelTimer = 0,
+		duelOutcomeTimer = 0,
 		duelChoice = 0
   }