Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Determining player's priority (...Is he under a tree?)

How do I basically tell if the player/event is under a tile layer? Like for instance, they're walking under a tree or a scaffolding or whatever?

I've looked through a bunch of different sections like Sprite_Character, Spriteset_Map, Game_Character, Scene_Map, etc and found a couple things on $game_map.priorities, tile_id and even screen_z, I'm just not sure to piece them all together to determine if player/event is 'under something'...
 
Well, the 'script' I'm writting doesn't know that !

The script spawns an arrow over the player's head if you're 'under' a tree and can't really see yourself. Thank you for spamming my topic, next please!
 

poccil

Sponsor

The following script I wrote checks the Z of the map's tiles and determines whether the player is under the tiles.  It defines two functions:

$game_player.isHidden? - returns true if the player is under the map's tileset, and false otherwise.
$game_map.z(x,y) - returns the Z of the map's tiles located at the specified x and y position.

Put the script in the script editor before the last script found there (normally "Main").  The code below follows.

Code:
class Bitmap
  # Fast methods for retrieving bitmap data
  RtlMoveMemory_pi = Win32API.new('kernel32', 'RtlMoveMemory', 'pii', 'i')
  RtlMoveMemory_ip = Win32API.new('kernel32', 'RtlMoveMemory', 'ipi', 'i')
  def setData(x)
    RtlMoveMemory_ip.call(self.address, x, x.length)    
  end
  def getData
    data = "rgba" * width * height
    RtlMoveMemory_pi.call(data, self.address, data.length)
    return data
  end
  def address
    if !@address
      buffer, ad = "rgba", object_id * 2 + 16
      RtlMoveMemory_pi.call(buffer, ad, 4)
      ad = buffer.unpack("L")[0] + 8
      RtlMoveMemory_pi.call(buffer, ad, 4)
      ad = buffer.unpack("L")[0] + 16
      RtlMoveMemory_pi.call(buffer, ad, 4)
      @address=buffer.unpack("L")[0]
    end
    return @address
  end
end

class Game_Player
  def isHidden?
    return self.screen_z < $game_map.z(self.x,self.y)
  end
end

class Game_Map
 def isFullyBlank?(tileid)
  return false if tileid<384
  tile=RPG::Cache.tile(@tileset_name,tileid,0)
  if tile.getData[/^(...\0)+$/] # Fast way to check whether alpha of all pixels is 0
    return true
  end
  return false
 end
 def z(x, y)
  zMax=0
  if @map_id != 0
   for i in [2, 1, 0]
    tile_id = data[x, y, i]
    next if tile_id == nil || tile_id==0 # Ignore blank tiles
    # Ignore tiles with priority nil or 0
    next if @priorities[tile_id] == nil
    if @priorities[tile_id]!=0
     ypos=(y*32)-((self.display_y) / 4)
     # Ignore if tile is fully blank
     if !isFullyBlank?(tile_id)
       # Now calculate Z of tile
       zMax=[zMax,ypos+@priorities[tile_id]*32+32].max
     end
    end
   end
  end
  return zMax
 end
end
 

poccil

Sponsor

The Bitmap class used above enables a bitmap's data to be read very quickly, instead of using the very slow functions "set_pixel" and "get_pixel".  Since the data is read as a string, it can even be saved to a file.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top