~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to restricted/app-fortress/fortress/MapBackground.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 flash.display.Sprite;
 
4
import flash.geom.Point;
 
5
import flash.events.IEventDispatcher;
 
6
import flash.events.MouseEvent;
 
7
import flash.events.Event;
 
8
import flash.display.Bitmap;
 
9
import flash.display.BitmapData;
 
10
import flash.filters.BevelFilter;
 
11
import flash.filters.BitmapFilterType;
 
12
import flash.filters.BitmapFilter;
 
13
import flash.filters.GlowFilter;
 
14
import flash.filters.ColorMatrixFilter;
 
15
 
 
16
import ui.StaticText;
 
17
import ui.AnimatedWidget;
 
18
 
 
19
import mathx.MatPoint;
 
20
import mathx.Point2;
 
21
import mathx.Matrix;
 
22
import mathx.Tracer;
 
23
 
 
24
class MapBackground extends AnimatedWidget, implements BuildDropTarget
 
25
{
 
26
        private var gameDriver : GameDriver;
 
27
        private var gm : GameMode;
 
28
        private var tracer : Tracer;
 
29
#if showcostmap
 
30
        private var costFields : Matrix<StaticText>;
 
31
#end
 
32
        
 
33
        var filtersBad : Array<BitmapFilter>;
 
34
        var landBackground : BitmapData;
 
35
        
 
36
        public function new( gd : GameDriver )
 
37
        {
 
38
                super();
 
39
                
 
40
                //requires Flash 9.0.28 (TODO: add backwards compatible mode)
 
41
                addEventListener( "addedToStage" /*Event.ADDED_TO_STAGE*/, onAddedToStage );
 
42
                addEventListener( "removedFromStage" /*Event.REMOVED_FROM_STAGE*/, onRemovedFromStage );
 
43
                
 
44
                gameDriver = gd;
 
45
                
 
46
                //switched to down to try an resolve a weird missing clicks defect (clearly FlashVM fault!)
 
47
                addEventListener( MouseEvent.MOUSE_DOWN, onMouseClicked );
 
48
                addEventListener( MouseEvent.DOUBLE_CLICK, onMouseDoubleClicked );
 
49
                //doubleClickEnabled = true;
 
50
                
 
51
                //not children should receive mouse events, this handles everything
 
52
                mouseChildren = false;
 
53
                
 
54
                tracer = new Tracer( gameDriver.data.map.landMap );
 
55
                
 
56
                //create placement filters
 
57
                filtersBad = new Array();
 
58
                var matrix = new Array();
 
59
        matrix = matrix.concat([1, 0, 0, 0, 0]); // red
 
60
        matrix = matrix.concat([0, 0, 0, 0, 0]); // green
 
61
        matrix = matrix.concat([0, 0, 0, 0, 0]); // blue
 
62
        matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha
 
63
        filtersBad.push( new ColorMatrixFilter( matrix ) );
 
64
                
 
65
                //landBackground = (new IBackground()).bitmapData;
 
66
                landBackground = new IBitmapBackground();
 
67
#if showcostmap
 
68
                costFields = null;
 
69
#end
 
70
                
 
71
                updateAll();
 
72
        }
 
73
        
 
74
        function onAddedToStage( evt : Event )
 
75
        {
 
76
                //TODO: Make an a mechanism to do this adding and removed automatically...
 
77
                gameDriver.addEventListener( GameEvent.MAP_CHANGED, onMapChanged );
 
78
                gameDriver.addEventListener( LiveEvent.OBJECT_PLACED, onObjectPlaced );
 
79
                gameDriver.addEventListener( LiveEvent.OBJECT_REMOVED, onObjectRemoved );
 
80
                stage.addEventListener( UIEvent.OPTIONS_CHANGED, onOptionsChanged );
 
81
        }
 
82
        
 
83
        function onRemovedFromStage( evt : Event )
 
84
        {
 
85
                //make sure there are removed otherwise the backgorund object may keep
 
86
                //piling up affecting performance!
 
87
                gameDriver.removeEventListener( GameEvent.MAP_CHANGED, onMapChanged );
 
88
                gameDriver.removeEventListener( LiveEvent.OBJECT_PLACED, onObjectPlaced );
 
89
                gameDriver.removeEventListener( LiveEvent.OBJECT_REMOVED, onObjectRemoved );
 
90
                stage.removeEventListener( UIEvent.OPTIONS_CHANGED, onOptionsChanged );
 
91
        }
 
92
 
 
93
        function updateAll()
 
94
        {       
 
95
                //reset drawing state (to ensure consistency)
 
96
                wavesAt = 0;
 
97
                doneOnce = false;
 
98
                
 
99
                //draw even without a know gamemode to make sure we
 
100
                //have a size after creation (needed by layout to get this.scaleX/y)
 
101
                drawPermanent();
 
102
                draw();
 
103
                redoLiveObjects();
 
104
        }
 
105
        
 
106
        function onOptionsChanged( evt : flash.events.Event )
 
107
        {
 
108
                updateAll();
 
109
        }
 
110
        
 
111
        public function onMouseDoubleClicked( evt : MouseEvent )
 
112
        {
 
113
                trace( "Not supposed to get these!" );
 
114
        }
 
115
        
 
116
        /** 
 
117
         * There is somethign strange about missing clicks, it just doesn't feel
 
118
         * as though all clicks are being handled correctly, especially under
 
119
         * windows where when you have lots of frogs it is quite clear actually.
 
120
         */
 
121
        public function onMouseClicked( evt : MouseEvent )
 
122
        {
 
123
                if( gm != GameMode.Action )
 
124
                        return;
 
125
                        
 
126
                //find closest available cannon and fire at location
 
127
                var mp  = gridGetPointF( new Point( evt.stageX, evt.stageY ) ); //why doesn't flash use it's own Point class in events?
 
128
                var co = gameDriver.data.map.getClosestAvailCannon( mp.x, mp.y );       //why don't I use my own MatPoint class ;)
 
129
                if( co == null )
 
130
                {
 
131
                        //show failed attempt
 
132
                        gameDriver.data.map.addLiveObject(      new InvalidShotObject( gameDriver, mp ) );
 
133
                        return;
 
134
                }
 
135
                
 
136
                co.fire( mp );
 
137
        }
 
138
        
 
139
        public function setGameMode( agm : GameMode )
 
140
        {
 
141
                gm = agm;
 
142
                draw();
 
143
        }
 
144
        
 
145
        private function onMapChanged( evt : GameEvent )
 
146
        {       
 
147
                draw();
 
148
        }
 
149
        
 
150
        var wavesAt : Float;
 
151
        var doneOnce : Bool;
 
152
        
 
153
        override function _animate( elapsed : Float )
 
154
        {
 
155
                //don't do animation ehre, it is far too slow...
 
156
                if( gm == GameMode.Build )
 
157
                        return;
 
158
                        
 
159
                //check global options
 
160
                if( !Options.animateWater )
 
161
                {
 
162
                        if( doneOnce )
 
163
                                return;
 
164
                }
 
165
                        
 
166
                doneOnce = true;
 
167
                        
 
168
                var waveSpeed = 5;                      
 
169
                wavesAt += elapsed;
 
170
                if( wavesAt > waveSpeed )
 
171
                        wavesAt -= waveSpeed;
 
172
                        
 
173
                var w = GConst.gridSizeX * 10;
 
174
                var wf = wavesAt / waveSpeed;
 
175
                var waveFactor = wf * w;
 
176
                
 
177
                //used for color/wavelength cycle
 
178
                var it = Math.sin( (wf*wf)* Math.PI );
 
179
                        
 
180
                ///////////////////////////////////////////////////////
 
181
                //draw background layer
 
182
                var m = new flash.geom.Matrix();
 
183
                m.createGradientBox( w, w, Math.PI/3, waveFactor*2 );
 
184
                var brush = draw.Brush.gradient(        
 
185
                        flash.display.GradientType.LINEAR,
 
186
                        [ clrWater.asInt(), clrWater.adjustBrightness( 1 + 0.5*it ).asInt(), clrWater.asInt() ],
 
187
                        [ 1, 1, 1 ],
 
188
                        [ 180 - (it * 50), 230, 255 ],  //alter wave-length in cycles
 
189
                        m,
 
190
                        flash.display.SpreadMethod.REPEAT );
 
191
                
 
192
                graphics.clear();
 
193
                brush.apply( graphics );
 
194
                graphics.drawRect( 0, 0, 
 
195
                        gameDriver.data.map.landMap.size.x * GConst.gridSizeX,
 
196
                        gameDriver.data.map.landMap.size.y * GConst.gridSizeY );
 
197
                graphics.endFill();
 
198
        }
 
199
        
 
200
        override function _resize( w : Float, h : Float )
 
201
        {
 
202
                //ignore these, this uses a different system (see Layout)
 
203
        }
 
204
        
 
205
        //see below
 
206
        static var clrWater = draw.Color.int(0x0000A0);
 
207
        static var clrLand = draw.Color.int(0x008000);
 
208
        
 
209
        var masses : Array<Sprite>;
 
210
                
 
211
        private function drawPermanent()
 
212
        {
 
213
                _animate( 0 );
 
214
                
 
215
                //cleanup old items (if any)
 
216
                if( masses != null )
 
217
                {
 
218
                        for( m in masses )
 
219
                                removeChild( m );
 
220
                }
 
221
                
 
222
                masses = new Array<Sprite>();
 
223
                for( i in 0...tracer.traces.length )
 
224
                {       
 
225
                        var tr = tracer.traces[i];
 
226
                        var sub = new Sprite();
 
227
                        sub.cacheAsBitmap = true;
 
228
                        var clr = tracer.tracesColor[i];
 
229
                        var dclr;
 
230
                        switch( clr )
 
231
                        {
 
232
                                case MapData.ctLand:
 
233
                                        dclr = clrLand.asInt();
 
234
                                        var c = new Array();
 
235
                                        c.push( 
 
236
                                                new BevelFilter( 
 
237
                                                        GConst.gridSizeX/2,     //width
 
238
                                                        45,     //angle?
 
239
                                                        clrLand.brighten().asInt(),     //highlight
 
240
                                                        0.5,
 
241
                                                        clrLand.darken().asInt(), //shadow
 
242
                                                        0.5,
 
243
                                                        GConst.gridSizeX/2,GConst.gridSizeY/2,  //blur
 
244
                                                        3,      //strenght?
 
245
                                                        flash.filters.BitmapFilterQuality.MEDIUM,       //quality,
 
246
                                                        BitmapFilterType.INNER
 
247
                                                        )
 
248
                                                );
 
249
                                        sub.filters = c;
 
250
                                case MapData.ctWater:
 
251
                                        //only draw lakes, otherwise allow ocean to shine through
 
252
                                        if( !tracer.tracesOverlap[i] )
 
253
                                                continue;
 
254
                                        dclr = clrWater.asInt();
 
255
                                default:
 
256
                                        dclr = 0;
 
257
                        }
 
258
                        
 
259
                        //reduce traces node count (produce more natural shoreline, at expense of accuracy)
 
260
                        var q = new Array();
 
261
                        var j = 0;
 
262
                        //var last = MatPoint.at( -1, -1 );
 
263
                        while( j < tr.length )
 
264
                        {
 
265
                                q.push( tr[j] );
 
266
                                j+=2;
 
267
                        }
 
268
                        
 
269
                        //draw curved shoreline
 
270
                        if( dclr == clrLand.asInt() )
 
271
                        {
 
272
                                var matrix = new flash.geom.Matrix(); 
 
273
                                //matrix.scale( 0.5, 0.5 );     //to get scaled filling, at original resolution, NOTE: unscaled form looks better somehow...
 
274
                                if( Options.texturedLand )
 
275
                                        sub.graphics.beginBitmapFill( landBackground, matrix );
 
276
                                else
 
277
                                        sub.graphics.beginFill( dclr );
 
278
                        }
 
279
                        else
 
280
                                sub.graphics.beginFill( dclr );
 
281
                        var s = q[0].promoteF().midTo( q[q.length-1].promoteF() );      //between first and last
 
282
                        sub.graphics.moveTo( s.x * GConst.gridSizeX, s.y * GConst.gridSizeY );
 
283
                        var j = 0;
 
284
                        while( j < q.length )   //last taken care of above
 
285
                        {
 
286
                                var ctl = q[j].promoteF();
 
287
                                //offset ctl to remove grid-like spacing
 
288
                                if( ctl.x != 0 && ctl.x != gameDriver.data.map.landMap.size.x ) //leave edges alone
 
289
                                        ctl.x += (Math.random() -0.5) / 2;      //decrease to make rougher, increase for smoother
 
290
                                if( ctl.y != 0 && ctl.y != gameDriver.data.map.landMap.size.y ) //leave edges alone
 
291
                                        ctl.y += (Math.random() -0.5) / 2;
 
292
                                        
 
293
                                var anc = 
 
294
                                        if( j == q.length - 1 )
 
295
                                                q[j].promoteF().midTo( q[0].promoteF() );
 
296
                                        else
 
297
                                                q[j].promoteF().midTo( q[j+1].promoteF() );
 
298
                                sub.graphics.curveTo( ctl.x * GConst.gridSizeX, ctl.y * GConst.gridSizeY,
 
299
                                        anc.x * GConst.gridSizeX, anc.y * GConst.gridSizeY );
 
300
                                j++;
 
301
                        }
 
302
                        sub.graphics.endFill();
 
303
                        
 
304
                        //sub.visible = false;  //for fun debugging without masses
 
305
                        addChild( sub );
 
306
                        masses.push( sub );
 
307
                }
 
308
                
 
309
                //draw water depth (taken out of draw, since this is permanent)
 
310
                var water = new Sprite();
 
311
                water.cacheAsBitmap = true;
 
312
                if( Options.depthWater )
 
313
                {
 
314
                        water.blendMode = flash.display.BlendMode.MULTIPLY;
 
315
                        for( m in gameDriver.data.map.landMap.xOrderIter() ) 
 
316
                        {
 
317
                                var ct =  gameDriver.data.map.landMap.get( m.x, m.y );
 
318
                                var sdist = gameDriver.data.map.shoreDistMap.get( m.x, m.y );
 
319
                                
 
320
                                var color;
 
321
                                if( ct != MapData.ctWater )
 
322
                                        continue;
 
323
                                if( sdist == 0 )
 
324
                                        continue;       //shore is handled by masses...
 
325
                                //color = draw.ColorUtil.adjustBrightness( clrWater, 1.0 - sdist/50);
 
326
                                color = draw.Color.int( 0xffffff ).adjustBrightness( 1.0 - sdist/35).asInt();
 
327
                                
 
328
                                water.graphics.beginFill( color );
 
329
                                water.graphics.drawRect( m.x * GConst.gridSizeX, m.y * GConst.gridSizeY,
 
330
                                        GConst.gridSizeX, GConst.gridSizeY );
 
331
                                water.graphics.endFill();
 
332
                        }
 
333
                }
 
334
                masses.push( water );
 
335
                addChild( water );
 
336
                
 
337
                
 
338
                createBuildOverlay();
 
339
        }
 
340
        
 
341
        var buildOverlay : Sprite;
 
342
        private function createBuildOverlay()
 
343
        {
 
344
                if( buildOverlay != null )
 
345
                {
 
346
                        removeChild( buildOverlay );
 
347
                }
 
348
                
 
349
                buildOverlay = new Sprite();
 
350
                buildOverlay.visible = false;
 
351
                for( m in gameDriver.data.map.landMap.xOrderIter() ) 
 
352
                {
 
353
                        //produce grid-like pattern
 
354
                        if( (m.x%2) ^ (m.y%2) == 1
 
355
                                //&& gameDriver.data.map.landMap.get( m.x, m.y ) == MapData.ctLand  //only on land pieces (Doesn't look as good, sicne near shore it overlaps with water...)
 
356
                                )
 
357
                        {
 
358
                                buildOverlay.graphics.beginFill( 0x303030 );
 
359
                                buildOverlay.graphics.drawRect( m.x * GConst.gridSizeX, m.y * GConst.gridSizeY,
 
360
                                        GConst.gridSizeX, GConst.gridSizeY );
 
361
                                buildOverlay.graphics.endFill();
 
362
                        }
 
363
                        //DIFF is good is background is light, ADD is good when background is dark
 
364
                        buildOverlay.blendMode = flash.display.BlendMode.DIFFERENCE;
 
365
                }
 
366
                
 
367
                addChild( buildOverlay );
 
368
        }
 
369
        
 
370
        var overlay : Sprite;
 
371
        var land : Sprite;
 
372
        var walls : Array<IClipWall>;
 
373
        
 
374
        private function draw()
 
375
        {
 
376
                var start = flash.Lib.getTimer();
 
377
                
 
378
                if( overlay == null )
 
379
                        overlay = new Sprite();
 
380
                
 
381
                //NOTE: wall logic takes less than 5% of time.
 
382
                if( walls == null )
 
383
                        walls = new Array<IClipWall>();
 
384
                var wallAt = 0;
 
385
                
 
386
                if( land != null )
 
387
                {
 
388
                        /*for( i in 0...land.numChildren )
 
389
                                if( Std.is( land.getChildAt( i ), IClipWall ) )
 
390
                                        walls.push( cast( land.getChildAt(i ) ) );*/
 
391
                }
 
392
                else
 
393
                        land = new Sprite();
 
394
                
 
395
                overlay.graphics.clear();
 
396
                overlay.cacheAsBitmap = true;
 
397
                
 
398
#if showcostmap
 
399
                if( costFields == null )
 
400
                {
 
401
                        var unused : StaticText = null;
 
402
                        costFields = Matrix.create( gameDriver.data.map.landMap.size.x, gameDriver.data.map.landMap.size.y, unused );
 
403
                        for( mp in gameDriver.data.map.landMap.xOrderIter() ) 
 
404
                        {
 
405
                                var t = StaticText.singleLine( '99' );
 
406
                                t.x = mp.x * GConst.gridSizeX;
 
407
                                t.y = mp.y * GConst.gridSizeY;
 
408
                                t.rescale( GConst.gridSizeX, GConst.gridSizeY );
 
409
                                addChild( t );
 
410
                                
 
411
                                costFields.set( mp.x, mp.y, t );
 
412
                        }
 
413
                }
 
414
#end
 
415
                
 
416
                //if not null then assume we are in cannon building mode
 
417
                var ca = gameDriver.data.map.cannonAllowance;
 
418
                
 
419
                for( my in 0...gameDriver.data.map.landMap.size.y )
 
420
                for( mx in 0...gameDriver.data.map.landMap.size.x )
 
421
                //for( m in gameDriver.data.map.landMap.xOrderIter() ) 
 
422
                {
 
423
                        //var mx = m.x;
 
424
                        //var my = m.y;
 
425
                        //NOTE: 25% of time is consumed strictly by the iterator! (replaced with direct iteration)
 
426
                                                
 
427
#if showcostmap
 
428
                        //costFields.get(m.x,m.y).text = "" + gameDriver.data.map.costMap.get(m.x,m.y);
 
429
                        costFields.get(mx,my).text = "" + gameDriver.data.map.modMap.get(mx,my);
 
430
#end
 
431
 
 
432
                        var ct =  gameDriver.data.map.landMap.get( mx, my );
 
433
                        
 
434
                        var color;
 
435
                        var alpha = 1.0;
 
436
                        if( ct == MapData.ctWater )
 
437
                                continue;       //handled in permanent
 
438
                        else if( ct == MapData.ctLand )
 
439
                        {
 
440
                                var mod = gameDriver.data.map.modMap.get( mx, my );
 
441
                                if( mod & MapData.modFort == MapData.modFort )
 
442
                                {
 
443
                                        if( ca != null && !ca.isAllowedAt( mx, my ) )
 
444
                                                color = 0xA08040;       //a bit red to indicate it isn't available
 
445
                                        else
 
446
                                                color = 0x408040;
 
447
                                        alpha = 0.75;
 
448
                                }
 
449
                                else
 
450
                                        continue;       //nothing speical, carry on
 
451
                        }
 
452
                        else if( ct == MapData.ctWall )
 
453
                        {
 
454
                                var wall : IClipWall;
 
455
                                var was : Bool = false;
 
456
                                if( wallAt < walls.length )
 
457
                                {
 
458
                                        wall = walls[wallAt++]; 
 
459
                                        wall.visible = true;
 
460
                                        was = true;
 
461
                                }
 
462
                                else
 
463
                                {
 
464
                                        wall = new IClipWall();
 
465
                                        //add to list (to avoid needing to recreate each time)
 
466
                                        walls.push( wall );
 
467
                                        wallAt++;
 
468
                                        //wall.cacheAsBitmap = true;    (strangeness with redrawing of border when moving around the display)
 
469
                                }
 
470
                                wall.x = mx * GConst.gridSizeX;
 
471
                                wall.y = my * GConst.gridSizeY;
 
472
                                wall.width = GConst.gridSizeX;
 
473
                                wall.height = GConst.gridSizeY;
 
474
                                if( !was )
 
475
                                        land.addChild( wall );
 
476
                                
 
477
                                var mod = gameDriver.data.map.modMap.get( mx, my );
 
478
                                if( mod & MapData.modFort == MapData.modFort )
 
479
                                        wall.filters = null;
 
480
                                else
 
481
                                        wall.filters = filtersBad;
 
482
                                continue;
 
483
                        }
 
484
                        else
 
485
                        {
 
486
#if debug                       
 
487
                                trace( ct );
 
488
                                Assert.unreachable();
 
489
#end
 
490
                                continue;       //what are these?
 
491
                        }
 
492
                                
 
493
                        overlay.graphics.beginFill( color, alpha );
 
494
                        overlay.graphics.drawRect( mx * GConst.gridSizeX, my * GConst.gridSizeY,
 
495
                                GConst.gridSizeX, GConst.gridSizeY );
 
496
                        overlay.graphics.endFill();
 
497
                }
 
498
                
 
499
                //remove excessive walls
 
500
                while( wallAt < walls.length )
 
501
                        walls[wallAt++].visible = false;
 
502
 
 
503
                addChildAt( overlay, getChildIndex( masses[masses.length-1] ) + 1 );
 
504
                addChildAt( land, getChildIndex( overlay ) );
 
505
                
 
506
                if( gm == GameMode.Build )
 
507
                {
 
508
                        buildOverlay.visible = true;
 
509
                        addChildAt( buildOverlay, getChildIndex( overlay ) + 1);
 
510
                }
 
511
                else
 
512
                        buildOverlay.visible = false;
 
513
                        
 
514
                //trace( "mbDraw: " + (flash.Lib.getTimer() - start) );
 
515
        }
 
516
        
 
517
        /**
 
518
         * Adjusts a stage point to be aligned with
 
519
         * the local grid system -- moving it to the
 
520
         * center of the closest cell
 
521
         */
 
522
        public function gridAdjustPoint( p : Point ) : Point
 
523
        {
 
524
                var mp = gridGetPoint( p );
 
525
                var l : Point = new Point();
 
526
                l.x = (mp.x + 0.5) * GConst.gridSizeX;
 
527
                l.y = (mp.y + 0.5) * GConst.gridSizeY;
 
528
                return localToGlobal( l );
 
529
        }
 
530
        
 
531
        /**
 
532
         * Translates a global point into the map grid point (the point
 
533
         * in the pointed-to cell)
 
534
         */
 
535
        public function gridGetPoint( p : Point ) : MatPoint
 
536
        {
 
537
                var l = globalToLocal( p );
 
538
                
 
539
                var m = MatPoint.at(
 
540
                        Math.floor( l.x / GConst.gridSizeX ),
 
541
                        Math.floor( l.y / GConst.gridSizeY )
 
542
                        );
 
543
                
 
544
                return m;
 
545
        }
 
546
        
 
547
        public function gridGetPointF( p : Point ) : Point2
 
548
        {
 
549
                var l = globalToLocal( p );
 
550
                
 
551
                var m = Point2.at(
 
552
                        l.x  / GConst.gridSizeX,
 
553
                        l.y  / GConst.gridSizeY
 
554
                    );
 
555
                
 
556
                return m;
 
557
        }
 
558
 
 
559
        function redoLiveObjects()
 
560
        {
 
561
                //clear all live object children (go backwards as to not affect ordering)
 
562
                for( i in new RangeIterI( numChildren-1, 0 ) )
 
563
                {
 
564
                        var child = getChildAt( i );
 
565
                        if( Std.is( child, LiveObject ) )
 
566
                                removeChild( child );
 
567
                }
 
568
                
 
569
                for( lo in gameDriver.data.map.allLiveObjects() )
 
570
                        addChild( lo );
 
571
        }
 
572
        
 
573
        public function onObjectPlaced( lo : LiveEvent )
 
574
        {
 
575
                for( lobj in lo.liveObjs )
 
576
                        addChild( lobj );
 
577
        }
 
578
        
 
579
        public function onObjectRemoved( lo : LiveEvent )
 
580
        {
 
581
                for( lobj in lo.liveObjs )
 
582
                        if( contains( lobj ) )  //this may be coming from a previous MapData during a level change
 
583
                                removeChild( lobj );
 
584
        }
 
585
        
 
586
        public function acceptObject( bo : BuildObject, sp : flash.geom.Point ) : Bool
 
587
        {
 
588
                var mp = gridGetPoint( sp );
 
589
                return gameDriver.data.map.placeBuildObj( mp.x, mp.y, bo );
 
590
        }
 
591
        
 
592
        public function canAcceptObject( bo : BuildObject, sp : flash.geom.Point ) : Bool
 
593
        {
 
594
                var tsp = gridAdjustPoint( sp );
 
595
                var mp = gridGetPoint( tsp );
 
596
                return gameDriver.data.map.canPlaceBuildObj( mp.x, mp.y, bo );
 
597
        }
 
598
        
 
599
        public function alignDropObject( tsp : flash.geom.Point ) : flash.geom.Point
 
600
        {
 
601
                return gridAdjustPoint( tsp );
 
602
        }
 
603
        
 
604
        public function getDropPriority( ) : Int
 
605
        {
 
606
                return BuildDropTargetPriority.prioBackground;
 
607
        }
 
608
        
 
609
}