flash on 2009-10-10
...
@author 9re
/**
* Copyright 9re ( http://wonderfl.net/user/9re )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/6I6L
*/
package
{
import Box2D.Collision.b2AABB;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2ContactListener;
import flash.display.Bitmap;
import flash.events.KeyboardEvent;
import flash.external.ExternalInterface;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.system.Security;
import flash.system.SecurityDomain;
import flash.ui.Keyboard;
import flash.utils.setTimeout;
import net.wonderfl.game.infinity_tank.bullet.BulletRendererBase;
import net.wonderfl.game.infinity_tank.core.GameWorld;
import net.wonderfl.game.infinity_tank.core.UserActionHandler;
import net.wonderfl.game.infinity_tank.tank.TankBase;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.text.TextField;
import flash.utils.Timer;
import net.hires.debug.Stats;
import net.wonderfl.game.infinity_tank.core.GameController;
import net.wonderfl.game.infinity_tank.info_panel.InfoPanel;
import net.wonderfl.game.infinity_tank.model.BattleScene;
import net.wonderfl.game.infinity_tank.model.Command;
import net.wonderfl.utils.ClassLoader;
/**
* ...
* @author 9re
*/
public class InfinityTankGame extends Sprite
{
private const FIRE_INTERVAL:int = 1200;
private var _ai1_is_user:Boolean;
private var _world:GameWorld;
private var _debugDraw:Sprite;
private var _currentScenes:Vector.<BattleScene>;
private var _timerIDs:Vector.<int>;
private var _tanks:Array = [];
private var _renderers:Array = [];
private var _battleScene:Array = [];
private var _i:int = 0;
private var _timer:Timer;
private var _tankDebugShape:Vector.<Shape>;
private var _nextUserAction:int;
private var _viewMode:int = 1;
private var _gunFlag:int = 0;
private var _gameView:Sprite;
private var _controller:GameController;
private var _stats:Stats;
private var _countDownImages:Vector.<Bitmap>;
private var _countDownImage:Bitmap;
[SWF(backgroundColor="#000000", frameRate="30")]
public function InfinityTankGame()
{
addEventListener(Event.ADDED_TO_STAGE, function (e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, arguments.callee);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
});
var tanks:Array = ["http://swf.wonderfl.net/swf/usercode/b/b2/b2b7/b2b79b0f6004838c3d966fa10fc2481db101b7d8.swf?" + getTimeStamp(), "http://swf.wonderfl.net/swf/usercode/b/b2/b2b7/b2b79b0f6004838c3d966fa10fc2481db101b7d8.swf?" + getTimeStamp()];
_ai1_is_user = (loaderInfo.parameters.ai1_is_user == 1);
var sp:Sprite = new Sprite;
addChild(sp);
//sp.addChild(new BitmapAssets.stageBackgroundImage);
sp.addChild(_gameView = new Sprite());
var spMask:Shape = new Shape();
spMask.graphics.beginFill(0);
spMask.graphics.drawRect(0, 0, 600, 550);
spMask.graphics.endFill();
_gameView.addChild(spMask);
_gameView.mask = spMask;
//_gameView.y = 50;
var info:InfoPanel = new InfoPanel;
//info.createLifePanel(BitmapAssets.lifeImage, BitmapAssets.lifeDamageImage);
info.createLifePanel( DummyAssets, DummyAssets);
sp.addChild(info);
_controller = new GameController;
_controller.panel = info;
_controller.onGameEndCallback = onGameEnd;
_controller.apiResponseCallback = redirect;
_currentScenes = new Vector.<BattleScene>;
_timerIDs = new Vector.<int>;
//load tanks
loadRuntimeClasses(tanks, "Tank", _tanks, tanksLoaded);
}
private function loadRuntimeClasses($swfPath:Array, $className:String, $classes:Array, $onComplete:Function = null, $onCompleteParams:Array = null):void {
var ldr:ClassLoader = new ClassLoader();
ldr.addEventListener(ClassLoader.CLASS_LOADED, function (e:Event):void {
ldr.removeEventListener(ClassLoader.CLASS_LOADED, arguments.callee);
var c:* = ldr.getClass($className);
$classes.push(new c);
if ($swfPath.length > 0)
loadRuntimeClasses($swfPath, $className, $classes, $onComplete, $onCompleteParams);
else if ($onComplete != null)
$onComplete.apply(null, $onCompleteParams);
});
try {
var url:String = $swfPath.shift() as String;
var context:LoaderContext = new LoaderContext(true);
context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
context.securityDomain = SecurityDomain.currentDomain;
ldr.load(url, context);
} catch (e:Error) {
// do nothing
//
throw e;
}
}
private function tanksLoaded():void
{
var bulletRenderers:Array = [];
var tank:TankBase;
for (var i:int = 0; i < 2; ++i) {
tank = _tanks[i] as TankBase;
bulletRenderers.push(tank.bulletRenderer + getTimeStamp());
}
loadRuntimeClasses(bulletRenderers, "BulletRenderer", _renderers, bulletsLoaded);
}
private function getTimeStamp():String {
return "?t=" + (new Date).time;
}
private function bulletsLoaded():void
{
// set boundary
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set( -10, -10);
worldAABB.upperBound.Set( 70, 65);
_world = new GameWorld(worldAABB);
_world.sprite = _gameView;
_world.controller = _controller;
_world.debugDraw = this;
var scene:BattleScene;
var tank:TankBase;
var bulletRenderer:BulletRendererBase;
for (var i:int = 0; i < 2; ++i) {
scene = new BattleScene(i);
tank = _tanks[i];
bulletRenderer = _renderers[i];
tank.__setScene(scene);
bulletRenderer.__setScene(scene);
_world.addBattleScene(scene);
_world.addTankRenderer(tank.draw);
_world.addTankContactListener(tank.hit);
_world.addBulletRenderer(bulletRenderer.draw);
_currentScenes.push(scene);
}
_world.setUpScenes();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, UserActionHandler.keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, UserActionHandler.keyUpHandler);
addChild(_stats = new Stats);
_stats.y = 50;
_stats.x = 1;
_stats.alpha = 0.7;
_stats.visible = false;
startBattle();
}
private function startBattle():void {
_timer = new Timer(100);
_timer.addEventListener(TimerEvent.TIMER, onTimer);
_timer.start();
_controller.panel.startTimer();
drawIcon();
}
private function onEnterFrame(e:Event):void
{
_world.Step(1 / stage.frameRate, 6);
_world.update();
}
private function drawIcon():void
{
for (var i:int = 0; i < 2; ++i)
_controller.panel.drawTankIcon(_tanks[i].draw, i);
}
private function changeViewMode():void
{
switch (_viewMode++) {
case 0:
_world.showHitArea = false;
_world.showUserBitmapData = true;
break;
case 1:
_world.showHitArea = true;
break;
case 2:
_world.showUserBitmapData = false;
break;
}
_viewMode %= 3;
}
private function onTimer(e:TimerEvent):void
{
var id:int = _timer.currentCount % 2;
var tank:TankBase = _tanks[id];
_controller.panel.updateTime();
try {
var action:int;
if (_ai1_is_user && id == 0) {
action = UserActionHandler.getCommand().action;
} else {
action = tank.action();
}
doAction(action, id);
} catch (e:Error) {
// error handling
return;
}
}
private function doAction(action:int, id:int):void
{
var sceneOne:BattleScene = _currentScenes[id];
var sceneTheOther:BattleScene = _currentScenes[1 - id];
if (action & Command.TANK_MASK) {
_world.moveTank(action & Command.TANK_MASK, id);
}
if (action & Command.GUN_MASK) {
_world.turnGun(action & Command.GUN_MASK, id);
}
if (action & Command.FIRE) {
if ((1 & (_gunFlag >> id)) == 0) {
_gunFlag |= (1 << id);
setTimeout(clearFlag, FIRE_INTERVAL, id);
_world.addBullet(id);
}
}
if ((action & Command.SYSTEM_MASK) && (_ai1_is_user && (id == 0))) {
if (action & Command.CHANGE_VIEW_MODE)
changeViewMode();
else if (action & Command.SHOW_STATS)
_stats.visible = !_stats.visible;
}
}
private function clearFlag(id:int):void
{
_gunFlag &= (1 << (1 - id));
}
public function onGameEnd():void {
_timer.stop();
_controller.panel.clearTime();
}
public function redirect():void {
}
}
}
import flash.display.Bitmap;
class DummyAssets extends Bitmap {
public function DummyAssets() {
}
}