home *** CD-ROM | disk | FTP | other *** search
- class Enemy extends Entity
- {
- var entity;
- var attacktype;
- var inputlocked;
- var grounded;
- var isboss;
- var inp;
- var animationtick;
- var oldanim;
- var newanim;
- var AIstate;
- var AItimer;
- var HP;
- var dead;
- var isenemy;
- var zv;
- var x;
- var y;
- var AIdestx;
- var AIdesty;
- var xv;
- var yv;
- var z;
- var myname;
- var collide;
- static var ACCELLERATION = 2;
- static var MAX_SPEED = 3;
- static var JUMP_VELOCITY = 10;
- function Enemy(target, entityname, entityart, startx, starty, startz, startsize, collides)
- {
- super(target,entityname,startx,starty,startz,startsize,collides);
- this.entity.gotoAndStop(entityart);
- this.attacktype = entityart;
- this.inputlocked = false;
- this.grounded = true;
- if(entityart == "enemy4")
- {
- this.isboss = true;
- }
- else
- {
- this.isboss = false;
- }
- this.entity.attacking = false;
- this.entity.hurting = false;
- this.inp = "neutral";
- this.animationtick = 0;
- this.oldanim = "null";
- this.newanim = "idle";
- this.AIstate = "idle";
- this.AItimer = random(50);
- this.HP = 100;
- if(this.isboss)
- {
- this.HP = 500;
- }
- this.dead = false;
- this.isenemy = true;
- }
- function update()
- {
- this.AItimer = this.AItimer - 1;
- if(this.AIstate != "falling" && this.AItimer < 0)
- {
- this.AItimer = random(50) + 50;
- var _loc3_ = random(10);
- if(_loc3_ < 7)
- {
- this.AIstate = "attack";
- }
- if(_loc3_ >= 7)
- {
- this.AIstate = "idle";
- }
- if(this.isboss)
- {
- this.AIstate = "attack";
- }
- }
- this.inp = "neutral";
- if(this.AIstate == "stunned")
- {
- if(!this.entity.hurting)
- {
- this.newanim = "stunned";
- }
- }
- if(this.AIstate == "falling")
- {
- if(this.grounded)
- {
- this.newanim = "fallen";
- }
- else if(this.zv >= 0)
- {
- this.newanim = "fallup";
- }
- else
- {
- this.newanim = "falldown";
- }
- }
- var _loc2_ = 100;
- if(this.attacktype == "enemy2")
- {
- _loc2_ = 200;
- }
- if(this.AIstate == "attack")
- {
- if(this.x > GameStage.STAGE.player.getX() && GameStage.STAGE.player.getX() > GameStage.RIGHT_LIMIT - _loc2_)
- {
- this.addinput("left");
- }
- else if(this.x < GameStage.STAGE.player.getX() && GameStage.STAGE.player.getX() < GameStage.LEFT_LIMIT + _loc2_)
- {
- this.addinput("right");
- }
- else if(Math.abs(this.y - GameStage.STAGE.player.getY()) > 50)
- {
- if(this.x - GameStage.STAGE.player.getX() < _loc2_ && this.x - GameStage.STAGE.player.getX() >= 0)
- {
- this.addinput("right");
- }
- if(this.x - GameStage.STAGE.player.getX() > - _loc2_ && this.x - GameStage.STAGE.player.getX() < 0)
- {
- this.addinput("left");
- }
- if(this.x - GameStage.STAGE.player.getX() >= _loc2_)
- {
- this.addinput("right");
- if(this.y < GameStage.STAGE.player.getY())
- {
- this.addinput("down");
- }
- else
- {
- this.addinput("up");
- }
- }
- if(this.x - GameStage.STAGE.player.getX() <= - _loc2_)
- {
- this.addinput("left");
- if(this.y < GameStage.STAGE.player.getY())
- {
- this.addinput("down");
- }
- else
- {
- this.addinput("up");
- }
- }
- }
- else
- {
- if(Math.abs(this.x - GameStage.STAGE.player.getX() + _loc2_) < 20 && Math.abs(this.y - GameStage.STAGE.player.getY()) < 20)
- {
- this.entity._xscale = 100;
- this.addinput("attack");
- this.AIstate = "idle";
- }
- if(Math.abs(this.x - GameStage.STAGE.player.getX() - _loc2_) < 20 && Math.abs(this.y - GameStage.STAGE.player.getY()) < 20)
- {
- this.entity._xscale = -100;
- this.addinput("attack");
- this.AIstate = "idle";
- }
- if(this.x - GameStage.STAGE.player.getX() >= 0 && this.x - GameStage.STAGE.player.getX() < _loc2_)
- {
- this.addinput("right");
- }
- if(this.x - GameStage.STAGE.player.getX() >= _loc2_)
- {
- this.addinput("left");
- }
- if(this.x - GameStage.STAGE.player.getX() >= - _loc2_ && this.x - GameStage.STAGE.player.getX() < 0)
- {
- this.addinput("left");
- }
- if(this.x - GameStage.STAGE.player.getX() < - _loc2_)
- {
- this.addinput("right");
- }
- if(this.y - GameStage.STAGE.player.getY() > 0)
- {
- this.addinput("up");
- }
- else
- {
- this.addinput("down");
- }
- }
- }
- if(this.AIstate == "idle")
- {
- if(random(30) == 1)
- {
- this.AIdestx = this.x - 100 + random(200);
- this.AIdesty = this.y - 100 + random(200);
- if(this.AIdestx < GameStage.LEFT_LIMIT)
- {
- this.AIdestx = GameStage.LEFT_LIMIT;
- }
- if(this.AIdestx > GameStage.RIGHT_LIMIT)
- {
- this.AIdestx = GameStage.RIGHT_LIMIT;
- }
- if(this.AIdesty < 0)
- {
- this.AIdesty = 0;
- }
- if(this.AIdesty > 150)
- {
- this.AIdesty = 150;
- }
- }
- if(this.AIdestx < this.x + 10)
- {
- this.addinput("left");
- }
- if(this.AIdestx > this.x - 10)
- {
- this.addinput("right");
- }
- if(this.AIdesty < this.y + 10)
- {
- this.addinput("up");
- }
- if(this.AIdesty > this.y - 10)
- {
- this.addinput("down");
- }
- }
- this.takeaction();
- this.updateanimation();
- this.x += this.xv * GameStage.TIME_STEP;
- this.y += this.yv * GameStage.TIME_STEP;
- this.z += this.zv * GameStage.TIME_STEP;
- this.entity._x = this.x;
- this.entity._y = GameStage.GROUND_LEVEL + this.y / 3 - this.z;
- this.animationtick += GameStage.TIME_STEP;
- if(this.animationtick >= 1)
- {
- this.animationtick -= 1;
- this.entity.anims.nextFrame();
- }
- if(!this.dead)
- {
- EntityManager.MANAGER.entityCollide(this.myname,this);
- }
- this.entity.x = this.x;
- this.entity.y = this.y;
- this.entity.z = this.z;
- }
- function addinput(newinp)
- {
- switch(this.inp)
- {
- case "default":
- this.inp = newinp;
- break;
- case "upleft":
- if(newinp == "right")
- {
- this.inp = "up";
- }
- if(newinp == "down")
- {
- this.inp = "left";
- }
- if(newinp == "jump")
- {
- this.inp = "jump";
- }
- if(newinp == "attack")
- {
- this.inp = "attack";
- }
- break;
- case "up":
- if(newinp == "left")
- {
- this.inp = "upleft";
- }
- if(newinp == "right")
- {
- this.inp = "upright";
- }
- if(newinp == "down")
- {
- this.inp = "neutral";
- }
- if(newinp == "jump")
- {
- this.inp = "jump";
- }
- if(newinp == "attack")
- {
- this.inp = "attack";
- }
- break;
- case "upright":
- if(newinp == "left")
- {
- this.inp = "up";
- }
- if(newinp == "down")
- {
- this.inp = "right";
- }
- if(newinp == "jump")
- {
- this.inp = "jump";
- }
- if(newinp == "attack")
- {
- this.inp = "attack";
- }
- break;
- case "left":
- if(newinp == "right")
- {
- this.inp = "neutral";
- }
- if(newinp == "up")
- {
- this.inp = "upleft";
- }
- if(newinp == "down")
- {
- this.inp = "downleft";
- }
- if(newinp == "jump")
- {
- this.inp = "jump";
- }
- if(newinp == "attack")
- {
- this.inp = "attack";
- }
- break;
- case "neutral":
- if(newinp == "left")
- {
- this.inp = "left";
- }
- if(newinp == "right")
- {
- this.inp = "right";
- }
- if(newinp == "up")
- {
- this.inp = "up";
- }
- if(newinp == "down")
- {
- this.inp = "down";
- }
- if(newinp == "jump")
- {
- this.inp = "jump";
- }
- if(newinp == "attack")
- {
- this.inp = "attack";
- }
- break;
- case "right":
- if(newinp == "left")
- {
- this.inp = "neutral";
- }
- if(newinp == "up")
- {
- this.inp = "upright";
- }
- if(newinp == "down")
- {
- this.inp = "downright";
- }
- if(newinp == "jump")
- {
- this.inp = "jump";
- }
- if(newinp == "attack")
- {
- this.inp = "attack";
- }
- break;
- case "downleft":
- if(newinp == "right")
- {
- this.inp = "down";
- }
- if(newinp == "up")
- {
- this.inp = "left";
- }
- if(newinp == "jump")
- {
- this.inp = "jump";
- }
- if(newinp == "attack")
- {
- this.inp = "attack";
- }
- break;
- case "down":
- if(newinp == "left")
- {
- this.inp = "downleft";
- }
- if(newinp == "right")
- {
- this.inp = "downright";
- }
- if(newinp == "up")
- {
- this.inp = "neutral";
- }
- if(newinp == "jump")
- {
- this.inp = "jump";
- }
- if(newinp == "attack")
- {
- this.inp = "attack";
- }
- break;
- case "downright":
- if(newinp == "left")
- {
- this.inp = "down";
- }
- if(newinp == "up")
- {
- this.inp = "right";
- }
- if(newinp == "jump")
- {
- this.inp = "jump";
- }
- if(newinp == "attack")
- {
- this.inp = "attack";
- }
- break;
- case "attack":
- if(newinp == "jump")
- {
- this.inp = "jumpattack";
- }
- }
- }
- function takeaction()
- {
- if(this.x < GameStage.LEFT_LIMIT)
- {
- this.inp = "right";
- }
- if(this.x > GameStage.RIGHT_LIMIT)
- {
- this.inp = "left";
- }
- if(this.y < 0)
- {
- this.inp = "down";
- }
- if(this.y > 150)
- {
- this.inp = "up";
- }
- if(!this.inputlocked)
- {
- if(this.inp == "left" || this.inp == "upleft" || this.inp == "downleft")
- {
- if(this.grounded)
- {
- this.xv -= Enemy.ACCELLERATION * GameStage.TIME_STEP;
- }
- }
- if(this.inp == "right" || this.inp == "upright" || this.inp == "downright")
- {
- if(this.grounded)
- {
- this.xv += Enemy.ACCELLERATION * GameStage.TIME_STEP;
- }
- }
- if(this.inp == "upleft" || this.inp == "up" || this.inp == "upright")
- {
- if(this.grounded)
- {
- this.yv -= Enemy.ACCELLERATION * GameStage.TIME_STEP;
- }
- }
- if(this.inp == "downleft" || this.inp == "down" || this.inp == "downright")
- {
- if(this.grounded)
- {
- this.yv += Enemy.ACCELLERATION * GameStage.TIME_STEP;
- }
- }
- else if(this.inp == "jump")
- {
- if(this.grounded)
- {
- this.xv *= 1.5;
- this.grounded = false;
- this.zv = Enemy.JUMP_VELOCITY;
- this.newanim = "jump";
- }
- }
- else if(this.inp == "attack")
- {
- if(this.grounded)
- {
- this.newanim = "attack";
- this.entity.attacking = true;
- this.inputlocked = true;
- }
- }
- }
- else if(!this.entity.attacking && !this.entity.hurting)
- {
- this.inputlocked = false;
- }
- if(this.grounded)
- {
- if(this.xv < - Enemy.MAX_SPEED)
- {
- this.xv = - Enemy.MAX_SPEED;
- }
- if(this.xv > Enemy.MAX_SPEED)
- {
- this.xv = Enemy.MAX_SPEED;
- }
- if(this.yv > Enemy.MAX_SPEED)
- {
- this.yv = Enemy.MAX_SPEED;
- }
- if(this.yv < - Enemy.MAX_SPEED)
- {
- this.yv = - Enemy.MAX_SPEED;
- }
- this.xv *= Math.pow(0.8,GameStage.TIME_STEP);
- this.yv *= Math.pow(0.8,GameStage.TIME_STEP);
- if(this.attacktype == "enemy2" && this.entity.attacking)
- {
- this.xv += Enemy.MAX_SPEED * this.entity._xscale / 70;
- }
- }
- else
- {
- this.zv -= GameStage.GRAVITY * GameStage.TIME_STEP;
- if(this.zv < -25)
- {
- this.zv = -20;
- }
- if(this.zv > 25)
- {
- this.zv = 20;
- }
- if(this.z + this.zv < 0)
- {
- if(this.AIstate == "falling")
- {
- new gamesound("fall",100);
- this.zv = (- this.zv) * 0.6;
- this.z = 0;
- if(this.zv < 7)
- {
- new gamesound("foof",100);
- var _loc3_ = EntityManager.MANAGER.addEntity(this.x,this.y,this.z,"explode","explode");
- _loc3_.entity._xscale = _parent._parent._xscale;
- this.entity.dead = true;
- }
- }
- else
- {
- this.zv = 0;
- this.z = 0;
- this.grounded = true;
- this.entity.anims.gotoAndStop("idle");
- this.entity.anim = "idle";
- this.inputlocked = false;
- }
- if(this.xv < - Enemy.MAX_SPEED)
- {
- this.xv = - Enemy.MAX_SPEED;
- }
- if(this.xv > Enemy.MAX_SPEED)
- {
- this.xv = Enemy.MAX_SPEED;
- }
- if(this.yv > Enemy.MAX_SPEED)
- {
- this.yv = Enemy.MAX_SPEED;
- }
- if(this.yv < - Enemy.MAX_SPEED)
- {
- this.yv = - Enemy.MAX_SPEED;
- }
- }
- }
- }
- function updateanimation()
- {
- if(this.grounded && !this.entity.attacking && !this.entity.hurting && this.AIstate != "stunned" && this.AIstate != "falling")
- {
- if(this.xv < -2)
- {
- this.entity._xscale = -100;
- }
- if(this.xv > 2)
- {
- this.entity._xscale = 100;
- }
- if(Math.abs(this.xv) < 0.7 && Math.abs(this.yv) < 0.7)
- {
- this.xv = 0;
- this.newanim = "idle";
- }
- else
- {
- this.newanim = "walk";
- }
- }
- if(this.newanim != this.oldanim)
- {
- switch(this.newanim)
- {
- case "idle":
- this.entity.anims.gotoAndStop("idle");
- this.entity.anim = "idle";
- break;
- case "walk":
- this.entity.anims.gotoAndStop("walk");
- this.entity.anim = "walk";
- break;
- case "attack":
- this.entity.anims.gotoAndStop("attack");
- this.entity.anim = "attack";
- this.xv = this.entity._xscale / 10;
- break;
- case "attack2":
- this.entity.anims.gotoAndStop("attack2");
- this.entity.anim = "attack2";
- this.xv = this.entity._xscale / 25;
- break;
- case "jump":
- this.entity.anims.gotoAndStop("jump");
- this.entity.anim = "jump";
- break;
- case "hurt":
- this.entity.anims.gotoAndStop("hurt");
- this.entity.anim = "hurt";
- break;
- case "stunned":
- this.entity.anims.gotoAndStop("stunned");
- this.entity.anim = "stunned";
- break;
- case "fallup":
- this.entity.anims.gotoAndStop("fallup");
- this.entity.anim = "fallup";
- break;
- case "falldown":
- this.entity.anims.gotoAndStop("falldown");
- this.entity.anim = "falldown";
- break;
- case "fallen":
- this.entity.anims.gotoAndStop("fallen");
- this.entity.anim = "fallen";
- }
- }
- this.oldanim = this.newanim;
- }
- function lift(amount)
- {
- this.zv += amount;
- this.grounded = false;
- }
- function hurt(amount, stun)
- {
- var _loc3_ = this.HP;
- this.HP -= amount;
- if(stun)
- {
- this.AItimer = 50;
- this.AIstate = "stunned";
- }
- if(this.HP < 0)
- {
- if(this.isboss)
- {
- _root.gotoAndStop("endcutscene");
- }
- this.AIstate = "falling";
- this.collide = false;
- if(this.grounded)
- {
- this.zv += 15;
- }
- this.dead = true;
- }
- else
- {
- this.newanim = "hurt";
- this.oldanim = "idle";
- }
- this.entity.hurting = true;
- this.inputlocked = true;
- }
- function remove()
- {
- this.entity.removeMovieClip();
- }
- }
-