def applyInputs(self, entryIndex): W, H = glfwGetWindowSize() mx, my = glfwGetMousePos() my = -my if (mx, my + H) != self.cursorPos: if mx <= 0: mx = 0 if mx >= W: mx = W if my >= 0: my = 0 if my <= -H: my = -H self.cursorPos = (mx, my + H) glfwSetMousePos(mx, -my) #if mousebutton1 is pressed, enter selected menu W, H = glfwGetWindowSize() menuY = H / 2 + 50 * self.entryFontScale for i in range(len(self)): pos = i + 1 string = self[i][0] #vertical positioning will work fine only with one height for all entries x0 = W / 2 - self.font_aa.getSize(string)[0] * self.entryFontScale / 2 y0 = menuY - pos * self.font_aa.getSize(string)[1] * self.entryFontScale #+ self.entryLineSpacing x1 = x0 + self.font_aa.getSize(string)[0] * self.entryFontScale y1 = y0 + self.entryLineSpacing - self.font_aa.getSize(string)[1] * self.entryFontScale if vectormath.isPointInSquare((self.cursorPos[0], 0, self.cursorPos[1]), (x0, y1), (x1, y0)): self[i][1] = True if glfwGetMouseButton(GLFW_MOUSE_BUTTON_1) == GLFW_PRESS\ and glfwGetMouseButton(GLFW_MOUSE_BUTTON_1) == GLFW_RELEASE: sounds.playSound("Menu", (0,0,0)) selection = self[i][0] gameglobals.gameState = self[i][2] self.setSelectionIndex(selection, entryIndex)
def getMovement(self): dx, dy, dz, rotationAngle = (0, 0, 0, 0) #move to ball/square border intersection currentPosition = self.body.getPosition() target = self.target.getPosition() if (target[0]-currentPosition[0]) * (target[0]-currentPosition[0]) + \ (target[2]-currentPosition[2]) * (target[2]-currentPosition[2]) < 2.0 and \ currentPosition[1]<1.1: dy = 2.0 if vectormath.isPointInSquare(target, self.min, self.max): targetPosition = target else: targetPosition = vectormath.getBallAndSquareIntersection( target, self.target.getLinearVel(), self.min, self.max) targetPosition = (targetPosition[0] + random() - 0.5, targetPosition[1], targetPosition[2] + random() - 0.5) vectorAB = vectormath.getVector(currentPosition, targetPosition) #to make oponents run to ball coords - self length vABAngle = atan2(vectorAB[2], vectorAB[0]) vABMagnitude = vectormath.getMagnitudeV(vectorAB) newVABMagnitude = vABMagnitude - 2.0 #malas garums/2 newVABX = [ cos(vABAngle) * newVABMagnitude, vectorAB[1], sin(vABAngle) * newVABMagnitude ] vectorAB = newVABX if vABMagnitude < 1.8: moveSpeed = 700 * self.body.getMass().mass else: moveSpeed = 4000 * self.body.getMass().mass currentRotationTuple = self.body.getRotation() currentRotation = currentRotationTuple[2], currentRotationTuple[ 5], currentRotationTuple[8] vectorAB = vectormath.normalize(vectorAB[0], 0, vectorAB[2]) rotationAngle = vectormath.getAngleBetweenVectors( currentRotation, vectorAB) currentRotation = vectormath.negate(currentRotation) dx = currentRotation[0] dz = currentRotation[2] #/------------------ return dx * moveSpeed, dy * moveSpeed, dz * moveSpeed, rotationAngle * moveSpeed
def registerPlayerEvent(self): ball = self.ball for object in self.objectList: if 'Player' in object.name: player = object #ball + player sounds.playSound("BallPlayer", ball.geom.getPosition()) lastTouchedObject = self.events['lastTouchedObject'] if lastTouchedObject == None: #picked from middle line self.scoreBoard.resetJoinedCombo() #clear combo self.scoreBoard.incrementCombo(player, self.hitMsgList) #(+1) else: #picked from ground or player if lastTouchedObject.name == 'Ground': #picked from ground inside self.scoreBoard.resetJoinedCombo() playerName = player.name self.scoreBoard.incrementCombo(player, self.hitMsgList) if (self.events['lastFieldOwner'] == player)\ and (self.events['lastTouchedPlayer'] == player)\ and vectormath.isPointInSquare(ball.geom.getPosition() , self.players[playerName].min , self.players[playerName].max): #critical event. double-touched -> fault if playerName == "Player": pl = self.players[[ "Player_Red", "Player_Green", "Player_Yellow" ][randint(0, 2)]] #pl2 = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]] #while pl2==pl: # pl2 = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]] sounds.playTauntMsg(pl, "AreYouCrazy", pl.body.getPosition()) self.addTaunt(pl, "Are you crazy?") #sounds.playTauntMsg(pl2, "No", pl.body.getPosition()) #self.addTaunt(pl2, "No!") resetCoords = self.getBallCoords(playerName) self.scoreBoard.addSelfPoints(playerName) self.faultMsgList.append( [playerName + ' touched twice!', '1', 0]) if self.scoreBoard[playerName][0] >= self.matchPoints: self.setGameOver() return self.initEvents() self.resetBall(resetCoords) self.resetPlayers() return else: #picked from player if self.events[ 'lastTouchedPlayer'] != player: #reset own combo, when picked from other self.scoreBoard.resetOwnCombo(player.name) self.scoreBoard.incrementCombo(player, self.hitMsgList) self.events['lastTouchedObject'] = player self.events['lastTouchedPlayer'] = player
def getMovement(self): dx, dy, dz, rotationAngle = (0,0,0,0) #move to ball/square border intersection currentPosition = self.body.getPosition() target = self.target.getPosition() if (target[0]-currentPosition[0]) * (target[0]-currentPosition[0]) + \ (target[2]-currentPosition[2]) * (target[2]-currentPosition[2]) < 2.0 and \ currentPosition[1]<1.1: dy = 2.0 if vectormath.isPointInSquare(target, self.min, self.max): targetPosition = target else: targetPosition = vectormath.getBallAndSquareIntersection(target, self.target.getLinearVel(), self.min, self.max) targetPosition = (targetPosition[0] + random() - 0.5, targetPosition[1], targetPosition[2] + random() - 0.5) vectorAB = vectormath.getVector(currentPosition, targetPosition) #to make oponents run to ball coords - self length vABAngle = atan2(vectorAB[2], vectorAB[0]) vABMagnitude = vectormath.getMagnitudeV(vectorAB) newVABMagnitude = vABMagnitude - 2.0 #malas garums/2 newVABX = [cos(vABAngle) * newVABMagnitude, vectorAB[1], sin(vABAngle) * newVABMagnitude] vectorAB = newVABX if vABMagnitude<1.8: moveSpeed = 700 * self.body.getMass().mass else: moveSpeed = 4000 * self.body.getMass().mass currentRotationTuple = self.body.getRotation() currentRotation = currentRotationTuple[2], currentRotationTuple[5], currentRotationTuple[8] vectorAB = vectormath.normalize(vectorAB[0], 0, vectorAB[2]) rotationAngle = vectormath.getAngleBetweenVectors(currentRotation, vectorAB) currentRotation = vectormath.negate(currentRotation) dx = currentRotation[0] dz = currentRotation[2] #/------------------ return dx * moveSpeed, dy * moveSpeed, dz * moveSpeed, rotationAngle * moveSpeed
def checkIsBallInSquare(self, ball, min, max): min0 = min[0] min1 = min[1] max0 = max[0] max1 = max[1] #0.5 is for the middle line if min0 == 0: min0 = max0 / 10 * 0.5 if min1 == 0: min1 = max1 / 10 * 0.5 if max0 == 0: max0 = min0 / 10 * 0.5 if max1 == 0: max1 = min1 / 10 * 0.5 return vectormath.isPointInSquare(ball, (min0, min1), (max0, max1))
def registerPlayerEvent(self): ball = self.ball for object in self.objectList: if 'Player' in object.name: player = object #ball + player sounds.playSound("BallPlayer", ball.geom.getPosition()) lastTouchedObject = self.events['lastTouchedObject'] if lastTouchedObject == None: #picked from middle line self.scoreBoard.resetJoinedCombo() #clear combo self.scoreBoard.incrementCombo(player, self.hitMsgList) #(+1) else:#picked from ground or player if lastTouchedObject.name == 'Ground': #picked from ground inside self.scoreBoard.resetJoinedCombo() playerName = player.name self.scoreBoard.incrementCombo(player, self.hitMsgList) if (self.events['lastFieldOwner'] == player)\ and (self.events['lastTouchedPlayer'] == player)\ and vectormath.isPointInSquare(ball.geom.getPosition() , self.players[playerName].min , self.players[playerName].max): #critical event. double-touched -> fault if playerName=="Player": pl = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]] #pl2 = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]] #while pl2==pl: # pl2 = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]] sounds.playTauntMsg(pl, "AreYouCrazy", pl.body.getPosition()) self.addTaunt(pl, "Are you crazy?") #sounds.playTauntMsg(pl2, "No", pl.body.getPosition()) #self.addTaunt(pl2, "No!") resetCoords = self.getBallCoords(playerName) self.scoreBoard.addSelfPoints(playerName) self.faultMsgList.append([playerName + ' touched twice!', '1', 0]) if self.scoreBoard[playerName][0] >= self.matchPoints: self.setGameOver() return self.initEvents() self.resetBall(resetCoords) self.resetPlayers() return else: #picked from player if self.events['lastTouchedPlayer'] != player: #reset own combo, when picked from other self.scoreBoard.resetOwnCombo(player.name) self.scoreBoard.incrementCombo(player, self.hitMsgList) self.events['lastTouchedObject'] = player self.events['lastTouchedPlayer'] = player
def applyInputs(self, entryIndex): W, H = glfwGetWindowSize() mx, my = glfwGetMousePos() my = -my if (mx, my + H) != self.cursorPos: if mx <= 0: mx = 0 if mx >= W: mx = W if my >= 0: my = 0 if my <= -H: my = -H self.cursorPos = (mx, my + H) glfwSetMousePos(mx, -my) #if mousebutton1 is pressed, enter selected menu W, H = glfwGetWindowSize() menuY = H / 2 + 50 * self.entryFontScale for i in range(len(self)): pos = i + 1 string = self[i][0] #vertical positioning will work fine only with one height for all entries x0 = W / 2 - self.font_aa.getSize( string)[0] * self.entryFontScale / 2 y0 = menuY - pos * self.font_aa.getSize( string)[1] * self.entryFontScale #+ self.entryLineSpacing x1 = x0 + self.font_aa.getSize(string)[0] * self.entryFontScale y1 = y0 + self.entryLineSpacing - self.font_aa.getSize( string)[1] * self.entryFontScale if vectormath.isPointInSquare( (self.cursorPos[0], 0, self.cursorPos[1]), (x0, y1), (x1, y0)): self[i][1] = True if glfwGetMouseButton(GLFW_MOUSE_BUTTON_1) == GLFW_PRESS\ and glfwGetMouseButton(GLFW_MOUSE_BUTTON_1) == GLFW_RELEASE: sounds.playSound("Menu", (0, 0, 0)) selection = self[i][0] gameglobals.gameState = self[i][2] self.setSelectionIndex(selection, entryIndex)
def registerGroundEvent(self): #our objects of interest: ball, ground = self.ball, None #asign objects of interest for object in self.objectList: if object.name == 'Ground': ground = object #handle events ballCoords = ball.geom.getPosition() #ball + ground if ball.body.getLinearVel()[1] < -0.3: sounds.playSound("BallGround", ballCoords) if not vectormath.isPointInSquare(ballCoords, (-10, -10), (10, 10)): #critical event #BALL HAS HIT THE FIELD OUTSIDE #and we handle score here out = False if self.events['lastTouchedObject'] != None: #if last touched was not middle line lastTouchedObject = self.events['lastTouchedObject'].name if 'Player' in lastTouchedObject: #player has kicked the ball out pts = self.scoreBoard.addSelfPoints(lastTouchedObject) resetCoords = self.getBallCoords(lastTouchedObject) self.faultMsgList.append(['Out from ' + lastTouchedObject + '!', str(pts), 0]) out = lastTouchedObject getsPoints = lastTouchedObject elif self.events['lastFieldOwner']: #if ground was touched in one of the players field last add points to owner owner = self.events['lastFieldOwner'].name if self.events['lastTouchedPlayer']: #case when noone touched the ball after throwing minus lastPlayer = self.events['lastTouchedPlayer'].name if owner == lastPlayer: pts = self.scoreBoard.addSelfPoints(owner) else: pts = self.scoreBoard.addTotalPoints(owner) else: pts = self.scoreBoard.addPoint(owner) out = owner self.faultMsgList.append(['Out from ' + owner + '`s field!', str(pts), 0]) resetCoords = self.getBallCoords(owner) getsPoints = owner if self.scoreBoard[getsPoints][0] >= self.matchPoints: self.setGameOver() return else: self.faultMsgList.append(['Ball out from middle line!', '', 0]) resetCoords = (0, 10, 0) if out=="Player": pl = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]] #pl2 = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]] #while pl2==pl: # pl2 = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]] sounds.playTauntMsg(pl, "GreatShot", pl.body.getPosition()) self.addTaunt(pl, "Great shot!") # sounds.playTauntMsg(pl2, "Yes", pl.body.getPosition()) # self.addTaunt(pl2, "Yes!") ## print self.scoreBoard #finishing handling critical branch self.scoreBoard.resetJoinedCombo() self.initEvents() self.resetBall(resetCoords) self.resetPlayers() if out!=False and out!="Player": pl = self.players[out] sounds.playTauntMsg(pl, "MoveIn", pl.body.getPosition()) self.addTaunt(pl, "Move in!") else: #BALL HAS HIT THE FIELD INSIDE #save the touched field for p in self.players.values(): if self.checkIsBallInSquare(ballCoords, p.min, p.max): self.events['lastFieldOwner'] = p.geom self.events['lastTouchedObject'] = ground break self.events['lastTouchedObject'] = None #if has hit the middle line
def registerGroundEvent(self): #our objects of interest: ball, ground = self.ball, None #asign objects of interest for object in self.objectList: if object.name == 'Ground': ground = object #handle events ballCoords = ball.geom.getPosition() #ball + ground if ball.body.getLinearVel()[1] < -0.3: sounds.playSound("BallGround", ballCoords) if not vectormath.isPointInSquare(ballCoords, (-10, -10), (10, 10)): #critical event #BALL HAS HIT THE FIELD OUTSIDE #and we handle score here out = False if self.events[ 'lastTouchedObject'] != None: #if last touched was not middle line lastTouchedObject = self.events['lastTouchedObject'].name if 'Player' in lastTouchedObject: #player has kicked the ball out pts = self.scoreBoard.addSelfPoints(lastTouchedObject) resetCoords = self.getBallCoords(lastTouchedObject) self.faultMsgList.append( ['Out from ' + lastTouchedObject + '!', str(pts), 0]) out = lastTouchedObject getsPoints = lastTouchedObject elif self.events[ 'lastFieldOwner']: #if ground was touched in one of the players field last add points to owner owner = self.events['lastFieldOwner'].name if self.events['lastTouchedPlayer']: #case when noone touched the ball after throwing minus lastPlayer = self.events['lastTouchedPlayer'].name if owner == lastPlayer: pts = self.scoreBoard.addSelfPoints(owner) else: pts = self.scoreBoard.addTotalPoints(owner) else: pts = self.scoreBoard.addPoint(owner) out = owner self.faultMsgList.append( ['Out from ' + owner + '`s field!', str(pts), 0]) resetCoords = self.getBallCoords(owner) getsPoints = owner if self.scoreBoard[getsPoints][0] >= self.matchPoints: self.setGameOver() return else: self.faultMsgList.append(['Ball out from middle line!', '', 0]) resetCoords = (0, 10, 0) if out == "Player": pl = self.players[[ "Player_Red", "Player_Green", "Player_Yellow" ][randint(0, 2)]] #pl2 = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]] #while pl2==pl: # pl2 = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]] sounds.playTauntMsg(pl, "GreatShot", pl.body.getPosition()) self.addTaunt(pl, "Great shot!") # sounds.playTauntMsg(pl2, "Yes", pl.body.getPosition()) # self.addTaunt(pl2, "Yes!") ## print self.scoreBoard #finishing handling critical branch self.scoreBoard.resetJoinedCombo() self.initEvents() self.resetBall(resetCoords) self.resetPlayers() if out != False and out != "Player": pl = self.players[out] sounds.playTauntMsg(pl, "MoveIn", pl.body.getPosition()) self.addTaunt(pl, "Move in!") else: #BALL HAS HIT THE FIELD INSIDE #save the touched field for p in self.players.values(): if self.checkIsBallInSquare(ballCoords, p.min, p.max): self.events['lastFieldOwner'] = p.geom self.events['lastTouchedObject'] = ground break self.events[ 'lastTouchedObject'] = None #if has hit the middle line