9. Scripting A Horseman
What if you want your baddy to ride a horse instead of just walk around.
Well, first of all, in the created part, we need to give the horseman
a horse gif. So lets do that.
//Giving the badyd a horse
if (created){
showcharacter;
this.speed=.5;
setcharprop #5,ride.gif; //this gives
the npc it's horse.
sprite=34; //thats the first horse riding
sprite. Needed so the player can see the horse.
this.pow=3;
hearts=3;
hurtdx=0;
hurtdy=0;
timeout=.05;
}
//End of Script
Now the horseman has a horse.
But we need to change the walking sprites.
//New Walking Script
if (timeout&&hearts>0){
if (sprite<36){ //the riding
sprites
sprite++;
}else{
sprite=35; //the first
riding sprite
}
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;
timeout=.05;
}
//End of Script
So now the horseman can walk and what not, so lets compile a basic
horseman.
//Basic Horseman
if (created){
showcharacter;
setcharprop #7,wbow1.gif;
setcharprop #n,Basic Archer;
hearts=3;
hurtdx=0;
hurtdy=0;
}
if (playerenters||wasthrown){
this.speed=.5;
timeout=.05;
}
if (timeout&&this.shoot!=1&&isleader){
if (hearts>0){
sprite=(sprite%8)+1;
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;
if((x-players[this.player].x<1&&players[this.player].x-x<1)||(y-players[this.player].y<1&&players[this.player].y-y<1)){
this.shoot=1;
sprite=33;
sleep 1;
}
timeout=.05;
}
}
if (timeout&&isleader&&hearts>0&&this.shoot==1){
shootarrow dir;
sleep .1;
this.shoot=0;
sprite=1;
timeout=.05;
}
if (this.hit!=1&&(washit||shotbyplayer||exploded||waspelt)&&hearts>0){
if (washit){
hurtdx=x;
hurtdy=y;
hearts-=playerswordpower;
}
if (shotbyplayer){
hearts--;
}
if (exploded){
hearts--;
}
if (waspelt){
if (peltwithbush) hearts-=.5;
if (peltwithsign || peltwithvase || peltwithstone)
hearts-=1;
if (peltwithblackstone) hearts -=1.5;
}
sprite=39;
this.shoot=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 (created){
showcharacter;
setcharprop #5,ride.gif;
setcharprop #n,Basic Horseman;
hearts=3;
hurtdx=0;
hurtdy=0;
}
if (playerenters||wasthrown){
sprite=34;
this.pow=3;
this.speed=.5;
timeout=.05;
}
if (timeout&&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;
timeout=.05;
}
}
if (hearts>0&&(playertouchsme||(abs((x+1.5)-(playerx+1.5))<=3
&&abs((playery+1.5)-(y+1.5))<=3))) {
hurt this.pow;
}
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;
}
timeout=.05;
}
if (hurtdx!=0||hurtdy!=0){
sprite=34;
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;
}
}
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
The NPC with the name Basic Horseman in the level basicbaddies.graal
will have that script. Also, in the next couple of sections I'll
suggest changes to that script. So any horseman scripting I refer
to will be taken from there.