GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2015-10-09 14:11:24

neighborhoodbaker
Member
Registered: 2015-10-09
Posts: 1

animate_to_alarm()

Expand///animate_to_alarm(alarm_set_rate,alarm_used,[start_index],[number_of_images]);
//
//  Returns the image index for a sprite that corresponds 
//  to the alarm being used 
//
//      alarm_set_rate          what the alarm being used is set at each time it is used, real
//
//      alarm_used               the alarm that the image_index will be stretched to, real.
//
//      start_index               (Optional) The starting image_index, real.
//
//      number_of_images    (Optional) The number of images, real.
//
/// GMLscripts.com/license
{
    //init
    if (image_speed != 0) {image_speed = 0;}
    var alarm_set_rate = argument[0];
    var alarm_used = argument[1];
    var start_index = 0;
    var number_of_images = image_number;
    
    //for when multiple animations are in one sprite
    if (argument_count > 2){
        start_index = argument[2];
        number_of_images = argument[3];
    } 
    
    //returns the image index according to the time left in alarm
    for (var i = 1;i <= number_of_images;i += 1){
        if ( alarm_used >= alarm_set_rate - (i/number_of_images*alarm_set_rate) ) {
            image_index = i-1+start_index;
            break;        
        }
    }
    
    return image_index;
}

Example: I make an enemy with a state machine.  The states are WANDER, ALERT, CHARGE, ATTACK, HIT, DEATH.  If the enemy has line of sight to the player then he enters the alert state.  If the enemy is alert for a set amount of time and within range, then enemy enters the charge state. The charge state represents the time the enemy will take to 'wind up' before actually producing a projectile/hitbox.  Time spent in the charge state is governed by an alarm and when the alarm triggers the enemy enters the attack state which also governed by an alarm. In the attack state the enemy immediately creates his hitbox/projectile in the players direction and then enters back into the wander state when the alarm triggers. Normally adjusting the charge alarm or attack alarm would mean that I would have to either create more images in the charge and attack sprites or I would have to fiddle with the image_speed to make the animation coincide with the time spent in the state.  I made this script so if for some reason I needed to make the enemy charge/'wind up' for a longer time, I could just change the alarm.  The script will evenly distribute the time spent on each image according to that alarm. So if I only had a 4 image animation of the enemy charging/'winding up', instead of adding more images or making sure the image speed perfectly matches the length of the alarm, I can just run this script and regardless of the alarm size and number of images it will adjust the animation to always fit perfectly within the alarm (as long as the alarm is not shorter than the number of images).  I feel like this is hard to explain but it has made matching animations to time spent attacking/charging/hit/dying much much easier because it will always match perfectly.

Expand///enemy state machine

switch (state){
      case WANDER:
          .............
      break;
      case ALERT:
          alarm = charge_alarm;
          state = CHARGE;
      break;
      case CHARGE:
          sprite_index = spr_charge;
          animate_to_alarm(charge_alarm,alarm);
          if alarm < 0{
              alarm = attack_alarm;
              state = ATTACK;
          }
      break;
      case ATTACK:
          sprite_index = spr_attack;
          animate_to_alarm(attack_alarm,alarm);
          if alarm < 0{
              state = WANDER;
          }
      break;
      case HIT:
          .............
      break;
      case DEATH:
          .............
      break;
  }
  
  if alarm >= 0{
      alarm--;
  }

Last edited by neighborhoodbaker (2016-01-14 16:46:43)

Offline

Board footer

Powered by FluxBB