OSDN Git Service

130128版スキーマ対応
[mikutoga/Pmd2XML.git] / src / main / java / jp / sfjp / mikutoga / pmd / binio / RigidBuilder.java
1 /*
2  * building rigid information
3  *
4  * License : The MIT License
5  * Copyright(c) 2010 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.pmd.binio;
9
10 import java.util.Iterator;
11 import java.util.List;
12 import jp.sfjp.mikutoga.pmd.model.BoneInfo;
13 import jp.sfjp.mikutoga.pmd.model.DynamicsInfo;
14 import jp.sfjp.mikutoga.pmd.model.ListUtil;
15 import jp.sfjp.mikutoga.pmd.model.PmdModel;
16 import jp.sfjp.mikutoga.pmd.model.RigidGroup;
17 import jp.sfjp.mikutoga.pmd.model.RigidInfo;
18 import jp.sfjp.mikutoga.pmd.model.RigidShape;
19 import jp.sourceforge.mikutoga.math.MkPos3D;
20 import jp.sourceforge.mikutoga.parser.ParseStage;
21 import jp.sourceforge.mikutoga.pmd.Rad3d;
22 import jp.sourceforge.mikutoga.pmd.RigidBehaviorType;
23 import jp.sourceforge.mikutoga.pmd.RigidShapeType;
24 import jp.sourceforge.mikutoga.pmd.parser.PmdLimits;
25 import jp.sourceforge.mikutoga.pmd.parser.PmdRigidHandler;
26
27 /**
28  * 剛体関係の通知をパーサから受け取る。
29  */
30 class RigidBuilder implements PmdRigidHandler {
31
32     private final List<BoneInfo> boneList;
33
34     private final List<RigidInfo> rigidList;
35     private Iterator<RigidInfo> rigidIt;
36     private RigidInfo currentRigid = null;
37
38     private final List<RigidGroup> rigidGroupList;
39
40     /**
41      * コンストラクタ。
42      * @param model モデル
43      */
44     RigidBuilder(PmdModel model){
45         super();
46         this.boneList = model.getBoneList();
47         this.rigidList = model.getRigidList();
48         this.rigidGroupList = model.getRigidGroupList();
49         return;
50     }
51
52     /**
53      * {@inheritDoc}
54      * @param stage {@inheritDoc}
55      * @param loops {@inheritDoc}
56      */
57     @Override
58     public void loopStart(ParseStage stage, int loops){
59         ListUtil.prepareDefConsList(this.rigidList, RigidInfo.class, loops);
60         ListUtil.assignIndexedSerial(this.rigidList);
61
62         this.rigidIt = this.rigidList.iterator();
63         if(this.rigidIt.hasNext()){
64             this.currentRigid = this.rigidIt.next();
65         }
66
67         ListUtil.prepareDefConsList(this.rigidGroupList,
68                                     RigidGroup.class,
69                                     PmdLimits.RIGIDGROUP_FIXEDNUM );
70         ListUtil.assignIndexedSerial(this.rigidGroupList);
71
72         return;
73     }
74
75     /**
76      * {@inheritDoc}
77      * @param stage {@inheritDoc}
78      */
79     @Override
80     public void loopNext(ParseStage stage){
81         if(this.rigidIt.hasNext()){
82             this.currentRigid = this.rigidIt.next();
83         }
84         return;
85     }
86
87     /**
88      * {@inheritDoc}
89      * @param stage {@inheritDoc}
90      */
91     @Override
92     public void loopEnd(ParseStage stage){
93         return;
94     }
95
96     /**
97      * {@inheritDoc}
98      * @param rigidName {@inheritDoc}
99      */
100     @Override
101     public void pmdRigidName(String rigidName){
102         this.currentRigid.getRigidName().setPrimaryText(rigidName);
103         return;
104     }
105
106     /**
107      * {@inheritDoc}
108      * @param rigidGroupId {@inheritDoc}
109      * @param linkedBoneId {@inheritDoc}
110      */
111     @Override
112     public void pmdRigidInfo(int rigidGroupId, int linkedBoneId){
113         BoneInfo bone;
114         if(linkedBoneId < 0 || 65535 <= linkedBoneId){
115             bone = null;
116         }else{
117             bone = this.boneList.get(linkedBoneId);
118         }
119         RigidGroup group = this.rigidGroupList.get(rigidGroupId);
120
121         this.currentRigid.setLinkedBone(bone);
122         this.currentRigid.setRigidGroup(group);
123         group.getRigidList().add(this.currentRigid);
124
125         return;
126     }
127
128     /**
129      * {@inheritDoc}
130      * @param shapeType {@inheritDoc}
131      * @param width {@inheritDoc}
132      * @param height {@inheritDoc}
133      * @param depth {@inheritDoc}
134      */
135     @Override
136     public void pmdRigidShape(byte shapeType,
137                               float width, float height, float depth){
138         RigidShape shape = this.currentRigid.getRigidShape();
139
140         shape.setWidth(width);
141         shape.setHeight(height);
142         shape.setDepth(depth);
143
144         RigidShapeType type = RigidShapeType.decode(shapeType);
145         shape.setShapeType(type);
146
147         return;
148     }
149
150     /**
151      * {@inheritDoc}
152      * @param posX {@inheritDoc}
153      * @param posY {@inheritDoc}
154      * @param posZ {@inheritDoc}
155      */
156     @Override
157     public void pmdRigidPosition(float posX, float posY, float posZ){
158         MkPos3D position = this.currentRigid.getPosition();
159         position.setXpos(posX);
160         position.setYpos(posY);
161         position.setZpos(posZ);
162         return;
163     }
164
165     /**
166      * {@inheritDoc}
167      * @param radX {@inheritDoc}
168      * @param radY {@inheritDoc}
169      * @param radZ {@inheritDoc}
170      */
171     @Override
172     public void pmdRigidRotation(float radX, float radY, float radZ){
173         Rad3d rotation = this.currentRigid.getRotation();
174         rotation.setXRad(radX);
175         rotation.setYRad(radY);
176         rotation.setZRad(radZ);
177         return;
178     }
179
180     /**
181      * {@inheritDoc}
182      * @param mass {@inheritDoc}
183      * @param dampingPos {@inheritDoc}
184      * @param dampingRot {@inheritDoc}
185      * @param restitution {@inheritDoc}
186      * @param friction {@inheritDoc}
187      */
188     @Override
189     public void pmdRigidPhysics(float mass,
190                                   float dampingPos,
191                                   float dampingRot,
192                                   float restitution,
193                                   float friction ){
194         DynamicsInfo info = this.currentRigid.getDynamicsInfo();
195
196         info.setMass(mass);
197         info.setDampingPosition(dampingPos);
198         info.setDampingRotation(dampingRot);
199         info.setRestitution(restitution);
200         info.setFriction(friction);
201
202         return;
203     }
204
205     /**
206      * {@inheritDoc}
207      * @param behaveType {@inheritDoc}
208      * @param collisionMap {@inheritDoc}
209      */
210     @Override
211     public void pmdRigidBehavior(byte behaveType, short collisionMap){
212         RigidBehaviorType type = RigidBehaviorType.decode(behaveType);
213         this.currentRigid.setBehaviorType(type);
214
215         for(int bitPos = 0; bitPos < PmdLimits.RIGIDGROUP_FIXEDNUM; bitPos++){
216             short mask = 0x0001;
217             mask <<= bitPos;
218             if((collisionMap & mask) == 0){
219                 RigidGroup group = this.rigidGroupList.get(bitPos);
220                 this.currentRigid.getThroughGroupColl().add(group);
221             }
222         }
223
224         return;
225     }
226
227 }