Permalink
Browse files

Added screenshot functionality on F12 and rewrote camera-management t…

…o be simpler and more dynamic.
  • Loading branch information...
Spooner committed Jul 26, 2011
1 parent 9429d69 commit 2e4704b1b93f7642f771510fe43b191b14e601a9
Showing with 37 additions and 40 deletions.
  1. +1 −0 .gitignore
  2. +8 −0 lib/game.rb
  3. +18 −24 lib/scenes/level.rb
  4. +10 −16 lib/scenes/pick_level.rb
View
@@ -2,6 +2,7 @@
/.idea
/doc
/pkg
+/screenshots
/README.html
/*.log
/*.exe
View
@@ -40,6 +40,14 @@
on :quit do
Kernel.exit
end
+
+ on :key_press, key(:f12) do
+ path = File.join(ROOT_PATH, 'screenshots')
+ FileUtils.mkdir_p path
+ files = Dir[File.join(path, "screenshot_*.*")]
+ last_number = files.map {|f| f =~ /(\d+)\.bmp$/; $1.to_i }.sort.last || 0
+ window.to_image.write(File.join(path, "screenshot_#{(last_number + 1).to_s.rjust(3, '0')}.bmp"))
+ end
end
window_view = window.default_view
View
@@ -14,10 +14,10 @@ class Level < GameScene
attr_reader :level_number
- def setup(level_number, background, background_camera, player_sheet)
+ def setup(level_number, background, player_sheet)
super()
- @level_number, @background, @background_camera, @player_sheet = level_number, background, background_camera, player_sheet
+ @level_number, @background, @player_sheet = level_number, background, player_sheet
@dynamic_objects = [] # Objects that need #update
@@ -27,22 +27,11 @@ def setup(level_number, background, background_camera, player_sheet)
@floor_map = FloorMap.new self, level_data['floor']['tiles'].split("\n"),
Kernel::const_get(level_data['floor']['default_tile'].to_sym), level_data['messages']
- # Create a camera for displaying the wall map
- @wall_camera = window.default_view
- @wall_camera.zoom_by window.scaling
- @wall_camera.center = @wall_camera.size / 2
-
- # Create a camera for displaying the floor map (which has origin set in the view)
- @floor_camera = @wall_camera.dup
- @floor_camera.y -= @wall_map.to_rect.height
-
start_tile = @floor_map.tile_at_grid([5, 2])
@player = Player.new(self, start_tile, start_tile.position + @floor_map.tile_size / 2, @player_sheet)
@initial_player_x = player.x
@distance_to_run = @floor_map.finish_line_x - @initial_player_x
- @half_size = @wall_camera.rect.size / 2
-
# Player's score, time remaining and progress through the level.
text_color = Color.new(190, 190, 255)
score_height = window.scaled_size.height - 6.625
@@ -114,9 +103,9 @@ def game_over(score)
when :menu
# Do nothing.
when :restart
- push_scene :level, @level_number, @background, @background_camera, @player_sheet
+ push_scene :level, @level_number, @background, @player_sheet
when :next
- push_scene :level, @level_number + 1, @background, @background_camera, @player_sheet
+ push_scene :level, @level_number + 1, @background, @player_sheet
end
end
end
@@ -181,24 +170,29 @@ def register
def calculate_visible_objects
# Update visible dynamic objects and stop them moving off the map. Others will just sleep off the side of the map.
- min_x = @player.x - @floor_camera.rect.width * 0.75
- max_x = @player.x + @floor_camera.rect.width
+ min_x = @player.x - window.scaled_size.width * 0.75 # Look behind.
+ max_x = @player.x + window.scaled_size.width # Look ahead a bit more to wake things up.
@visible_objects = @dynamic_objects.select {|o| o.x >= min_x and o.x <= max_x }
@visible_objects.sort_by!(&:z_order)
end
def render(win)
start_at = Time.now
- win.with_view @background_camera do
- win.draw @background
- end
+ win.draw @background
- win.with_view @wall_camera do
+ # Create a camera for displaying the wall map
+ wall_camera = window.default_view
+ wall_camera.zoom_by window.scaling
+ wall_camera.center = [@camera_x, wall_camera.rect.height / 2]
+ win.with_view wall_camera do
@wall_map.draw_on(win)
end
- win.with_view @floor_camera do
+ # Create a camera for displaying the floor map (which has origin set in the view)
+ floor_camera = wall_camera.dup
+ floor_camera.y -= @wall_map.to_rect.height
+ win.with_view floor_camera do
@floor_map.draw_on(win)
@visible_objects.each {|obj| obj.draw_shadow_on win }
@@ -219,8 +213,8 @@ def update_shaders
def move_camera
# Move the cameras to the player position (left side, plus an amount asked for from the player).
- @wall_camera.x = @floor_camera.x =
- @player.x + (@wall_camera.rect.width / 2) - (@wall_camera.rect.width * @player.screen_offset_x)
+
+ @camera_x = @player.x + (window.scaled_size.width / 2) - (window.scaled_size.width * @player.screen_offset_x)
end
def init_fps
View
@@ -69,12 +69,6 @@ def setup(player_number = 0)
protected
def create_floor
- @floor_camera = window.default_view
- @floor_camera.size = GAME_RESOLUTION * window.scaling
- @floor_camera.zoom_by window.scaling * 2
- @floor_camera.x = 30.5
- @floor_camera.y = -8
-
@floor_map = FloorMap.new self, FLOOR_TILES, CheckeredFloor, []
end
@@ -208,10 +202,7 @@ def create_background
end
@background = sprite @@background_image
-
- @background_camera = window.default_view
- @background_camera.size = @@background_image.size
- @background_camera.center = @background_camera.size / 2
+ @background.scale = [0.25] * 2
end
public
@@ -221,7 +212,7 @@ def clean_up
protected
def start_level(level_number)
- push_scene :level, level_number, @background, @background_camera, @player_sheets[@player_number]
+ push_scene :level, level_number, @background, @player_sheets[@player_number]
end
public
@@ -246,11 +237,14 @@ def register
public
def render(win)
- win.with_view @background_camera do
- win.draw @background
- end
-
- win.with_view @floor_camera do
+ win.draw @background
+
+ floor_camera = win.view
+ floor_camera.size = GAME_RESOLUTION * window.scaling
+ floor_camera.zoom_by window.scaling * 2
+ floor_camera.x = 30.5
+ floor_camera.y = -8
+ win.with_view floor_camera do
@floor_map.draw_on win
end

0 comments on commit 2e4704b

Please sign in to comment.