Dev Blog / Challenge Solution – Using Hime’s Script to Rotate Direction Arrows

A long long time ago, we talked about Hime’s tile swap script and how it’s great for changing the tile at a specific location. I issued a challenge that’s actually implemented in Vidar – using the script to rotate an arrow tile clockwise. Let’s dig into the solution.

Direction Arrow Gif

Making the Arrows

The first thing to do is actually make the arrows. These live in – you guessed it – a Puzzle Option. I call it the direction_hash, and each arrow is represented by the following

[x coordinate, y coordinate] => ["facing", unique ID]

I do that for every arrow, using “N” “S” “E” or “W” for facing,
and using just 1, 2, 3, etc. for each arrow’s unique ID.

Once we get that, we call a function called rotate_tile, where we give the function the unique ID (here called tile_id) and a rotation direction (90 degrees clockwise, 90 degrees counterclockwise, or flip, meaning 180-degrees). The function in all its glory looks like this:

def rotate_tile(tile_id, dir = "")
 rotating_hash = {1660 => "A246", 1653 => "A254", 1661 => "A245", 1652 => "A253"}
 flip_hash = {1660 => "A254", 1653 => "A245", 1661 => "A253", 1652 => "A246"}
 revrotating_hash = {1660 => "A245", 1653 => "A253", 1661 => "A246", 1652 => "A254"}
 my_hash = {}
 case dir
      when "Clock"
           my_hash = rotating_hash
      when "Flip"
           my_hash = flip_hash
      when "Cntrclock"
           my_hash = revrotating_hash
      end
 direction_hash = self.puzzle.choice.direction_hash 
 #figure out the coordinates
 direction_hash.each { |key, value|
      if value[1] != nil && value[1] == tile_id
           coord = key
           #figure out what the tile is currently
           current_id = $game_map.tile_id(coord[0] + self.puzzle.x, coord[1] + self.puzzle.y, 0)
           #swap the tile
           $gi.pos_swap(coord[0] + self.puzzle.x, coord[1] + self.puzzle.y, my_hash[current_id], 0, self.puzzle.map)
           break
      end
 }
end

Let’s talk about how we build this. First, we need to know two things about our direction arrows:

  1. What their RPG Maker Tile ID is. This is different than any other number you’ve come across. It’s a number assigned by the engine to each tile in the tileset. Why do we want this? We can get the ID of any tile by coordinate by calling $game_map.tile_id(x,y,layer). For more information about layers, check out the original post about Hime’s script.[[link]] The ID looks like 1660. To get it requires trial and error. Make a map with all of your arrows, and then puts $game_map.tile_id(x,y,layer) for each of them to get the correct numbers.
  2. What their Hime’s Tile ID is. It looks like “A246,” and I talked about how to get it in the original post.

Now I have a way to figure out which arrow is at a given position, and I have a way to place any arrow tile we want at a current position! This is the meat. The rest is gravy.

Replace With What?

The next step we need to figure out is what to replace an arrow with. At the top of my function, I have three hashes – rotating, flip, and revrotating. Each keys a RPG Maker Tile ID to a different tile’s Hime Tile ID. Specifically:

  • For rotating_hash, if the RPG Maker Tile ID is the tile for North, the Hime Tile ID is east. This is the case for all four, going clockwise.
  • For flip_hash, if the RPG Maker Tile ID is the tile for North, the Hime Tile ID is south. This is the case for all four, going 180 degrees.
  • You guess it, for revrotating_hash, we go from North to West, counterclockwise.

I ask what direction I’m supposed to go in our case statement, and then I have “my_hash” – the correct key to how to flip an arrow.

Putting it All Together

The second half of the function runs through the direction hash and finds the correct tile, by its unique id. It gets the current id using our $game_map.tile_id function. And then it calls Hime’s pos_swap based on that key. We throw the break in there to stop from looping through everything needlessly.

This function actually lives in the Switch class in Vidar, just because it’s the only time I’ll ever rotate an arrow.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s