95Roadkill

College work Game (Done)

Oct 27th, 2014
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //----------------------------\
  2. //Main Variables              |
  3. //----------------------------/
  4. var bulletID:Number = 0;
  5. var maxSpeed:Number = 5;
  6. var speed:Number = 0;
  7. var accel:Number = 0.15;
  8. var decel:Number = 0.95;
  9. var backSpeed:Number = 0;
  10. var maxBackSpeed:Number = -5;
  11. var healthPoints:Number = 100;
  12. var cheatsOn:Boolean = false;
  13. var isMouseDown:Boolean = false;
  14. var numBullets:Number = 0;
  15. var ammo:Number = 10;
  16. var reloadTime:Number = 0;
  17. var numBullets:Number = 0;
  18. var numEnemies:Number = 0;
  19. var bulletList:Array = new Array ();
  20. var enemyList:Array = new Array ();
  21. var bulletSpeed:Number = 10;
  22. var canFire:Boolean = true;
  23. var radians:Number = Math.PI/180;
  24. var enemySpeed:Number = 2;
  25. var turnRate:Number = 0.1;
  26. var canSpawn:Boolean = true;
  27. var spDelay:Number = 0;
  28. var score:Number = 0;
  29.  
  30. //zombies
  31. var moveX:Number = 0;
  32. var moveY:Number = 0;
  33.  
  34. // Timers
  35. var fireTime:Number = 0;
  36. var reloadTime:Number = 0;
  37.  
  38.  
  39. //boundry variables
  40. var tankW = Tank._width /2;
  41. var tankH = Tank._height /2;
  42. var stageW = Stage.width;
  43. var stageH = Stage.height;
  44. var boundT = 0 + tankH;
  45. var boundR = stageW - tankW
  46. var boundL = 0 + tankW;
  47. var boundB = stageH - 100 - tankH;
  48.  
  49. //sound variables
  50. fire = new Sound ();
  51. fire.attachSound("Fire")
  52.  
  53. //---------------------------------------------------------
  54.  
  55. //---------------------------------\
  56. // Find out if the mouse is down   |
  57. //---------------------------------/
  58.  
  59. this.onMouseDown = function() //if Mouse mosue is down, set isMouseDown to true
  60. {
  61.     isMouseDown = true;
  62.     trace("Down")
  63. }
  64. this.onMouseUp = function() //if Mouse mosue is up, set isMouseDown to false
  65.     {
  66.     isMouseDown = false;
  67.     trace("Up")
  68. }
  69. //---------------------------------------------------------
  70.  
  71. //----------------\
  72. // Define Angle   |
  73. //----------------/
  74.  
  75. Mouse.hide();
  76.  
  77. Tank.onEnterFrame = function()
  78. {
  79.     if(canSpawn == true){ // if canSpawn is equal to true, run the code.
  80.         spawnEnemy() //Run function "spawnEnemy"
  81.         canSpawn = false // set canSpawn to false so the spawn delay can be triggered
  82.      }
  83.      for (i = 0; i <= numEnemies; i++){
  84.           if (Tank.hitTest(enemyList[i]) && speed <= -2){ //if player drives over zombies past a set speed, then kill the zombies.
  85.             enemyList[i].removeMovieClip();
  86.             enemyList.splice(i,1);
  87.             numEnemies--;
  88.             score+=5  // increase score by 5
  89.              }
  90.          if (Tank.hitTest(enemyList[i]) && speed >= 2){//if player reverses over zombies past a set speed, then kill the zombies.
  91.             enemyList[i].removeMovieClip();
  92.             enemyList.splice(i,1);
  93.             numEnemies--;
  94.             score+=5 // increase score by 5
  95.          }
  96.            
  97.      }
  98.      if(healthPoints <= 0){
  99.          gotoAndStop(2)
  100.      }
  101.     mouseCursor.gotoAndPlay(reloadTime)
  102.     ammoLeft = "Current ammo: " + ammo / 2 // make the text tell us the current ammo
  103.     sped = "Speed: " + Math.round(speed) // make the text tell us the current speed
  104.     turRot = "Turret Rotation: "+ Math.round(Tank.turret._rotation); // make the text tell us the current turret rotation
  105.     tankRot = "Tank Rotation: " + Math.round(Tank._rotation); // make the text tell us the current tank rotation
  106.     hP = "Health Points: " + Math.round(healthPoints) // make the text tell us the current HP
  107.     scoreTxt = "Score : " + score // make the text tell us the current score
  108.     //----------------------------------------------------------
  109.    
  110.     //----------------\
  111.     // Define Angle   |
  112.     //----------------/
  113.    
  114.     var __x = _xmouse - Tank._x;
  115.     var __y = _ymouse - Tank._y;
  116.     var angle = Math.atan2(__y, __x) * 180 / Math.PI; //make a variable to difine angle so it can be used to rotate the turret
  117.     //----------------------------------------------------------
  118.  
  119.     mouseCursor._x = _xmouse; //make the crosshair follow the mouse
  120.     mouseCursor._y = _ymouse;
  121.    
  122.    
  123.     if(Tank._x > boundR){ //if tank xPos is more than the right boundry, tank xPos = boundL
  124.         Tank._x = boundR;
  125.         trace("At Right Boundry")
  126.     }
  127.     if(Tank._x < boundL){//if tank xPos is less than the left boundry, tank xPos = boundL
  128.         Tank._x = boundL;
  129.         trace("At Left Boundry")
  130.     }
  131.     if(Tank._y < boundT){ //if tank xPos is less than the left boundry, tank xPos = boundL
  132.         Tank._y = boundT;
  133.         trace ("At Top  Boundry")
  134.     }
  135.     if(Tank._y > boundB){
  136.         Tank._y = boundB;
  137.         trace("At Bottom Boundry")
  138. }
  139.    
  140.    
  141.    
  142.     //-------------------------\
  143.     //Character movement code  |
  144.     //-------------------------/
  145.         if (Key.isDown(Key.UP))// if the up key has been pressed increase speed by acceleration until it is equal to maxSpeed
  146.     {
  147.         trace("Up key is pressed");
  148.         if (speed <= maxSpeed){
  149.             speed += accel;
  150.         }
  151.     }else //else times the speed by decel
  152.     {
  153.         speed *= decel;
  154.     }
  155.     if(Key.isDown(Key.DOWN)){ //if the down key has been pressed decrease speed by acceleration
  156.         if(Math.abs(speed) <= maxSpeed){ // Math.abs so the speed can go into negative
  157.             speed -= accel;
  158.         }
  159.     }
  160.     else{ //else times the speed by decel
  161.         speed *= decel;
  162.     }
  163.     if (Key.isDown(Key.RIGHT))// if the right key is pressed,rotate the tank right.
  164.     {
  165.         trace("Right Rotation");
  166.         this._rotation += 2;
  167.     }
  168.     if (Key.isDown(Key.LEFT))// if the left key is pressed,rotate the tank left.
  169.     {
  170.    
  171.         trace("Left Rotation");
  172.         this._rotation -= 2;
  173.     }
  174.    
  175.     //----------------------------------------------------------    
  176.     //---------------\
  177.     // Trigonometry  |
  178.     //---------------/
  179.     this._y += Math.sin(Tank._rotation / 180 * Math.PI) * speed;// allows the tank to move forward in the direction it is facing
  180.     this._x += Math.cos(Tank._rotation / 180 * Math.PI) * speed;
  181.     //----------------------------------------------------------
  182.  
  183.     //------------------\
  184.     //Turret Rotation   |
  185.     //------------------/
  186.     Tank.turret._rotation = angle - Tank._rotation; // makes the turret follow the mouse.
  187.    
  188.     //----------------------------------------------------------
  189.     //--------------------\
  190.     // Bullets & reloading|
  191.     //--------------------/
  192.     if (ammo <= 0){ // if ammo is less than or equal to 0, run the "reload" function with the number 100
  193.          reload(100); // givees it a reload time so you can't spam shoot.
  194.     }
  195.     if(isMouseDown == true){ //if mouse is down,ammo >= 1 and canFire = true, run the "shoot" function,decrease the ammo by 1 and set canFire to false.
  196.         if (ammo >= 1){// this will place a delay between each fire to make it feel more realisitc
  197.             if (canFire == true){
  198.                 shoot()
  199.                 ammo -=1
  200.                 canFire = false
  201.         }
  202.     }
  203. }
  204.     //------------------------------------------------------------------
  205.     //----------------\
  206.     // Function calls |
  207.     //----------------/
  208.     rotateBullets()
  209.     fireDelay(40)
  210.     spawnDelay(60)
  211.     moveEnemy()
  212.     deathBox()
  213.     //------------------------------------------------------------------
  214.        
  215.  
  216.  
  217. };  // onEnterFrame end
  218.  
  219. function deathBox(){ // function to kill bullets when it collides with the black boxes around the map.
  220.     for(i = 1; i <= numBullets; i++){
  221.         if(bulletList[i].hitTest(box1)){
  222.         bulletList[i].removeMovieClip();
  223.         }
  224.     if(bulletList[i].hitTest(box2)){
  225.      
  226.      bulletList[i].removeMovieClip();
  227.        }
  228.     if(bulletList[i].hitTest(box3)){
  229.       bulletList[i].removeMovieClip();
  230.        }
  231.        if(bulletList[i].hitTest(box4)){
  232.      bulletList[i].removeMovieClip();
  233.      }
  234.     }
  235. }
  236. function spawnEnemy() {
  237.     numEnemies++; //increase the numEnemies variable by 1 everytime an enemy is spawned.
  238.     trace("Number of Enemies: " + numEnemies); //tells us how many enemys there are.
  239.     enemyList[numEnemies] = this.attachMovie("Enemy", "EnemyName" + numEnemies, this.getNextHighestDepth(), { _x:-50 , _y:100}); // spawn enemies at the x cord of -50 so its slightly off the screen.
  240. }
  241. function moveEnemy(){
  242.     if (numEnemies > 0){ //if there are moire than one enemy on the screen.
  243.     for(num = 1; num <= numEnemies; num++) {
  244.     doFollow(enemyList[num],Tank,1) //make the enemys walk towards to tank
  245.     if(Tank.hitTest(enemyList[num])){ //if the enemys hit the tank,decrease health by 0.03 every frame
  246.        healthPoints-=0.03
  247.        }
  248.         for(i = 1; i <= numBullets; i++) {
  249.         if(enemyList[num].hitTest(bulletList[i])){ //if bullet hits an enemy remove the bullet
  250.         enemyList[num].removeMovieClip();
  251.         enemyList.splice(num,1);
  252.         numEnemies--;
  253.         bulletList[i].removeMovieClip();
  254.         score+=10//increase score when an enemy is killed
  255.         }
  256.         }
  257.     }
  258. }
  259. }
  260. function shoot() {
  261.     numBullets++;//increase the numBullets variable by 1 everytime a bullet is spawned.
  262.     bulletList[numBullets] = this.attachMovie("Bullet", "bulletName" + numBullets, this.getNextHighestDepth(), { _x:Tank._x , _y:Tank._y});
  263.     ammo--; //decrease ammo by one
  264.     bulletList[numBullets]._rotation = Tank.turret._rotation + Tank._rotation //make the bullet face the direction the turret it rotated to
  265.     fire.start();//play the souund "fire" when you shoot.
  266. }
  267.  
  268. function rotateBullets(){
  269.     for(num = 1; num <= numBullets; num++) {
  270.         bulletList[num]._y += Math.sin(bulletList[num]._rotation / 180 * Math.PI) * bulletSpeed; //makes the bullets travel towards the direction it is facing.
  271.         bulletList[num]._x += Math.cos(bulletList[num]._rotation / 180 * Math.PI) * bulletSpeed;
  272.     }
  273. }
  274. function reload (time:Number){ //1 second = 24,5 seconds = 120.
  275.     if (reloadTime < time){//if reload is less than time, increase roloadTime every frame and set canFire to false
  276.         reloadTime ++
  277.         canFire = false
  278.     }
  279.     if (reloadTime >= time){//if reload is more than or eqial to time, set ammo to 10,set relaodTime to 0 and canFire to true.
  280.         ammo = 10
  281.         reloadTime = 0
  282.         canFire = true
  283.     }
  284. }
  285. function doFollow(follower:MovieClip, target:MovieClip, enemySpeed:Number)
  286. {
  287.     var originalSpeed:Number = enemySpeed
  288.  
  289.  
  290.     var distanceX:Number = target._x - follower._x;//calculate distance between follower and target
  291.     var distanceY:Number = target._y - follower._y;
  292.  
  293.    
  294.     distanceTotal = Math.sqrt(distanceX * distanceX + distanceY * distanceY);//get total distance as one number
  295.    
  296.     if (target.hitTest(follower)){
  297.         enemySpeed = 0;
  298.     }else{
  299.         enemySpeed = originalSpeed;
  300.     }
  301.  
  302.  
  303.        
  304.         var moveDistanceX:Number = turnRate * distanceX / distanceTotal;//calculate how much to move
  305.         var moveDistanceY:Number = turnRate * distanceY / distanceTotal;
  306.  
  307.        
  308.         moveX += moveDistanceX;//increase current speed
  309.         moveY += moveDistanceY;
  310.  
  311.        
  312.         var totalmove = Math.sqrt(moveX * moveX + moveY * moveY);//get total move distance
  313.  
  314.        
  315.         moveX = enemySpeed * moveX / totalmove;//apply easing
  316.         moveY = enemySpeed * moveY / totalmove;
  317.  
  318.        
  319.         follower._x += moveX;//move follower
  320.         follower._y += moveY;
  321.  
  322.        
  323.         follower._rotation = 180 * Math.atan2(moveY, moveX) / Math.PI;//rotate follower toward target
  324.  
  325.  
  326. }
  327.  
  328. function fireDelay(i:Number){
  329.     if (canFire == false){//the function for the fireDelay with chnagable delay times.
  330.     if (fireTime < i){
  331.      fireTime ++
  332.  }
  333.     if (fireTime >= i){//if fireTime is more than or equal to i then fireTime will be reset and canFire will be set to true
  334.         fireTime = 0
  335.         canFire = true
  336.     }
  337. }
  338. }
  339. function spawnDelay (i:Number){//a fumction to prevent a flood of zombies spawning at once, Changable times to increase or descrease the delay.
  340.     if (canSpawn == false){
  341.         if (spDelay < i){
  342.             spDelay++
  343.     }
  344. }
  345.         if (spDelay>= i){//if spDelay is more than or equal to i then spDelay will be reset and ccanSpawn will be set to true
  346.             spDelay = 0
  347.             canSpawn = true
  348.         }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment