Jump to content

Announcing our new website

To find out the latest about our new website, visit Game Dev Unlimited

- - - - -

Compass script

compass script bob423 help dood

  • You cannot reply to this topic
4 replies to this topic

#1 Bob423

    Bless your face

  • Member
  • 884 posts

Posted 30 December 2011 - 06:36 AM

So here's what i need. a compass that appears in the lower left, or upper right hand corner of the screen, and points toward the place the player is supposed to go next, regardless of what map the player is on.

#2 kellessdee

    mrrow ~!

  • Moderator
  • 904 posts

Posted 30 December 2011 - 04:25 PM

The problem with this kind of script, is that rpg maker does not provide any way of getting some kind of map directional relativity.
i.e. there's no easy way to find the path from Map A to Map B

Nonetheless, I thought I might try and tackle this one.

Instructions:
-Put above Main
-Configure MapManager, C_FORE (_IMG, _X, _Y)

To configure MapManager, think of it as a 2d grid or matrix of your world map, and put every map id that would belong to that x, y location within an array.
i.e you have 5 maps in your world, map01 is a map which contains an inner map (house), map02 - and then map03, map04 and map05 are to the left, bottom left and bottom of map01, respectively
MapManager =
[
[ [1, 2], [3] ],
[ [5], [4] ]
]
It looks a little odd, but this was the quickest (that I could see) way to query relative map locations.
If you think of the array like a grid, it may be easier to see what I mean
[
[[ (0,0) ], [ (1,0) ]],
[[ (0,1) ], [ (1,1) ]]
]

Each x, y location must be an array of MapIDs in order to account for inner maps, etc.

SOME WARNINGS(IMPORTANT):
-I made this in linux, so I haven't really tested it...I hope it does SOMETHING
-I have not yet accounted for what happens when you are on the same map as your target location -- if the script works, right now it should make the compass point north. I wanted to see that the basics work before touching on that.
-currently, to use this script you must first call
$compass.show
to show the compass,
$compass.hide
hides it; as well as set a target location, by using
$game_map.set_target_location(map_id)

Lemme know what happens!

# Configure your map here.
# Think of the world as a 2D Grid, and place each
# map id into the array accordingly.
MapManager =
[
[[id], [id], [id], [id]],
[[id], [id], [id], [id]]
]

# If you want a background for the compass
# use these configurations
C_BACK_IMG = nil
C_BACK_X = nil
C_BACK_Y = nil

# The following configuration is for the foreground
# of the compass. This will be the rotating part
C_FORE_IMG = ""
C_FORE_X = 0
C_FORE_Y = 0

# NOTE: any _IMG should be a filename
#	any _X or _Y should be integers, representing
#	  the compass's screen x, y position

class Game_Map
  attr_reader  :map_id
  def initialize
    @map_id = 18
  end
  def set_target_location(id)
	    MapManager.each_index do |y|
		  MapManager[y].each_index do |x|
			    if MapManager[y][x].include?(id)
				  @target_x, @target_y = x, y
				  return
			    end
		  end
	    end
  end

  def remove_target
	    @target_x = @target_y = nil
  end

  def current_map_location
	    MapManager.each_index do |y|
		  MapManager[y].each_index do |x|
			    if MapManager[y][x].include?(@map_id)
				  return [x, y]
			    end
		  end
	    end
  end

  def relative_direction_to_target
	    return 0 if !@target_x || !@target_y
	    x, y = current_map_location
	    # Get new target x,y relative to 0, 0
	    target_x, target_y = @target_x - x, @target_y - y
	    # Get relative angle
	    angle = Math.atan2(target_y, target_x)
	    # Convert to degrees, where 0 is north
	    (angle * 180.0 / Math::PI + 90) % 360
  end

end



class Spriteset_Map

alias :new_update_compass :update unless method_defined? :new_update_compass

  def update
	new_update_compass
	$compass.update
  end
end

class Compass

  def show
	return if @sprite
	@sprite = Sprite.new
	@sprite.bitmap = RPG::Cache.picture(C_FORE_IMG)
	@sprite.x, @sprite.y = C_FORE_X, C_FORE_Y
	if C_BACK_IMG
	  @back = Sprite.new
	  @back.bitmap = RPG::Cache.picture(C_BACK_IMG)
	  @back.x, @back.y = C_BACK_X, C_BACK_Y
	end
  end

  def hide
	return unless @sprite
	@sprite.dispose; @sprite = nil
	if @back
	  @back.dispose; @back = nil
	end
  end

  def update
	return unless @sprite
	angle = $game_map.relative_direction_to_target.to_i
	@sprite.angle = angle if @sprite.angle != angle
  end  
end

$compass = Compass.new


#3 Bob423

    Bless your face

  • Member
  • 884 posts

Posted 30 December 2011 - 07:07 PM

cool, thanks. one question though. the way you explained the map manager doesnt really make sense to me.

edit: i typed in some numbers into the map manager
MapManager =
[
[[1], [2], [3], [4]],
[[5], [6], [7], [18]]
]

tested it, and got a no method error with this line
alias :new_update_compass :update unless method_defined? new_update_compass
line 70

i have 18 in there, because thats the id for the debug room

Edited by Bob423, 30 December 2011 - 07:18 PM.


#4 kellessdee

    mrrow ~!

  • Moderator
  • 904 posts

Posted 30 December 2011 - 08:37 PM

WHOOPs, I forgot the `:` in front of the symbol is should read:
alias :new_update_compass :update unless method_defined? :new_update_compass

As for the MapManager, try to think of it like your rpg maker xp tiles. A 2 dimensional grid. Rather than tiles, each square would be an entire map...imagine you took all the maps in your game and laid them out, side by side, as they would appear in game flow.
[N W][ N ][N E]
[ W ][ X ][ E ]
[S W][ S ][S E]
Assuming X is the current map the player is on, and N is to the north
That would be, say, your entire world. However, some maps might be INSIDE another map (i.e. a house in a village) and thus, that would mean 2 maps actually belong to that "section" of your world.


So, say, on your map1, if the player walks south they end up in map2
if they walk east from map2 they end up in map3
if they walk north from map3 they end up in map4

it would look like this:
[
[[1],[4]],
[[2],[3]]
]

Now, lets say map 1 also has a house in it...well technically, that house map is in the same location as map 1...so
[
[[1,5],[4]],
[[2  ],[3]]
]
Now, the "house" map (map 5) is in the same tile as map1

EDIT: Also, i should probably mention it, you'll have to build a grid that includes ALL your game maps,

[
[[ids],[ids],[ids],[ids],[ids]],
[[ids],[ids],[ids],[ids],[ids]],
[[ids],[ids],[ids],[ids],[ids]]
[[ids],[ids],[ids],[ids],[ids]]
]
This would be a world, that has 4x5 main maps -- it will be bigger/smaller depending on the number of maps in your game.

I know it's an irritating work around--but it's really the best you'll get with RPG Maker (that I know of, if anyone knows a better way speak up).

[ASIDE, JUST SOME OF KELLS THOUGHTS]Does a 2d game really need a persistent compass? It's not like you can change your orientation...as well as, its just using up what little screen space RMXP gives you to work with...
What about just giving the player a world map and drawing an x on it?

#5 Bob423

    Bless your face

  • Member
  • 884 posts

Posted 31 December 2011 - 07:22 AM

thanks dood





0 user(s) are reading this topic

members, guests, anonymous users