Advertisement
Guest User

Trebor777

a guest
Nov 21st, 2009
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.58 KB | None | 0 0
  1. # Code extracted from Castlevania: Moonlight Requiem,
  2. # It uses a resolution of 480*272 pixels.
  3.  
  4. # When creating a map, you give the size in tiles, corresponding to the size of the background / 32 ( the size of the tiles )
  5. # it could be automated I guess, by providing the background image instead and calculate the "tile" dimensions from it.
  6.  
  7. class Camera
  8.   CENTER_X = 240 # Screen Width / 2
  9.   CENTER_Y = 136 # Screen Height / 2
  10.  
  11.   @@x = 0
  12.   @@y = 0
  13.  
  14.   def self.y=(v)
  15.     @@y = v
  16.   end
  17.   def self.x=(v)
  18.     @@x = v
  19.   end
  20.  
  21.   def self.y
  22.     @@y
  23.   end
  24.   def self.x
  25.     @@x
  26.   end
  27.  
  28.   def self.get_x(x)
  29.     unless Map.get.nil?
  30.       (Camera.adjust_x(Map.get, x*8) + 8007) / 8 - 1000
  31.     else
  32.       0
  33.     end
  34.   end
  35.  
  36.   def self.get_y(y)
  37.     unless Map.get.nil?
  38.       (Camera.adjust_y(Map.get,y*8) + 8007) / 8 - 1000
  39.     else
  40.       0
  41.     end
  42.   end
  43.  
  44.   def self.center(x, y)
  45.     display_x = (x - CENTER_X)*8
  46.     display_y = (y - CENTER_Y)*8
  47.  
  48.     max_x = (Map.get.width - 15) * 256   # 15 = 480/32 ( 32 size of the tiles )
  49.     max_y = (Map.get.height - 8.5) * 256 # 8.5 = 272/32
  50.  
  51.     display_x = [0, [display_x, max_x].min].max
  52.     display_y = [0, [display_y, max_y].min].max
  53.     Camera.set_display_pos(Map.get,display_x, display_y)
  54.   end
  55.  
  56.   def self.set_display_pos(map,x,y)
  57.     map.display_x = (x + (map.width) * 256) %((map.width) * 256)
  58.     map.display_y = (y + (map.height) * 256)%((map.height) * 256)
  59.     self.x = x
  60.     self.y = y
  61.   end
  62.   #--------------------------------------------------------------------------
  63.   # layer_speed :   1 > front (faster)
  64.   #                 2 > normal (basic speed)
  65.   #                 4 > bg1 ( a bit slower )
  66.   #                 8 > bg2 ( even slower)
  67.   #--------------------------------------------------------------------------
  68.   def self.calc_layer_x(map,bitmap,layer_speed)
  69.     return map.display_x/8 if bitmap.nil?
  70.     w1 = bitmap.width - 480 # Screen Width
  71.     w2 = map.width * 32 - 480
  72.     if w1 < 0 or w2 <= 0  # IF background smaller than map or vice-versa
  73.       return 0 # >Fixed background
  74.     else
  75.       return self.x * w1 / w2 / (4*layer_speed)
  76.     end
  77.   end
  78.   #--------------------------------------------------------------------------
  79.   def self.calc_layer_y(map,bitmap,layer_speed)
  80.     return map.display_y/8 if bitmap.nil?
  81.     h1 = bitmap.height - 272 # Screen Height
  82.     h2 = map.height * 32 - 272
  83.     if h1 < 0 or h2 <= 0
  84.       return 0
  85.     else
  86.       return self.y * h1 / h2 / (4*layer_speed)
  87.     end
  88.   end
  89.   #--------------------------------------------------------------------------
  90.   def self.adjust_x(map,x)
  91.     x - map.display_x
  92.   end
  93.   #--------------------------------------------------------------------------
  94.   def self.adjust_y(map,y)
  95.     y - map.display_y
  96.   end
  97. end
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. class Player
  105.   attr_accessor :real_x, :real_y
  106.   def initialize
  107.     @real_x = 0
  108.     @real_y = 0
  109.     @image = Gosu::Image.new(WINDOW,"Player.png", true)
  110.   end
  111.  
  112.   def draw
  113.     Camera.center(@real_x, @real_y) # Will Keep the camera centered on that position ( except when we reach the limit of the map )
  114.     @image.draw(Camera.get_x(@real_x), Camera.get_y(@real_y),100)
  115.   end
  116. end
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. class Map
  124.   attr_reader :height, :width
  125.   attr_accessor :display_x, :display_y
  126.  
  127.   @@current = nil
  128.  
  129.   def self.get
  130.     @@current
  131.   end
  132.  
  133.   def initialize(w=15,h=8.5)
  134.     @display_y = @display_x = 0
  135.     @width = w
  136.     @height = h
  137.     @image = Gosu::Image.new(WINDOW,"Map.png", true)
  138.     @@current = self
  139.   end
  140.  
  141.   def draw
  142.     @image.draw(-Camera.calc_layer_x(self,@image,2), -Camera.calc_layer_y(self,@image,2), -90)
  143.   end
  144. end
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement