Skip to content
Browse files

WeaponTypeモデルを追加した。

ItemはWeaponType, ItemType, EquipCategoryを持つ。
  • Loading branch information...
1 parent eb21616 commit 50ad01dab676ab06b1adffe882076b6a43e8bcf3 @sakomoko committed Jan 7, 2013
View
2 app/lib/eretheal/combat_actor.rb
@@ -36,7 +36,7 @@ def speed
def weapon_item_type
if self.respond_to? :equip
if self.equip.weapon
- return self.equip.weapon.item.item_type.key
+ return self.equip.weapon.item.weapon_type.key
end
return 'fist'
end
View
1 app/models/equip_category.rb
@@ -4,7 +4,6 @@ class EquipCategory
field :key, type: String
field :name, type: String
- field :equip, type: Boolean, default: 1
slug :key
end
View
11 app/models/inventory_item.rb
@@ -13,8 +13,8 @@ class InventoryItem
field :sort, :type => Integer, :default => 0
def equip
- return false unless self.item.item_type.equip_category.equip?
- category = self.item.item_type.equip_category.key
+ return false unless self.item.equip?
+ category = self.item.equip_category.key
if category == 'weapon' && self.item.two_handed?
self.character.equip.shield = nil
elsif category == 'shield' && self.character.equip.weapon && self.character.equip.weapon.item.two_handed?
@@ -25,8 +25,8 @@ def equip
end
def unequip
- return false if !self.item.item_type.equip_category.equip? || !self.equipping?
- category = self.item.item_type.equip_category.key
+ return false if !self.item.equip? || !self.equipping?
+ category = self.item.equip_category.key
self.character.equip.send category + '=', nil
self.character.unmemoize_all
true
@@ -43,8 +43,9 @@ def remove(num = 1)
end
def equipping?
+ return false unless item.equip?
equip = self.character.equip
- category = self.item.item_type.equip_category.key
+ category = self.item.equip_category.key
if equip.respond_to? category
equipping = equip.send(category)
return true if equipping && equipping.id == self.id
View
6 app/models/item.rb
@@ -3,6 +3,8 @@ class Item
include Mongoid::Paranoia
belongs_to :item_type
+ belongs_to :equip_category
+ belongs_to :weapon_type
embeds_one :status_adjustment
embeds_one :status_coefficient
@@ -24,4 +26,8 @@ class Item
field :demand, :type => Integer, :default => 5
+ def equip?
+ equip_category ? true : false
+ end
+
end
View
3 app/models/item_type.rb
@@ -4,9 +4,6 @@ class ItemType
field :key, :type => String
field :name, :type => String
- field :range, :type => Integer, :default => 0
slug :key
- belongs_to :equip_category, index: true
- belongs_to :attribute, index: true
end
View
10 app/models/weapon_type.rb
@@ -0,0 +1,10 @@
+class WeaponType
+ include Mongoid::Document
+ include Mongoid::Slug
+
+ field :key, type: String
+ field :name, type: String
+ field :range, type: Integer, default: 0
+
+ belongs_to :attribute, index: true
+end
View
72 spec/factories.rb
@@ -52,83 +52,11 @@
end
end
- factory :inventory_item do
- character
- item
- num 1
- factory :inventory_sword_item do
- association :item, factory: :sword_item
- end
- end
-
- factory :item do
- power 0
- speed 0
- weight 1
- two_handed false
- stack true
- price 100
- color '#ffffff'
- item_type
-
- trait :unstacked do
- stack false
- end
-
- factory :unstacked_item, traits: [:unstacked]
-
- factory :sword_item, traits: [:unstacked] do
- association :item_type, :factory => :sword_type
- end
- factory :two_handed_weapon, traits: [:unstacked] do
- two_handed true
- association :item_type, :factory => :sword_type
- end
- factory :shield_item, traits: [:unstacked] do
- association :item_type, :factory => :shield_type
- end
- factory :armor_item, traits: [:unstacked] do
- association :item_type, :factory => :cloth_armor_type
- end
-
- end
factory :equip do
character
end
- factory :item_type do
- key 'item'
- name 'Item'
- range 0
- association :equip_category, factory: :item_category
-
- factory :sword_type do
- key 'sword'
- name 'Sword'
- range 2
- association :equip_category, factory: :weapon_category
- end
-
- factory :spear_type do
- key 'spear'
- name 'Spear'
- range 2
- association :equip_category, factory: :weapon_category
- end
-
- factory :cloth_armor_type do
- key 'cloth_armor'
- name 'ClothArmor'
- association :equip_category, factory: :armor_category
- end
-
- factory :shield_type do
- key 'shield'
- name 'Shield'
- association :equip_category, factory: :shield_category
- end
- end
factory :enemy do
name 'Enemy'
View
3 spec/factories/equip_categories.rb
@@ -3,12 +3,10 @@
FactoryGirl.define do
factory :equip_category do
- equip true
factory :item_category do
key 'item'
name '道具'
- equip false
end
factory :weapon_category do
@@ -34,7 +32,6 @@
factory :material_category do
key 'material'
name '素材'
- equip false
end
end
View
20 spec/factories/inventory_items.rb
@@ -0,0 +1,20 @@
+FactoryGirl.define do
+ factory :inventory_item do
+ character
+ item
+ num 1
+ factory :inventory_sword_item do
+ association :item, factory: :sword_item
+ end
+
+ factory :inventory_armor_item do
+ association :item, factory: :armor_item
+ end
+
+ factory :inventory_shield_item do
+ association :item, factory: :shield_item
+ end
+
+
+ end
+end
View
21 spec/factories/item_types.rb
@@ -0,0 +1,21 @@
+FactoryGirl.define do
+ factory :item_type do
+ key 'item'
+ name 'Item'
+
+ factory :weapon_type do
+ key 'weapon'
+ name 'Weapon'
+ end
+
+ factory :cloth_armor_type do
+ key 'cloth_armor'
+ name 'ClothArmor'
+ end
+
+ factory :shield_type do
+ key 'shield'
+ name 'Shield'
+ end
+ end
+end
View
43 spec/factories/items.rb
@@ -0,0 +1,43 @@
+FactoryGirl.define do
+ factory :item do
+ power 0
+ speed 0
+ weight 1
+ two_handed false
+ stack true
+ price 100
+ color '#ffffff'
+ item_type
+
+ trait :unstacked do
+ stack false
+ end
+
+ factory :unstacked_item, traits: [:unstacked]
+
+ factory :sword_item, traits: [:unstacked] do
+ association :item_type, :factory => :weapon_type
+ association :equip_category, factory: :weapon_category
+ association :weapon_type, factory: :sword_type
+ end
+
+ factory :two_handed_weapon, traits: [:unstacked] do
+ two_handed true
+ association :item_type, :factory => :weapon_type
+ association :equip_category, factory: :weapon_category
+ association :weapon_type, factory: :sword_type
+ end
+
+ factory :shield_item, traits: [:unstacked] do
+ association :item_type, :factory => :shield_type
+ association :equip_category, factory: :shield_category
+ end
+
+ factory :armor_item, traits: [:unstacked] do
+ association :item_type, :factory => :cloth_armor_type
+ association :equip_category, factory: :armor_category
+ end
+
+ end
+
+end
View
15 spec/factories/weapon_types.rb
@@ -0,0 +1,15 @@
+# Read about factories at https://github.com/thoughtbot/factory_girl
+
+FactoryGirl.define do
+ factory :sword_type, class: WeaponType do
+ key 'sword'
+ name 'Sword'
+ range 2
+ end
+
+ factory :ax_type, class: WeaponType do
+ key 'ax'
+ name 'Ax'
+ range 2
+ end
+end
View
10 spec/models/equip_category_spec.rb
@@ -9,14 +9,4 @@
end
it { EquipCategory.find('weapon').should eq weapon_category }
end
-
- describe '装備品かどうかを真偽できること' do
- subject { weapon_category }
- it { should be_equip }
-
- context '対象が素材のとき' do
- subject { FactoryGirl.create(:material_category) }
- it { should_not be_equip }
- end
- end
end
View
20 spec/models/inventory_item_spec.rb
@@ -25,24 +25,20 @@
end
context '鎧Aを装備したとき' do
- before do
- inventory_item.item.item_type = FactoryGirl.create :cloth_armor_type
- end
- it { inventory_item.equip.should be_true }
+ subject { FactoryGirl.create :inventory_armor_item, character: pc }
+ it { subject.equip.should be_true }
it '鎧として装備されていること' do
- inventory_item.equip
- equip.armor.should eq inventory_item
+ subject.equip
+ should eq pc.equip.armor
end
end
context '楯Aを装備したとき' do
- before do
- inventory_item.item.item_type = FactoryGirl.create :shield_type
- end
- it { inventory_item.equip.should be_true }
+ subject { FactoryGirl.create :inventory_shield_item, character: pc }
+ it { subject.equip.should be_true }
it '楯として装備されていること' do
- inventory_item.equip
- equip.shield.should eq inventory_item
+ subject.equip
+ should eq pc.equip.shield
end
end
View
6 spec/models/item_type_spec.rb
@@ -3,10 +3,10 @@
describe ItemType do
describe 'keyからアイテムタイプを探すことができる' do
- let(:sword_type) { FactoryGirl.create :sword_type }
+ let(:weapon_type) { FactoryGirl.create :weapon_type }
before do
- sword_type
+ weapon_type
end
- it { ItemType.find('sword').should eq sword_type }
+ it { ItemType.find('weapon').should eq weapon_type }
end
end
View
5 spec/models/weapon_type_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe WeaponType do
+ pending "add some examples to (or delete) #{__FILE__}"
+end

0 comments on commit 50ad01d

Please sign in to comment.
Something went wrong with that request. Please try again.