package
{
import Andrograde.API;
import Andrograde.APIButton;
import Andrograde.User;
import Andrograde.UI;
import flash.display.Bitmap;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.GlowFilter;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.net.navigateToURL;
import flash.text.TextFormat;
import flash.ui.Mouse;
import flash.utils.getTimer;
import flash.geom.ColorTransform;
import flash.system.ApplicationDomain;
import flash.events.KeyboardEvent;
import Andrograde.APIClient;
public class Main extends Sprite
{
// the main interface to the Andrograde API
private var AndrogradeAPI:Andrograde.APIClient;
public var login_button:APIButton;
public var register_button:APIButton;
public var logout_button:APIButton;
public var buy_button:APIButton;
public var upgrade_button:APIButton;
public var shareware_text:TextField;
public var scoreText:TextField;
public var highScoreText:TextField;
public var timeText:TextField;
public var score:uint;
public static const NUM_RAINDROPS:uint = 50;
public static const TIME:Number = 30;
public var catcher:Sprite;
public var raindrops:Array;
public var time:Number;
public var lastTime:int;
[Embed(source = '../drop.png')] private var RAINDROP:Class;
[Embed(source = '../drop2.png')] private var RAINDROP_FV:Class;
[Embed(source = '../bigcatcher_upsell.png')] private var BIGCATCHER_UPSELL:Class;
[Embed(source = '../rain.mp3')] private var RAIN:Class;
private var rainSound:Sound = new RAIN();
private var rainSoundChannel:SoundChannel;
private var loadingPanel:TextField;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// initialize the Andrograde API with debugging enabled
AndrogradeAPI = new Andrograde.APIClient("Andrograde_API_TestGame", initGame, true);
addChild(AndrogradeAPI);
loadingPanel = new TextField();
loadingPanel.defaultTextFormat = new TextFormat("Arial", null, 0, true);
loadingPanel.antiAliasType = "advanced";
loadingPanel.autoSize = "left";
loadingPanel.text = "Loading Game";
loadingPanel.selectable = false;
loadingPanel.mouseEnabled = false;
loadingPanel.x = stage.stageWidth/2 - loadingPanel.width/2;
loadingPanel.y = stage.stageHeight / 2 - loadingPanel.height / 2;
addChild(loadingPanel);
}
// handle pausing and unpausing the game
private var _paused:Boolean= false;
private function set paused(value:Boolean):void {
trace("paused " + value);
if (value == true) {
mouse_show();
} else {
mouse_hide();
lastTime = getTimer();
}
_paused = value;
}
public function destroyGame():void {
rainSoundChannel.stop();
if (logout_button && contains(logout_button)) removeChild(logout_button);
if (register_button && contains(register_button)) removeChild(register_button);
if (login_button && contains(login_button)) removeChild(login_button);
if (buy_button && contains(buy_button)) removeChild(buy_button);
if (upgrade_button && contains(upgrade_button)) removeChild(upgrade_button);
if (shareware_text && contains(shareware_text)) removeChild(shareware_text);
removeRain();
removeChild(catcher);
removeChild(timeText);
removeChild(scoreText);
if (highScoreText && contains(highScoreText)) removeChild(highScoreText)
}
private function removeRain():void {
if (raindrops)
for (var i:uint = 0; i < raindrops.length; i++) {
if (contains(raindrops[i].image)) removeChild(raindrops[i].image);
}
}
private function createRain():void {
removeRain();
raindrops = new Array(NUM_RAINDROPS);
for (var i:int = 0; i < raindrops.length; i++) {
raindrops[i] = new Object();
if (AndrogradeAPI.user && AndrogradeAPI.user.owns("full version")) {
raindrops[i].image = new RAINDROP_FV();
} else {
raindrops[i].image = new RAINDROP();
}
raindrops[i].image.x = Math.random() * 640;
raindrops[i].image.y = -Math.random() * 480;
addChild(raindrops[i].image);
}
}
private function mouse_show():void {
Mouse.show();
catcher.visible = false;
}
private function mouse_hide():void {
Mouse.hide();
catcher.visible = true;
}
private function mouse_hover(display:DisplayObject):void {
display.addEventListener(MouseEvent.MOUSE_OVER, function (e:Event):void { mouse_show(); } );
display.addEventListener(MouseEvent.MOUSE_OUT,
function (e:Event):void {
if (!_paused) mouse_hide();
}
);
}
// this is called once the API has initialized
public function initGame(success:Boolean):void {
if (!success) {
trace("API failed to init!");
loadingPanel.text = "Andrograde API Failed to initialize";
AndrogradeAPI.debugDisplay.y = 50;
addChild(AndrogradeAPI.debugDisplay);
return; // API failed to init
}
if (contains(loadingPanel)) removeChild(loadingPanel);
// test resolving a user name to an identity
AndrogradeAPI.getIdentity("Will", gotIdentity);
function gotIdentity(identities:Array):void {
if (identities == null) return;
for each (var s:String in identities)
trace('found identity ' + s + ' for Will');
trace("Using identity: " + identities[0]);
// test getting another user's info using the identity we got from getIdentity
AndrogradeAPI.getUserInfo(identities[0], getinfo);
function getinfo(user:User):void {
if (user!=null) {
trace("Will's highscore: " + user.getStat('highscore'));
trace("Will's owns big catcher? " + user.owns('big catcher'));
// test setting another user's stats
user.setStat("highscore", 200, User.STAT_REPLACE, function (success:Boolean):void {
trace("Will's updated HS " + user.getStat('highscore'));
});
user.getRank("highscore", APIClient.HIGHSCORE_HIGHEST, null);
}
}
}
// output server parameters
trace("bigCatcherPrice param= " + AndrogradeAPI.parameters_server.bigCatcherPrice);
trace("fullVersionPrice param= " + AndrogradeAPI.parameters_server.fullVersionPrice);
opaqueBackground = 0x505050;
// create the rain
createRain();
addEventListener(Event.ENTER_FRAME, update);
// initialize the score stuff
score = 0;
scoreText = new TextField();
scoreText.textColor = 0x80ff80;
scoreText.x = 0;
scoreText.y = 0;
scoreText.text = "Score: " + score;
scoreText.autoSize = "left";
addChild(scoreText);
// only display their highscore if they are logged in
if (AndrogradeAPI.user) {
highScoreText = new TextField();
highScoreText.x = 0;
highScoreText.y = 20;
highScoreText.text = "Your High Score: " + int(AndrogradeAPI.user.getStat("highscore"));
highScoreText.autoSize = "left";
highScoreText.textColor = 0xffff80;
addChild(highScoreText);
}
// initialize time stuff
time = 0;
lastTime = getTimer();
timeText = new TextField();
timeText.x = 200;
timeText.y = 0;
timeText.textColor = 0xffffff;
timeText.text = int(TIME - time) + " seconds left!";
addChild(timeText);
function toggleDebug(e:Event):void {
if (contains(AndrogradeAPI.debugDisplay))
removeChild(AndrogradeAPI.debugDisplay);
else {
AndrogradeAPI.debugDisplay.y = 50;
addChild(AndrogradeAPI.debugDisplay);
}
}
var debug_button:APIButton = new APIButton(520, 458, AndrogradeAPI.UI.text("TOGGLE DEBUG", AndrogradeAPI.UI.COLOR_BUTTON_TEXT), toggleDebug);
mouse_hover(debug_button);
addChild(debug_button);
if (!AndrogradeAPI.user) { // no user was already logged in, lets give them the option to login or register manually
showNormalMenu();
} else { // there was already someone logged in when the game started, don't display the login/register buttons
showLoggedInMenu();
}
// hide the normal mouse cursor
Mouse.hide();
// draw a new mouse cursor for the catcher
catcher = new Sprite();
catcher.graphics.lineStyle(1, 0xff0000);
catcher.graphics.beginFill(0xffff00);
if (AndrogradeAPI.user && AndrogradeAPI.user.owns("big catcher"))
catcher.graphics.drawRoundRect(0, 0, 70, 20, 10, 10);
else catcher.graphics.drawRoundRect(0, 0, 45, 10, 10, 10);
catcher.graphics.endFill();
catcher.filters = new Array(new GlowFilter(0xff8000, 1, 12, 12, 3, 2));
addChild(catcher);
// update the catcher
addEventListener(Event.ENTER_FRAME, function (e:Event):void {
catcher.x = mouseX;
catcher.y = mouseY;
});
rainSoundChannel= rainSound.play(0, 100000);
}
// this is called when they have successfully logged in or registered or cancelled out of the dialog to do so
public function UserLoggedIn(success:Boolean):void {
paused = false;
if (success) { // login/register succeeded
destroyGame(); // just reload the game
initGame(true);
} else {
login_button.enabled= true;
}
}
// this is called when they have successfully logged out
public function UserLoggedOut(success:Boolean):void {
paused = false;
if (success) { // logout succeeded
destroyGame(); // just reload the game
initGame(true);
} else {
logout_button.enabled= true;
}
}
private function show_shareware():void {
if (shareware_text && contains(shareware_text)) removeChild(shareware_text);
shareware_text = new TextField();
shareware_text.defaultTextFormat = new TextFormat("Arial", null, 0xffffff, true);
shareware_text.antiAliasType = "advanced";
//textField.embedFonts = true;
shareware_text.autoSize = "left";
shareware_text.height = 20;
shareware_text.text = "Shareware Version";
shareware_text.selectable = false;
shareware_text.mouseEnabled = false;
shareware_text.x = 400;
addChild(shareware_text);
}
private function showSharedMenu():void {
var mainApp:Sprite = this;
// test for buying stuff
if (!AndrogradeAPI.user || !AndrogradeAPI.user.owns("big catcher")) {
// called when they click the buy item button
function buyClick(e:Event):void {
paused = true;
buy_button.enabled= false;
function buy_complete(success:Boolean):void {
buy_button.enabled= true;
if (success) { // they successfully bought the big catcher
destroyGame();
initGame(true);
}
paused = false;
}
// pop up a dialog at 200,200 to buy the big catcher that costs 50 points
AndrogradeAPI.buy(AndrogradeAPI.parameters_server.bigCatcherPrice, "big catcher", new BIGCATCHER_UPSELL(), buy_complete, mainApp);
}
buy_button = new APIButton(180, 458, AndrogradeAPI.UI.text("BUY BIGGER CATCHER", AndrogradeAPI.UI.COLOR_BUTTON_TEXT), buyClick);
mouse_hover(buy_button);
addChild(buy_button);
}
// test for upgrading the game to the full version
if (!AndrogradeAPI.user || !AndrogradeAPI.user.owns("full version")) {
show_shareware();
function fullversionClick(e:Event):void {
upgrade_button.enabled = false;
// charge them $10 and display that text as a note when they purchase it
paused = true;
function buy_complete(success:Boolean):void {
if (success) { // they bought it
removeChild(upgrade_button);
removeChild(shareware_text);
createRain();
} else {
upgrade_button.enabled= true;
}
paused = false;
}
AndrogradeAPI.buy(AndrogradeAPI.parameters_server.fullVersionPrice, "full version", new RAINDROP_FV(), buy_complete, mainApp);
}
upgrade_button = new APIButton(350, 458, AndrogradeAPI.UI.text("GET FULL VERSION", AndrogradeAPI.UI.COLOR_BUTTON_TEXT), fullversionClick);
mouse_hover(upgrade_button);
addChild(upgrade_button);
}
}
private function showNormalMenu():void {
var mainApp:Sprite = this;
function loginClick(e:Event):void {
paused = true;
login_button.enabled= false;
AndrogradeAPI.login(UserLoggedIn, mainApp); // display the login prompt
}
login_button = new APIButton(0, 460, AndrogradeAPI.UI.text("LOGIN", AndrogradeAPI.UI.COLOR_BUTTON_TEXT), loginClick);
mouse_hover(login_button);
addChild(login_button);
show_shareware();
showSharedMenu();
}
// called upon successful login/registration
private function showLoggedInMenu():void {
trace("# points: " + AndrogradeAPI.user.points);
trace("is premium: " + AndrogradeAPI.user.premium);
trace("has upgraded: " + AndrogradeAPI.user.owns("full version"));
function logoutClick(e:Event):void {
logout_button.enabled = false;
AndrogradeAPI.logout(UserLoggedOut);
}
logout_button = new APIButton(0, 460, AndrogradeAPI.UI.text("LOGOUT", AndrogradeAPI.UI.COLOR_BUTTON_TEXT), logoutClick);
mouse_hover(logout_button);
addChild(logout_button);
showSharedMenu();
}
private function update(e:Event):void {
if (_paused) return;
// update the timer
var currentTime:int = getTimer();
time += (currentTime - lastTime) / 1000;
if (time > TIME) time = TIME;
lastTime = currentTime;
timeText.text = int(TIME - time) + " seconds left!";
// end the game when there's no time left
if (time >= TIME) {
removeEventListener(Event.ENTER_FRAME, update);
initGameOver();
}
// update the rain positions
for (var i:int = 0; i < raindrops.length; i++) {
raindrops[i].image.y += 15;
if (raindrops[i].image.y > 640) {
raindrops[i].image.y = 0;
raindrops[i].image.x = Math.random() * 640;
}
if (catcher.x+catcher.width > raindrops[i].image.x && catcher.x < raindrops[i].image.x + raindrops[i].image.width &&
catcher.y+catcher.height > raindrops[i].image.y && catcher.y < raindrops[i].image.y + raindrops[i].image.height) {
raindrops[i].image.y = 0;
raindrops[i].image.x = Math.random() * 640;
score++;
scoreText.text = "Score: " + score;
}
}
}
private var rank:uint;
private function initGameOver():void {
rainSoundChannel.stop();
// attempt to set the highscore and get the rank of the player if they are logged in
if (AndrogradeAPI.user) {
function statCallback(success:Boolean):void {
getRank();
}
AndrogradeAPI.user.setStat("highscore", score, Andrograde.User.STAT_MAX, statCallback);
} else {
// proceed normally to the game over stuff
GameOver();
}
}
private function getRank():void {
// get the rank of this user for this stat once we've set the highscore
function rankCallback(_rank:uint):void {
rank = _rank;
GameOver();
}
AndrogradeAPI.user.getRank("highscore", APIClient.HIGHSCORE_HIGHEST, rankCallback);
}
private function GameOver():void {
// get the list of highscores from the server
const NUM_SCORES_TO_FETCH:uint = 10;
var startingOffset:uint = 0;
var highscoresNames:TextField = new TextField();
var highscoresScores:TextField = new TextField();
var highscoresPanel:Sprite = new Sprite();
function prevClick(e:Event):void {
if (startingOffset >= NUM_SCORES_TO_FETCH) {
startingOffset -= NUM_SCORES_TO_FETCH;
prev.alpha = 0.5;
prev.mouseEnabled = false; // don't allow them to click the button twice before it fetching the stuff
AndrogradeAPI.getHighScores("highscore", APIClient.HIGHSCORE_HIGHEST, startingOffset, NUM_SCORES_TO_FETCH, highscoreCallback);
}
}
// make the next button if clicked will show more scores
var prev:APIButton = new APIButton(0, 160, AndrogradeAPI.UI.text("Prev",AndrogradeAPI.UI.COLOR_BUTTON_TEXT), prevClick);
highscoresPanel.addChild(prev);
function nexClick(e:Event):void {
startingOffset += NUM_SCORES_TO_FETCH;
next.alpha = 0.5;
next.mouseEnabled = false; // don't allow them to click the button twice before it fetching the stuff
AndrogradeAPI.getHighScores("highscore", APIClient.HIGHSCORE_HIGHEST, startingOffset, NUM_SCORES_TO_FETCH, highscoreCallback);
}
// make the next button if clicked will show more scores
var next:APIButton = new APIButton(150, 160, AndrogradeAPI.UI.text("Next",AndrogradeAPI.UI.COLOR_BUTTON_TEXT), nexClick);
highscoresPanel.addChild(next);
function highscoreCallback(scores:Array):void {
// reenable the previous and next buttons
next.alpha = 1;
next.mouseEnabled = true;
prev.alpha = 1;
prev.mouseEnabled = true;
if (highscoresPanel.contains(highscoresNames)) highscoresPanel.removeChild(highscoresNames);
if (highscoresPanel.contains(highscoresScores)) highscoresPanel.removeChild(highscoresScores);
highscoresNames.multiline = true;
highscoresScores.multiline = true;
highscoresNames.htmlText = "";
for (var i:int = 0; i < scores.length; i++) {
if (scores[i][1] != null) {// if they actually got a highscore for this
if (i + startingOffset == rank-1) // render yourself as bold
highscoresNames.htmlText += "" + scores[i][1] + "
";
else // render everyone else normal
highscoresNames.htmlText += scores[i][1] + "
";
}
}
highscoresScores.htmlText = "";
for (i = 0; i < scores.length; i++) {
if (scores[i][1] != null) {// if they actually got a highscore for this
if (i + startingOffset == rank-1) // render yourself as bold
highscoresScores.htmlText += "" + scores[i][2] + "
";
else
highscoresScores.htmlText += scores[i][2] + "
";
}
}
highscoresNames.width = 100;
highscoresNames.height = 158;
highscoresNames.x = 10;
highscoresNames.y = 0;
highscoresPanel.addChild(highscoresNames);
highscoresScores.width = 85;
highscoresScores.height = 158;
highscoresScores.x = 110;
highscoresScores.y = 0;
highscoresPanel.addChild(highscoresScores);
highscoresPanel.graphics.clear();
AndrogradeAPI.UI.panel(highscoresPanel, null, null, 1);
}
AndrogradeAPI.getHighScores("highscore", APIClient.HIGHSCORE_HIGHEST, startingOffset, NUM_SCORES_TO_FETCH, highscoreCallback);
function playagainClick(e:Event):void {
removeChild(highscoresPanel);
destroyGame();
initGame(true);
}
var playagain:APIButton = new APIButton(55, 160, AndrogradeAPI.UI.text("PLAY AGAIN",AndrogradeAPI.UI.COLOR_BUTTON_TEXT), playagainClick);
highscoresPanel.addChild(playagain);
highscoresPanel.x = 190;
highscoresPanel.y = 200;
addChild(highscoresPanel);
}
}
}