1 /* FloboPuyo
2  * Copyright (C) 2004
3  *   Florent Boudet        <flobo@ios-software.com>,
4  *   Jean-Christophe Hoelt <jeko@ios-software.com>,
5  *   Guillaume Borios      <gyom@ios-software.com>
6  *
7  * iOS Software <http://www.ios-software.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  *
24  */
25 
26 #include "AnimatedPuyo.h"
27 #include "PuyoView.h"
28 
29 // crade, mais basta
30 extern IIM_Surface *puyoCircle[32];
31 extern IIM_Surface *puyoEye[3];
32 
AnimatedPuyo(PuyoState state,PuyoView * attachedView)33 AnimatedPuyo::AnimatedPuyo(PuyoState state, PuyoView *attachedView) : PuyoPuyo(state)
34 {
35     puyoEyeState = random() % 700;
36     visibilityFlag = true;
37     this->attachedView = attachedView;
38 }
39 
~AnimatedPuyo()40 AnimatedPuyo::~AnimatedPuyo()
41 {
42     while (animationQueue.getSize() > 0)
43            removeCurrentAnimation();
44 }
45 
addAnimation(PuyoAnimation * animation)46 void AnimatedPuyo::addAnimation(PuyoAnimation *animation)
47 {
48     animationQueue.addElement(animation);
49 }
50 
getCurrentAnimation() const51 PuyoAnimation * AnimatedPuyo::getCurrentAnimation() const
52 {
53     if (animationQueue.getSize() == 0)
54         return NULL;
55     return (PuyoAnimation *)animationQueue.getElementAt(0);
56 }
57 
removeCurrentAnimation()58 void AnimatedPuyo::removeCurrentAnimation()
59 {
60     if (animationQueue.getSize() == 0)
61         return;
62     delete (PuyoAnimation *)animationQueue.getElementAt(0);
63     animationQueue.removeElementAt(0);
64 }
65 
cycleAnimation()66 void AnimatedPuyo::cycleAnimation()
67 {
68     smallTicksCount+=2;
69     PuyoAnimation * animation = getCurrentAnimation();
70     if ((animation != NULL)) {
71         animation->cycle();
72         if (animation->isFinished()) {
73             removeCurrentAnimation();
74         }
75     }
76 }
77 
isRenderingAnimation() const78 bool AnimatedPuyo::isRenderingAnimation() const
79 {
80     PuyoAnimation *animation = getCurrentAnimation();
81     if (animation == NULL)
82         return false;
83     if (animation->isFinished())
84         return false;
85     return animation->isEnabled();
86 }
87 
render()88 void AnimatedPuyo::render()
89 {
90     SDL_Painter &painter = attachedView->getPainter();
91 
92     puyoEyeState++;
93     if (attachedView == NULL)
94         return;
95     if (!visibilityFlag)
96         return;
97     PuyoGame *attachedGame = attachedView->getAttachedGame();
98 
99     bool falling  = attachedGame->getFallingState() < PUYO_EMPTY;
100 
101     SDL_Rect drect;
102     PuyoAnimation *animation = getCurrentAnimation();
103     if (!isRenderingAnimation()) {
104         IIM_Surface *currentSurface = attachedView->getSurfaceForPuyo(this);
105         if (currentSurface != NULL) {
106             drect.x = getScreenCoordinateX();
107             drect.y = getScreenCoordinateY();
108             if (this->getPuyoState() < PUYO_EMPTY)
109                 drect.y -= attachedGame->getSemiMove() * TSIZE / 2;
110             drect.w = currentSurface->w;
111             drect.h = currentSurface->h;
112             painter.requestDraw(currentSurface, &drect);
113 
114             /* Main puyo show */
115             if (falling && (this == attachedGame->getFallingPuyo()))
116                 painter.requestDraw(puyoCircle[(smallTicksCount >> 2) & 0x1F], &drect);
117 
118             /* Eye management */
119             if (getPuyoState() != PUYO_NEUTRAL) {
120                 while (puyoEyeState >= 750) puyoEyeState -= 750;
121                 int eyePhase = puyoEyeState;
122                 if (eyePhase < 5)
123                     painter.requestDraw(puyoEye[1], &drect);
124                 else if (eyePhase < 15)
125                     painter.requestDraw(puyoEye[2], &drect);
126                 else if (eyePhase < 20)
127                     painter.requestDraw(puyoEye[1], &drect);
128                 else
129                     painter.requestDraw(puyoEye[0], &drect);
130             }
131         }
132     }
133     else {
134         if (!animation->isFinished()) {
135             animation->draw(attachedGame->getSemiMove());
136         }
137     }
138 }
139 
getScreenCoordinateX() const140 int AnimatedPuyo::getScreenCoordinateX() const
141 {
142     return attachedView->getScreenCoordinateX(getPuyoX());
143 }
144 
getScreenCoordinateY() const145 int AnimatedPuyo::getScreenCoordinateY() const
146 {
147     return attachedView->getScreenCoordinateY(getPuyoY());
148 }
149 
150 
151 
152 
AnimatedPuyoFactory(PuyoView * attachedView)153 AnimatedPuyoFactory::AnimatedPuyoFactory(PuyoView *attachedView)
154 {
155     this->attachedView = attachedView;
156 }
157 
~AnimatedPuyoFactory()158 AnimatedPuyoFactory::~AnimatedPuyoFactory()
159 {
160     while (puyoWalhalla.getSize() > 0) {
161         AnimatedPuyo *currentPuyo = (AnimatedPuyo *)(puyoWalhalla.getElementAt(0));
162         puyoWalhalla.removeElementAt(0);
163         delete currentPuyo;
164     }
165 }
166 
createPuyo(PuyoState state)167 PuyoPuyo *AnimatedPuyoFactory::createPuyo(PuyoState state)
168 {
169     return new AnimatedPuyo(state, attachedView);
170 }
171 
deletePuyo(PuyoPuyo * target)172 void AnimatedPuyoFactory::deletePuyo(PuyoPuyo *target)
173 {
174     puyoWalhalla.addElement(target);
175 }
176 
177 
renderWalhalla()178 void AnimatedPuyoFactory::renderWalhalla()
179 {
180     for (int i = puyoWalhalla.getSize() - 1 ; i >= 0 ; i--) {
181         AnimatedPuyo *currentPuyo = (AnimatedPuyo *)(puyoWalhalla.getElementAt(i));
182         currentPuyo->render();
183     }
184 }
185 
cycleWalhalla()186 void AnimatedPuyoFactory::cycleWalhalla()
187 {
188     for (int i = puyoWalhalla.getSize() - 1 ; i >= 0 ; i--) {
189         AnimatedPuyo *currentPuyo = (AnimatedPuyo *)(puyoWalhalla.getElementAt(i));
190         if (currentPuyo->getCurrentAnimation() != NULL) {
191             currentPuyo->cycleAnimation();
192         } else {
193             puyoWalhalla.removeElementAt(i);
194             delete currentPuyo;
195         }
196     }
197 }
198 
199