Changeset 27516
- Timestamp:
- 12/28/08 18:19:42 (4 years ago)
- Location:
- lang/ruby/starframe
- Files:
-
- 2 modified
-
lib/starframe/sprite/collection.rb (modified) (1 diff)
-
test/starframe/sprite/test_collection.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/ruby/starframe/lib/starframe/sprite/collection.rb
r27508 r27516 1 require "starframe/eventable" 2 1 3 module StarFrame #:nodoc: 2 4 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 5 28 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 23 44 # スプライトを所属させる。 24 45 # 25 46 # そのスプライトが所属していたコレクションからは自動的に削除される。 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 29 52 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 31 70 end 32 71 33 72 # 所属している全てのスプライトの更新時イベントをコールする。 34 def call_update35 each{ |sprite| sprite.call }73 def update 74 @sprites.each &:call_update 36 75 end 37 alias call call_update 76 # 所属している全てのスプライトの更新時イベントをコールする。 77 def render 78 @sprites.each &:call_render 79 end 38 80 39 81 # 所属している全てのスプライトをtextureに描画する。 40 82 def render_to texture 41 each{ |sprite| sprite.render_to texture }83 @sprites.each{ |sprite| sprite.render_to texture } 42 84 end 43 85 end -
lang/ruby/starframe/test/starframe/sprite/test_collection.rb
r27474 r27516 1 1 require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "helper")) 2 2 require "starframe/sprite" 3 require "starframe/sprite/collection" 3 4 4 5 class TestStarFrameSpriteCollection < Test::Unit::TestCase 5 class TestSprite < StarFrame::Sprite6 attr_reader :texture, :updated6 class CollectableObject 7 include StarFrame::Sprite::Collectable 7 8 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 12 15 end 13 16 end … … 15 18 def setup 16 19 @collection = StarFrame::Sprite::Collection.new 17 @sprite = TestSprite.new(0, 0)20 @sprite = CollectableObject.new 18 21 end 19 22 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) 25 25 end 26 26 27 def test_initialize 28 assert_kind_of Array, @collection 27 28 def test_collection_initialize 29 assert_kind_of Enumerable, @collection 29 30 assert_equal 0, @collection.size 30 31 end 31 32 32 def test_ add33 @collection <<@sprite33 def test_collection_add 34 @collection.add @sprite 34 35 assert_equal 1, @collection.size 35 36 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 38 41 assert_equal 1, @collection.size 39 42 assert_equal @collection, @sprite.collection 40 43 end 41 44 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 49 55 end 50 56 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 60 64 end 61 65 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 71 69 end 72 70 73 def assert_c allmethod74 @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 76 74 77 assert !sprite1.updated 78 assert !sprite2.updated 75 @collection.add sprite1 = CollectableObject.new 76 @collection.add sprite2 = CollectableObject.new 77 79 78 @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 82 88 end 83 89 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 95 92 texture = StarRuby::Texture.new 1, 1 96 93 97 94 @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 102 97 end 103 98 104 def test_sprite_vanish105 sprite = TestSprite.new(0, 0)106 @ collection << sprite99 100 def test_collectable_collection_writer 101 @sprite.collection = @collection 107 102 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 110 116 assert_equal 0, @collection.size 111 117 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 116 121 end 117 122 end
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)