~extremepopcorn/dhlib/dhlib_ep

« back to all changes in this revision

Viewing changes to app-dicey/dicey/Laser.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 dicey;
 
7
 
 
8
import DrawBasicTypes;
 
9
 
 
10
/**
 
11
 * The laser is the visible laser beam from one point to another.
 
12
 */
 
13
class Laser extends GameObject
 
14
{
 
15
        var image : ui.Widget;
 
16
        var from : Point2;
 
17
        var to : Point2;
 
18
        var remainTime : Float;
 
19
        var surgeAt : Float;
 
20
        
 
21
        public function new( game : Game, from : Point2, to : Point2 )
 
22
        {
 
23
                super( game );
 
24
                
 
25
                this.from = from.clone();
 
26
                this.to = to.clone();
 
27
                
 
28
                remainTime = 2.0 * Global.laserSpeed;
 
29
                surgeAt = 0;
 
30
                
 
31
                //we just cover the whole surface and draw a single line in it.
 
32
                at = Point2.at( 0, 0 );
 
33
                image = ui.DrawBox.scaled( drawLaser, Point2.at( 1, 1 ) );
 
34
                game.actionDisplay.addActor( image );
 
35
                game.actionDisplay.placeActor( image, at );
 
36
                //must be sized last, since a draw will actually occur and we need to be initialized
 
37
                game.actionDisplay.sizeActor( image, Point2.at( 1, 1 ) );
 
38
        }
 
39
        
 
40
        override public function stepTime( elapsed : Float )
 
41
        {
 
42
                remainTime -= elapsed;
 
43
                if( remainTime <= 0 )
 
44
                {
 
45
                        destroy();
 
46
                        return;
 
47
                }
 
48
                
 
49
                if( remainTime <= Global.laserSpeed )
 
50
                        image.getNative().alpha = remainTime / Global.laserSpeed;
 
51
                        
 
52
                //TODO: could optimize to stop drawing after surge is gone, but we
 
53
                //need to ensure that it acutally reaches the maximum distance...
 
54
                surgeAt += elapsed / Global.laserSpeed;
 
55
                //trace( surgeAt );
 
56
                image.update();
 
57
        }
 
58
        
 
59
        override function destroy()
 
60
        {
 
61
                super.destroy();
 
62
                game.actionDisplay.removeActor( image );
 
63
        }
 
64
        
 
65
        function drawLaser( gfx : draw.GraphicsX )
 
66
        {
 
67
                var seg = to.sub( from );
 
68
                var sb = Math.max( 0, Math.min( 1, surgeAt - 0.1 ) );
 
69
                var se = Math.min( 1, surgeAt );
 
70
                
 
71
                //draw trail
 
72
                gfx.use( Pen.solid( 2, Color.rgb( 0.5, 0.8, 1.0 ), 0.5 ) );
 
73
                gfx.drawLinePt( from, from.add( seg.mul( se ) ) );
 
74
                
 
75
                //draw surge moving along the line
 
76
                gfx.use( Pen.solid( 3, Color.rgb( 1, 1, 1 ) ) );
 
77
                gfx.drawLinePt( from.add( seg.mul( sb ) ), from.add( seg.mul( se ) ) );
 
78
        }
 
79
}