10. Scripting A Horseman With a Sword
So you want the Horseman to use his sword instead of trampling the
player?
First off, we need to assign a few things at the beginning just like
with the swordsman.
//New Beginning Part
if (created){
showcharacter;
this.speed=.5;
setcharprop #5,ride.gif;
setcharprop #1,sword1.gif; //This
sets the sword image to be used by the NPC
swordpower=1; //This is the power
of the sword used, i removed the variable this.pow
this.attackdist=3;
//this sets how close the baddy must be before using his sword.
sprite=34;
hearts=3;
hurtdx=0;
hurtdy=0;
timeout=.05;
}
//End of Script
Thats it for the beginning portion.
Now we got to add in the attack detection stuff, as well as what to
do when its supposed to use its sword.
//New Movement/Attack stuff
if (timeout&&hearts>0&&this.swing!=1){
if (sprite<36){
sprite++;
}else{
sprite=35;
}
this.distx=abs(playerx-x);
this.disty=abs(playery-y);
if (this.distx<=this.disty){
if (playery<y)dir=0;
if (playery>y)dir=2;
}else{
if (playerx<x)dir=1;
if (playerx>x)dir=3;
}
if (this.distx==0){
this.addy=this.speed;
}else if (this.disty==0){
this.addx=this.speed;
}else if (this.distx>this.disty){
this.ratio=this.disty/this.distx;
this.addx=this.speed;
this.addy=this.speed*this.ratio;
}else if (this.disty>this.distx){
this.ratio=this.distx/this.disty;
this.addy=this.speed;
this.addx=this.speed*this.ratio;
}
if (y<playery&&!onwall(x+1.5,y+3+this.addy)) y+=this.addy;
if (y>playery&&!onwall(x+1.5,y-this.addy)) y-=this.addy;
if (x<playerx&&!onwall(x+3+this.addx,y+1.5)) x+=this.addx;
if (x>playerx&&!onwall(x-this.addx,y+1.5)) x-=this.addx;
this.distance=((this.distx*this.distx)+(this.disty*this.disty))^.5;
if (this.distance<=this.attackdist){ //if
this is true then the baddy is close enough to use his sword
this.swing=1; //tells
the npc that it needs to use its sword
sprite=9; //the first sword
swinging sprite
}
timeout=.05;
}
if (timeout&&this.swing==1&&hearts>0){ //this
is the sword using stuff
if (sprite<13){ //contiues the
sword swinging sprits
sprite++;
}else{
sprite=35; //resets
it to the first horse riding sprite
this.distance=this.attackdist+5; //to
prevent the npc from swinging his sword again
this.swing=0; //to say
the npc is done using his sword
}
timeout=.05; //to continue the other
timeout loop
}
//End of Script
Well this does what it's supposed to...
But the horseman doesn't face the player when using his sword... so
it's kind of pointless. We can add in a simple small portion of code
and that will be fixed. Just add in the following script in with
the aprt that checks for this.distance<=this.attackdistance and itll
work:
//New attack distance stuff
if (this.distance<=this.attackdist){
this.swing=1;
if (dir==3){dir=2}; //This
dir stuff makes the horesman face the right way to hit the player
else if (dir==2){dir=1};
else if (dir==1){dir=0};
else if (dir==0){dir=3};
sprite=9;
}
//End of Script
Now it works right.
And as you'll remember from the sword man, we need to fix a glitch
in the part where the baddy is hit, so lets do that.
//Fixing the sword glitch
if ((washit||wasshot||exploded||waspelt)&&hearts>0){
if (washit){
hurtdx=x;
hurtdy=y;
hearts-=playerswordpower;
}
if (wasshot){
hearts--;
}
if (exploded){
hearts--;
}
if (waspelt){
if (peltwithbush) hearts-=.5;
if (peltwithsign || peltwithvase
|| peltwithstone) hearts-=1;
if (peltwithblackstone) hearts
-=1.5;
}
sprite=39;
this.hit=1;
while(this.hit==1){
i+=.5;
if (x>playerx&&!onwall(x+2.5,y+1.5)) x+=.5;
if (x<playerx&&!onwall(x-.5,y+1.5)) x-=.5;
if (y>playery&&!onwall(x+1.5,y+2.5)) y+=.5;
if (y<playery&&!onwall(x-1.5,y-.5)) y-=.5;
if (i>=3.5){
this.hit=0;
i=0;
}
sleep .05;
}
this.swing=0; //Necessary to stop
any glitches accomodating the sword swing.
timeout=.05;
}
//End of Script
That should be it.
So we'll do the Basic Sword Horseman script:
//Basic Sword Horseman
if (created){
showcharacter;
setcharprop #5,ride.gif;
setcharprop #1,sword1.gif;
setcharprop #n,Basic Sword Horseman;
hearts=3;
hurtdx=0;
hurtdy=0;
}
if (playerenters||wasthrown){
swordpower=1;
this.attackdist=3;
sprite=34;
this.speed=.5;
timeout=.05;
}
if (timeout&&this.swing!=1&&isleader){
if (hearts>0){
if (sprite<36){
sprite++;
}else{
sprite=35;
}
this.distx=abs(playerx-x);
this.disty=abs(playery-y);
if (this.distx<=this.disty){
if (playery<y)dir=0;
if (playery>y)dir=2;
}else{
if (playerx<x)dir=1;
if (playerx>x)dir=3;
}
if (this.distx==0){
this.addy=this.speed;
}else if (this.disty==0){
this.addx=this.speed;
}else if (this.distx>this.disty){
this.ratio=this.disty/this.distx;
this.addx=this.speed;
this.addy=this.speed*this.ratio;
}else if (this.disty>this.distx){
this.ratio=this.distx/this.disty;
this.addy=this.speed;
this.addx=this.speed*this.ratio;
}
if (y<playery&&!onwall(x+1.5,y+3+this.addy))
y+=this.addy;
if (y>playery&&!onwall(x+1.5,y-this.addy))
y-=this.addy;
if (x<playerx&&!onwall(x+3+this.addx,y+1.5))
x+=this.addx;
if (x>playerx&&!onwall(x-this.addx,y+1.5))
x-=this.addx;
this.distance=((this.distx*this.distx)+(this.disty*this.disty))^.5;
if (this.distance<=this.attackdist){
this.swing=1;
if (dir==3){dir=2};
else if (dir==2){dir=1};
else if (dir==1){dir=0};
else if (dir==0){dir=3};
sprite=9;
}
timeout=.05;
}
}
if (timeout&&isleader&&this.swing==1&&hearts>0){
if (sprite<13){
sprite++;
}else{
sprite=35;
this.distance=this.attackdist+5;
this.swing=0;
}
timeout=.05;
}
if ((washit||wasshot||exploded||waspelt)&&hearts>0){
if (washit){
hurtdx=x;
hurtdy=y;
hearts-=playerswordpower;
}
if (wasshot){
hearts--;
}
if (exploded){
hearts--;
}
if (waspelt){
if (peltwithbush) hearts-=.5;
if (peltwithsign || peltwithvase || peltwithstone)
hearts-=1;
if (peltwithblackstone) hearts -=1.5;
}
sprite=39;
this.swing=0;
this.hit=1;
while(this.hit==1){
i+=.5;
if (x>playerx&&!onwall(x+2.5,y+1.5)) x+=.5;
if (x<playerx&&!onwall(x-.5,y+1.5)) x-=.5;
if (y>playery&&!onwall(x+1.5,y+2.5)) y+=.5;
if (y<playery&&!onwall(x-1.5,y-.5)) y-=.5;
if (i>=3.5){
this.hit=0;
i=0;
}
sleep .05;
}
timeout=.05;
}
if (hurtdx!=0||hurtdy!=0){
sprite=39;
hurtdx=0;
hurtdy=0;
}
if (timeout&&hearts<=0&&isleader){
while(d<=8){
dir=(dir+3)%4;
d++;
sleep .1;
}
i = random(0,100);
if (i<10) lay greenrupee;
else if (i<15) lay bluerupee;
else if (i<30) lay heart;
sprite=40;
}
//End of Script
This will have the name Basic Sword Horseman in the level basicswordmen.graal.