Sujet: [VXace] Variables locales Lun 2 Jan 2012 - 17:00
Ce script ajoute un espace nom aux variables et permet de créer une notion de "portée locale". Elle fonctionne comme la version VX. Il est possible d'ajouter des commandes en monkeypatchant le module Commandes et en donnant a ses méthodes une portée statique :
Code:
# Utilitaire de variables locales pour RPG MAKER VX ACE # http://funkywork.blogspot.com # ------------------------------------------------------------------------------- #Liste des méthodes utilisables #=============================================================
# get(map_id, evt_id, id) -> retourne la variable n°id de l'evenement n°evt_id de la map n°map_id # => Alias : get_by_map(map_id, evt_id, id)
# get(evt_id, id) -> retourne la variable n°id de l'evenement n°evt_id de la map courante # => Alias : get_by_event(evt_id, id)
# get(id) -> retourne la variable n°id de l'evenement courant # => Alias : get_by_id(id)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # set(map_id, evt_id, id, valeur) -> attribue valeur à la variable n°id de l'evenement n°evt_id de la map n°map_id # => Alias : set_by_map(map_id, evt_id, id, valeur)
# set(evt_id, id, valeur) -> attribue valeur à la variable n°id de l'evenement n°evt_id de la map courante # => Alias : set_by_event(evt_id, id, valeur)
# set(id, valeur) -> attribue valeur à la variable n°id de l'evenement courant # => Alias : set_by_id(id, valeur)
#============================================================= # cmd("commande_name", args) => Retourne une opérande des variables # OU cmd(:commande, args) #=============================================================
# Liste des commandes utilisables (à appeler comme ceci : cmd(:nom_de_la_commande, parametre1, parametre2 etc...) ) module Commandes extend self # Random def rand(*args) return args[0] + Kernel.rand(args[1] - args[0]) end # id Map def id_map(*args) return $game_map.map_id end #Taille de l'équipe def team_size(*args) return $game_party.members.size end # Argent de l'équipe def gold(*args) return $game_party.gold end # Nombre de pas def steps(*args) return $game_party.steps end # temps de jeu def play_time(*args) return Graphics.frame_count / Graphics.frame_rate end # Chronometre def timer(*args) return $game_system.timer / Graphics.frame_rate end # nombre de sauvegarde def save_count(*args) return $game_system.save_count end # coord_x evenement def event_x(*args) return $game_map.events[args[0]].x if args[0] != -1 return $game_player.x end # coord_y evenement def event_y(*args) return $game_map.events[args[0]].y if args[0] != -1 return $game_player.y end # coord screen y def event_screen_y(*args) return $game_map.events[args[0]].screen_y if args[0] != -1 return $game_player.screen_y end # coord screen x def event_screen_x(*args) return $game_map.events[args[0]].screen_x if args[0] != -1 return $game_player.screen_x end # coord screen x def event_direction(*args) return $game_map.events[args[0]].direction if args[0] != -1 return $game_player.direction end # coord heros def heroes_x(*args) return Commandes::event_x(-1) end def heroes_y(*args) return Commandes::event_y(-1) end def heroes_screen_x(*args) return Commandes::event_screen_x(-1) end def heroes_screen_y(*args) return Commandes::event_screen_y(-1) end def heroes_direction(*args) return Commandes::event_direction(-1) end # Level of an actor def level(*args) return $game_actors[args[0]].level end # exp of an actor def exp(*args) return $game_actors[args[0]].exp end #hp of an actor def hp(*args) return $game_actors[args[0]].hp end # mp of an actor def mp(*args) return $game_actors[args[0]].mp end #max hp of an actor def max_hp(*args) return $game_actors[args[0]].maxhp end # max mp of an actor def max_mp(*args) return $game_actors[args[0]].maxmp end # attaque of an actor def atk(*args) return $game_actors[args[0]].atk end # defense of an actor def def(*args) return $game_actors[args[0]].def end # attaque of an actor def spi(*args) return $game_actors[args[0]].spi end # attaque of an actor def agi(*args) return $game_actors[args[0]].agi end # item def nb_item(*args) return $game_party.item_number($data_items[args[0]]) end # Get a true var value def var(*args) return $game_variables[args[0]] end def distance_px(*args) event1 = (args[0] == 0) ? $game_player : $game_map.events[args[0]] event2 = (args[1] == 0) ? $game_player : $game_map.events[args[1]] return Math.hypot((event1.screen_x - event2.screen_x), (event1.screen_y-event2.screen_y)) end # donne la distance entre 2 events en case def distance_case(*args) event1 = (args[0] == 0) ? $game_player : $game_map.events[args[0]] event2 = (args[1] == 0) ? $game_player : $game_map.events[args[1]] return Math.hypot((event1.x - event2.x), (event1.y-event2.y)) end end class Game_SelfVar attr_reader :id, :event_id, :map_id attr_accessor :value def initialize id, event_id, map_id, value @id, @event_id, @map_id, @value = id, event_id, map_id, value end def +(value) @value += value end def -(value) @value -= value end def *(value) @value *= value end def /(value) @value /= value end def %(value) @value %= value end def **(value) @value = @value ** value end class << self def get map_id, event_id, id elt = $game_selfVars.find do |var | var.id == id and var.event_id == event_id and var.map_id == map_id end if elt == nil return 0 else return elt.value end end def set map_id, event_id, id, value elt = $game_selfVars.index do |var | var.id == id and var.event_id == event_id and var.map_id == map_id end if elt == nil $game_selfVars << self.new(id, event_id, map_id, value) else $game_selfVars[elt].value = value end end end end module DataManager class << self alias ace_create_game_objects create_game_objects alias ace_extract_save_contents extract_save_contents alias ace_make_save_contents make_save_contents def create_game_objects ace_create_game_objects $game_selfVars = Array.new end def make_save_contents contents = ace_make_save_contents contents[:selfVars] = $game_selfVars return contents end def extract_save_contents(contents) ace_extract_save_contents contents $game_selfVars = contents[:selfVars] end end end
class Game_Interpreter def set_by_map map_id, event_id, id, value Game_SelfVar.set map_id, event_id, id, value end def set_by_event event_id, id, value Game_SelfVar.set @map_id, event_id, id, value end def set_by_id id, value Game_SelfVar.set @map_id, @event_id, id, value end def get_by_map map_id, event_id, id Game_SelfVar.get map_id, event_id, id end def get_by_event event_id, id Game_SelfVar.get @map_id, event_id, id end def get_by_id id Game_SelfVar.get @map_id, @event_id, id end def set *array case array.length when 2 then set_by_id array[0], array[1] when 3 then set_by_event array[0], array[1], array[2] when 4 then set_by_map array[0], array[1], array[2], array[3] else return false end end def get *array case array.length when 1 then return (get_by_id array[0]) when 2 then (get_by_event array[0], array[1]) when 3 then (get_by_map array[0], array[1], array[2]) else return 0 end end def cmd(commande, *args) case args.length when 0 return Commandes::send(commande) when 1 return Commandes::send(commande, args[0]) when 2 return Commandes::send(commande, args[0], args[1]) end end end
Je n'ai pas testé toutes les commandes qui viennent de l'ancien script de Nuki. Ce script est utile pour créer des systèmes complexes rapidement si vous avez besoin d'aide : molok6000_AT_hotmail.com je ne lis pas souvent mes mails sur cette adresse mais je vais souvent sur MSN. Bonne année.
Je me permet de fixer ce script parce que l'auteur n'a adapté que l'API des variables mais pas celle du module commande, donc évidement, ça pose problème. J'ai aussi amélioré le stockage des variables (qui ne demande plus un parcours de O(n²) mais O(1) + des fioritures en log). (Il aurait été possible de faire mieux en générant une clé unique mais bon... Il ajoute quelques petites données de lecture de la base de données:
Code:
# Variables locales version 1.1 # Par Grim (http://funkywork.blogspot.com) #============================================================================== # ** Simple_Matrix #------------------------------------------------------------------------------ # Manipulation de matrice facilement #==============================================================================
#-------------------------------------------------------------------------- # * Singleton de Command #-------------------------------------------------------------------------- extend self
#-------------------------------------------------------------------------- # * Opérandes standards #-------------------------------------------------------------------------- def random(x, y) (x + rand(y - x)) end def map_id() $game_map.map_id end def var(id) $game_variables[id] end def set_var(id, value) $game_variables[id] = value end #-------------------------------------------------------------------------- # * Opérandes de partie #-------------------------------------------------------------------------- def team_size() $game_party.members.size end def gold() $game_party.gold end def steps() $game_party.steps end def play_time() (Graphics.frame_count / Graphics.frame_rate) end def timer() $game_timer.sec end def save_count() $game_system.save_count end def battle_count() $game_system.battle_count end #-------------------------------------------------------------------------- # * Opérandes d'objets #-------------------------------------------------------------------------- def item_count(id) $game_party.item_number($data_items[id]) end def weapon_count(id) $game_party.item_number($data_weapons[id]) end def armor_count(id) $game_party.item_number($data_armors[id]) end #-------------------------------------------------------------------------- # * Opérandes d'acteurs #-------------------------------------------------------------------------- def actor(id) $game_actors[id] end def level(id) $game_actors[id].level end def experience(id) $game_actors[id].exp end def hp(id) $game_actors[id].hp end def mp(id) $game_actors[id].mp end def max_hp(id) $game_actors[id].maxhp end def max_mp(id) $game_actors[id].maxmp end def attack(id) $game_actors[id].atk end def defense(id) $game_actors[id].def end def magic(id) $game_actors[id].mat end def magic_defense(id) $game_actors[id].mdf end def agility(id) $game_actors[id].agi end def lucky(id) $game_actors[id].luk end #-------------------------------------------------------------------------- # * Opérandes d'events #-------------------------------------------------------------------------- def event_x(id) character = $game_player character = $game_map.events[id] unless id == 0 character.x end def event_y(id) character = $game_player character = $game_map.events[id] unless id == 0 character.y end def event_direction(id) character = $game_player character = $game_map.events[id] unless id == 0 character.direction end def event_screen_x(id) character = $game_player character = $game_map.events[id] unless id == 0 character.screen_x end def event_screen_y(id) character = $game_player character = $game_map.events[id] unless id == 0 character.screen_y end def heroes_x() Command.event_x(0) end def heroes_y() Command.event_y(0) end def heroes_direction() Command.event_direction(0) end def heroes_screen_x() Command.event_screen_x(0) end def heroes_screen_y() Command.event_screen_y(0) end def distance_between_case(ev1, ev2) event1 = (ev1 == 0) ? $game_player : $game_map.events[ev1] event2 = (ev2 == 0) ? $game_player : $game_map.events[ev2] Math.hypot((event1.x - event2.x), (event1.y-event2.y)) end def distance_between_pixel(ev1, ev2) event1 = (ev1 == 0) ? $game_player : $game_map.events[ev1] event2 = (ev2 == 0) ? $game_player : $game_map.events[ev2] Math.hypot((event1.screen_x - event2.screen_x), (event1.screen_y-event2.screen_y)) end #-------------------------------------------------------------------------- # * Opérandes de partie #-------------------------------------------------------------------------- def actor_id(position) $game_party.members[position] return actor ? actor.id : 0 end #-------------------------------------------------------------------------- # * Commande de lecture de la base de données #-------------------------------------------------------------------------- def read_data_monster(id, method = false) monster = $data_enemies[id] return monster unless method method = method.to_sym value = false value = 0 if method == :mhp || method == :hp value = 1 if method == :mmp || method == :mp value = 2 if method == :atk || method == :attack value = 3 if method == :def || method == :defense value = 4 if method == :mat || method == :magic_attack value = 5 if method == :mdf || method == :magic_defense value = 6 if method == :agi || method == :agility value = 7 if method == :luk || method == :lucky return monster.params[value] if value monster.send(method) end def read_data_skill(id, method = false) skill = $data_skills[id] return skill unless method skill.send(method.to_sym) end end
#============================================================================== # ** Game_Interpreter #------------------------------------------------------------------------------ # Ajout du support des variables locales #==============================================================================
class Game_Interpreter
#-------------------------------------------------------------------------- # * Récupération d'une variable local #-------------------------------------------------------------------------- def get(*arguments) result = 0 case arguments.length when 1; result = $game_selfVars[@map_id, @event_id, arguments[0]] when 2; result = $game_selfVars[@map_id, arguments[0], arguments[1]] when 3; result = $game_selfVars[arguments[0], arguments[1], arguments[2]] end return (result) ? result : 0 end
#-------------------------------------------------------------------------- # * Attribution d'une variable locale #-------------------------------------------------------------------------- def set(*arguments) case arguments.length when 2; $game_selfVars[@map_id, @event_id, arguments[0]] = arguments[1] when 3; $game_selfVars[@map_id, arguments[0], arguments[1]] = arguments[2] when 4; $game_selfVars[arguments[0], arguments[1], arguments[2]] = arguments[3] end end
#-------------------------------------------------------------------------- # * API de manipulation des commandes #-------------------------------------------------------------------------- def cmd(command, *arguments) Command.send(command.to_sym, *arguments) end def command(command, *arguments) cmd(command, *arguments) end
#-------------------------------------------------------------------------- # * API de récupérations des monstres/acteurs/techniques #-------------------------------------------------------------------------- def ennemy(id) cmd(:read_data_monster, id) end def actor(id) cmd(:actor, id) end def skill(id) cmd(:read_data_skill, id) end
class << self #-------------------------------------------------------------------------- # * Alias #-------------------------------------------------------------------------- alias local_create_game_objects create_game_objects alias local_make_save_contents make_save_contents alias local_extract_save_contents extract_save_contents
#-------------------------------------------------------------------------- # * Crée les objets du jeu #-------------------------------------------------------------------------- def create_game_objects local_create_game_objects $game_selfVars = Simple_Matrix.new end
#-------------------------------------------------------------------------- # * Sauve le contenu du jeu #-------------------------------------------------------------------------- def make_save_contents contents = local_make_save_contents contents[:self_vars] = $game_selfVars contents end
#-------------------------------------------------------------------------- # * Charge le contenu du jeu #-------------------------------------------------------------------------- def extract_save_contents(contents) local_extract_save_contents $game_selfVars = contents[:self_vars] end
end
end
(il est possible que l'indentation foire un petit peu... merci SublimeText ;D)