~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to restricted/app-fortress/fortress/WallObject.hx

  • Committer: edA-qa mort-ora-y
  • Date: 2010-02-16 05:36:32 UTC
  • Revision ID: eda-qa@disemia.com-20100216053632-60lt7fndfi3fgblw
first

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package fortress;
 
2
 
 
3
import mathx.MatrixUtil;
 
4
import mathx.Matrix;
 
5
import mathx.MatPoint;
 
6
import mathx.Point2;
 
7
 
 
8
import flash.display.Sprite;
 
9
 
 
10
/**
 
11
 * A wall object is a type of BuildObject which adds to the
 
12
 * fortresses walls
 
13
 */
 
14
typedef WallLayout = mathx.Matrix<Int>;
 
15
class WallObject extends BuildObject
 
16
{
 
17
        
 
18
        static private var wls : Array<Array<Array<Int>>> = [
 
19
                        [ [ 1 ] ],
 
20
                        
 
21
                        [ [ 1, 1 ] ],
 
22
                        
 
23
                        [ [ 1, 1, 1 ] ],
 
24
                        
 
25
                        [ [ 1, 1 ],
 
26
                          [ 1, 0 ] ],
 
27
                          
 
28
                        [ [ 1, 0 ],
 
29
                          [ 1, 1 ],
 
30
                          [ 0, 1 ] ],
 
31
                          
 
32
                        [ [ 1, 1, 1 ],
 
33
                          [ 1, 0, 1 ] ],
 
34
                          
 
35
                        [ [ 1, 1, 1 ],
 
36
                          [ 1, 0, 0 ] ],
 
37
                                                
 
38
                        [ [ 1, 1, 1 ],
 
39
                          [ 0, 1, 0 ] ]
 
40
                ];
 
41
                
 
42
        static public var maxSize = MatPoint.at( 3, 3 );
 
43
        
 
44
        public var wl : WallLayout; //external [read-only] 
 
45
        
 
46
        static private function transformCopy( wl : WallLayout ) : WallLayout
 
47
        {
 
48
                var nwl = wl.clone();
 
49
                if( Math.random() > 0.5 )       //either flip or maintain as is (flip is only different than rotate for a few items)
 
50
                        nwl = nwl.createFlip( );
 
51
                        
 
52
                for( i in 0...Math.floor( Math.random() * 4 ) ) //rotate a 0-3 times (90 degrees each)
 
53
                        nwl.rotate90C();
 
54
                        
 
55
                return nwl;
 
56
        }
 
57
        
 
58
        override public function getBuildMap() : Matrix<Int>
 
59
        {
 
60
                return wl;
 
61
        }
 
62
        
 
63
        override public function canBuildAt( at : MatPoint, ct : Int, mod : Int, lo : LiveObject ) : Bool
 
64
        {
 
65
                return ct == MapData.ctLand
 
66
                        && !(mod & MapData.modOccupied == MapData.modOccupied);
 
67
        }
 
68
        
 
69
        /**
 
70
         * Creates a new randomly shaped wall bit.
 
71
         */
 
72
        public function new( )
 
73
        {
 
74
                super();
 
75
                wl = transformCopy( Matrix.fromArrayMat( wls[ Math.floor( Math.random() * wls.length ) ] ) );
 
76
                
 
77
                draw();
 
78
        }
 
79
        
 
80
        override public function rotate()
 
81
        {
 
82
                wl.rotate90C();
 
83
                draw();
 
84
        }
 
85
        
 
86
        private function draw() 
 
87
        {
 
88
                //center on top-left cell
 
89
                var left = -wl.size.x * GConst.gridSizeX / 2.0;
 
90
                var top = -wl.size.y * GConst.gridSizeY / 2.0;
 
91
                
 
92
                while( numChildren > 0 )
 
93
                        removeChild( getChildAt( 0 ) );
 
94
                        
 
95
                graphics.clear();
 
96
                for( y in 0...wl.size.y )
 
97
                {
 
98
                        for( x in 0...wl.size.x )
 
99
                        {
 
100
                                if( wl.get(x,y) == 1 ) 
 
101
                                {
 
102
                                        //see MapBackground, this is too slow to use real Objects...
 
103
                                        var seg = new IClipWall();
 
104
                                        seg.x = left + x * GConst.gridSizeX;
 
105
                                        seg.y = top + y * GConst.gridSizeY;
 
106
                                        seg.width = GConst.gridSizeX;
 
107
                                        seg.height = GConst.gridSizeY;
 
108
                                        addChild( seg );
 
109
                                        
 
110
                                        /*drawWall( false, graphics, 
 
111
                                                left + x * GConst.gridSizeX, top + y * GConst.gridSizeY,  
 
112
                                                GConst.gridSizeX, GConst.gridSizeY );*/
 
113
                                }
 
114
                                
 
115
                        }
 
116
                }
 
117
        }       //end draw
 
118
        
 
119
        override public function dragGridTransform() : Point2
 
120
        {       
 
121
                return Point2.at(       
 
122
                        0.5 - 0.5/wl.size.x,
 
123
                        0.5 - 0.5/wl.size.y
 
124
                        );
 
125
        }
 
126
        
 
127
        static public function drawWall( danger : Bool, g : flash.display.Graphics, x : Float, y : Float, w : Float, h : Float )
 
128
        {
 
129
                g.lineStyle( 1, 0 );
 
130
                g.beginFill( if( danger ) 0xFF8080 else 0x808080 );
 
131
                g.drawRoundRect( x, y, w-1, h-1, 4, 4 );
 
132
                g.endFill();
 
133
                g.lineStyle();
 
134
        }
 
135
}