Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 01-07-2011, 08:58 AM
Switch Switch is offline
o.o
Switch's Avatar
Join Date: Jan 2007
Location: Philadelphia
Posts: 3,038
Switch has a spectacular aura about
Send a message via MSN to Switch
Baddie Core

Back on Ruins I was attempting to make a baddie system. At the time I saved it to use/finish in the future but I doubt I'll get a chance to. The only thing missing are wall/water checks because I was having trouble with them, as well as dealing damage, taking damage, and healing since we didn't have a health or damage system on Ruins. Enjoy an almost complete baddie system.

Core:
PHP Code:
/*
  This is the core system of baddies.
  No scripts but those that are a baddie class
  should be linking to this.
  -Switch
*/

/*
  this.bname -> baddie name
  this.range
   [0] -> aggro
   [1] -> attack
  this.speed
  this.damage
  this.freeze -> freeze time after an attack
  this.health
   [0] -> current
   [1] -> max
*/

enum {
  
IDLE,
  
WALK,
  
ATTACK,
  
DEAD
}

function 
onCreated() {
  
cancelEvents("PlayerAggro");
  
this.spawn_coords = {this.level.namethis.xthis.y};
  
this.mode IDLE;
  
this.showCharacter();
  
this.dir 2;
  
this.nick this.bname;
  
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
  
  if (
findNearestPlayer(this.xthis.y) != null) {
    
//begin loop
    
onTimeout();
  }
}

function 
onPlayerEnters() {
  if (
this.mode == IDLE) {
    
//begin loop
    
onTimeout();
  }
}
function 
onTimeout() {  
  
temp.pl findNearestPlayer(this.xthis.y);
  
temp.dx temp.pl.x-this.x;
  
temp.dy temp.pl.y-this.y;
  
temp.dist = (temp.dx^2+temp.dy^2)^0.5;
  if (
temp.dist <= this.range[0]) {
    
this.dcount 0;
    
this.mode ATTACK;
    
onPlayerAggro(temp.pl);
    return;
  }

  if (
this.dcount == 20) {
    
temp.rand int(random(0100));
    if (
temp.rand in |080|) {
      
this.mode IDLE;
    }
    else if (
temp.rand in |81100|) {
      
this.mode WALK;
    }
  }
  if (
this.mode == IDLE) {
    
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
    if (
this.dcount == 20) {
      
this.dcount 0;
      
this.dir int(random(03));
    }
  }
  else if (
this.mode == WALK) {
    
setCharAni("baddie_" @lowercase(this.bname)@ "-walk"null);
    if (
this.dcount == 20) {
      
this.dcount 0;
      for (
temp.i=0temp.i<359temp.i++) {
        
temp.angle random(0pi*2);
        
temp.dx cos(temp.angle)*this.speed*20;
        
temp.dy = -sin(temp.angle)*this.speed*20;
        if (
this.x+temp.dx in |064| && this.y+temp.dy in |064|) {
          
this.dir = (temp.angle in |0pi*0.25|?3:(temp.angle in |pi*0.25pi*0.75|?0:(temp.angle in |pi*0.75pi*1.25|?1:(temp.angle in |pi*1.25pi*1.75|?2:3))));
          
move(temp.dx/20temp.dy/200.10);
          
this.wdx temp.dx/20;
          
this.wdy temp.dy/20;
          break;
        }
      }
    }
    else {
      
move(this.wdxthis.wdy0.10);
    }
  }
  
this.dcount++;
  
setTimer(0.1);
}

function 
onPlayerAggro(pl) {
  if (
this.mode != ATTACK) {
    
//stop walk ani
    
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
    
onTimeout();
    return;
  }
  
  
//get distance to player
  
temp.dx pl.x-this.x;
  
temp.dy pl.y-this.y;
  
temp.dist = (temp.dx^2+temp.dy^2)^0.5;
  
//in hurt range
  
if (temp.dist <= this.range[1]) {
    
//so that you can see the attack
    
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
    
attackPlayer(pl);
    return;
  }
  
//out of aggro range (plus a quarter of it, to make it harder to get out of)
  
if (temp.dist this.range[0]*1.25) {
    
//stop walk ani
    
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
    
onTimeout();
    return;
  }
  
//walk
  
setCharAni("baddie_" @lowercase(this.bname)@ "-walk"null);
  
//return dx and dy to a distance of 1, then add in its speed
  
temp.angle getangle(temp.dxtemp.dy);
  
temp.movex cos(temp.angle)*this.speed*2;
  
temp.movey = -sin(temp.angle)*this.speed*2;
  
//change dir
  
this.dir = (temp.angle in |0pi*0.25|?3:(temp.angle in |pi*0.25pi*0.75|?0:(temp.angle in |pi*0.75pi*1.25|?1:(temp.angle in |pi*1.25pi*1.75|?2:3))));
  
//move baddie towards player
  
move(temp.movextemp.movey0.10);
  
  
scheduleEvent(0.1"PlayerAggro"pl);
}
function 
attackPlayer(pl) {
  
setCharAni("baddie_" @lowercase(this.bname)@ "-attack"null);
  
//hurt stuff goes here
  
scheduleEvent(1.5"PlayerAggro"pl);
}

function 
onRespawn() {
  
cancelEvents("PlayerAggro");
  
setTimer(0);
  
warpto(this.spawn_coords[0], this.spawn_coords[1], this.spawn_coords[2]);
  
this.this.spawn_coords[1];
  
this.this.spawn_coords[2];
  
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
  
this.mode IDLE;
  if (
findNearestPlayer(this.xthis.y) != null) {
    
onTimeout();
  }
}

function 
onPlayerLeaves() {
  if (
findNearestPlayer(this.xthis.y) == null) {
    
this.mode IDLE;
    
//end loop
    
setTimer(0);
    
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
  }

Baddie Class (example):
PHP Code:
/*
  This is an example of a baddie class that
  would link to the baddie core.
*/

function onCreated() {
  
this.bname "Slug";
  
this.range = {51};
  
this.speed 0.4;
  
this.damage 9001;
  
this.freeze 0.3;
  
this.health = {90019001};
  
this.join("baddie-core");
}

// It's over 9000!!!!!! 
__________________
Oh squiggly line in my eye fluid. I see you lurking there on the peripheral of my vision.
But when I try to look at you, you scurry away.
Are you shy, squiggly line?
Why only when I ignore you, do you return to the center of my eye?
Oh, squiggly line, it's alright, you are forgiven.
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 11:45 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.