Initial implementation of the new abstraction architecture.
[tagua/yd.git] / src / variants / chess.cpp
blob9226a6f5fe88b9b3db327cb89b9d18ecee5f55bd
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 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 #include <QPainter>
12 #include <boost/shared_ptr.hpp>
13 #include "variants/chess.h"
14 #include "common.h"
15 #include "highlevel.h"
16 #include "moveserializer.impl.h"
17 #include "xchess/animator.impl.h"
18 #include "piecefunction.h"
20 using namespace boost;
22 const char *ChessVariant::m_name = "Chess";
23 const char *ChessVariant::m_theme_proxy = "Chess";
24 VariantInfo* ChessVariant::static_chess_variant = 0;
26 class ChessAnimator : public SimpleAnimator<ChessVariant> {
27 public:
28 ChessAnimator(PointConverter* converter,
29 const shared_ptr<GPosition>& position)
30 : SimpleAnimator<ChessVariant>(converter, position) { }
31 protected:
32 shared_ptr<MovementAnimation>
33 createMovementAnimation(const GElement& element, const QPoint& destination) {
34 if (element.piece().type() == KNIGHT) {
35 return shared_ptr<MovementAnimation>(
36 new KnightMovementAnimation(element.sprite(),
37 destination, m_anim_rotate));
39 else {
40 return shared_ptr<MovementAnimation>(
41 new MovementAnimation(element.sprite(),
42 destination, 1.0));
48 //BEGIN Dream code
51 typedef UnwrappedGraphicalAPI<ChessVariant> ChessGraphicalAPI;
53 class ChessAnimator {
54 ChessGraphicalAPI* m_cinterface;
55 public:
56 ChessAnimator(ChessGraphicalAPI* cinterface)
57 : m_cinterface(cinterface) {
60 AnimationPtr warp(const ChessPosition* final) {
61 const ChessPosition* current = m_cinterface->position();
62 AnimationPtr res(new AnimationGroup);
64 for (Point i = m_current->first(); i <= m_position->last(); i = m_position->next(i)) {
65 ChessPiece* c = current->get(i);
66 ChessPiece* f = final->get(i);
68 if( !c && f ) {
69 //current->set(i, f);
70 PieceSpritePtr sprite = m_cinterface->setSprite(i, *f, false, false);
71 res->addPreAnimation( shared_ptr<DropAnimation>(new DropAnimation(sprite)) );
73 else if (c && !f) {
74 //current->set(i, shared_ptr<ChessPiece>());
75 PieceSpritePtr old_sprite = m_cinterface->getSprite(i);
76 res->addPreAnimation( shared_ptr<CaptureAnimation>(new CaptureAnimation(sprite)) );
78 else if(!c.equals(f) ) {
79 current->set(i, f);
80 PieceSpritePtr old_sprite = m_cinterface->takeSprite(i);
81 res->addPreAnimation( shared_ptr<PromotionProAnimation>(new PromotionAnimation(sprite)) );
86 //TODO: implement pool update
88 return res;
93 typedef UnwrappedGraphicalAPI<ChessVariant> ChessGraphicalAPI;
95 class ChessAnimator {
96 ChessGraphicalAPI* m_cinterface;
97 public:
98 ChessAnimator(ChessGraphicalAPI* cinterface)
99 : m_cinterface(cinterface) {
102 AnimationPtr warp(ChessPosition* final) {
103 ChessPosition* current = m_cinterface->position();
104 AnimationPtr res(new AnimationGroup);
106 for (Point i = m_current->first(); i <= m_position->last(); i = m_position->next(i)) {
107 ChessPiece* c = current->get(i);
108 ChessPiece* f = final->get(i);
110 if( !c && f ) {
111 current->set(i, f);
112 PieceSpritePtr sprite = m_cinterface->setSprite(i, *f, false, false);
113 res->addPreAnimation( shared_ptr<DropAnimation>(new DropAnimation(sprite)) );
115 else if (c && !f) {
116 current->set(i, shared_ptr<ChessPiece>());
117 PieceSpritePtr old_sprite = m_cinterface->getSprite(i);
118 res->addPreAnimation( shared_ptr<CaptureAnimation>(new CaptureAnimation(sprite)) );
120 else if(!c.equals(f) ) {
121 current->set(i, f);
122 PieceSpritePtr old_sprite = m_cinterface->takeSprite(i);
123 res->addPreAnimation( shared_ptr<PromotionProAnimation>(new PromotionAnimation(sprite)) );
128 return res;
132 //END Dream code
135 void ChessVariant::forallPieces(PieceFunction& f) {
136 f(WHITE, KING);
137 f(WHITE, QUEEN);
138 f(WHITE, ROOK);
139 f(WHITE, BISHOP);
140 f(WHITE, KNIGHT);
141 f(WHITE, PAWN);
142 f(BLACK, KING);
143 f(BLACK, QUEEN);
144 f(BLACK, ROOK);
145 f(BLACK, BISHOP);
146 f(BLACK, KNIGHT);
147 f(BLACK, PAWN);
150 VariantInfo* ChessVariant::info() {
151 if (!static_chess_variant)
152 static_chess_variant = new WrappedVariantInfo<ChessVariant>;
153 return static_chess_variant;