~eda-qa/dhlib/main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* <license>
 * This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
 * For full copyright and license information please refer to doc/license.txt.
 * </license> 
 */
package dicey;

import DrawBasicTypes;

/**
 * The laser is the visible laser beam from one point to another.
 */
class Laser extends GameObject
{
	var image : ui.Widget;
	var from : Point2;
	var to : Point2;
	var remainTime : Float;
	var surgeAt : Float;
	
	public function new( game : Game, from : Point2, to : Point2 )
	{
		super( game );
		
		this.from = from.clone();
		this.to = to.clone();
		
		remainTime = 2.0 * Global.laserSpeed;
		surgeAt = 0;
		
		//we just cover the whole surface and draw a single line in it.
		at = Point2.at( 0, 0 );
		image = ui.DrawBox.scaled( drawLaser, Point2.at( 1, 1 ) );
		game.actionDisplay.addActor( image );
		game.actionDisplay.placeActor( image, at );
		//must be sized last, since a draw will actually occur and we need to be initialized
		game.actionDisplay.sizeActor( image, Point2.at( 1, 1 ) );
	}
	
	override public function stepTime( elapsed : Float )
	{
		remainTime -= elapsed;
		if( remainTime <= 0 )
		{
			destroy();
			return;
		}
		
		if( remainTime <= Global.laserSpeed )
			image.getNative().alpha = remainTime / Global.laserSpeed;
			
		//TODO: could optimize to stop drawing after surge is gone, but we
		//need to ensure that it acutally reaches the maximum distance...
		surgeAt += elapsed / Global.laserSpeed;
		//trace( surgeAt );
		image.update();
	}
	
	override function destroy()
	{
		super.destroy();
		game.actionDisplay.removeActor( image );
	}
	
	function drawLaser( gfx : draw.GraphicsX )
	{
		var seg = to.sub( from );
		var sb = Math.max( 0, Math.min( 1, surgeAt - 0.1 ) );
		var se = Math.min( 1, surgeAt );
		
		//draw trail
		gfx.use( Pen.solid( 2, Color.rgb( 0.5, 0.8, 1.0 ), 0.5 ) );
		gfx.drawLinePt( from, from.add( seg.mul( se ) ) );
		
		//draw surge moving along the line
		gfx.use( Pen.solid( 3, Color.rgb( 1, 1, 1 ) ) );
		gfx.drawLinePt( from.add( seg.mul( sb ) ), from.add( seg.mul( se ) ) );
	}
}