robert@464: /* robert@464: * game.cpp robert@464: * robert@464: * Created on: Nov 10, 2014 robert@464: * Author: parallels robert@464: */ robert@464: robert@464: #include robert@464: #include robert@464: #include "vector_graphics.h" robert@464: #include robert@464: robert@464: // Virtual screen size robert@464: int screenWidth, screenHeight; robert@464: robert@464: // Basic information on the terrain and the tanks robert@464: float *groundLevel; // Y coordinate of the ground for each X coordinate robert@464: float tank1X, tank1Y, tank2X, tank2Y; // Positions of the two tanks robert@464: float tankRadius = 20; // Radius of the tanks robert@464: float cannonLength = 40; // How long the cannon on each tank extends robert@464: float gravity = 0.05; // Strength of gravity robert@464: robert@464: // Current state of the game robert@464: int playerHasWon = 0; // 1 if player 1 wins, 2 if player 2 wins, 0 if game in progress robert@464: bool player1Turn = true; // true if it's player 1's turn; false otherwise robert@464: float tank1CannonAngle = M_PI/2; robert@464: float tank2CannonAngle = M_PI/2; // Direction the tank cannons are pointing robert@464: float tank1CannonStrength = 3; robert@464: float tank2CannonStrength = 3; // Strength of intended projectile launch robert@464: robert@464: // Location of the projectile robert@464: bool projectileInMotion = false; robert@464: float projectilePositionX, projectilePositionY; robert@464: float projectileVelocityX, projectileVelocityY; robert@464: robert@464: // Infor needed for sound rendering robert@464: bool collisionJustOccurred = false; robert@464: bool tankHitJustOccurred = false; robert@464: robert@464: // Useful utility function for generating random floating-point values robert@464: float randomFloat(float low, float hi) robert@464: { robert@464: float r = (float)random() / (float)RAND_MAX; robert@464: return map(r, 0, 1, low, hi); robert@464: } robert@464: robert@464: // Restart the game, without reallocating memory robert@464: void restartGame() robert@464: { robert@464: float player1Height = screenHeight * 3/4; // randomFloat(screenHeight/2, screenHeight-5); robert@464: float player2Height = screenHeight - 5; // randomFloat(screenHeight/2, screenHeight-5); robert@464: for(int i = 0; i < screenWidth * 0.2; i++) { robert@464: groundLevel[i] = player1Height; robert@464: } robert@464: for(int i = screenWidth * 0.2; i < screenWidth * 0.8; i++) { robert@464: groundLevel[i] = player1Height + (player2Height - player1Height) * (i - screenWidth*0.2)/(screenWidth*0.6); robert@464: } robert@464: for(int i = screenWidth * 0.8; i < screenWidth; i++) { robert@464: groundLevel[i] = player2Height; robert@464: } robert@464: robert@464: // Set the location of the two tanks so they rest on the ground at opposite sides robert@464: tank1X = screenWidth * 0.1; robert@464: tank1Y = player1Height; robert@464: tank2X = screenWidth * 0.9; robert@464: tank2Y = player2Height; robert@464: robert@464: playerHasWon = 0; robert@464: projectileInMotion = false; robert@464: } robert@464: robert@464: // Initialise the game robert@464: void setupGame(int width, int height) robert@464: { robert@464: // Set the screen size robert@464: screenWidth = width; robert@464: screenHeight = height; robert@464: robert@464: // Initialize the ground level robert@464: groundLevel = new float[screenWidth]; robert@464: robert@464: restartGame(); robert@464: } robert@464: robert@464: // Advance the turn to the next player robert@464: void nextPlayersTurn() { robert@464: player1Turn = !player1Turn; robert@464: } robert@464: robert@464: robert@464: // Move forward one frame on the game physics robert@464: void nextGameFrame() robert@464: { robert@464: if(!projectileInMotion) robert@464: return; robert@464: robert@464: // Update position of projectile robert@464: projectilePositionX += projectileVelocityX; robert@464: projectilePositionY += projectileVelocityY; robert@464: projectileVelocityY += gravity; robert@464: robert@464: // Check collision with tanks first: a collision with tank 1 means player 2 wins and vice-versa robert@464: if((tank1X - projectilePositionX)*(tank1X - projectilePositionX) + robert@464: (tank1Y - projectilePositionY)*(tank1Y - projectilePositionY) robert@464: <= tankRadius * tankRadius) robert@464: { robert@464: projectileInMotion = false; robert@464: collisionJustOccurred = false; robert@464: tankHitJustOccurred = true; robert@464: playerHasWon = 2; robert@464: } robert@464: else if((tank2X - projectilePositionX)*(tank2X - projectilePositionX) + robert@464: (tank2Y - projectilePositionY)*(tank2Y - projectilePositionY) robert@464: <= tankRadius * tankRadius) robert@464: { robert@464: projectileInMotion = false; robert@464: collisionJustOccurred = false; robert@464: tankHitJustOccurred = true; robert@464: playerHasWon = 1; robert@464: } robert@464: else if(projectilePositionX < 0 || projectilePositionX >= screenWidth) { robert@464: // Check collision whether projectile has exited the screen to the left or right robert@464: projectileInMotion = false; robert@464: collisionJustOccurred = true; robert@464: nextPlayersTurn(); robert@464: } robert@464: else if(projectilePositionY >= groundLevel[(int)floorf(projectilePositionX)]) { robert@464: // Check for projectile collision with ground robert@464: projectileInMotion = false; robert@464: collisionJustOccurred = true; robert@464: nextPlayersTurn(); robert@464: } robert@464: } robert@464: robert@464: // Updates for game state robert@464: void setTank1CannonAngle(float angle) robert@464: { robert@464: tank1CannonAngle = angle; robert@464: } robert@464: robert@464: void setTank2CannonAngle(float angle) robert@464: { robert@464: tank2CannonAngle = angle; robert@464: } robert@464: robert@464: void setTank1CannonStrength(float strength) robert@464: { robert@464: tank1CannonStrength = strength; robert@464: } robert@464: robert@464: void setTank2CannonStrength(float strength) robert@464: { robert@464: tank2CannonStrength = strength; robert@464: } robert@464: robert@464: // FIRE! robert@464: void fireProjectile() robert@464: { robert@464: // Can't fire while projectile is already moving, or if someone has won robert@464: if(projectileInMotion) robert@464: return; robert@464: if(playerHasWon != 0) robert@464: return; robert@464: robert@464: if(player1Turn) { robert@464: projectilePositionX = tank1X + cannonLength * cosf(tank1CannonAngle); robert@464: projectilePositionY = tank1Y - cannonLength * sinf(tank1CannonAngle); robert@464: projectileVelocityX = tank1CannonStrength * cosf(tank1CannonAngle); robert@464: projectileVelocityY = -tank1CannonStrength * sinf(tank1CannonAngle); robert@464: } robert@464: else { robert@464: projectilePositionX = tank2X + cannonLength * cosf(tank2CannonAngle); robert@464: projectilePositionY = tank2Y - cannonLength * sinf(tank2CannonAngle); robert@464: projectileVelocityX = tank2CannonStrength * cosf(tank2CannonAngle); robert@464: projectileVelocityY = -tank2CannonStrength * sinf(tank2CannonAngle); robert@464: } robert@464: robert@464: // GO! robert@464: projectileInMotion = true; robert@464: } robert@464: robert@464: // Game state queries robert@464: bool gameStatusPlayer1Turn() robert@464: { robert@464: return player1Turn; robert@464: } robert@464: robert@464: bool gameStatusProjectileInMotion() robert@464: { robert@464: return projectileInMotion; robert@464: } robert@464: robert@464: int gameStatusWinner() robert@464: { robert@464: return playerHasWon; robert@464: } robert@464: robert@464: bool gameStatusCollisionOccurred() robert@464: { robert@464: if(collisionJustOccurred) { robert@464: collisionJustOccurred = false; robert@464: return true; robert@464: } robert@464: return false; robert@464: } robert@464: robert@464: bool gameStatusTankHitOccurred() robert@464: { robert@464: if(tankHitJustOccurred) { robert@464: tankHitJustOccurred = false; robert@464: return true; robert@464: } robert@464: return false; robert@464: } robert@464: robert@464: robert@464: float gameStatusProjectileHeight() robert@464: { robert@464: return projectilePositionY / (float)screenHeight; robert@464: } robert@464: robert@464: // Clean up any allocated memory for the game robert@464: void cleanupGame() robert@464: { robert@464: delete groundLevel; robert@464: } robert@464: robert@464: // Drawing routines. Arguments are (interleaved) buffer to render robert@464: // into, the available size, and the target for how many samples robert@464: // to use (actual usage might vary slightly). Regardless of robert@464: // lengthTarget, never use more than bufferSize samples. robert@464: robert@464: int drawGround(float *buffer, int bufferSize, int framesTarget) robert@464: { robert@464: int length; robert@464: robert@464: // Calculate total length of ground line, to arrive at a speed calculation robert@464: float totalLineLength = 0.4f*screenWidth robert@464: + sqrtf(0.36f*screenWidth*screenWidth robert@464: + (tank2Y-tank1Y)*(tank2Y-tank1Y)); robert@464: robert@464: // Speed is calculated in pixels per frame robert@464: float speed = totalLineLength / (float)framesTarget; robert@464: robert@464: // Draw three lines: platforms for tanks and the connecting line. robert@464: // Eventually, render a more complex ground from the array. robert@464: length = renderLine(0, tank1Y, screenWidth * 0.2, tank1Y, robert@464: speed, buffer, bufferSize); robert@464: length += renderLine(screenWidth * 0.2, tank1Y, screenWidth * 0.8, tank2Y, robert@464: speed, &buffer[length], bufferSize - length); robert@464: length += renderLine(screenWidth * 0.8, tank2Y, screenWidth, tank2Y, robert@464: speed, &buffer[length], bufferSize - length); robert@464: robert@464: return length; robert@464: } robert@464: robert@464: int drawTanks(float *buffer, int bufferSize, int framesTarget) robert@464: { robert@464: int length = 0; robert@464: robert@464: // Calculate total length of tank lines, to arrive at a speed calculation robert@464: float totalLineLength = 2.0*M_PI*tankRadius + 2.0*(cannonLength - tankRadius); robert@464: robert@464: // Speed is calculated in pixels per frame robert@464: float speed = totalLineLength / (float)framesTarget; robert@464: robert@464: if(playerHasWon != 2) { robert@464: // Tank 1 body = semicircle + line robert@464: length += renderArc(tank1X, tank1Y, tankRadius, M_PI, 2.0 * M_PI, robert@464: speed, buffer, bufferSize); robert@464: length += renderLine(tank1X + tankRadius, tank1Y, robert@464: tank1X - tankRadius, tank1Y, robert@464: speed, &buffer[length], bufferSize - length); robert@464: // Tank 1 cannon (line depending on angle) robert@464: length += renderLine(tank1X + tankRadius * cosf(tank1CannonAngle), robert@464: tank1Y - tankRadius * sinf(tank1CannonAngle), robert@464: tank1X + cannonLength * cosf(tank1CannonAngle), robert@464: tank1Y - cannonLength * sinf(tank1CannonAngle), robert@464: speed, &buffer[length], bufferSize - length); robert@464: } robert@464: robert@464: if(playerHasWon != 1) { robert@464: // Same idea for tank 2 robert@464: length += renderArc(tank2X, tank2Y, tankRadius, M_PI, 2.0 * M_PI, robert@464: speed, &buffer[length], bufferSize - length); robert@464: length += renderLine(tank2X + tankRadius, tank2Y, robert@464: tank2X - tankRadius, tank2Y, robert@464: speed, &buffer[length], bufferSize - length); robert@464: length += renderLine(tank2X + tankRadius * cosf(tank2CannonAngle), robert@464: tank2Y - tankRadius * sinf(tank2CannonAngle), robert@464: tank2X + cannonLength * cosf(tank2CannonAngle), robert@464: tank2Y - cannonLength * sinf(tank2CannonAngle), robert@464: speed, &buffer[length], bufferSize - length); robert@464: } robert@464: robert@464: return length; robert@464: } robert@464: robert@464: int drawProjectile(float *buffer, int bufferSize, int framesTarget) robert@464: { robert@464: if(!projectileInMotion) robert@464: return 0; robert@464: robert@464: // Draw a point for a specified number of frames (each containing X and Y) robert@464: // Return the number of items used in the buffer, which will be twice robert@464: // the number of frames unless the buffer is full robert@464: robert@464: if(bufferSize/2 < framesTarget) { robert@464: renderPoint(projectilePositionX, projectilePositionY, buffer, bufferSize/2); robert@464: return bufferSize; robert@464: } robert@464: else { robert@464: renderPoint(projectilePositionX, projectilePositionY, buffer, framesTarget); robert@464: return framesTarget*2; robert@464: } robert@464: } robert@464: robert@464: // Main drawing routine entry point robert@464: int drawGame(float *buffer, int bufferSize) robert@464: { robert@464: int length; robert@464: robert@464: // Based on buffer size, come up with speeds for each of the elements robert@464: // 50% of time to ground; 30% to the tanks and 20% to the projectile robert@464: // Give a margin of 25% beyond so we don't run out of buffer space robert@464: // if things take longer to draw than we guess they will robert@464: const float amountToUse = 0.375; // 0.75/2 because two samples per frame robert@464: const float groundFraction = 0.5 * amountToUse; robert@464: const float tankFraction = 0.3 * amountToUse; robert@464: const float projectileFraction = 0.2 * amountToUse; robert@464: robert@464: length = drawGround(buffer, bufferSize, bufferSize * groundFraction); robert@464: length += drawTanks(&buffer[length], bufferSize - length, robert@464: bufferSize * tankFraction); robert@464: length += drawProjectile(&buffer[length], bufferSize - length, robert@464: bufferSize * projectileFraction); robert@464: robert@464: return length; robert@464: }