Advertisement
cenestral

Attribute system

Jun 26th, 2021
2,009
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. enum operator{
  2.     add,
  3.     base_mult,
  4.     mult
  5. }
  6.  
  7.  
  8. function Attribute(_default) constructor{
  9.     base = _default;
  10.     minimum = -infinity;
  11.     maximum = infinity
  12.     modifiers = [[],[],[]];
  13.     calculated = false;
  14.     calculated_value = 0;
  15.    
  16.     get_value = function(){
  17.         if !calculated{
  18.             calculate_value();
  19.         }
  20.        
  21.         return calculated_value;
  22.     }
  23.    
  24.     //TODO
  25.     //variable_name = _variable_name;
  26.     //apply = function(){
  27.     //  variable_instance_set(other.id,variable_name,get_value()); 
  28.     //}
  29.    
  30.     //name  = _name;
  31.    
  32.     set_max = function(_max){
  33.         maximum = _max;
  34.         calculated = false;
  35.     }
  36.    
  37.     set_min = function(_min){
  38.         minimum = _min;
  39.         calculated = false;
  40.     }
  41.    
  42.     set_min_max = function(_min,_max){
  43.         minimum = _min;
  44.         maximum = _max;
  45.         calculated = false;
  46.     }
  47.    
  48.     add_modifier = function(_mod){
  49.         switch(_mod.op_type){
  50.             case 0: array_push(modifiers[0], _mod); break; 
  51.             case 1: array_push(modifiers[1], _mod); break; 
  52.             case 2: array_push(modifiers[2], _mod); break; 
  53.         }
  54.        
  55.         calculated = false 
  56.     }
  57.    
  58.     clear_modifiers = function(){
  59.         for(var op=0;op<array_length(modifiers);op++){
  60.             array_delete(modifiers[op],0,array_length(modifiers[0]))
  61.         }
  62.     }
  63.    
  64.     remove_modifier = function(_modifier_name){
  65.         for(var op=0;op<array_length(modifiers);op++){
  66.             for(var modifier=0;modifier<array_length(modifiers[op]);modifier++){
  67.                 var current_modifier = modifiers[op][modifier];
  68.                 if current_modifier.name==_modifier_name{
  69.                     delete current_modifier;
  70.                     array_delete(modifiers[op],modifier,1);
  71.                     calculated = false
  72.                     return true
  73.                 }
  74.             }
  75.         }
  76.         return false
  77.     }
  78.    
  79.     calculate_value = function(){
  80.         var calc = base;
  81.  
  82.         for(var a=0; a<array_length(modifiers[0]); a++){
  83.             calc+=modifiers[0][a].amount;  
  84.         }
  85.  
  86.         for(var a=0;a<array_length(modifiers[1]);a++){
  87.             calc += calc*modifiers[1][a].amount;   
  88.         }
  89.  
  90.         for(var a=0;a<array_length(modifiers[2]);a++){
  91.             calc = calc*(1+modifiers[2][a].amount);
  92.         }
  93.        
  94.         calculated_value = clamp(calc,minimum,maximum);
  95.         calculated = true;
  96.            
  97.     }
  98. }
  99.  
  100. function AttributeModifier(_name,_amount,_op_type) constructor{
  101.     name = _name;
  102.     amount = _amount;
  103.     op_type = _op_type;
  104. }
  105.  
Advertisement
Comments
  • RefresherTowel
    1 year (edited)
    # text 0.58 KB | 0 0
    1. With the add modifier, you could completely remove the switch statement, so instead of this:
    2.  
    3. switch(_mod.op_type){
    4. case 0: array_push(modifiers[0], _mod); break;
    5. case 1: array_push(modifiers[1], _mod); break;
    6. case 2: array_push(modifiers[2], _mod); break;
    7. }
    8.  
    9. You would simply have this:
    10.  
    11. array_push(modifiers[_mod.op_type], _mod);
    12.  
    13. You could probably also write some functions to do the calculations and have them stored in a global array that corresponds to op_type, which would let you shorten the calculations code as well.
  • cenestral
    1 year
    # text 0.17 KB | 0 0
    1. wow, i forgot i put this here.
    2. yea, i already removed the switch statement when i cleaned up the code.
    3. but i dont understand how would a global array help shortening the code.
Add Comment
Please, Sign In to add comment
Advertisement