Chaos Project

RPG Maker => RPG Maker Scripts => Script Troubleshooting => Topic started by: legacyblade on July 22, 2011, 04:19:16 pm

Title: Character-specific zHud graphics
Post by: legacyblade on July 22, 2011, 04:19:16 pm
Hey all. I'm expanding the blizzABS zHud a bit. And one thing I'm doing is allowing for actor-specific hud graphics. I was designing a hud for a friend's game, and I had the idea to make the mana bar a "quintessence" (play quintessence if you want to know what that is). This was all well and good till I realized some characters will be using skills but don't have quintessence. So if you're using one of those characters, the big ball of quintessence doesn't make any sense. So I came up with the idea to let certain actors have a different mana-bar graphic. I actually got it working, except I can't figure out how to dispose of the mana bars from previous actors (they'll display below the new one). I came up with a simple way to check if the actor has changed. But the problem is that the mana-graphics are local variables, so I don't have a way to access them after the method has run once.

Spoiler: ShowHide
  def draw_sp
    @sp, @maxsp = actor.sp, actor.maxsp
    rate = (@maxsp > 0 ? @sp.to_f / @maxsp : 0)
    picPath = 'HUD/'
    if BlizzCFG::UNIQUE_ACTOR_GRAPHICS == true
      name = BlizzCFG.actor_SP(actor.id)
      picPath = picPath + 'actor/' + name + '_' if name != nil
      if (@prev_actor != actor.id && @prev_actor && @prev_actor != -1)
          b1.dispose
          b2.dispose
      end
    end
    b1 = RPG::Cache.picture(picPath + BlizzCFG::Z_SP_FILE)
    b2 = RPG::Cache.picture(picPath + BlizzCFG::Z_SP_FILE_EMPTY)
    @prev_actor = actor.id
    if BlizzCFG::Z_SP_TILING
      tiles = @maxsp / BlizzCFG::Z_SP_PER_TILE
      rows = (tiles.to_f / BlizzCFG::Z_SP_TILE_COLUMNS).ceil
      w, h = b1.width, b1.height
      self.bitmap.fill_rect(@sp_x, @sp_y, w * BlizzCFG::Z_SP_TILE_COLUMNS,
          h * rows, Color.new(0, 0, 0, 0))
      full_tiles = (rate * tiles).to_i
      semi_full = ((rate * tiles != full_tiles) ? 1 : 0)
      (0...full_tiles).each {|i|
          x = @sp_x + (i % BlizzCFG::Z_SP_TILE_COLUMNS) * w
          y = @sp_y + (i / BlizzCFG::Z_SP_TILE_COLUMNS) * h
          self.bitmap.blt(x, y, b1, Rect.new(0, 0, w, h))}
      if semi_full > 0
        x = @sp_x + (full_tiles % BlizzCFG::Z_SP_TILE_COLUMNS) * w
        y = @sp_y + (full_tiles / BlizzCFG::Z_SP_TILE_COLUMNS) * h
        if BlizzCFG::Z_SP_FILL_UP
          h2 = ((1 - rate * tiles + full_tiles) * h).to_i
          self.bitmap.blt(x, y, b2, Rect.new(0, 0, w, h2))
          self.bitmap.blt(x, y + h2, b1, Rect.new(0, h2, w, h - h2))
        else
          w2 = ((rate * tiles - full_tiles) * w).to_i
          self.bitmap.blt(x, y, b1, Rect.new(0, 0, w2, h))
          self.bitmap.blt(x + w2, y, b2, Rect.new(w2, 0, w - w2, h))
        end
      end
      ((full_tiles + semi_full)...tiles).each {|i|
          x = @sp_x + (i % BlizzCFG::Z_SP_TILE_COLUMNS) * w
          y = @sp_y + (i / BlizzCFG::Z_SP_TILE_COLUMNS) * h
          self.bitmap.blt(x, y, b2, Rect.new(0, 0, w, h))}
    else
      w, h = b1.width, b1.height
      x, y = @sp_x, @sp_y
      h2 = ((1 - rate) * h).to_i
      self.bitmap.blt(x, y, b2, Rect.new(0, 0, w, h2))
      self.bitmap.blt(x, y + h2, b1, Rect.new(0, h2, w, h - h2))
    end
  end


That gives me an error saying b1 and b2 are undefined. I have verified (via print commands) that this part of the code ONLY runs when you change actors and only if the SP bar has already been drawn. Does anyone have advice on how to dispose of these without making the sp bar an instance variable? I'm planning on making it possible to overide any of the graphics in the hud based on your current controlled character, so I wouldn't want to make the hud lag by adding all those extra instance variables.
Title: Re: Character-specific zHud graphics
Post by: nathmatt on July 22, 2011, 04:36:05 pm
thats because they are i would suggest making some kind of config module for the actors with or without quintessence then checking to see which 1 to draw through the config local variables are never saved so b1 and b2 will never be defined instead do what blizzard did on his if which is erase it with self.bitmap.fill_rect if you need any more help you can send me a demo and can look at fixing the hud but yea use the fill_rect method to erase it since you cant actually dispose it
Title: Re: Character-specific zHud graphics
Post by: legacyblade on July 22, 2011, 04:42:39 pm
I made a config module. It looks up the actor id and will return a string to be appended to the beginning of the file path.

And how would I use the self.bitmap.fill_rect to erase them? There are two bitmaps being made, and since the hud only runs the draw_sp method whenever the data in the hud changed (actor changed, character gets hit, uses a spell, changes which item is qued up), I can't just use it to erase b1 and b2 at the end of the function. (since they'd be erased 99% of the time and you'd only see them for a frame or two whenever any hud data changes)
Title: Re: Character-specific zHud graphics
Post by: nathmatt on July 22, 2011, 05:05:55 pm
you dont erase them at the end of the method you do it and the beginning of it this is how its being erased in blizzards part of the method
self.bitmap.fill_rect(@sp_x, @sp_y, w * BlizzCFG::Z_SP_TILE_COLUMNS,
         h * rows, Color.new(0, 0, 0, 0))
so right before you put
self.bitmap.blt(x, y, b2, Rect.new(0, 0, w, h2))
put
self.bitmap.fill_rect(x, y, w, h, Color.new(0,0,0,0))
that will erase your bar

edit use this it should work i wasn't thinking right you need to erase both images before either 1 is created
Spoiler: ShowHide
  def draw_sp
    @sp, @maxsp = actor.sp, actor.maxsp
    rate = (@maxsp > 0 ? @sp.to_f / @maxsp : 0)
    picPath = 'HUD/'
    if BlizzCFG::UNIQUE_ACTOR_GRAPHICS == true
      name = BlizzCFG.actor_SP(actor.id)
      picPath = picPath + 'actor/' + name + '_' if name != nil
      if (@prev_actor != actor.id && @prev_actor && @prev_actor != -1)
          b1.dispose
          b2.dispose
      end
    end
    b1 = RPG::Cache.picture(picPath + BlizzCFG::Z_SP_FILE)
    b2 = RPG::Cache.picture(picPath + BlizzCFG::Z_SP_FILE_EMPTY)
    @prev_actor = actor.id
    if BlizzCFG::Z_SP_TILING
      tiles = @maxsp / BlizzCFG::Z_SP_PER_TILE
      rows = (tiles.to_f / BlizzCFG::Z_SP_TILE_COLUMNS).ceil
      w, h = b1.width, b1.height
      self.bitmap.fill_rect(x, y, w, h, Color.new(0,0,0,0))
      self.bitmap.fill_rect(@sp_x, @sp_y, w * BlizzCFG::Z_SP_TILE_COLUMNS,
          h * rows, Color.new(0, 0, 0, 0))
      full_tiles = (rate * tiles).to_i
      semi_full = ((rate * tiles != full_tiles) ? 1 : 0)
      (0...full_tiles).each {|i|
          x = @sp_x + (i % BlizzCFG::Z_SP_TILE_COLUMNS) * w
          y = @sp_y + (i / BlizzCFG::Z_SP_TILE_COLUMNS) * h
          self.bitmap.blt(x, y, b1, Rect.new(0, 0, w, h))}
      if semi_full > 0
        x = @sp_x + (full_tiles % BlizzCFG::Z_SP_TILE_COLUMNS) * w
        y = @sp_y + (full_tiles / BlizzCFG::Z_SP_TILE_COLUMNS) * h
        if BlizzCFG::Z_SP_FILL_UP
          h2 = ((1 - rate * tiles + full_tiles) * h).to_i
          self.bitmap.blt(x, y, b2, Rect.new(0, 0, w, h2))
          self.bitmap.blt(x, y + h2, b1, Rect.new(0, h2, w, h - h2))
        else
          w2 = ((rate * tiles - full_tiles) * w).to_i
          self.bitmap.blt(x, y, b1, Rect.new(0, 0, w2, h))
          self.bitmap.blt(x + w2, y, b2, Rect.new(w2, 0, w - w2, h))
        end
      end
      ((full_tiles + semi_full)...tiles).each {|i|
          x = @sp_x + (i % BlizzCFG::Z_SP_TILE_COLUMNS) * w
          y = @sp_y + (i / BlizzCFG::Z_SP_TILE_COLUMNS) * h
          self.bitmap.blt(x, y, b2, Rect.new(0, 0, w, h))}
    else
      w, h = b1.width, b1.height
      x, y = @sp_x, @sp_y
      h2 = ((1 - rate) * h).to_i
      self.bitmap.fill_rect(x, y, w, h, Color.new(0,0,0,0))
      self.bitmap.fill_rect(@sp_x, @sp_y, w * BlizzCFG::Z_SP_TILE_COLUMNS,
          h * rows, Color.new(0, 0, 0, 0))
      self.bitmap.blt(x, y, b2, Rect.new(0, 0, w, h2))
      self.bitmap.blt(x, y + h2, b1, Rect.new(0, h2, w, h - h2))
    end
  end
Title: Re: Character-specific zHud graphics
Post by: legacyblade on July 22, 2011, 05:50:54 pm
Thanks :D I didn't know drawing a rectangle with no color or opacity would erase the one under it. That works perfectly! Now back to working on the hud!
Title: Re: Character-specific zHud graphics
Post by: nathmatt on July 22, 2011, 06:38:28 pm
yea that's 1 of the best ways to remove part of a bitmap when you can't use bitmap.clear