6. Ways to make your baddy even better!
This section will list a couple of ways you can add to the basic baddy
we have already scripter to make it better.
Bouncing
The first way we'll improve it, is with "bouncing". When the
NPC is hit by the player, it ought to bounce away (like all baddies do).
So the following script ought to be added to do such a feat. We're
going to put this in our section that we check to see if it was hit.
//Bouncing when hit
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; //states that the baddy has been "hit"
while(this.hit==1){ //what to do if hit
i+=.5; //keeps track of how long the baddy
has been "bouncing"
if (x>playerx&&!onwall(x+2.5,y+1.5)) x+=.5;
//moves baddy away from player .5 spaces
if (x<playerx&&!onwall(x-.5,y+1.5)) x-=.5;
//moves baddy away from player .5 spaces
if (y>playery&&!onwall(x+1.5,y+2.5)) y+=.5;
//moves baddy away from player .5 spaces
if (y<playery&&!onwall(x-1.5,y-.5)) y-=.5;
//moves baddy away from player .5 spaces
if (i>=3.5){ //makes
sure the loop doesn't go on infinately
this.hit=0;
//states that the baddy is done "bouncing"
i=0; //resets
the var
}
sleep .05; //to keep
the while loop moving
}
timeout=.05;
}
//End of Script
This, simply put, pauses all interaction by the npc while it bounces
away from the player.
Laying Stuff When its Killed
Most people like their baddies to drop items for the player to pick
up once it has been defeated. To do so, we'll just add some stuff
to part where we tell it to die.
//Dropping Stuff when dead
if (timeout&&hearts<=0){
i = random (0,100); //sets the var
"i" to a random number between 0 and 100
if (i<10) lay greenrupee; //
makes the NPC lay a green rupee if "i" is less than 10
else if (i<15) lay bluerupee; //makes
the npc lay a blue rupee if "i" is greater than 10 and less than 15
else if (i<30) lay heart; //makes
the npc lay a heart if "i" is greater than 15 and less than 30
sprite=40;
}
//End of Script
Of course you can adjust the numbers so the baddy is more/less likely
to lay an item, or add more items for it to lay.
The "Spinning" Death
When players die, they turn in circles for a little bit, so we might
as well add that into our baddy. We'll just add a small bit of code
to the death script:
//Spinning When Dead
if (timeout&&hearts<=0){
while(d<=8){ //starts the while
loop so the baddy won't do anything else
dir=(dir+3)%4; //this
makes the direction the baddy faces the remainder of: (dir+3)/4
d++; //keeps track of
how many times the baddy has turned
sleep .1; //pauses the
spinning animation
}
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
Since we changed some basic parts to the script, the following is the
latest "Basic Baddy" script, that can be found on the level basicbaddies.graal.
//Basic Baddy
if (created){
showcharacter;
setcharprop #n,Basic Baddy;
hearts=3;
hurtdx=0;
hurtdy=0;
}
if (playerenters||wasthrown){
this.speed=.3;
this.pow=2;
timeout=.05;
}
if (timeout&&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;
timeout=.05;
}
}
if (hearts>0&&(playertouchsne||(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=1;
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
Any other suggestions made to NPC later in this file will be changes
/ additions to the above script. If you open the level basicbaddies.graal
the NPC with the name "Basic Baddy" has that exact script.