Removed all old variants.
[tagua/yd.git] / src / hlvariant / animator.impl.h
bloba7f66d6bb23cc42c35617177b112ff7b0c930993
1 /*
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.
9 */
11 #ifndef HLVARIANT__ANIMATOR_IMPL_H
12 #define HLVARIANT__ANIMATOR_IMPL_H
14 #include "animator.h"
15 #include "animationfactory.h"
16 #include "common.h"
17 #include "namedsprite.h"
19 namespace HLVariant {
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++) {
28 Point p(i, 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)),
35 Animate::Instant);
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)),
47 Animate::Instant);
52 return res;
55 template <typename Variant>
56 AnimationGroupPtr BaseAnimator<Variant>::forward(const GameState& final, const Move&) {
57 return warp(final);
61 template <typename Variant>
62 AnimationGroupPtr BaseAnimator<Variant>::back(const GameState& final, const Move&) {
63 return warp(final);
66 template <typename Variant>
67 AnimationGroupPtr SimpleAnimator<Variant>::warp(const GameState& final) {
68 AnimationFactory res(m_cinterface->inner());
70 res.setGroup(Base::warp(final));
72 return res;
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;
78 int mtype = 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);
92 if (piece)
93 res.addPreAnimation(*movement(piece, move.from(), move.to()));
94 else
95 ERROR("Bug!!!");
97 if (captured)
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));
110 else
111 ERROR("Bug!!!");
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));
131 return res;
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));
146 if (!piece) {
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);
161 piece = 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));
186 return res;
194 #endif //HLVARIANT__ANIMATOR_IMPL_H