~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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* <license>
 * This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
 * For full copyright and license information please refer to doc/license.txt.
 * </license> 
 */
package squix;

import flash.display.Sprite;
import mathx.Point2;
import mathx.MatPoint;
import mathx.Matrix;

/**
 * Implements the animations/logic for ending the level with a completed mini-puzzle
 */
class MiniPuzzleComplete extends GameObject
{
	var miniPuzzle : MiniPuzzle;	//maintains beyond end of game level
	var timeout : Float;
		
	var board : Matrix<ui.anim.Actor>;
	
	var cleared : Bool;	//the board has been cleared completely
	
	public function new( game : Game, miniPuzzle : MiniPuzzle )
	{
		super( game, GameObject.activeLevelObject );
		this.miniPuzzle = miniPuzzle;
		cleared = false;
		
		timeout = game.global.puzzleEndTime;
		game.startLevelEnd();
		
		//TODO: taken from PuzzleBoard, could it be combined?
		board = Matrix.create( miniPuzzle.grid.size.x, miniPuzzle.grid.size.y, null );
		for( mpgi in miniPuzzle.grid.xOrderIter() )
		{
			var mpg = miniPuzzle.grid.get( mpgi.x, mpgi.y );
			var actor = new ui.anim.lib.PuzzlePiece(
				ui.anim.lib.PuzzlePiece.decodePiece( mpg.code )
				);
			actor.setVisible( false );
			game.corePanel.addActor( actor );	
			board.set( mpgi.x, mpgi.y, actor );
			
			//filter setup
			var fil = new Array<flash.filters.BitmapFilter>();
			fil.push( 
				new flash.filters.BevelFilter( 
					5, 30, 	//tODO: should be done in resize where the sizes are known
					draw.Color.rgb( 0.5,1,0 ).asInt(), 1,
					draw.Color.rgb( 0, 0.5, 0).asInt(), 1,
					4, 4, 	//blur
					10, //strength
					flash.filters.BitmapFilterQuality.HIGH, //quality
					flash.filters.BitmapFilterType.INNER
					)
				);
			actor.filters = fil;
		}
		doSize();
		
		//update stats
		Options.ifSetLevelStatus( game.curLevel, Options.statusPuzzle );
		game.numLives+=game.global.puzzleCompleteLivesBonus;
		
		flashx.SoundManager.playSound( "puzzleComplete" ); 
	}
	
	override function _stepTime( elapsed : Float )
	{
		timeout -= elapsed;
		if( timeout < 0 )
		{
			game.endLevelEnd();
			destroy();
		}
		
		//time relative to overall time
		var tat = timeout / game.global.puzzleEndTime;
		
		//in the middle of the animation we actually clear the surface (producing a revealing effect)
		if( !cleared && tat < 0.5 )
		{
			cleared = true;
			var dc = new Sprite();
			dc.graphics.beginFill( 0 );
			dc.graphics.drawRect( 0, 0, game.surface.width, game.surface.height );
			game.clearSurface( dc );
		}
		
		var maxli = board.toLinearIndex( board.size );
		for( pbti in board.xOrderIter() )
		{
			//get a proportional index of the piece to compare to time
			var actor = board.get(pbti.x,pbti.y);
			var li = board.toLinearIndex( pbti );
			var at = li / maxli;
			
			//phase in initially
			var inTat = 1 - (tat - 0.5)*2;
			if( at < inTat )
				actor.setVisible( true );
				
			//then phase out
			var outTat = (0.5 - tat)*2;
			if( at < outTat )
				actor.setVisible( false );
		}
	}
	
	function doSize()
	{
		//position is always 0,0
		var pw = game.surface.width / board.size.x;
		var ph = game.surface.height / board.size.y;
		for( pbti in board.xOrderIter() )
		{
			var actor = board.get(pbti.x,pbti.y);
			game.corePanel.placeActor( actor, Point2.at( pw * (pbti.x+0.5), ph * (pbti.y+0.5) ) );
			game.corePanel.sizeActor( actor, Point2.at( pw, ph ) );	
		}
	}
	
	override function destroy()
	{
		super.destroy();
		for( pbti in board.xOrderIter() )
			game.corePanel.removeActor( board.get(pbti.x,pbti.y) );
	}
}