OSDN Git Service

Merge branch 'master' of o_ggy@git.sourceforge.jp:/gitroot/moflib/moflib
[moflib/moflib.git] / saisei-1.0 / rpg / rpg / oldcode / CharacterBoard.cpp
1 #include "CharacterBoard.hpp"
2 #include "mof/Matrix3D.hpp"
3 #include "mof/ConsoleIO.hpp"
4 #include "mof/KeyFrameAnimation.hpp"
5 #include "mof/RotationAnimation3D.hpp"
6 #include "mof/TranslationAnimation3D.hpp"
7 #include "mof/ScalingAnimation3D.hpp"
8 #include "mof/Constant.hpp"
9 #include "mof/CascadingAnimation.hpp"
10 #include "mof/LoopAnimation.hpp"
11 #include "mof/Interpolation.hpp"
12 #include "mof/Font.hpp"
13 #include "mof/Vector2D.hpp"
14 #include "mof/utilities.hpp"
15 #include <vector>
16
17
18 struct ::CharacterBoard::Impl{
19     std::vector<mof::Board*> list;
20     mof::Animation<mof::Matrix3D>::Handler worldMatrix;
21     mof::FrameNumber frame;
22
23     Impl()
24         :  frame(0) ,
25         worldMatrix(mof::makeConstantHandler(mof::Matrix3D::createIdentity())) 
26     {
27     }
28
29     ~Impl(){
30         for(std::vector<mof::Board*>::iterator itr = list.begin() ;
31             itr != list.end() ;
32             ++itr ){
33             delete *itr;
34         }
35         
36     }
37 };
38
39 ::CharacterBoard::CharacterBoard( const mof::tstring& text)
40 : m_pImpl(new Impl())
41 {
42     
43
44     mof::Board* pBoard;
45     pBoard = new mof::Board();
46     pBoard->setColor(mof::makeConstantHandler(mof::Color4f(0.3f , 0.3f , 1)));
47     m_pImpl->list.push_back(pBoard);
48
49     pBoard = new mof::Board();
50     pBoard->setColor(mof::makeConstantHandler(mof::Color4f(0.6f , 0.6f , 1)));
51     m_pImpl->list.push_back(pBoard);
52
53     mof::Font font(mof::Font::MS_GOTHIC , 70);
54     std::shared_ptr<mof::PixelMap> pPixelMap( font.createText(text) );
55     mof::Vector2D sizeVec(pPixelMap->shape()[0] , pPixelMap->shape()[1]);
56     std::shared_ptr<mof::Texture> pTexture(new mof::Texture( pPixelMap));
57     float endX = static_cast<float>(pPixelMap->shape()[0]) / pTexture->getWidth();
58     float endY = static_cast<float>(pPixelMap->shape()[1]) / pTexture->getHeight();
59
60     pBoard = new mof::Board();
61     pBoard->setColor(mof::makeConstantHandler(mof::Color4f(1 , 1 , 1)));
62     pBoard->setTexture(pTexture);
63     pBoard->setTextureCoordinates(mof::Rectangle<float>(0 , 0 , endX , endY));
64     m_pImpl->list.push_back(pBoard);
65 }
66
67 ::CharacterBoard::~CharacterBoard(){
68
69 }
70
71 void ::CharacterBoard::setFrameNumber(mof::FrameNumber frame){
72     m_pImpl->frame = frame;
73     for(std::vector<mof::Board*>::iterator itr = m_pImpl->list.begin() ;
74         itr != m_pImpl->list.end() ;
75         ++itr ){
76             (*itr)->setFrameNumber(frame);
77     }
78 }
79
80
81 void ::CharacterBoard::nextFrame(){
82     setFrameNumber(m_pImpl->frame + 1);
83 }
84
85
86 void ::CharacterBoard::prevFrame(){
87     if(m_pImpl->frame > 0)setFrameNumber(m_pImpl->frame - 1);
88 }
89
90
91 void ::CharacterBoard::update(){
92     m_pImpl->frame++;
93     for(std::vector<mof::Board*>::iterator itr = m_pImpl->list.begin() ;
94         itr != m_pImpl->list.end() ;
95         ++itr ){
96             (*itr)->update();
97     }
98 }
99
100
101 void ::CharacterBoard::setWorldMatrix(const mof::Animation<mof::Matrix3D>::Handler& handler){
102     m_pImpl->worldMatrix = handler;
103     std::vector<mof::Animation<mof::Matrix3D>::Handler> handlers;
104     handlers.push_back( mof::makeConstantHandler(
105         mof::Matrix3D::createTranslation(mof::Vector3D(0 , 0.5f , 0))
106         ));
107     handlers.push_back( handler);
108     
109     
110     m_pImpl->list.at(0)->setWorldMatrix(
111         mof::Animation<mof::Matrix3D>::Handler(
112         new mof::CascadingAnimation<mof::Matrix3D , mof::Multiply<mof::Matrix3D>>(handlers.front() , handlers.back())
113         )
114         );
115
116     handlers.clear();
117     handlers.push_back( mof::makeConstantHandler(
118         mof::Matrix3D::createTranslation(mof::Vector3D(0 , 0.5f , 0.01f))
119         ));
120     handlers.push_back(mof::makeConstantHandler(
121         mof::Matrix3D::createRotation(mof::Vector3D(0 , mof::deg2rad(180) , 0))
122         ));
123     handlers.push_back(handler);
124
125     m_pImpl->list.at(1)->setWorldMatrix(
126         mof::Animation<mof::Matrix3D>::Handler(
127         new mof::CascadingAnimation<mof::Matrix3D , mof::Multiply<mof::Matrix3D>>(handlers.front() , handlers.back())
128         )
129         );
130
131     handlers.clear();
132     handlers.push_back( mof::makeConstantHandler(
133         mof::Matrix3D::createTranslation(mof::Vector3D(0 , 0.5f , -0.01f))
134         ));
135     handlers.push_back( handler);
136     
137     
138     m_pImpl->list.at(2)->setWorldMatrix(
139         mof::Animation<mof::Matrix3D>::Handler(
140         new mof::CascadingAnimation<mof::Matrix3D , mof::Multiply<mof::Matrix3D>>(handlers.front() , handlers.back())
141         )
142         );
143     
144 }
145
146
147 mof::Matrix3D CharacterBoard::getWorldMatrix() const{
148     return m_pImpl->worldMatrix->getValue(m_pImpl->frame);
149 }
150
151 mof::Vector3D CharacterBoard::getPosition() const{
152     mof::Matrix3D m = getWorldMatrix();
153     return mof::Vector3D(m.at(3 , 0) , m.at(3 , 1) , m.at(3 , 2));
154 }
155
156 void ::CharacterBoard::append(std::list<mof::Board*>& list_){
157     list_.insert(list_.begin() , m_pImpl->list.begin(), m_pImpl->list.end());
158     
159 }
160
161 void ::CharacterBoard::setFinishColor(mof::FrameNumber delay , mof::FrameNumber interval){
162     {
163         std::pair<mof::FrameNumber , mof::Color4f> keyFrames[] = {
164             std::pair<mof::FrameNumber , mof::Color4f>( delay , mof::Color4f(0.3f , 0.3f , 1) ) ,
165             std::pair<mof::FrameNumber , mof::Color4f>( delay + interval , mof::Color4f(1 , 1 , 1) )
166         };
167         m_pImpl->list.at(0)->setColor(
168             mof::Animation<mof::Color4f>::Handler(new mof::KeyFrameAnimation<mof::Color4f>(keyFrames[0] , keyFrames[1]))
169             );
170     }
171
172     {
173         std::pair<mof::FrameNumber , mof::Color4f> keyFrames[] = {
174             std::pair<mof::FrameNumber , mof::Color4f>( delay , mof::Color4f(1 , 1 , 1) ) ,
175             std::pair<mof::FrameNumber , mof::Color4f>( delay + interval , mof::Color4f(0.3f , 0.3f , 1) )
176         };
177         m_pImpl->list.at(2)->setColor(
178             mof::Animation<mof::Color4f>::Handler(new mof::KeyFrameAnimation<mof::Color4f>(keyFrames[0] , keyFrames[1]))
179             );
180     }    
181
182 }
183
184
185 void ::CharacterBoard::setPartColor(mof::FrameNumber delay , mof::FrameNumber interval){
186     {
187         std::pair<mof::FrameNumber , mof::Color4f> keyFrames[] = {
188             std::pair<mof::FrameNumber , mof::Color4f>( delay , mof::Color4f(0 , 1 , 1 , 1) ) ,
189             std::pair<mof::FrameNumber , mof::Color4f>( delay + interval , mof::Color4f(0 , 1 , 1 , 1) )
190         };
191         m_pImpl->list.at(0)->setColor(
192             mof::Animation<mof::Color4f>::Handler(new mof::KeyFrameAnimation<mof::Color4f>(keyFrames[0] , keyFrames[1]))
193             );
194     }
195
196     {
197         std::pair<mof::FrameNumber , mof::Color4f> keyFrames[] = {
198             std::pair<mof::FrameNumber , mof::Color4f>( delay , mof::Color4f(0 , 1 , 1 , 1) ) ,
199             std::pair<mof::FrameNumber , mof::Color4f>( delay + interval , mof::Color4f(0 , 1 , 1 , 1) )
200         };
201         m_pImpl->list.at(1)->setColor(
202             mof::Animation<mof::Color4f>::Handler(new mof::KeyFrameAnimation<mof::Color4f>(keyFrames[0] , keyFrames[1]))
203             );
204     }
205
206     {
207         std::pair<mof::FrameNumber , mof::Color4f> keyFrames[] = {
208             std::pair<mof::FrameNumber , mof::Color4f>( delay , mof::Color4f(0.8f , 0.5f , 0.2f) ) ,
209             std::pair<mof::FrameNumber , mof::Color4f>( delay + interval , mof::Color4f(0.8f , 0.5f , 0.2f) )
210         };
211         m_pImpl->list.at(2)->setColor(
212             mof::Animation<mof::Color4f>::Handler(new mof::KeyFrameAnimation<mof::Color4f>(keyFrames[0] , keyFrames[1]))
213             );
214     }    
215
216 }