Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum operator{
- add,
- base_mult,
- mult
- }
- function Attribute(_default) constructor{
- base = _default;
- minimum = -infinity;
- maximum = infinity
- modifiers = [[],[],[]];
- calculated = false;
- calculated_value = 0;
- get_value = function(){
- if !calculated{
- calculate_value();
- }
- return calculated_value;
- }
- //TODO
- //variable_name = _variable_name;
- //apply = function(){
- // variable_instance_set(other.id,variable_name,get_value());
- //}
- //name = _name;
- set_max = function(_max){
- maximum = _max;
- calculated = false;
- }
- set_min = function(_min){
- minimum = _min;
- calculated = false;
- }
- set_min_max = function(_min,_max){
- minimum = _min;
- maximum = _max;
- calculated = false;
- }
- add_modifier = function(_mod){
- switch(_mod.op_type){
- case 0: array_push(modifiers[0], _mod); break;
- case 1: array_push(modifiers[1], _mod); break;
- case 2: array_push(modifiers[2], _mod); break;
- }
- calculated = false
- }
- clear_modifiers = function(){
- for(var op=0;op<array_length(modifiers);op++){
- array_delete(modifiers[op],0,array_length(modifiers[0]))
- }
- }
- remove_modifier = function(_modifier_name){
- for(var op=0;op<array_length(modifiers);op++){
- for(var modifier=0;modifier<array_length(modifiers[op]);modifier++){
- var current_modifier = modifiers[op][modifier];
- if current_modifier.name==_modifier_name{
- delete current_modifier;
- array_delete(modifiers[op],modifier,1);
- calculated = false
- return true
- }
- }
- }
- return false
- }
- calculate_value = function(){
- var calc = base;
- for(var a=0; a<array_length(modifiers[0]); a++){
- calc+=modifiers[0][a].amount;
- }
- for(var a=0;a<array_length(modifiers[1]);a++){
- calc += calc*modifiers[1][a].amount;
- }
- for(var a=0;a<array_length(modifiers[2]);a++){
- calc = calc*(1+modifiers[2][a].amount);
- }
- calculated_value = clamp(calc,minimum,maximum);
- calculated = true;
- }
- }
- function AttributeModifier(_name,_amount,_op_type) constructor{
- name = _name;
- amount = _amount;
- op_type = _op_type;
- }
Advertisement
Comments
-
- With the add modifier, you could completely remove the switch statement, so instead of this:
- switch(_mod.op_type){
- case 0: array_push(modifiers[0], _mod); break;
- case 1: array_push(modifiers[1], _mod); break;
- case 2: array_push(modifiers[2], _mod); break;
- }
- You would simply have this:
- array_push(modifiers[_mod.op_type], _mod);
- 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.
-
- wow, i forgot i put this here.
- yea, i already removed the switch statement when i cleaned up the code.
- but i dont understand how would a global array help shortening the code.
Add Comment
Please, Sign In to add comment
Advertisement