Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Code extracted from Castlevania: Moonlight Requiem,
- # It uses a resolution of 480*272 pixels.
- # When creating a map, you give the size in tiles, corresponding to the size of the background / 32 ( the size of the tiles )
- # it could be automated I guess, by providing the background image instead and calculate the "tile" dimensions from it.
- class Camera
- CENTER_X = 240 # Screen Width / 2
- CENTER_Y = 136 # Screen Height / 2
- @@x = 0
- @@y = 0
- def self.y=(v)
- @@y = v
- end
- def self.x=(v)
- @@x = v
- end
- def self.y
- @@y
- end
- def self.x
- @@x
- end
- def self.get_x(x)
- unless Map.get.nil?
- (Camera.adjust_x(Map.get, x*8) + 8007) / 8 - 1000
- else
- 0
- end
- end
- def self.get_y(y)
- unless Map.get.nil?
- (Camera.adjust_y(Map.get,y*8) + 8007) / 8 - 1000
- else
- 0
- end
- end
- def self.center(x, y)
- display_x = (x - CENTER_X)*8
- display_y = (y - CENTER_Y)*8
- max_x = (Map.get.width - 15) * 256 # 15 = 480/32 ( 32 size of the tiles )
- max_y = (Map.get.height - 8.5) * 256 # 8.5 = 272/32
- display_x = [0, [display_x, max_x].min].max
- display_y = [0, [display_y, max_y].min].max
- Camera.set_display_pos(Map.get,display_x, display_y)
- end
- def self.set_display_pos(map,x,y)
- map.display_x = (x + (map.width) * 256) %((map.width) * 256)
- map.display_y = (y + (map.height) * 256)%((map.height) * 256)
- self.x = x
- self.y = y
- end
- #--------------------------------------------------------------------------
- # layer_speed : 1 > front (faster)
- # 2 > normal (basic speed)
- # 4 > bg1 ( a bit slower )
- # 8 > bg2 ( even slower)
- #--------------------------------------------------------------------------
- def self.calc_layer_x(map,bitmap,layer_speed)
- return map.display_x/8 if bitmap.nil?
- w1 = bitmap.width - 480 # Screen Width
- w2 = map.width * 32 - 480
- if w1 < 0 or w2 <= 0 # IF background smaller than map or vice-versa
- return 0 # >Fixed background
- else
- return self.x * w1 / w2 / (4*layer_speed)
- end
- end
- #--------------------------------------------------------------------------
- def self.calc_layer_y(map,bitmap,layer_speed)
- return map.display_y/8 if bitmap.nil?
- h1 = bitmap.height - 272 # Screen Height
- h2 = map.height * 32 - 272
- if h1 < 0 or h2 <= 0
- return 0
- else
- return self.y * h1 / h2 / (4*layer_speed)
- end
- end
- #--------------------------------------------------------------------------
- def self.adjust_x(map,x)
- x - map.display_x
- end
- #--------------------------------------------------------------------------
- def self.adjust_y(map,y)
- y - map.display_y
- end
- end
- class Player
- attr_accessor :real_x, :real_y
- def initialize
- @real_x = 0
- @real_y = 0
- @image = Gosu::Image.new(WINDOW,"Player.png", true)
- end
- def draw
- Camera.center(@real_x, @real_y) # Will Keep the camera centered on that position ( except when we reach the limit of the map )
- @image.draw(Camera.get_x(@real_x), Camera.get_y(@real_y),100)
- end
- end
- class Map
- attr_reader :height, :width
- attr_accessor :display_x, :display_y
- @@current = nil
- def self.get
- @@current
- end
- def initialize(w=15,h=8.5)
- @display_y = @display_x = 0
- @width = w
- @height = h
- @image = Gosu::Image.new(WINDOW,"Map.png", true)
- @@current = self
- end
- def draw
- @image.draw(-Camera.calc_layer_x(self,@image,2), -Camera.calc_layer_y(self,@image,2), -90)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement