Report abuse

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package {
	/******************************** Glossary ********************************/
	/* A Spanish explorer who was also a mathematician spent time learning a
	/* a mystery system from African Sufi mystics. They used a peculiar system
	/* for divining future events and explaining patterns in the natural world.
	/* They would draw lines in the sand such as ----, ====, -=-=, =-=-, etc.
	/* Each of these quadrigrams were called "sacred words" and represented
	/* aspects of nature. It turns out that these "mystics" were practicing the
	/* worlds first binary mathematical logic and artificial chaos system or
	/* random number generator.
	/* The numbers sacred to this system were 2, 4, 8, 16, 32, and 256 and they
	/* used 16 as the base of their mathematics rather than 10. This system
	/* would be transmitted to Europe as "Geomancy" and eventually be the sole
	/* model for memory storage and computation in the modern age.
	/*
	/* Ifa is the sacred system whence this all derives and since they have 
	/* concise and succinct words for handling concepts inherent to programming
	/* I tend to make use of this system in my own nomenclature. This is done for
	/* much the same reason contempory scientists use the Greco-Roman sufix/prefix system
	/*
	/* The follow is a list of words used in this ap and their definitions:
	/* Ile: Home or Abode
	/* Ire: Yang, Possitive, Expansion
	/* Ibi: Yin, Negative, Contraction
	/* Ori: Head, Self, Soul
	/* Obi: Parent
	/* Omo: Child
	/* Oba: Chief, King/Queen
	/* Oku: Ghost, Spirit
	/* Oju: Face
	/* Egun: Ancestor
	/* Oruko: Full Name
	/* Ologun: Warrior, Contender
	/**************************************************************************/
	import flash.display.*;
	import flash.geom.Point;
	import fl.motion.easing.*;
	import flash.events.Event;
	import flash.utils.getTimer;
	import flash.text.TextFormat;
	import flash.utils.getDefinitionByName;
	import flash.external.ExternalInterface;
	import ayz.templates.Component;
	import ayz.interfaces.IComponent;
	import ayz.loaders.ImageLoader;
	import ayz.utils.Debugger;
	import ayz.managers.*;
	import rw.handlers.Liaison;
	import rw.data.XMLObject;
	import rw.utils.Copy;
	import gs.*;

	public class TournamentBrackets extends Component implements IComponent {
		//Inherent Elements
		public var cell:MovieClip;
		public var closeButton:MovieClip;
		public var expandButton:MovieClip;
		public var predictMessage:MovieClip;
		public var trailerMessage:MovieClip;
		//Variables
		private var ourMode:String;
		private var ourQue:uint = 0;
		private var ourBreadth:Number;
		private var ourDepth:Number;
		private var ourTree:CombatTree;
		private var ourFocus:ContenderSlot;
		private var ourOlogun:Array;//Ologun means Warrior
		private var ourOku:Contender;//Oku means Ghost
		private var ourMarquee:*;
		private var ourSlot:Object = new Object();//Registry of ContenderSlots
		private var ourBanished:Object = new Object();
		private var ourDebugger:Debugger = new Debugger();
		private var ourSeed:XMLObject = new XMLObject();

		private var ourIsInitialized:Boolean = false;
		
		public function get isInitialized():Boolean { return ourIsInitialized; }
		/******************************** INSPECTABLES ********************************/
		protected var ourInspectablesSet:Array = new Array();
		protected var ourHPadding:Number;
		protected var ourVPadding:Number;
		protected var ourAutoInit:Boolean;

		[Inspectable(name = "1) Horizontal Padding (Pixels)", defaultValue = 10, type = Number)]
		public function set hPadding(val:Number):void{setInspectable("HPadding", val);}
		public function get hPadding():Number { return ourHPadding; }

		[Inspectable(name = "2) Vertical Padding (Pixels)", defaultValue = 10, type = Number)]
		public function set vPadding(val:Number):void { setInspectable("VPadding", val); }
		public function get vPadding():Number { return ourVPadding; }

		[Inspectable(name = "5) Auto Initialize (Boolean)", defaultValue = true, type = Boolean)]
		public function set autoInit(val:Boolean):void { setInspectable("AutoInit", val); }
		public function get autoInit():Boolean { return ourAutoInit; }
		
		protected function setInspectable(propName:String, val:* = null):void {
			ourInspectablesSet.push(propName);
			if (val != null) { this["our"+propName] = val; }
			checkInspectables();
		}

		protected function checkInspectables():void {
			if (ourInspectablesSet.length >= 3) {
				// do after all inspectables are set...
				if (autoInit && ourIsInitialized == false) { init(); }
			}
		}
		/******************************************************************************/
		
		public function TournamentBrackets():void {
			addInterests(new Array("NiOri", "RegisterSlot", "SlotFocus", "SlotBlur", "TrapOku", "FreeOku"));
		}
		
		public function init():void {
			/**** Expand/Close Button ****/
			var isExpanded:Boolean = ExternalInterface.call("isBracketsExpanded");
			expandButton.alpha = int(expandButton.mouseEnabled = !isExpanded);
			closeButton.alpha = int(closeButton.mouseEnabled = isExpanded);
			if (isExpanded) toggle(true);
			expandButton.buttonMode = expandButton.useHandCursor = closeButton.buttonMode = closeButton.useHandCursor = true;
			/*****************************/
			ourBreadth = cell.width - (hPadding*2);
			ourDepth = cell.height - (vPadding*2);
			ourIsInitialized = true;
			ourSeed.key		= "id";
			ourSeed.onParse	= buildBrackets;
			ourSeed.load("_xml/brackets.xml");
			new Liaison(this);
		}
		
		public function buildBrackets(myData:Object):void {
			ourOlogun = ourSeed.data.contender.slice(0);
			ourTree			= new CombatTree(ourSeed.data.structure.tier);
			ourTree.breadth	= ourBreadth/2;
			ourTree.depth	= ourDepth;
			ourTree.grow();
			cell.addChild(ourTree);
			setMode(Boolean(loaderInfo.parameters.mode) ? loaderInfo.parameters.mode : "predict");
		}
		
		public function setMode(myRef:String):void {
			switch (ourMode = myRef) {
				case "view":
					TweenLite.to(cell.predictMessage, 1, {alpha:0});
					TweenLite.to(cell.trailerMessage, 1, {alpha:1});
				break;
				case "predict":
					TweenLite.to(cell.predictMessage, 1, {alpha:1});
					TweenLite.to(cell.trailerMessage, 1, {alpha:0});
				break;
			}
		}
		
		override public function catchEvent(myEvent:String, myParticular:Object):void {
			switch (myEvent) {
				case "RegisterSlot": ourSlot[myParticular.target.oruko] = myParticular.target; break;
				case "SlotFocus":
					ourFocus = myParticular.target;
					if (Boolean(ourFocus.contender) && !Boolean(ourOku) && ourFocus.name != "champion") evokeOku(ourFocus.contender);
				break;
				case "SlotBlur":
					TweenLite.to(ourFocus.glow, .5, {alpha:0});
					ourFocus = null;
					if (Boolean(ourOku)) {
						if (ourOku.isInHand) return;
						fadeOku();
					}
				break;
				case "NiOri"://Establish Ori; Ni means possess and Ori means Head, Self, Soul
					if (!Boolean(myParticular.oju)) {//Get next available Ologun
						var myOlogun:Object = ourOlogun[binToDec(myParticular.target.ile.oruko)];
						ImageLoader.load(imgRef(myOlogun.image), myParticular.target.render);
						myParticular.target.name	= myOlogun.image;
						myParticular.target.oruko	= myOlogun.name;
						if (String(myOlogun.placement) != "") {//Preselection Contender placement...
							if (myParticular.target.ile.oruko.indexOf(myOlogun.placement) == 0 || myOlogun.placement == "champion") {//If the calling ContenderSlot and proposed ContenderSlot are from the same branch...
								ourSlot[myOlogun.placement].contender = myParticular.target as Contender;//Set Contender for proposed
								ourTree.bearFruits(myParticular.target, ourSlot[myOlogun.placement].contender);//Set ContendersSlot leading to proposed ContenderSlot
							}
						}
					} else {//Load image of specified oju
						ImageLoader.load(imgRef(myParticular.oju), myParticular.target.render);
					}
				break;
				case "TrapOku":
					if (ourMode == "view") {trace(ourSeed.data.media.src+ourOku.name+ourSeed.data.media.ext);
						if (Boolean(ourOku)) ExternalInterface.call("openVideo", ourSeed.data.media.src+ourOku.name+ourSeed.data.media.ext);
						return;
					}
					var mySize:Number = ourOku.size/ourSeed.data.structure.viewSize;
					ourOku.isInHand = true;
					TweenLite.to(ourOku, .5, {size:ourFocus.size});
				break;
				case "FreeOku":
					if (ourMode == "view") return;
					ourOku.isInHand = false;
					if (Boolean(ourFocus)) {
						var myFoci:String = (Boolean(ourFocus.ile) ? ourFocus.ile.name : "")+ourFocus.name;
						var isEligable:Boolean = ourFocus.name == "champion" ? true : ourOku.oba.name.indexOf(myFoci) == 0;
						if (ourFocus.contender != ourOku.haunt && !isEligable) {//ourOku has been freed but doesn't originate from ourFocus.contender
							fadeOku();
							EventManager.fireEvent("SlotFocus", {target:ourFocus});
						} else if (isEligable) {//ourOku is eligable to occupy ourFocus
							ourOku.size = ourFocus.size;
							ourFocus.contender		= ourOku;
							ourFocus.contender.name	= ourOku.haunt.name;
							EventManager.fireEvent("NiOri", {target:ourFocus.contender, oju:ourOku.haunt.name});
							ourTree.bearFruits(ourOku.haunt, ourFocus.contender);
							fadeOku(true);
							EventManager.fireEvent("SlotFocus", {target:ourFocus});
						} else {//ourOku centers on ourFocus
							var myLocus:Point = ourFocus.localToGlobal(new Point(x,y));
							TweenLite.to(ourOku, .25, {x:myLocus.x, y:myLocus.y});
						}
					} else { fadeOku(); }//Nothing is in focus
				break;
			}
		}
		
		private function evokeOku(myContender:Contender):void {
			var mySpan:uint = ourSeed.data.structure.viewSize;
			var myLocus:Point = myContender.localToGlobal(new Point(ourTree.x,ourTree.y));
			myLocus = cell.globalToLocal(myLocus);
			ourOku			= new Contender(myContender);
			ourOku.x		= myLocus.x;
			ourOku.y		= myLocus.y;
			ourOku.haunt	= ourFocus.contender;
			EventManager.fireEvent("NiOri", {target:ourOku, oju:myContender.name});
			TweenLite.to(ourOku, .5, {size:mySpan, ease:Sine.easeOut});
			cell.addChild(ourOku);
			ourMarquee			= new Copy(ourOku.oruko.toUpperCase(), Eurostile, {width:ourSeed.data.structure.viewSize*1.5, wordWrap:true});//new Marquee();
			ourMarquee.format	= new TextFormat(null, ourSeed.data.structure.fontSize, null, null, null, null, null, null, "center", null, null, null, -2);
			ourMarquee.x		= myLocus.x - ourMarquee.width/2;
			ourMarquee.y		= myLocus.y + mySpan/2 - int(ourOku.haunt.ile.name)*(mySpan + ourMarquee.height);
			ourMarquee.alpha	= 0;
			ourMarquee.applyGlow({color:0xFFFFFF, alpha:1, blurX:4, blurY:4, strength:16});
			TweenLite.to(ourMarquee, 1, {alpha:1, ease:Sine.easeInOut});
//			ourMarquee.addChild(myWrighter);
			cell.addChild(ourMarquee);
		}
		
		private function fadeOku(isEligable:Boolean = false):void {
			TweenLite.to(ourOku, .25, {alpha:0, size:ourSeed.data.structure.viewSize*2, ease:Sine.easeInOut});
			TweenLite.to(ourMarquee, .25, {alpha:0, ease:Sine.easeInOut, onComplete:banish});
			ourBanished[ourOku.name] = ourOku;
			ourBanished[ourMarquee.name] = ourMarquee;
			ourMarquee = null;
			ourOku = null;
		}
		
		private function banish():void {
			for (var member:String in ourBanished) {
				if (ourBanished[member].alpha == 0) {
					if (cell.contains(ourBanished[member])) {
						cell.removeChild(ourBanished[member]);
						delete ourBanished[member];
					}
				}
			}
		}
		
		public function toggle(myState:Boolean):void {
			TweenLite.to(cell, 1, {width:myState ? 1238 : 825, height:myState ? 698 : 465, onComplete:myState ? activate : close});
		}
		
		public function close():void {
			ExternalInterface.call("closeBrackets");
			activate();
		}
		
		public function activate(myState:Boolean = true):void {
			ourTree.mouseChildren = myState;
		}
		
		//Utilities
		private function binToDec(myBinary:*):uint {
			var myResult:uint = 0;
			myBinary = myBinary.split("").reverse();
			for (var n:uint = 0; n < myBinary.length; n++) myResult += int(myBinary[n])*Math.pow(2, n);
			return myResult;
		}
		
		private function imgRef(myRef:String):String { return ourSeed.data.image.src+myRef+ourSeed.data.image.ext; }
		
		//Events
		public function clickHandler(e:Event):void {
			switch (e.target) {
				case expandButton:
					activate(false);
					closeButton.mouseEnabled = true;
					ExternalInterface.call("expandBrackets");
					expandButton.alpha = int(expandButton.mouseEnabled = false);
					TweenLite.to(closeButton, .25, {alpha:1, delay:1});
					toggle(true);
				break;
				case closeButton:
					activate(false);
					expandButton.mouseEnabled = true;
					closeButton.alpha = int(closeButton.mouseEnabled = false);
					TweenLite.to(expandButton, .25, {alpha:1, delay:1});
					toggle(false);
				break;
			}
		}
		
		public function mouse_upHandler(e:Event):void {
			if (Boolean(ourOku) && !Boolean(ourFocus)) fadeOku();
		}
		
		public function mouse_moveHandler(e:Event):void {
			if (Boolean(ourOku)) {
				if (ourOku.isInHand) {//ourOku follows mouse when isInHand
					ourOku.x = cell.mouseX;
					ourOku.y = cell.mouseY;
				}
				if (Boolean(ourFocus)) {
					var myFoci:String = (Boolean(ourFocus.ile) ? ourFocus.ile.name : "")+ourFocus.name;
					var isEligable:Boolean = myFoci == "champion" ? true : ourOku.haunt.oba.name.indexOf(myFoci) == 0;
					if (ourOku.isInHand && isEligable && ourFocus.glow.alpha == 0) TweenLite.to(ourFocus.glow, .5, {alpha:1});//If ourFocus is empty and available, highlight ourFocus
				}
			}
		}
	}
}