Guest User

Untitled

a guest
Sep 21st, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Variables
  2. var xDir = 0;
  3. var yDir = 0;
  4. var speed = 10;
  5. var enemyList = new Array();
  6. var enemySpeed = 2.5;
  7.  
  8. var bulletList = new Array();
  9. var bulletI = 0;
  10. var bulletSpeed = 15;
  11. var bulletType = 0;
  12. var bulletDamage = new Array(5, 10);
  13. var bulletDelay = new Array(10, 5);
  14. var bulletTimer = 0;
  15. var bulletDepth = 2;
  16. var range = 10
  17.  
  18. var plantTotal = 6;
  19.  
  20. var mobTimer = 51;
  21. var mobDelay = 51;
  22. var mobI = 0;
  23. var mobDepth = 1
  24.  
  25. var waveID = 1
  26. var waveCounter = 0
  27. var waveMax = 10
  28. var spawnDelay = 300
  29. var spawnI = 0
  30. var isWave = false
  31.  
  32. var money = 0
  33. var upgradeCosts = new Array(250, 400, 750, 1200, 1800)
  34. var upgradeRank = new Array(0,0,0)
  35.      
  36.  
  37. var keylistener = new Object();
  38.  
  39. //some depth/var settings that have to be set before anything else is ran
  40. hut.swapDepths(1100000)
  41. shop._visible = false
  42.  
  43. for(i=1;i<=6;i++){
  44.     _root["plant" + i].health = 100
  45.     _root["plant" + i].maxHealth = 100
  46. }
  47.  
  48. for(i=0;i<=11;i++){
  49.     _root["tree" + i].swapDepths(1000000 + i)
  50.     _root["bush" + i].swapDepths(999997 + i)
  51. }
  52.  
  53. // Basic WASD movement shit
  54. keylistener.onKeyDown = function()
  55. {
  56.     if (Key.isDown(87))
  57.     {
  58.         yDir = -1;
  59.     }
  60.     else if (Key.isDown(83))
  61.     {
  62.         yDir = 1;
  63.     }
  64.  
  65.     if (Key.isDown(65))
  66.     {
  67.         xDir = -1;
  68.     }
  69.     else if (Key.isDown(68))
  70.     {
  71.         xDir = 1;
  72.     }
  73. };
  74.  
  75. keylistener.onKeyUp = function()
  76. {
  77.     if (Key.isDown(83))
  78.     {
  79.         yDir = 1;
  80.     }
  81.     else if (Key.isDown(87))
  82.     {
  83.         yDir = -1;
  84.     }
  85.     else
  86.     {
  87.         yDir = 0;
  88.     }
  89.  
  90.     if (Key.isDown(68))
  91.     {
  92.         xDir = 1;
  93.     }
  94.     else if (Key.isDown(65))
  95.     {
  96.         xDir = -1;
  97.     }
  98.     else
  99.     {
  100.         xDir = 0;
  101.     }
  102. };
  103.  
  104. movePlayer = function ()
  105. {
  106.     // sets player rotation as one of the 4 cardinal directions depending on mouse position
  107.     player._rotation = 180 + (90 * (Math.floor((45 + Math.atan2(player._x - _xmouse, _ymouse - player._y) * 180 / Math.PI) / 90)));
  108.     player._y += speed * yDir;
  109.     player._x += speed * xDir;
  110.    
  111.     // boundary collision shit
  112.     if(player._x <  20 +player._width / 2){
  113.         player._x = 20 + player._width/2
  114.     }
  115.     if(player._x > 625){
  116.         player._x = 625
  117.     }
  118.     if(player._y > 475){
  119.         player._y = 475
  120.     }
  121.     if(player._y < 125){
  122.         player._y = 125
  123.     }
  124. };
  125.  
  126. // Algorithm to spawn X mobs every Y frames etc
  127. runWave = function(){
  128.     if(isWave == true){
  129.         if(waveCounter < waveMax){
  130.             if (mobTimer == mobDelay){
  131.                 waveCounter ++
  132.                 spawnEnemy();
  133.             }
  134.             else
  135.             {
  136.                 mobTimer++;
  137.             }
  138.            
  139.         }else{
  140.             isWave = false
  141.         }
  142.     }else{
  143.         if(spawnI >= spawnDelay){
  144.             isWave = true
  145.             waveID ++
  146.             spawnI = 0
  147.             waveCounter = 0
  148.         }else{
  149.             spawnI ++
  150.         }
  151.     }
  152. }
  153.  
  154. // Yay, enemies!
  155. spawnEnemy = function ()
  156. {
  157.    
  158.         // Resets the timer and creates the mob
  159.         mobTimer = 0;
  160.         var mob:MovieClip = _root.attachMovie("mob", "enemy" + mobI, mobDepth);
  161.         mobDepth += 2
  162.         // positions outside one of the doors
  163.         mob._x = 260 + 110 * Math.floor(Math.random() * 3)
  164.         // 260 370 500
  165.         mob._y = -10;
  166.         // Scale reduction because massive mobs = bad
  167.         mob._xscale *= 0.25;
  168.         mob._yscale *= 0.25;
  169.         // Adds the mob to the array for loops and shit yo
  170.         enemyList[enemyList.length] = mobI;
  171.         mob.health = 9 + waveID;
  172.         mob.maxHealth = 9 + waveID;
  173.         mob.attackDelay = 10
  174.         mob.delayMax = 10
  175.         mob.dmg = 5
  176.         mob.money = 5
  177.         // incriments mob counter
  178.         mobI++;
  179.    
  180. };
  181.  
  182. // Oh joy, tracking algorithms
  183. moveEnemy = function ()
  184. {
  185.     // Loops through active enemies
  186.     for (i = 0; i < enemyList.length; i++)
  187.     {
  188.         // Define a var to save trouble later
  189.         var enemy:MovieClip = _root["enemy" + enemyList[i]];
  190.         // Fucklarge default values to make sure it doesnt just target nothing
  191.         var closestPlant = 100000;
  192.         var closestDist = 100000;
  193.         // Needs to be outside of loop for later
  194.         var plant:MovieClip;
  195.         // Loop through plants
  196.         for (a = 1; a <= plantTotal; a++)
  197.         {
  198.             // sets plant number for easyness
  199.             plant = _root["plant" + a];
  200.             // If the plant is active
  201.             if (plant._visible == true)
  202.             {
  203.                 // Check the horisontal distance to it (no point doing vertical, they're all coming from the top)
  204.                 if ((Math.abs(plant._x - enemy._x)) < closestDist)
  205.                 {
  206.                     // If its the closest, set new values and do it again
  207.                     closestPlant = a;
  208.                     closestDist = Math.abs(plant._x - enemy._x);
  209.                 }
  210.             }
  211.         }
  212.         // Well wasnt that fun
  213.        
  214.         plant = _root["plant" + closestPlant];
  215.         // Calculate angle to the plant
  216.         var dx = enemy._x - plant._x;
  217.         var dy = plant._y - enemy._y;
  218.         enemy._rotation = Math.atan2(dx, dy) * 180 / Math.PI;
  219.         // If its not already bumfucking the plant
  220.         if (Math.sqrt((dy * dy) + (dx * dx)) > 30)
  221.         {
  222.             // move towards the plant
  223.             if(enemy._y > 100){
  224.                 enemy._x -= enemySpeed * Math.sin(enemy._rotation * Math.PI / 180);
  225.                 enemy._y += enemySpeed * Math.cos(enemy._rotation * Math.PI / 180);
  226.             }else{
  227.                 // stops em from tracking until they get past the trees
  228.                 enemy._y += enemySpeed
  229.             }
  230.         }else{
  231.             // if the delay between attacks = 0
  232.             if(enemy.attackDelay == 0){
  233.                 // attack and reset delay
  234.                 plant.health -= enemy.dmg
  235.                 if(plant.health <= 0){
  236.                     plant._visible = false
  237.                 }else if(plant.health <= (plant.maxHealth / 3)){
  238.                     plant.gotoAndPlay("Dam2")
  239.                 }else if (plant.health <= (2 * plant.maxHealth / 3)){
  240.                     plant.gotoAndPlay("Dam1")
  241.                 }
  242.                 enemy.attackDelay = enemy.delayMax
  243.             }else{
  244.                 // if not, decrease
  245.                 enemy.attackDelay --
  246.             }
  247.         }
  248.     }
  249. };
  250.  
  251. // Bullet movement (strangely enough)
  252. moveBullets = function ()
  253. {
  254.     // Well this is a convoluted fucking mess
  255.    
  256.     // Loops through bullets
  257.     for (i = 0; i < bulletList.length; i++)
  258.     {
  259.         var bul:MovieClip = _root["bullet" + bulletList[i]];
  260.         // Moves bullet the direction it should be going
  261.         bul.life --
  262.         if(bul.life == 0){
  263.             bul.gotoAndPlay("Dead")
  264.         }
  265.         bul._x += bulletSpeed * Math.sin(bul._rotation * Math.PI / 180);
  266.         bul._y -= bulletSpeed * Math.cos(bul._rotation * Math.PI / 180);
  267.        
  268.         // if the bullet is out of the screen
  269.         if ((bul._x < -100) || (bul._x > 750) || (bul._y < -100) || (bul._y > 550))
  270.         {
  271.             // GTFO, bullet
  272.             bul.gotoAndPlay("Dead")
  273.             // copypasta'd loop is copypasta'd
  274.             for (b = 0; b < bulletList.length; b++)
  275.             {
  276.                 // removes bullet from the array
  277.                 if (bulletList[b] == bulletList[i])
  278.                 {
  279.                     bulletList.splice(b,1);
  280.                     break;
  281.                 }
  282.             }
  283.         }
  284.         // Well fuck, more nested loops
  285.         for (a = 0; a < enemyList.length; a++)
  286.         {
  287.             var en:MovieClip = _root["enemy" + enemyList[a]];
  288.             // If the bullet hits an enemy
  289.             if (en.hitTest(bul))
  290.             {
  291.                 // damage the enemy
  292.                 en.health -= bulletDamage[bulletType];
  293.                 en.healthBar._width = (en.health / en.maxHealth) * 100
  294.                 // OH GOD WHEN WILL IT STOP
  295.                 for (b = 0; b < bulletList.length; b++)
  296.                 {
  297.                     // remove the bullet from the array
  298.                     if (bulletList[b] == bulletList[i])
  299.                     {
  300.                         bulletList.splice(b,1);
  301.                         break;
  302.                     }
  303.                 }
  304.                 // remove bullet from the screen
  305.                 bul.gotoAndPlay("Dead")
  306.                 if (en.health <= 0)
  307.                 {
  308.                     // removes enemy from the array if it is dead
  309.                     for (c = 0; c < enemyList.length; c++)
  310.                     {
  311.                         if (enemyList[c] == enemyList[a])
  312.                         {
  313.                             enemyList.splice(c,1);
  314.                             break;
  315.                         }
  316.                     }
  317.                     // deletes enemy on death
  318.                     money += en.money
  319.                     en.removeMovieClip();
  320.                    
  321.                 }
  322.             }
  323.         }
  324.     }
  325. };
  326. // And breathe
  327.  
  328. // SHOOTING
  329. map.onRelease = function()
  330. {
  331.     // If theres been enough time since i last fired
  332.     if (bulletTimer >= bulletDelay[bulletType])
  333.     {
  334.         var bullet:MovieClip = _root.attachMovie("bullet", "bullet" + bulletI, bulletDepth);
  335.         bulletDepth += 2
  336.         // funky array shit
  337.         bulletList[bulletList.length] = bulletI;
  338.         bulletI++;
  339.         // Place bullet on player
  340.         bullet._x = player._x;
  341.         bullet._y = player._y;
  342.         bullet._rotation = player._rotation;
  343.         bullet.life = range
  344.         // reset timer
  345.         bulletTimer = 0;
  346.     }
  347. };
  348.  
  349. // The easy bit
  350. onEnterFrame = function ()
  351. {
  352.     movePlayer();
  353.     runWave();
  354.     moveEnemy();
  355.     moveBullets();
  356.     bulletTimer++;
  357. };
  358.  
  359. Key.addListener(keylistener);
Add Comment
Please, Sign In to add comment