2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2007 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
11 #ifndef HLVARIANT__ANIMATOR_IMPL_H
12 #define HLVARIANT__ANIMATOR_IMPL_H
15 #include "animationfactory.h"
17 #include "namedsprite.h"
21 template <typename Variant
>
22 AnimationGroupPtr BaseAnimator
<Variant
>::warp(const GameState
& final
) {
23 const GameState
* current
= m_cinterface
->position();
24 AnimationFactory
res(m_cinterface
->inner());
26 for (int i
= 0; i
< current
->board().size().x
; i
++) {
27 for (int j
= 0; j
< current
->board().size().y
; j
++) {
30 NamedSprite sprite
= m_cinterface
->getSprite(p
);
31 Piece piece
= final
.board().get(p
);
33 if (!sprite
&& piece
!= Piece()) {
34 res
.addPreAnimation(Animate::appear(m_cinterface
->setPiece(p
, piece
, false)),
38 else if (sprite
&& piece
== Piece()) {
39 m_cinterface
->takeSprite(p
);
40 res
.addPreAnimation(Animate::disappear(sprite
), Animate::Instant
);
43 // sprite and piece differ
44 else if (sprite
&& piece
!= Piece() && piece
.name() != sprite
.name()) {
45 m_cinterface
->takeSprite(p
);
46 res
.addPreAnimation(Animate::morph(sprite
, m_cinterface
->setPiece(p
, piece
, false)),
55 template <typename Variant
>
56 AnimationGroupPtr BaseAnimator
<Variant
>::forward(const GameState
& final
, const Move
&) {
61 template <typename Variant
>
62 AnimationGroupPtr BaseAnimator
<Variant
>::back(const GameState
& final
, const Move
&) {
66 template <typename Variant
>
67 AnimationGroupPtr SimpleAnimator
<Variant
>::warp(const GameState
& final
) {
68 AnimationFactory
res(m_cinterface
->inner());
70 res
.setGroup(Base::warp(final
));
75 template <typename Variant
>
76 SchemePtr SimpleAnimator
<Variant
>::movement(const NamedSprite
& sprite
, const Point
& from
, const Point
& to
) {
77 bool knight
= m_cinterface
->position()->board().get(from
).type() == Piece::KNIGHT
;
79 ? Animate::move::LShaped
| Animate::move::Rotating
80 : Animate::move::Straight
;
81 return SchemePtr(new Animate::move(sprite
, to
, mtype
));
84 template <typename Variant
>
85 AnimationGroupPtr SimpleAnimator
<Variant
>::forward(const GameState
& final
, const Move
& move
) {
86 AnimationFactory
res(m_cinterface
->inner());
88 NamedSprite piece
= m_cinterface
->takeSprite(move
.from());
89 NamedSprite captured
= m_cinterface
->takeSprite(move
.captureSquare());
90 m_cinterface
->setSprite(move
.to(), piece
);
93 res
.addPreAnimation(*movement(piece
, move
.from(), move
.to()));
98 res
.addPostAnimation(Animate::destroy(captured
));
100 if (move
.promoteTo() != -1) {
101 Piece promoted
= final
.board().get(move
.to());
103 if (promoted
!= Piece()) {
104 QPoint real
= m_cinterface
->converter()->toReal(move
.to());
105 NamedSprite old_sprite
= m_cinterface
->getSprite(move
.to());
106 NamedSprite new_sprite
= m_cinterface
->setPiece(move
.to(), promoted
, /*false,*/ false);
108 res
.addPostAnimation(Animate::morph(old_sprite
, new_sprite
));
113 else if (move
.kingSideCastling()) {
114 Point rookSquare
= move
.to() + Point(1,0);
115 Point rookDestination
= move
.from() + Point(1,0);
117 NamedSprite rook
= m_cinterface
->takeSprite(rookSquare
);
118 m_cinterface
->setSprite(rookDestination
, rook
);
119 res
.addPreAnimation(Animate::move(rook
, rookDestination
));
121 else if (move
.queenSideCastling()) {
122 Point rookSquare
= move
.to() + Point(-2,0);
123 Point rookDestination
= move
.from() + Point(-1,0);
125 NamedSprite rook
= m_cinterface
->takeSprite(rookSquare
);
126 m_cinterface
->setSprite(rookDestination
, rook
);
127 res
.addPreAnimation(Animate::move(rook
, rookDestination
));
130 res
.group()->addPostAnimation(warp(final
));
134 template <typename Variant
>
135 AnimationGroupPtr SimpleAnimator
<Variant
>::back(const GameState
& final
, const Move
& move
) {
136 AnimationFactory
res(m_cinterface
->inner());
138 NamedSprite piece
= m_cinterface
->takeSprite(move
.to());
139 NamedSprite captured
;
140 Piece captured_piece
= final
.board().get(move
.captureSquare());
141 if (captured_piece
!= Piece()) {
142 captured
= m_cinterface
->setPiece(move
.captureSquare(), captured_piece
, false);
143 res
.addPreAnimation(Animate::appear(captured
));
147 piece
= m_cinterface
->createPiece(move
.to(), final
.board().get(move
.from()), false);
148 res
.addPreAnimation(Animate::appear(piece
));
151 m_cinterface
->setSprite(move
.from(), piece
);
153 if (move
.promoteTo() != -1) {
154 Piece pawn_piece
= final
.board().get(move
.from());
155 if (pawn_piece
!= Piece()) {
156 NamedSprite pawn
= m_cinterface
->createPiece(move
.to(), pawn_piece
, false);
157 res
.addPreAnimation(Animate::morph(piece
, pawn
));
159 // replace piece with pawn
160 m_cinterface
->setSprite(move
.from(), pawn
);
164 else if (move
.kingSideCastling()) {
165 Point rookSquare
= move
.to() + Point(1,0);
166 Point rookDestination
= move
.from() + Point(1,0);
168 NamedSprite rook
= m_cinterface
->takeSprite(rookDestination
);
169 m_cinterface
->setSprite(rookSquare
, rook
);
171 res
.addPreAnimation(Animate::move(rook
, rookSquare
));
173 else if (move
.queenSideCastling()) {
174 Point rookSquare
= move
.to() + Point(-2,0);
175 Point rookDestination
= move
.from() + Point(-1,0);
177 NamedSprite rook
= m_cinterface
->takeSprite(rookDestination
);
178 m_cinterface
->setSprite(rookSquare
, rook
);
180 res
.addPreAnimation(Animate::move(rook
, rookSquare
));
183 res
.addPreAnimation(*movement(piece
, move
.to(), move
.from()));
184 res
.group()->addPostAnimation(warp(final
));
194 #endif //HLVARIANT__ANIMATOR_IMPL_H