Last active
December 16, 2015 18:09
-
-
Save error454/5475401 to your computer and use it in GitHub Desktop.
Kongregate API Integration with Shiva 3D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------------------------------------------------------------------- | |
// Basic Instructions for getting Kongregate into your export | |
// In your shiva project, anytime you want to send something to Kongregate stats API you will | |
// call: | |
// system.callClientFunction ( "onEngineEvent", sStatisticType, sStatisticName, nValue ) | |
// where sStatisticType is a Kongregate statistic type like "add", "max", "replace" | |
// sStatisticName is the name of the statistic you've defined on Kongregate | |
// nValue is the value to send | |
// Example: system.callClientFunction ( "onEngineEvent", "add", "deaths", 1 ) | |
//Integration Guide | |
// 1. Download this file | |
// 2. Rename the file to match your project and find/replace TheDemo with your class name. | |
// For instance, my file is named TheDemo.as, but it could be named Pacman.as and then | |
// I'd find/replace TheDemo with Pacman | |
// 3. Change onEngineEvent so that it properly reads your events and passes them to | |
// the kongregate stats API. | |
package | |
//----------------------------------------------------------------------------- | |
{ | |
//------------------------------------------------------------------------- | |
import flash.text.*; | |
import flash.utils.*; | |
import flash.events.*; | |
import flash.display.*; | |
import flash.display3D.*; | |
import flash.system.Capabilities; | |
import flash.net.URLRequest; | |
import flash.system.Security; | |
import com.stonetrip.shiva.engine.*; | |
//------------------------------------------------------------------------- | |
[SWF(width = "600", height = "338", backgroundColor="#000000")] | |
public class TheDemo extends Sprite | |
//------------------------------------------------------------------------- | |
{ | |
//--------------------------------------------------------------------- | |
private const kTotalStackSizeInBytes : int = 2 * 1024 * 1024 ; | |
private const kTotalHeapSizeInBytes : int = 256 * 1024 * 1024 ; | |
private const kVarTypeNil : int = 0 ; | |
private const kVarTypeNumber : int = 1 ; | |
private const kVarTypeString : int = 2 ; | |
private const kVarTypeBoolean : int = 3 ; | |
// Kongregate API reference | |
private var kongregate:*; | |
//--------------------------------------------------------------------- | |
private var splashScreen : DisplayObject = null ; | |
//--------------------------------------------------------------------- | |
public function TheDemo ( ) : void | |
{ | |
addEventListener ( Event.ADDED_TO_STAGE, onAddedToStage ) ; | |
} | |
//--------------------------------------------------------------------- | |
public function init ( splash:DisplayObject ) : void | |
{ | |
splashScreen = splash ; | |
} | |
private function initKongregate( ) : void | |
{ | |
var paramObj:Object = LoaderInfo(stage.loaderInfo).parameters; | |
var apiPath:String = paramObj.kongregate_api_path || "http://www.kongregate.com/flash/API_AS3_Local.swf"; | |
// Allow the API access to this SWF | |
Security.allowDomain(apiPath); | |
// Load the API | |
var request:URLRequest = new URLRequest(apiPath); | |
var loader:Loader = new Loader(); | |
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadKongregateComplete); | |
loader.load(request); | |
this.addChild(loader); | |
} | |
private function loadKongregateComplete(event:Event) : void | |
{ | |
// Save Kongregate API reference | |
kongregate = event.target.content; | |
// Connect to the back-end | |
kongregate.services.connect(); | |
} | |
//--------------------------------------------------------------------- | |
private function onAddedToStage ( _e:Event ) : void | |
{ | |
removeEventListener ( Event.ADDED_TO_STAGE, onAddedToStage ) ; | |
// Configure stage initial params | |
// | |
stage.scaleMode = StageScaleMode.NO_SCALE ; | |
stage.align = StageAlign.TOP_LEFT ; | |
stage.frameRate = 60 ; | |
if ( getFlashPlayerMajorVersion ( ) < 11 ) | |
{ | |
displayFlashPlayerVersionError ( ) ; | |
} | |
else | |
{ | |
// Request 3D context | |
// | |
stage.stage3Ds[0].addEventListener ( Event.CONTEXT3D_CREATE, onContext3DCreate ) ; | |
stage.stage3Ds[0].requestContext3D ( ) ; | |
} | |
} | |
//--------------------------------------------------------------------- | |
private function getFlashPlayerMajorVersion ( ) : int | |
{ | |
var flashPlayerMajorVersion:int = 0; | |
var versionString:String = Capabilities.version; | |
var pattern:RegExp = /^(\w*) (\d*),(\d*),(\d*),(\d*)$/; | |
var result:Object = pattern.exec(versionString); | |
if (result != null) | |
{ | |
flashPlayerMajorVersion = int(result[2]); | |
} | |
return flashPlayerMajorVersion; | |
} | |
//--------------------------------------------------------------------- | |
private function displayFlashPlayerVersionError ( ) : void | |
{ | |
// Create a text field (TODO: localize it) | |
// | |
var fmt:TextFormat = new TextFormat ( ) ; | |
fmt.font = "Helvetica" ; | |
fmt.size = 11 ; | |
var txt:TextField = new TextField ( ) ; | |
txt.text = "Sorry, Flash Player 11 is required to view this content." ; | |
txt.textColor = 0x808080 ; | |
txt.autoSize = TextFieldAutoSize.CENTER ; | |
txt.antiAliasType = AntiAliasType.ADVANCED ; | |
txt.x = 0 ; | |
txt.y = stage.stageHeight / 2 ; | |
txt.width = stage.stageWidth ; | |
txt.height = 11 ; | |
txt.setTextFormat ( fmt ) ; | |
stage.addChild ( txt ) ; | |
} | |
//--------------------------------------------------------------------- | |
private function onContext3DCreate ( _e:Event ) : void | |
{ | |
if ( _e != null && _e.target != null ) | |
{ | |
Bridge.init ( kTotalStackSizeInBytes, kTotalHeapSizeInBytes, stage, _e.target as Stage3D, onEngineInitialized, onEngineEvent, [ ], 2, 1 ) ; | |
} | |
else | |
{ | |
trace ( "Could not create Context3D!" ) ; | |
} | |
} | |
//--------------------------------------------------------------------- | |
private function onEngineInitialized ( ) : void | |
{ | |
addEventListener ( Event.ENTER_FRAME, onEnterFrame ) ; | |
initKongregate( ); | |
} | |
//--------------------------------------------------------------------- | |
private function onEngineEvent ( _args:Array, _returns:Array ) : void | |
{ | |
// This function is called when following script function is called: | |
// ... = system.callClientFunction ( "onEngineEvent", ... ) | |
// Values are arranged as follows: | |
// type1, value1, type2, value2, type3, value3, ... | |
// system.callClientFunction ( "onEngineEvent", "add", "deaths", 1 ) | |
// 0 String | |
// 1 add or max | |
// 2 String | |
// 3 add (deaths or collectables) max (score or time or collectables) | |
// 4 number | |
// 5 #deaths | |
// How to get call arguments: | |
// | |
// if ( _args[0] == kVarTypeString ) doSomethingWithAString ( _args[1] ) ; | |
if ( _args[0] == kVarTypeString ){ | |
if(_args[1] == "max"){ | |
if(_args[3] == "score"){ | |
kongregate.stats.submit("HighScore", _args[5]); | |
} else if(_args[3] == "time"){ | |
kongregate.stats.submit("LevelTime", _args[5]); | |
} else if(_args[3] == "collectables"){ | |
kongregate.stats.submit("Collectables", _args[5]); | |
} | |
} else if(_args[1] == "add"){ | |
if(_args[3] == "deaths"){ | |
kongregate.stats.submit("Deaths", _args[5]); | |
} else if(_args[3] == "collectables"){ | |
kongregate.stats.submit("TotalCollectables", _args[5]); | |
} | |
} | |
} | |
// How to return some values: | |
// | |
// _returns[0] = kVarTypeString ; _returns[1] = "Hello Flash!" ; | |
// _returns[2] = kVarTypeNumber ; _returns[3] = 123.456 ; | |
// _returns[4] = kVarTypeBoolean ; _returns[5] = true ; | |
} | |
//--------------------------------------------------------------------- | |
private function onEnterFrame ( _e:Event ) : void | |
{ | |
// Fade out splash screen if any | |
// | |
if ( splashScreen != null ) | |
{ | |
splashScreen.alpha = Math.max ( 0.0, splashScreen.alpha - 2.0 / stage.frameRate ) ; | |
if ( splashScreen.alpha == 0 ) | |
{ | |
stage.removeChild ( splashScreen ) ; | |
splashScreen = null ; | |
} | |
} | |
} | |
//------------------------------------------------------------------------- | |
} | |
//----------------------------------------------------------------------------- | |
} | |
//----------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment