~extremepopcorn/dhlib/dhlib_ep

« back to all changes in this revision

Viewing changes to lib/draw/TileStructure.mhx

  • 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
/* <license>
 
2
 * This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
 
3
 * For full copyright and license information please refer to doc/license.txt.
 
4
 * </license> 
 
5
 */
 
6
package draw;
 
7
 
 
8
import mathx.Point2;
 
9
 
 
10
/**
 
11
 * Produce iterators for locations of points.  It is intended to be
 
12
 * used with a TileCreator, but there is no limitation on this.
 
13
 */
 
14
/*final*/ class TileStructure
 
15
{
 
16
        var type : String;
 
17
        var size : Point2;
 
18
        var units : Point2;
 
19
        var times : Int;
 
20
        
 
21
        private function new( type : String, size : Point2, ?units : Point2, times : Int )
 
22
        {
 
23
                this.type = type;
 
24
                this.size = size;
 
25
                this.units = units;
 
26
                this.times = times;
 
27
        }
 
28
        
 
29
        public function iterator() : Iterator<Point2>
 
30
        {
 
31
                if( type == "grid" )
 
32
                        return new TSRegular( size, units, false, false );
 
33
                else if( type == "staggeredX" )
 
34
                        return new TSRegular( size, units, true, false );
 
35
                else if( type == "staggeredY" )
 
36
                        return new TSRegular( size, units, false, true );
 
37
                else if( type == "random" )
 
38
                        return new TSRandom( size, times );
 
39
                Assert.unreachable();
 
40
                return null;
 
41
        }
 
42
        
 
43
        /**
 
44
         * Creates a regular grid pattern of points covering the given
 
45
         * size in the given units.
 
46
         * Note that the units may be fractional values; all points within
 
47
         * the given size will be produced in a top-down, left-right fashion
 
48
         */
 
49
        static public function grid( size : Point2, units : Point2 ) : TileStructure
 
50
        {
 
51
                return new TileStructure( "grid", size, units , 0);
 
52
        }
 
53
        
 
54
        static public function staggeredX( size : Point2, units : Point2 ) : TileStructure
 
55
        {
 
56
                return new TileStructure( "staggeredX", size, units, 0 );
 
57
        }
 
58
        
 
59
        static public function staggeredY( size : Point2, units : Point2 ) : TileStructure
 
60
        {
 
61
                return new TileStructure( "staggeredY", size, units, 0 );
 
62
        }
 
63
        
 
64
        static public function random( size : Point2, times : Int ) : TileStructure
 
65
        {
 
66
                return new TileStructure( "random", size, null, times );
 
67
        }
 
68
}
 
69
 
 
70
//HAXE: Why can't I implement Iterator<Point2>?
 
71
/*final*/ private class TSRegular /*implements Iterator<Point2>*/
 
72
{
 
73
        var size : Point2;
 
74
        var step : Point2;
 
75
        var at : Point2;
 
76
        var staggerAt : Point2;
 
77
        
 
78
        var row : Int;
 
79
        var col : Int;
 
80
        var staggerX : Bool;
 
81
        var staggerY : Bool;
 
82
        
 
83
        public function new( size : Point2, units : Point2, staggerX : Bool, staggerY : Bool )
 
84
        {
 
85
                MixinSimpleIterator();
 
86
                
 
87
                ASSERT_TRUE( size.x > 0 && size.y > 0 )
 
88
                ASSERT_TRUE( units.x > 0 && units.y > 0 )
 
89
                this.size = size;
 
90
                this.step = Point2.at( size.x/units.x, size.y/units.y );
 
91
                this.at = null;
 
92
                
 
93
                row = 0;
 
94
                col = 0;
 
95
                this.staggerX = staggerX;
 
96
                this.staggerY = staggerY;
 
97
                this.staggerAt = Point2.at( 0, 0 );
 
98
        }
 
99
 
 
100
        function _next() : Point2
 
101
        {
 
102
                if( at == null )
 
103
                {
 
104
                        at = Point2.at( 0, 0 );
 
105
                        return at;
 
106
                }
 
107
                
 
108
                at.x += step.x;
 
109
                col++;
 
110
                if( at.x >= size.x )
 
111
                {
 
112
                        at.x = 0;
 
113
                        col = 0;
 
114
                        at.y += step.y;
 
115
                        row++;
 
116
                        if( at.y >= size.y )
 
117
                                return null;
 
118
                }
 
119
                
 
120
                //stagger in the X/Y direction if desired
 
121
                if( staggerX || staggerY )
 
122
                {
 
123
                        staggerAt.x = at.x;
 
124
                        staggerAt.y = at.y;
 
125
                        
 
126
                        if( staggerX && (row % 2) == 1 )
 
127
                                staggerAt.x += step.x/2;
 
128
                        if( staggerY && (col % 2) == 1 )
 
129
                                staggerAt.y += step.y/2;
 
130
                                
 
131
                        return staggerAt;
 
132
                }
 
133
                
 
134
                return at;
 
135
        }
 
136
        
 
137
        define(`SimpleIteratorType',`Point2')
 
138
        include(`MixinSimpleIterator.ihx')
 
139
}
 
140
 
 
141
/*final*/ private class TSRandom /*implements Iterator<Point2>*/
 
142
{
 
143
        var size : Point2;
 
144
        var remain : Int;
 
145
                
 
146
        public function new( size : Point2, times : Int )
 
147
        {
 
148
                MixinSimpleIterator();
 
149
                
 
150
                ASSERT_TRUE( size.x > 0 && size.y > 0 )
 
151
                this.remain = times;
 
152
                this.size = size;
 
153
        }
 
154
 
 
155
        function _next() : Point2
 
156
        {
 
157
                if( this.remain <= 0 )
 
158
                        return null;
 
159
                        
 
160
                this.remain--;
 
161
                return Point2.at(
 
162
                                mathx.Random.s_nextFloat( 0, size.x ),
 
163
                                mathx.Random.s_nextFloat( 0, size.y )
 
164
                        );
 
165
        }
 
166
                
 
167
        define(`SimpleIteratorType',`Point2')
 
168
        include(MixinSimpleIterator.ihx)
 
169
}