1 /*
2     Bastet - tetris clone with embedded bastard block chooser
3     (c) 2005-2009 Federico Poloni <f.polonithirtyseven@sns.it> minus 37
4 
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 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "BlockPosition.hpp"
20 
21 #include "Block.hpp"
22 #include "Well.hpp"
23 #include "boost/foreach.hpp"
24 
25 namespace Bastet{
GetDots(BlockType b) const26   const DotMatrix BlockPosition::GetDots(BlockType b) const{
27     return _pos+((blocks[b].GetOrientationMatrix())[_orientation]);
28   }
29 
Move(Movement m)30   void BlockPosition::Move(Movement m){
31     switch(m){
32     case RotateCW:
33       _orientation=_orientation.Next();
34       break;
35     case RotateCCW:
36       _orientation=_orientation.Prior();
37       break;
38     case Left:
39       _pos.x-=1;
40       break;
41     case Right:
42       _pos.x+=1;
43       break;
44     case Down:
45       _pos.y+=1;
46       break;
47     }
48   }
49 
MoveIfPossible(Movement m,BlockType b,const Well * w)50   bool BlockPosition::MoveIfPossible(Movement m, BlockType b, const Well *w){
51     BlockPosition p(*this);
52     p.Move(m);
53     if (p.IsValid(b,w)){
54 	*this=p;
55 	return true;
56       }
57     else return false;
58   }
59 
60 
IsValid(BlockType bt,const Well * w) const61   bool BlockPosition::IsValid(BlockType bt, const Well *w) const{
62     return w->Accomodates(GetDots(bt)); //XX: must change, unoptimized
63   }
64 
Drop(BlockType bt,const Well * w)65   void BlockPosition::Drop(BlockType bt, const Well *w){
66     while(MoveIfPossible(Down,bt,w));
67   }
68 
IsOutOfScreen(BlockType bt) const69   bool BlockPosition::IsOutOfScreen(BlockType bt) const{
70     BOOST_FOREACH(const Dot &d, GetDots(bt)){
71       if(d.y>=0) return false;
72     }
73     return true;
74   }
75 
76 }
77