Changeset 27516

Show
Ignore:
Timestamp:
12/28/08 18:19:42 (4 years ago)
Author:
isaisstillalive
Message:
  • Collection関連をリファクタリング。Eventableモジュールをincludeする形にした。
  • Collectableモジュールを作成し、Sprite自身のメソッドを再定義しないようにした。
Location:
lang/ruby/starframe
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/starframe/lib/starframe/sprite/collection.rb

    r27508 r27516  
     1require "starframe/eventable" 
     2 
    13module StarFrame #:nodoc: 
    24  class Sprite 
    3     # 現在所属しているコレクション。 
    4     attr_reader :collection 
     5    # = コレクション所属モジュール 
     6    # コレクションに所属できるようにするモジュール。 
     7    # Sprite::Collectionをロードすると自動的にSpriteにインクルードされる。 
     8    module Collectable 
     9      # 現在所属しているコレクション。 
     10      attr_reader :collection 
     11      # 新たなコレクションに所属させる。 
     12      #  
     13      # 所属していたコレクションからは自動的に削除される。 
     14      def collection= collection 
     15        collection.add self 
     16      end 
     17       
     18      # 所属しているコレクションから自分を削除する。 
     19      def vanish 
     20        return unless @collection 
     21        @collection.delete self 
     22        @collection = nil 
     23      ensure 
     24        return self 
     25      end 
     26    end 
     27    include Collectable 
    528     
    6     # 所属しているコレクションから自分を削除する。 
    7     def vanish 
    8       return unless @collection 
    9       @collection.delete self 
    10       @collection = nil 
    11     ensure 
    12       return self 
    13     end 
    14      
    15     # 新たなコレクションに所属させる。 
    16     #  
    17     # 所属していたコレクションからは自動的に削除される。 
    18     def collection= collection 
    19       collection << self 
    20     end 
    21      
    22     class Collection < Array 
     29    # = コレクション 
     30    class Collection 
     31      include Enumerable 
     32      include Eventable 
     33       
     34      #:stopdoc: 
     35      protected 
     36      attr_reader :sprites 
     37       
     38      def initialize #:nodoc: 
     39        @sprites = [] 
     40      end 
     41      #:startdoc: 
     42       
     43      public 
    2344      # スプライトを所属させる。 
    2445      #  
    2546      # そのスプライトが所属していたコレクションからは自動的に削除される。 
    26       def << sprite 
    27         defect_collection = sprite.instance_variable_get :@collection 
    28         defect_collection.delete sprite if defect_collection 
     47      def add sprite 
     48        defect_collection = sprite.collection 
     49        defect_collection.sprites.delete sprite if defect_collection 
     50         
     51        @sprites << sprite 
    2952        sprite.instance_variable_set :@collection, self 
    30         super 
     53      end 
     54      alias << add 
     55       
     56      # スプライトを所属から外す。 
     57      def delete sprite 
     58        @sprites.delete sprite 
     59        sprite.instance_variable_set :@collection, nil 
     60      end 
     61       
     62      # 所属している全てのスプライトに対してブロックを評価する。 
     63      def each &block 
     64        @sprites.each &block 
     65      end 
     66       
     67      # 所属しているスプライトの数を取得する。 
     68      def size 
     69        @sprites.size 
    3170      end 
    3271       
    3372      # 所属している全てのスプライトの更新時イベントをコールする。 
    34       def call_update 
    35         each{ |sprite| sprite.call } 
     73      def update 
     74        @sprites.each &:call_update 
    3675      end 
    37       alias call call_update 
     76      # 所属している全てのスプライトの更新時イベントをコールする。 
     77      def render 
     78        @sprites.each &:call_render 
     79      end 
    3880       
    3981      # 所属している全てのスプライトをtextureに描画する。 
    4082      def render_to texture 
    41         each{ |sprite| sprite.render_to texture } 
     83        @sprites.each{ |sprite| sprite.render_to texture } 
    4284      end 
    4385    end 
  • lang/ruby/starframe/test/starframe/sprite/test_collection.rb

    r27474 r27516  
    11require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "helper")) 
    22require "starframe/sprite" 
     3require "starframe/sprite/collection" 
    34 
    45class TestStarFrameSpriteCollection < Test::Unit::TestCase 
    5   class TestSprite < StarFrame::Sprite 
    6     attr_reader :texture, :updated 
     6  class CollectableObject 
     7    include StarFrame::Sprite::Collectable 
    78     
    8     @texture = StarRuby::Texture.load :sprite_texture 
    9      
    10     def update 
    11       @updated = true 
     9    attr_reader :called_methods 
     10    def initialize 
     11      @called_methods = [] 
     12    end 
     13    def method_missing *args 
     14      @called_methods << args 
    1215    end 
    1316  end 
     
    1518  def setup 
    1619    @collection = StarFrame::Sprite::Collection.new 
    17     @sprite = TestSprite.new(0, 0) 
     20    @sprite = CollectableObject.new 
    1821  end 
    1922   
    20   def test_require 
    21     assert StarFrame::Sprite::Collection.include?(Enumerable) 
    22     assert_respond_to @sprite, :collection 
    23     assert_respond_to @sprite, :collection= 
    24     assert_respond_to @sprite, :vanish 
     23  def test_sprite_include 
     24    assert StarFrame::Sprite.include?(StarFrame::Sprite::Collectable) 
    2525  end 
    2626   
    27   def test_initialize 
    28     assert_kind_of Array, @collection 
     27   
     28  def test_collection_initialize 
     29    assert_kind_of Enumerable, @collection 
    2930    assert_equal 0, @collection.size 
    3031  end 
    3132   
    32   def test_add 
    33     @collection << @sprite 
     33  def test_collection_add 
     34    @collection.add @sprite 
    3435    assert_equal 1, @collection.size 
    3536    assert_equal @collection, @sprite.collection 
    36      
    37     @collection << @sprite 
     37  end 
     38  def test_collection_add_do_not_duplication 
     39    @collection.add @sprite 
     40    @collection.add @sprite 
    3841    assert_equal 1, @collection.size 
    3942    assert_equal @collection, @sprite.collection 
    4043  end 
    4144   
    42   def assert_collection in_collection, out_collection 
    43     assert_equal 1,       in_collection.size 
    44     assert_equal @sprite, in_collection.last 
    45      
    46     assert_equal 0,       out_collection.size 
    47      
    48     assert_equal in_collection, @sprite.collection 
     45  def test_collection_delete 
     46    @collection.add @sprite 
     47    @collection.delete @sprite 
     48    assert_equal 0, @collection.size 
     49    assert_nil @sprite.collection 
     50  end 
     51  def test_collection_delete_not_member 
     52    @collection.delete @sprite 
     53    assert_equal 0, @collection.size 
     54    assert_nil @sprite.collection 
    4955  end 
    5056   
    51   def test_add_duplication 
    52     collection1 = StarFrame::Sprite::Collection.new 
    53     collection2 = StarFrame::Sprite::Collection.new 
    54      
    55     collection1 << @sprite 
    56     assert_collection collection1, collection2 
    57      
    58     collection2 << @sprite 
    59     assert_collection collection2, collection1 
     57  def test_collection_each 
     58    @collection.add @sprite 
     59    @sprites = [] 
     60    @collection.each do |sprite| 
     61      @sprites << sprite 
     62    end 
     63    assert_equal [@sprite], @sprites 
    6064  end 
    6165   
    62   def test_sprite_collection_writer 
    63     collection1 = StarFrame::Sprite::Collection.new 
    64     collection2 = StarFrame::Sprite::Collection.new 
    65      
    66     @sprite.collection = collection1 
    67     assert_collection collection1, collection2 
    68      
    69     @sprite.collection = collection2 
    70     assert_collection collection2, collection1 
     66  def test_collection_size 
     67    @collection.add @sprite 
     68    assert_equal 1, @collection.size 
    7169  end 
    7270   
    73   def assert_call method 
    74     @collection << sprite1 = TestSprite.new(1, 2) 
    75     @collection << sprite2 = TestSprite.new(1, 2) 
     71  def assert_collection_call_event method 
     72    @collection.add sprite1 = CollectableObject.new 
     73    @collection.add sprite2 = CollectableObject.new 
    7674     
    77     assert !sprite1.updated 
    78     assert !sprite2.updated 
     75    @collection.add sprite1 = CollectableObject.new 
     76    @collection.add sprite2 = CollectableObject.new 
     77     
    7978    @collection.__send__ method 
    80     assert  sprite1.updated 
    81     assert  sprite2.updated 
     79     
     80    assert_equal [method], sprite1.called_methods.last 
     81    assert_equal [method], sprite2.called_methods.last 
     82  end 
     83  def test_collection_call_update 
     84    assert_collection_call_event :call_update 
     85  end 
     86  def test_collection_call_render 
     87    assert_collection_call_event :call_render 
    8288  end 
    8389   
    84   def test_call 
    85     assert_call :call 
    86   end 
    87    
    88   def test_call_update 
    89     assert_call :call_update 
    90   end 
    91    
    92   def test_render_to 
    93     sprite = TestSprite.new(1, 2) 
    94     @collection << sprite 
     90  def test_collection_render_to 
     91    @collection.add @sprite 
    9592    texture = StarRuby::Texture.new 1, 1 
    9693     
    9794    @collection.render_to texture 
    98     assert_equal :render_texture, texture.last_method 
    99     assert_equal sprite.texture,  texture.last_method_args[0] 
    100     assert_equal 1,               texture.last_method_args[1] 
    101     assert_equal 2,               texture.last_method_args[2] 
     95     
     96    assert_equal [:render_to, texture],  @sprite.called_methods.last 
    10297  end 
    10398   
    104   def test_sprite_vanish 
    105     sprite = TestSprite.new(0, 0) 
    106     @collection << sprite 
     99   
     100  def test_collectable_collection_writer 
     101    @sprite.collection = @collection 
    107102    assert_equal 1, @collection.size 
    108     assert_equal sprite, sprite.vanish 
    109     assert_nil sprite.collection 
     103    assert_equal @collection, @sprite.collection 
     104  end 
     105  def test_collectable_collection_writer_deny_duplication 
     106    collection2 = StarFrame::Sprite::Collection.new 
     107    @sprite.collection = collection2 
     108    @sprite.collection = @collection 
     109    assert_equal 0, collection2.size 
     110  end 
     111   
     112  def test_collectable_vanish 
     113    @sprite.collection = @collection 
     114    assert_equal @sprite, @sprite.vanish 
     115    assert_nil @sprite.collection 
    110116    assert_equal 0, @collection.size 
    111117  end 
    112   def test_sprite_vanish_uncollection 
    113     sprite = TestSprite.new(0, 0) 
    114     assert_equal sprite, sprite.vanish 
    115     assert_nil sprite.collection 
     118  def test_collectable_vanish_not_member 
     119    assert_equal @sprite, @sprite.vanish 
     120    assert_nil @sprite.collection 
    116121  end 
    117122end