/lib/chingu/traits/viewport.rb
Ruby | 77 lines | 35 code | 9 blank | 33 comment | 4 complexity | 420f3ce7183342b5d5ed3035cddde9b7 MD5 | raw file
Possible License(s): LGPL-2.1
1#-- 2# 3# Chingu -- OpenGL accelerated 2D game framework for Ruby 4# Copyright (C) 2009 ippa / ippa@rubylicio.us 5# 6# This library is free software; you can redistribute it and/or 7# modify it under the terms of the GNU Lesser General Public 8# License as published by the Free Software Foundation; either 9# version 2.1 of the License, or (at your option) any later version. 10# 11# This library is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14# Lesser General Public License for more details. 15# 16# You should have received a copy of the GNU Lesser General Public 17# License along with this library; if not, write to the Free Software 18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19# 20#++ 21 22module Chingu 23 module Traits 24 # 25 # A chingu trait providing velocity and acceleration logic. 26 # Adds parameters: velocity_x/y, acceleration_x/y and modifies self.x / self.y 27 # Also keeps previous_x and previous_y which is the x, y before modification. 28 # Can be useful for example collision detection 29 # 30 module Viewport 31 attr_accessor :viewport 32 33 module ClassMethods 34 def initialize_trait(options = {}) 35 trait_options[:viewport] = {:apply => true}.merge(options) 36 end 37 end 38 39 def setup_trait(options) 40 @viewport_options = {:debug => false}.merge(options) 41 42 @viewport = Chingu::Viewport.new() 43 @viewport.x = options[:viewport_x] || 0 44 @viewport.y = options[:viewport_y] || 0 45 46 super 47 end 48 49 def inside_viewport?(object) 50 puts "Deprecated, use self.viewport.inside?() instead" 51 object.x >= @viewport.x && object.x <= (@viewport.x + $window.width) && 52 object.y >= @viewport.y && object.y <= (@viewport.y + $window.height) 53 end 54 55 # Returns true object is outside the view port 56 def outside_viewport?(object) 57 puts "Deprecated, use self.viewport.outside?() instead" 58 not inside_viewport?(object) 59 end 60 61 # Take care of laggy viewport movements 62 def update_trait 63 @viewport.move_towards_target 64 super 65 end 66 67 # 68 # Override game states default draw that draws objects relative to the viewport. 69 # It only draws game objects inside the viewport. (GOSU does no such optimizations) 70 # 71 def draw 72 #self.game_objects.draw_relative(-@viewport.x, -@viewport.y) 73 @viewport.apply { super } 74 end 75 end 76 end 77end