-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.as
More file actions
114 lines (102 loc) · 2.62 KB
/
Stack.as
File metadata and controls
114 lines (102 loc) · 2.62 KB
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
package
{
import com.greensock.TweenLite;
/**
* La pile des nombres qui s'affichent
* @author Neamar
*/
public final class Stack
{
protected var _movingNumbers:Vector.<MovingNumber> = new Vector.<MovingNumber>(Main.NB_LANES);
protected var _nbMovingNumbers:int = Main.NB_LANES;
protected var _x:int = 0;
protected var _y:int = 0;
public function Stack()
{
for (var i:int = 0; i < _nbMovingNumbers; i++)
{
_movingNumbers[i] = new MovingNumber();
_movingNumbers[i].view.x = Main.LANE_OFFSETX;
_movingNumbers[i].view.y = (i + .5) * Main.LANE_HEIGHT;
}
}
/**
* Essaie le nombre passé en paramètre et renvoie true s'il correspond au premier MovingNumber
* @param v
* @return true si le nombre passé est correct
*/
public function tryNumber(v:int):Boolean
{
if (v == _movingNumbers[0].getValue())
{
shift();
return true;
}
else
{
return false;
}
}
/**
* Décale tous les numéros de quelques pixels
* @param vitesse
*/
public function iterate(vitesse:int):void
{
_x += vitesse;
//l'abscisse à atteindre.
var objectiveX:int;
//l'abscisse actuelle
var currentX:int;
for (var i:int = 0; i < _nbMovingNumbers; i++)
{
objectiveX = _x / Math.pow(2, i);
currentX = _movingNumbers[i].view.x - Main.LANE_OFFSETX;
if (objectiveX > currentX)
{
//Les choses avancent !
_movingNumbers[i].view.x = Main.LANE_OFFSETX + objectiveX;
}
else
{
//Il faut faire reculer le chariot, mais pas trop rapidement pour ne pas avoir d'animation saccadée
_movingNumbers[i].view.x = Main.LANE_OFFSETX + objectiveX + 1.5 * (currentX - objectiveX) / 2
}
}
}
/**
* Supprime le nombre actuellement en tête
*/
public function shift():void
{
//Décaler tous les numéros d'un cran
for (var i:int = 0; i < _nbMovingNumbers - 1; i++)
{
_movingNumbers[i].setValue(_movingNumbers[i + 1].getValue());
}
//Et inventer un nouveau numéro pour le petit dernier :
_movingNumbers[i].setValue(MovingNumber.getRandomValue());
calmDown();
}
/**
* Descend le premier nombre une voie plus bas
*/
public function moveOneLaneDown():void
{
var movingNumber:MovingNumber = _movingNumbers.shift();
movingNumber.destroy();
_nbMovingNumbers--;
_y += Main.LANE_HEIGHT;
calmDown();
}
public function getX():int { return _x; }
/**
* Ramène à la position correcte après la perte d'une voie, ou quand on a validé correctement un numéro.
*/
protected function calmDown():void
{
//Ramener à la position correcte :
_x /= 2;
}
}
}