// Update is called once per frame void Update() { // check to see if the player droid should be removed from the stage for maintenance if (maintenancePoints <= 0) { removeDroid(); } else { // if the droid has maintenance points, we play with it FindPlayerInput(); HandleAnimation(); HandleCamera(); moveSpeed = Random.Range(20000f, 21000f); // Create a new opponent on stage if // 1) we can find an opponent on the stage with less than 12 maintenance points, // 2) the total number of opponents on stage is less than maxOpponents, and // 3) there has not been an opponent created for the minSecondsTweenOpponents, // and set it in place on the stage int i; opponentScript ptrOppoScript = null; for (i = 1; i < maxOpponents; i++) { objOpponentOnStage = (GameObject)GameObject.FindWithTag("Opponent0" + i.ToString()); if (objOpponentOnStage != null) { ptrOppoScript = (opponentScript)objOpponentOnStage.GetComponent(typeof(opponentScript)); if (ptrOppoScript.maintenancePoints <= 12) { break; } } } if (objOpponentOnStage != null && opponentCount < maxOpponents && (Time.time - opponentSpawnedAtTime) > minSecondsTweenOpponents) { if (ptrOppoScript.maintenancePoints <= 12) { if (preFabOpponent != null) { GameObject objCreatedOpponent = (GameObject)Instantiate(preFabOpponent, new Vector3(0, 0, 0), Quaternion.LookRotation(new Vector3(((float)Random.Range(1, 3)), 0, ((float)Random.Range(0, 3))))); for (i = 1; i < maxOpponents; i++) { objOpponentOnStage = (GameObject)GameObject.FindWithTag("Opponent0" + i.ToString()); if (objOpponentOnStage == null) { break; } } objCreatedOpponent.tag = "Opponent0" + i.ToString(); ptrOppoScript = (opponentScript)objCreatedOpponent.GetComponent(typeof(opponentScript)); ptrOppoScript.maintenancePoints = 20; opponentCount++; // set the time when this opponent was spawned opponentSpawnedAtTime = Time.time; } } } } }
void checkForSwarmMode() { if (!inSwarmMode && (Time.time - stopSwarmTime) > 30f) { // if the opponent droids are not presently in swarm mode, and // more than 30 seconds have passed since the last swarm, // check to see if there's a new swarm at hand // trigger swarm mode 30% of the time if (Random.Range(0, 99) > 65) { inSwarmMode = true; // need to communicate with all the opponent scripts and put them into swarm mode here int i; opponentScript ptrOppoScript = null; for (i = 1; i < maxOpponents; i++) { objOpponentOnStage = (GameObject)GameObject.FindWithTag("Opponent0" + i.ToString()); if (objOpponentOnStage != null) { ptrOppoScript = (opponentScript)objOpponentOnStage.GetComponent(typeof(opponentScript)); ptrOppoScript.inSwarmMode = true; } } swarmedAtTime = Time.time; } } else if (inSwarmMode && (Time.time - swarmedAtTime) > 10f) { // if the opponent droids are presently in swarm mode, and // more than 10 seconds have passed since swarm mode began, // stop swarm mode inSwarmMode = false; // need to communicate with all the opponent scripts and get them out of swarm mode here int i; opponentScript ptrOppoScript = null; for (i = 1; i < maxOpponents; i++) { objOpponentOnStage = (GameObject)GameObject.FindWithTag("Opponent0" + i.ToString()); if (objOpponentOnStage != null) { ptrOppoScript = (opponentScript)objOpponentOnStage.GetComponent(typeof(opponentScript)); ptrOppoScript.inSwarmMode = false; } } stopSwarmTime = Time.time; } }
void ProcessAnimation() { // iteration int i int i; // the animation framerate is managed by setting animationTime initially to 1/framerate // then subtracting the gameplay framerate passed since the last call to ProcessAnimation // When animationTime eventually becomes negative after enough subtractions, it is time // to display a new frame of animation. Then, after the new animation frame is displayed, we // reset animationTime to 1/framerate, so we can begin the process again // This essentially creates a Flash-like local timeline for this sprite using the gameplay framerate animationTime -= Time.deltaTime; // if the game is running at 30 frames per second // animationTime will subtract 0.033 of a second (1/30) if (animationTime <= 0) { frameNumber += 1; // manage the animationFight animation if (currentAnimation == AnimationLoops.animationFight) { frameNumber = Mathf.Clamp(frameNumber, fightAnimationMin, fightAnimationMax + 1); spriteScalerY = 0.9150f; spriteFightY = -0.009f; if (frameNumber > fightAnimationMax) { // once we have finished the fight animation, detect if we are still in FightingPlayer state doFightDamage = false; if (state == ControlState.FightingPlayer) { // when looping the fight animation it looks better to start at fightAnimationMin + 4 frameNumber = fightAnimationMin + 4; } else { currentAnimation = AnimationLoops.animationWalk; frameNumber = walkAnimationMin; } } // if the player is within 2 units of the opponent and // we are at least 4 frames into the animation, and we haven't attacked yet if (state == ControlState.FightingPlayer && frameNumber > fightAnimationMin + 4 && doFightDamage == false) { // damage the droid doFightDamage = true; for (i = 1; i <= maxOpponents; i++) { objOpponentOnStage = (GameObject)GameObject.FindWithTag("Opponent0" + i.ToString()); if (objOpponentOnStage != null) { if (Vector3.Distance(objPlayer.transform.position, objOpponentOnStage.transform.position) < 2.0f) { opponentScript ptrOppoScript = (opponentScript)objOpponentOnStage.GetComponent(typeof(opponentScript)); ptrOppoScript.maintenancePoints -= fightDamage; //print ("Opponent0" + i.ToString () + " damaged"); break; } } } } } // manage the animationWave animation if (currentAnimation == AnimationLoops.animationWave) { frameNumber = Mathf.Clamp(frameNumber, waveAnimationMin, waveAnimationMax + 1); spriteScalerY = 0.8918f; spriteFightY = 0f; if (frameNumber > waveAnimationMax) { frameNumber = waveAnimationMin; } } // manage the animationWalk animation if (currentAnimation == AnimationLoops.animationWalk) { frameNumber = Mathf.Clamp(frameNumber, walkAnimationMin, walkAnimationMax + 1); spriteScalerY = 0.9150f; spriteFightY = 0f; if (frameNumber > walkAnimationMax) { frameNumber = walkAnimationMin; } } // reset the animationTime to 1/framerate, so we can begin the local timeline process again animationTime += (1 / animationFrameRate); } // The remaining code finds the image for the animation frame on the sprite sheet, // then displays it on the surface of the droid's plane model as a texture // First, find the number of frames down the animation is on the sprite sheet and set the y coordinate accordingly spriteSheetCount.y = 0; for (i = (int)frameNumber; i > (int)spriteSheetTotalRows; i -= (int)spriteSheetTotalRows) { spriteSheetCount.y += 1; } // Second, find the number of frames across the animation is on the sprite sheet and set the x coordinate accordingly spriteSheetCount.x = i - 1; // Third, calculate the exact X and Y pixel coordinate on the sprite sheet of the frame to display double calcY = spriteScalerY * (spriteSheetCount.y / spriteSheetTotalRows) + spriteFightY; double calcX = 0.9150 * (spriteSheetCount.x / spriteSheetTotalCols) + 0.033; spriteSheetOffset = new Vector2((float)(1 - calcX), (float)(1 - calcY)); // Last, offset the texture to display the correct frame renderer.material.SetTextureOffset("_MainTex", spriteSheetOffset); }