Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Variables
- var xDir = 0;
- var yDir = 0;
- var speed = 10;
- var enemyList = new Array();
- var enemySpeed = 2.5;
- var bulletList = new Array();
- var bulletI = 0;
- var bulletSpeed = 15;
- var bulletType = 0;
- var bulletDamage = new Array(5, 10);
- var bulletDelay = new Array(10, 5);
- var bulletTimer = 0;
- var bulletDepth = 2;
- var range = 10
- var plantTotal = 6;
- var mobTimer = 51;
- var mobDelay = 51;
- var mobI = 0;
- var mobDepth = 1
- var waveID = 1
- var waveCounter = 0
- var waveMax = 10
- var spawnDelay = 300
- var spawnI = 0
- var isWave = false
- var money = 0
- var upgradeCosts = new Array(250, 400, 750, 1200, 1800)
- var upgradeRank = new Array(0,0,0)
- var keylistener = new Object();
- //some depth/var settings that have to be set before anything else is ran
- hut.swapDepths(1100000)
- shop._visible = false
- for(i=1;i<=6;i++){
- _root["plant" + i].health = 100
- _root["plant" + i].maxHealth = 100
- }
- for(i=0;i<=11;i++){
- _root["tree" + i].swapDepths(1000000 + i)
- _root["bush" + i].swapDepths(999997 + i)
- }
- // Basic WASD movement shit
- keylistener.onKeyDown = function()
- {
- if (Key.isDown(87))
- {
- yDir = -1;
- }
- else if (Key.isDown(83))
- {
- yDir = 1;
- }
- if (Key.isDown(65))
- {
- xDir = -1;
- }
- else if (Key.isDown(68))
- {
- xDir = 1;
- }
- };
- keylistener.onKeyUp = function()
- {
- if (Key.isDown(83))
- {
- yDir = 1;
- }
- else if (Key.isDown(87))
- {
- yDir = -1;
- }
- else
- {
- yDir = 0;
- }
- if (Key.isDown(68))
- {
- xDir = 1;
- }
- else if (Key.isDown(65))
- {
- xDir = -1;
- }
- else
- {
- xDir = 0;
- }
- };
- movePlayer = function ()
- {
- // sets player rotation as one of the 4 cardinal directions depending on mouse position
- player._rotation = 180 + (90 * (Math.floor((45 + Math.atan2(player._x - _xmouse, _ymouse - player._y) * 180 / Math.PI) / 90)));
- player._y += speed * yDir;
- player._x += speed * xDir;
- // boundary collision shit
- if(player._x < 20 +player._width / 2){
- player._x = 20 + player._width/2
- }
- if(player._x > 625){
- player._x = 625
- }
- if(player._y > 475){
- player._y = 475
- }
- if(player._y < 125){
- player._y = 125
- }
- };
- // Algorithm to spawn X mobs every Y frames etc
- runWave = function(){
- if(isWave == true){
- if(waveCounter < waveMax){
- if (mobTimer == mobDelay){
- waveCounter ++
- spawnEnemy();
- }
- else
- {
- mobTimer++;
- }
- }else{
- isWave = false
- }
- }else{
- if(spawnI >= spawnDelay){
- isWave = true
- waveID ++
- spawnI = 0
- waveCounter = 0
- }else{
- spawnI ++
- }
- }
- }
- // Yay, enemies!
- spawnEnemy = function ()
- {
- // Resets the timer and creates the mob
- mobTimer = 0;
- var mob:MovieClip = _root.attachMovie("mob", "enemy" + mobI, mobDepth);
- mobDepth += 2
- // positions outside one of the doors
- mob._x = 260 + 110 * Math.floor(Math.random() * 3)
- // 260 370 500
- mob._y = -10;
- // Scale reduction because massive mobs = bad
- mob._xscale *= 0.25;
- mob._yscale *= 0.25;
- // Adds the mob to the array for loops and shit yo
- enemyList[enemyList.length] = mobI;
- mob.health = 9 + waveID;
- mob.maxHealth = 9 + waveID;
- mob.attackDelay = 10
- mob.delayMax = 10
- mob.dmg = 5
- mob.money = 5
- // incriments mob counter
- mobI++;
- };
- // Oh joy, tracking algorithms
- moveEnemy = function ()
- {
- // Loops through active enemies
- for (i = 0; i < enemyList.length; i++)
- {
- // Define a var to save trouble later
- var enemy:MovieClip = _root["enemy" + enemyList[i]];
- // Fucklarge default values to make sure it doesnt just target nothing
- var closestPlant = 100000;
- var closestDist = 100000;
- // Needs to be outside of loop for later
- var plant:MovieClip;
- // Loop through plants
- for (a = 1; a <= plantTotal; a++)
- {
- // sets plant number for easyness
- plant = _root["plant" + a];
- // If the plant is active
- if (plant._visible == true)
- {
- // Check the horisontal distance to it (no point doing vertical, they're all coming from the top)
- if ((Math.abs(plant._x - enemy._x)) < closestDist)
- {
- // If its the closest, set new values and do it again
- closestPlant = a;
- closestDist = Math.abs(plant._x - enemy._x);
- }
- }
- }
- // Well wasnt that fun
- plant = _root["plant" + closestPlant];
- // Calculate angle to the plant
- var dx = enemy._x - plant._x;
- var dy = plant._y - enemy._y;
- enemy._rotation = Math.atan2(dx, dy) * 180 / Math.PI;
- // If its not already bumfucking the plant
- if (Math.sqrt((dy * dy) + (dx * dx)) > 30)
- {
- // move towards the plant
- if(enemy._y > 100){
- enemy._x -= enemySpeed * Math.sin(enemy._rotation * Math.PI / 180);
- enemy._y += enemySpeed * Math.cos(enemy._rotation * Math.PI / 180);
- }else{
- // stops em from tracking until they get past the trees
- enemy._y += enemySpeed
- }
- }else{
- // if the delay between attacks = 0
- if(enemy.attackDelay == 0){
- // attack and reset delay
- plant.health -= enemy.dmg
- if(plant.health <= 0){
- plant._visible = false
- }else if(plant.health <= (plant.maxHealth / 3)){
- plant.gotoAndPlay("Dam2")
- }else if (plant.health <= (2 * plant.maxHealth / 3)){
- plant.gotoAndPlay("Dam1")
- }
- enemy.attackDelay = enemy.delayMax
- }else{
- // if not, decrease
- enemy.attackDelay --
- }
- }
- }
- };
- // Bullet movement (strangely enough)
- moveBullets = function ()
- {
- // Well this is a convoluted fucking mess
- // Loops through bullets
- for (i = 0; i < bulletList.length; i++)
- {
- var bul:MovieClip = _root["bullet" + bulletList[i]];
- // Moves bullet the direction it should be going
- bul.life --
- if(bul.life == 0){
- bul.gotoAndPlay("Dead")
- }
- bul._x += bulletSpeed * Math.sin(bul._rotation * Math.PI / 180);
- bul._y -= bulletSpeed * Math.cos(bul._rotation * Math.PI / 180);
- // if the bullet is out of the screen
- if ((bul._x < -100) || (bul._x > 750) || (bul._y < -100) || (bul._y > 550))
- {
- // GTFO, bullet
- bul.gotoAndPlay("Dead")
- // copypasta'd loop is copypasta'd
- for (b = 0; b < bulletList.length; b++)
- {
- // removes bullet from the array
- if (bulletList[b] == bulletList[i])
- {
- bulletList.splice(b,1);
- break;
- }
- }
- }
- // Well fuck, more nested loops
- for (a = 0; a < enemyList.length; a++)
- {
- var en:MovieClip = _root["enemy" + enemyList[a]];
- // If the bullet hits an enemy
- if (en.hitTest(bul))
- {
- // damage the enemy
- en.health -= bulletDamage[bulletType];
- en.healthBar._width = (en.health / en.maxHealth) * 100
- // OH GOD WHEN WILL IT STOP
- for (b = 0; b < bulletList.length; b++)
- {
- // remove the bullet from the array
- if (bulletList[b] == bulletList[i])
- {
- bulletList.splice(b,1);
- break;
- }
- }
- // remove bullet from the screen
- bul.gotoAndPlay("Dead")
- if (en.health <= 0)
- {
- // removes enemy from the array if it is dead
- for (c = 0; c < enemyList.length; c++)
- {
- if (enemyList[c] == enemyList[a])
- {
- enemyList.splice(c,1);
- break;
- }
- }
- // deletes enemy on death
- money += en.money
- en.removeMovieClip();
- }
- }
- }
- }
- };
- // And breathe
- // SHOOTING
- map.onRelease = function()
- {
- // If theres been enough time since i last fired
- if (bulletTimer >= bulletDelay[bulletType])
- {
- var bullet:MovieClip = _root.attachMovie("bullet", "bullet" + bulletI, bulletDepth);
- bulletDepth += 2
- // funky array shit
- bulletList[bulletList.length] = bulletI;
- bulletI++;
- // Place bullet on player
- bullet._x = player._x;
- bullet._y = player._y;
- bullet._rotation = player._rotation;
- bullet.life = range
- // reset timer
- bulletTimer = 0;
- }
- };
- // The easy bit
- onEnterFrame = function ()
- {
- movePlayer();
- runWave();
- moveEnemy();
- moveBullets();
- bulletTimer++;
- };
- Key.addListener(keylistener);
Add Comment
Please, Sign In to add comment