/**
* Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/qIkw
*/
//
//パズルゲーム作る。
//ロード画面 http://wonderfl.net/c/zZl2
package {
import flash.display.*;
import caurina.transitions.Tweener;
import net.hires.debug.Stats;
[SWF(width="465", height="465", backgroundColor="0x111122", frameRate="60")]
public class Game extends Sprite {
private var o:Array = [],
X:Array = [ ],
a:Array = [ ["brick"] ],
b:Array = [ ["wall"] ],
c:Array = [ ["enemy"] ],
d:Array = [ ["item"] ],
m:Array = [ ["man"] ];
private var map:Array = [
[ //xの位置はここで変更しても、反映されません。
[ o, o , o , o , o , o , o , o , o , o , o ],
[ o, X , o , X , o , X , o , X , o , X , o ],
[ o, o , c , o , o , o , o , o , o , o , o ],
[ o, X , o , X , o , X , o , X , o , X , o ],
[ o, o , o , o , o , o , o , o , o , o , o ],
[ o, X , o , X , o , X , o , X , o , X , o ],
[ o, o , o , o , o , o , o , o , o , o , o ],
[ o, X , o , X , o , X , o , X , o , X , o ],
[ o, o , m , o , o , o , o , o , o , o , o ],
[ o, X , o , X , o , X , o , X , o , X , o ],
[ o, o , o , o , o , o , o , o , o , o , o ]
]
];
static private var stageNum:int = 0;
private var loaders:Vector.<Loader>;
public function Game() {
game = this;
loaders = Data.load();
var nowload:NowLoading = new NowLoading(stage,init);
for each(var loader:Loader in loaders){
nowload.addLoader(loader);
}
}
private function init():void{
Tweener.addTween(stage.getChildAt(1),{alpha:0,time:3,onComplete:function():void{stage.removeChildAt(1)} });
stage.quality = "low";
var room:Room = new Room( map[0] );
room.scaleX = room.scaleY = 1;
addChild( room );
alpha=0; Tweener.addTween(this,{delay:1,alpha:1,time:3} );
addChild( new Stats() ).y = 365;
}
}
}
var game:Game;
var data:Data = new Data();
import flash.utils.ByteArray;
import flash.system.LoaderContext;
import flash.geom.*;
import flash.display.*;
import flash.text.TextField;
import flash.events.*;
import flash.net.*;
import caurina.transitions.*;
//オブジェクト作成はデータのロード後に行う。
class Room extends Sprite {
public var map:Vector.<Vector.<Vector.<GameObject>>> = new Vector.<Vector.<Vector.<GameObject>>>;
public var focus:GameObject;
private var keyEvent:KeyboardEvent;
private var stageNum:int = 0; //現在のステージ番号
public var stop:int; //設定されている時間だけ一時停止
private var bitmap:Bitmap;
private var backmapData:BitmapData;
public function Room( arr:Array ) {
bitmap=new Bitmap( new BitmapData(1,1) );
addChild(bitmap);
readData( arr );
addEventListener("addedToStage",init);
}
private function init(e:Event):void{
removeEventListener("addedToStage",init);
addEventListener("enterFrame", onFrame );
stage.addEventListener("keyDown", onKeyDown );
stage.addEventListener("keyUp", onKeyUp );
}
private function readData(arr:Array):void {
map= new Vector.<Vector.<Vector.<GameObject>>>;
focus = null;
var mapWidth:int = arr[0].length + 2;
var mapHeight:int = arr.length + 2;
bitmap.bitmapData = new BitmapData(mapWidth * Data.cellWidth, mapHeight * Data.cellHeight + Data.margin, true, 0x0);
backmapData = new BitmapData(mapWidth * Data.cellWidth, mapHeight * Data.cellHeight + Data.margin, true, 0x0);
for(var j:int=0;j<mapHeight;j++){
map[j] = new Vector.<Vector.<GameObject>>;
for(var i:int=0;i<mapWidth;i++){
map[j][i] = new Vector.<GameObject>;
if( !( i < mapHeight-1 && i > 0 && j < mapWidth-1 && j>0) || ( i % 2 == 0 && j % 2 == 0 ) ){
Data.setObject( this, j, i, "wall");
}else{
var obj:Array = arr[ i-1 ][j-1];
for(var k:int=0; k < obj.length; k++){
var name:String = obj[k][0];
var o:GameObject = Data.setObject( this, j, i, name);
if( o.tags.indexOf("focus") > -1 ){ focus = o; }
}
}
data.draw( backmapData, "block", "back", j*Data.cellWidth, i*Data.cellHeight );
}
}
}
//マップ全体のタグ検索
public function search( tag:String = "" ):Vector.<GameObject>{
var vec:Vector.<GameObject> = new Vector.<GameObject>();
for( var x:int = 0; x < map.length; x++ ){
for( var y:int = 0; y < map[x].length; y++ ){
for each( var o:GameObject in map[x][y] ){
if(o.mapX == x && o.mapY == y && o.tags.indexOf(tag) > -1 ){ vec.push(o) }
}
}
}
return vec;
}
//特定のマスのタグ検索
public function searchCell(x:int,y:int,tag:String = ""):Vector.<GameObject>{
var vec:Vector.<GameObject> = new Vector.<GameObject>();
for each( var o:GameObject in map[x][y] ){
if(o.mapX == x && o.mapY == y && o.tags.indexOf(tag) > -1 ){ vec.push(o) }
}
return vec;
}
private function onFrame(e:Event = null):void {
if (stop > 0) {
stop--;
}else{
bitmap.bitmapData.lock();
bitmap.bitmapData.fillRect(bitmap.bitmapData.rect,0xFF000000);
bitmap.bitmapData.draw(backmapData);
for( var x:int = 0; x < map.length; x++ ){
for( var y:int = 0; y < map[x].length; y++ ){
for each( var o:GameObject in map[x][y] ){
if(o.tween.length > 0){ o.tweenMove() }
if(o.tween.length == 0 && o.frame != null ){ o.frame( o ) }
if( o.tween.length == 0 && keyEvent != null && o.key != null ){
o.key( o, keyEvent );
}
if( o.anim != null ){ o.animation() }
}
}
}
for( x = 0; x < map.length; x++ ){
for( y = 0; y < map[x].length; y++ ){
for each( o in map[y][x] ){
data.draw( bitmap.bitmapData, o.type, o.dir + o.state, o.x, o.y );
}
}
}
if(focus != null){
bitmap.x = -focus.x * scaleX+( -Data.cellWidth + stage.stageWidth)/2;
bitmap.y = -focus.y * scaleY+( -Data.cellHeight + stage.stageHeight)/2;
}
bitmap.bitmapData.unlock();
}
}
private function onKeyDown(e:KeyboardEvent):void{ keyEvent = e; }
private function onKeyUp(e:KeyboardEvent):void{ keyEvent = null; }
}
//マップに配置するオブジェクト
class GameObject extends Object {
public var name:String; //オブジェクトの名前
public var depth:int = 0; //深度
public var room:Room; //部屋
//オブジェクトの性質のリスト。
public var tags:Array = [];
public var params:Object = {}; //マップ作成時に設定したパラメータ
//関数
public var frame:Function; //毎フレーム呼び出される関数
public var key:Function; //キー操作をした時呼び出される関数
public var func:Object; //その他の関数
public var x:Number,y:Number;
public var mapX:int,mapY:int;
public var type:String; //使用する画像のタイプ
public var state:String; //画像の状態
public var dir:String = ""; //画像の状態
public var anim:Object; //アニメーションに関するデータ
public var count:int = 0;
public var animState:String;//現在選択されているアニメーション
//自作tween
public var tweenFrame:Vector.<int> = new Vector.<int>();
public var tween:Vector.<Object> = new Vector.<Object>();
//アニメーション時に呼び出される
public function animation():void{
if(anim[animState]!=null){
var data:Array = anim[animState];
//配列から現在のカウントと一致するものを取り出す。
data = data.filter(function(d:*, i:int, a:Array):Boolean{return d[0]==this.count},this);
for each(var act:Array in data){
switch(act[1]){
case "goto":
count = act[2];break;
case "action":
animState = act[2];
count = -1; break;
default:
state = act[1];
}
}
count++;
}
}
public function action(state:String,count:int=0):void{
animState = state; this.count = count;
}
public function addTween( o:Object, frame:int = 1, delay:int = 0):void{
tween.push(o); tweenFrame.push(frame);
};
public function tweenMove():void{
for(var str:String in tween[0]){
this[str] = ( this[str]* (tweenFrame[0]-1) + tween[0][str] ) / tweenFrame[0];
}
tweenFrame[0]--;
if( tweenFrame[0] == 0){ tween.shift(); tweenFrame.shift(); }
}
public function move(x:int, y:int ):void{
Data.removeObject(room,mapX,mapY,this);
mapX += x; mapY += y;
Data.putObject(room,mapX,mapY,this);
}
}
//主人公に関するデータ用クラス
class Man{
static public const anim:Object = {
"walk0":[[0,"w0"],[3,"w1"]],
"walk1":[[0,"w2"],[3,"w1"]],
"stand":[[0,"w1"]]
}
static public function key( o:GameObject, e:KeyboardEvent ):void{
switch( Data.keyString[e.keyCode] ){
case "←": walk(o,"l"); break;
case "→": walk(o,"r"); break;
case "↑": walk(o,"b"); break;
case "↓": walk(o,"f"); break;
}
}
static public function walk( o:GameObject, dir:String ):void{
var d:int = "lrbf".indexOf(dir);
if(d > -1){
o.dir = dir;
var arr:Array = [[-1,0],[1,0],[0,-1],[0,1]][d]
if( o.room.searchCell( o.mapX+arr[0], o.mapY+arr[1], "block" ).length == 0 ){
o.move(arr[0],arr[1]);
}
if( o.animState == "walk1" ){ o.action("walk0"); }
else { o.action("walk1") }
o.addTween( { x:o.mapX * Data.cellWidth, y:o.mapY * Data.cellHeight }, 6 );
}
}
}
//障害物用データクラス
class Block{
}
class Enemy{
static public const anim:Object = {
"walk": [[0,"w0"],[20,"w1"],[39,"goto",-1]]
}
}
//ゲームデータ用クラス
class Data{
static public const keyString:Object = {37:"←",38:"↑",39:"→",40:"↓",88:"x",90:"z"}
static public const DIR_URL:String = "http://assets.wonderfl.net/images/related_images/";
static public const IMG_URL:Object = [
[ "block0", "9/98/982b/982b91596580972429015cf9c07c12837a296393" ],
[ "enemy0", "d/df/df17/df17c0adbba27a09bfe46f8bfc20a95a2ba1a2db" ],
[ "item0", "f/f1/f12e/f12ea6459c10a46ca8573e9d62bb23124dd52739" ],
[ "man0", "6/69/69ce/69ce741b5f0574d3730e085cf32ac7d794e46b03" ]
];
//各オブジェクトに使うビットマップデータの設定
public var MAP_SET:Object = {
"block":"block0",
"item":"item0",
"man":"man0",
"enemy":"enemy0"
}
static public const IMG_NAME:Object = {
"block":["wall", "back", "brick"],
"item":[ "b0", "b1", "f0", "f1", "i"],
"enemy":[ "rw0", "rw1", "lw0", "lw1", "fw0", "fw1", "bw0", "bw1" ],
"man":[ "lw0", "lw1", "lw2", "rw0", "rw1", "rw2", "fw0", "fw1", "fw2", "bw0", "bw1", "bw2" ]
}
static public const OBJECT:Object = {
"man":{ type:"man", state:"w1", dir:"f", tags:["focus"], key:Man.key, anim:Man.anim, animState:"stand" },
"enemy":{ type:"enemy", state:"w1", dir:"f", tags:["chase"], anim:Enemy.anim, animState:"walk" },
"wall":{ type:"block", state:"wall", tags:["block"] }
}
static public const cellWidth:int = 21;
static public const cellHeight:int = 12;
static public const margin:int = 9;
//ビットマップを記録したオブジェクト。ロード後に使用可能
static public var images:Object = {};
static private var loaders:Vector.<Loader> = new Vector.<Loader>();
static private var loadNum:int = 0;
//画像をロード。ローダーの配列を作る。
static public function load():Vector.<Loader>{
if( IMG_URL.length > 0 ){
for(var str:String in IMG_URL){
var loader:Loader = new Loader( );
loaders.push(loader);
}
loaders[0].load( new URLRequest( DIR_URL + IMG_URL[0][1] ), new LoaderContext(true) );
loaders[0].contentLoaderInfo.addEventListener( "complete", onLoad, false, 1000 );
}
return loaders;
}
static private function onLoad(e:Event):void{
e.currentTarget.removeEventListener("complete",onLoad);
setImageRect();
loadNum++;
if( IMG_URL.length > loadNum ){
loaders[loadNum].load(new URLRequest( DIR_URL + IMG_URL[loadNum][1] ), new LoaderContext(true));
loaders[loadNum].contentLoaderInfo.addEventListener("complete",onLoad,false,1000);
}
}
static private function setImageRect():void{
var loader:Loader = loaders[ loadNum ];
var map:BitmapData = new BitmapData( loader.width, loader.height, true, 0 );
map.draw( loader );
images[ IMG_URL[loadNum][0] ] = []
var lineColor:uint = map.getPixel32(map.width-1,map.height-1);
var x:int = 0; var y:int=0; var height:int=0; var width:int=0; var count:int=0;
while(true){
width=0;height=0;
if(lineColor != map.getPixel32(x,y) ){
for(var i:int=1;i+x<map.width;i++){
if( lineColor == map.getPixel32(x+i,y) ){break;}
}
width=i;
for(var j:int=1; j+y < map.height; j++){
if( lineColor == map.getPixel32(x,y+j) ){break;}
}
height=j;
var rect:Rectangle = new Rectangle(x,y,width,height);
var rect2:Rectangle = new Rectangle(0,0,width,height);
var cell:BitmapData = new BitmapData(rect.width,rect.height,true,0x0)
cell.setVector( rect2,map.getVector( rect ) );
images[ IMG_URL[loadNum][0] ].push( cell );
}
x+=width+1;
if(x>=map.width){ y+=height+1;x=0; }
if(y>=map.height){ break; }
count++;
}
}
//bitmapdataに画像を描画する
public function draw(target:BitmapData,type:String,name:String, x:int=0, y:int=0 ):void{
var map:BitmapData = images[ MAP_SET[type] ][ IMG_NAME[type].indexOf(name) ];
var mtr:Matrix = new Matrix( 1, 0, 0, 1, x, y + cellHeight - map.height + margin );
if( -1 < IMG_NAME[type].indexOf(name) ){
target.draw( map , mtr )
}
}
//指定した条件で部屋に物体を設置する。
static public function setObject( room:Room, x:int,y:int,name:String):GameObject{
if(name != null && OBJECT[name] != null){
var obj:Object = OBJECT[name];
var gObj:GameObject = new GameObject();
for(var str:String in obj){
gObj[str] = obj[str];
}
gObj.room = room;
gObj.name = name;
gObj.mapX=x; gObj.x = x*cellWidth;
gObj.mapY=y; gObj.y = y*cellHeight;
putObject( room, x, y, gObj );
}
return gObj;
}
static public function putObject( room:Room, x:int,y:int,o:GameObject):void{
//深度が低い順に並べる
for(var i:int=0; i < room.map[x][y].length; i++){
if( room.map[x][y][i].depth < o.depth ){break;}
}
room.map[x][y].splice(i,0, o );
}
static public function removeObject( room:Room, x:int,y:int,o:GameObject):void{
//深度が低い順に並べる
var i:int = room.map[x][y].indexOf(o);
room.map[x][y].splice(i,1);
}
}
//ロード画面
class NowLoading extends Sprite{
static public const COMPLETE:String = "complete";
public var loaders:Vector.<Object> = new Vector.<Object>;
public var bytesTotal:uint=0,bytesLoaded:uint=0;
private var _loaderNum:uint=0,_completedNum:uint=0,_openNum:uint=0; //ローダーの数
private var text:Bitmap, sprite:ProgressSprite;
private var onLoaded:Function;
private var LETTER:Object = {
"1":[[0,1,1],[0,0,1],[0,0,1],[0,0,1],[0,0,1]],"2":[[1,1,1],[0,0,1],[0,1,1],[1,0,0],[1,1,1]],"3":[[1,1,1],[0,0,1],[1,1,1],[0,0,1],[1,1,1]],"4":[[1,0,1],[1,0,1],[1,0,1],[1,1,1],[0,0,1]],"5":[[1,1,1],[1,0,0],[1,1,1],[0,0,1],[1,1,1]],
"6":[[1,1,1],[1,0,0],[1,1,1],[1,0,1],[1,1,1]],"7":[[1,1,1],[0,0,1],[0,0,1],[0,0,1],[0,0,1]],"8":[[1,1,1],[1,0,1],[1,1,1],[1,0,1],[1,1,1]],"9":[[1,1,1],[1,0,1],[1,1,1],[0,0,1],[0,0,1]],"0":[[1,1,1],[1,0,1],[1,0,1],[1,0,1],[1,1,1]],
".":[[0],[0],[0],[0],[1]]," ":[[0],[0],[0],[0],[0]],"n":[[0,0,0],[0,0,0],[1,1,1],[1,0,1],[1,0,1]],"w":[[0,0,0,0,0],[0,0,0,0,0],[1,0,1,0,1],[1,0,1,0,1],[1,1,1,1,1]],"o":[[0,0,0],[0,0,0],[1,1,1],[1,0,1],[1,1,1]],
"a":[[0,0,0],[0,0,1],[1,1,1],[1,0,1],[1,1,1]],"l":[[1],[1],[1],[1],[1]],"i":[[1],[0],[1],[1],[1]],"d":[[0,0,1],[0,0,1],[1,1,1],[1,0,1],[1,1,1]],"g":[[0,0,0],[0,0,0],[1,1,1],[1,0,1],[1,1,1],[0,0,1],[1,1,1]],
"C":[[1,1,1],[1,0,0],[1,0,0],[1,0,0],[1,1,1]],"O":[[1,1,1],[1,0,1],[1,0,1],[1,0,1],[1,1,1]],"M":[[1,1,1,1,1],[1,0,1,0,1],[1,0,1,0,1],[1,0,1,0,1],[1,0,1,0,1]],"P":[[1,1,1],[1,0,1],[1,1,1],[1,0,0],[1,0,0]],
"T":[[1,1,1],[0,1,0],[0,1,0],[0,1,0],[0,1,0]],"L":[[1,0,0],[1,0,0],[1,0,0],[1,0,0],[1,1,1]],"E":[[1,1,1],[1,0,0],[1,1,1],[1,0,0],[1,1,1]]
}
//ステージと関数を渡す
public function NowLoading(stage:Stage, onLoaded:Function = null){
if(onLoaded == null){ this.onLoaded=nullFunc }else{ this.onLoaded=onLoaded }
sprite = new ProgressSprite(stage.stageWidth,stage.stageHeight);
text = new Bitmap( new BitmapData(30*4,8,true,0x00000000 ) );
stage.addChild(this); addChild(sprite); addChild(text);
with(text){scaleX=scaleY=1; blendMode="invert"; x=stage.stageWidth-text.width; y=stage.stageHeight-text.height;}
}
//ローダーの追加
public function addLoader(loader:Loader):Loader{ setListener(loader.contentLoaderInfo);_loaderNum++;return loader;}
public function addURLLoader(loader:URLLoader):URLLoader{setListener(loader); _loaderNum++; return loader;}
private function nullFunc():void{}
private function setListener(loader:*):void{
loader.addEventListener("open", onOpen);
loader.addEventListener("complete", onComplete);
loader.addEventListener("progress", update);
}
private function update(e:Event=null):void{
bytesLoaded=0; bytesTotal=0;
for each(var loadObj:Object in loaders){
bytesLoaded += loadObj.bytesLoaded;
bytesTotal += loadObj.bytesTotal;
};
sprite.progress(bytesLoaded/bytesTotal * _openNum/_loaderNum);
if(bytesTotal!=0){ setText( "now loading... "+(bytesLoaded/bytesTotal* _openNum/_loaderNum*100).toFixed(1) ); }
}
private function onOpen(e:Event):void{ _openNum++;loaders.push(e.currentTarget); bytesTotal+=e.currentTarget.bytesTotal; }
private function onComplete(e:Event):void{ _completedNum++;if(_loaderNum == _completedNum){ setText( "COMPLETE" );onLoaded(); } }
private function setText(str:String):void{
var b:BitmapData = text.bitmapData; var l:int = str.length; var position:int = b.width;
b.lock();b.fillRect(b.rect,0x000000);
for(var i:int=0;i<l;i++){
var letterData:Array = LETTER[str.substr(l-i-1,1)];position-=letterData[0].length+1;
for(var n:int=0;n<letterData.length;n++){ for(var m:int=0;m<letterData[n].length;m++){
if(letterData[n][m]==1){b.setPixel32(m+position,n+1,0xFF000000);}
} }
}
b.unlock();
}
}
//このスプライトを編集することでロード画面を変えることができる。
class ProgressSprite extends Sprite{
private var mapData:BitmapData,sphereData:BitmapData,noizeData:BitmapData;
//コンストラクタ
public function ProgressSprite(width:int,height:int):void{
mapData = new BitmapData(width,height,true,0x00000000);
addChild(new Bitmap(mapData)).blendMode="invert";
}
//ロードが進行したときに呼び出される。 rateはロードの進行度で0-1
public function progress(rate:Number):void{
var thick:int = mapData.height*(0.61803)*(0.61803)/1.61803;
mapData.fillRect( new Rectangle(0,mapData.height-thick,mapData.width*rate,thick), 0x1000000*int(0xFF));
}
}
class EffectMap extends Bitmap{
}
//SiON
import org.si.sion.*;
import org.si.sound.*;
class MyDriver extends SiONDriver {
public var dm:DrumMachine = new DrumMachine(0, 0, 0, 1, 1, 1);
public var fill:SiONData;
function MyDriver():void{
super();
volume = 2.0
dm.volume = 0.05
setVoice(0, new SiONVoice(5,2,63,63,-10,0,2,20));
setSamplerData(0, render("%2@4 v8 l24 c<<c"));
setSamplerData(1, render("%2@4 l60 ccc"));
setSamplerData(2, render("%3@8 l12 <<<<<a0b0c0b0e0d0g"));
setSamplerData(3, render("%3@4 l60 <<<<<c>c"));
setSamplerData(4, render("%2@60 v2 l48 c<c"));
setSamplerData(5, render("%3@0q0,c"));
setSamplerData(6, render("%2@4, l24q0 <<c<<c>>c<<c>>"));
play() ;
}
}
class Sound{
static public var driver:MyDriver = new MyDriver();
static public function se(i:int,delay:int=0):void{
driver.playSound(i,0,delay);
}
static public function bgm(i:int=1):void{
}
}
//配列の複製を返す
import flash.utils.getQualifiedClassName;
function clone(arg:*):*{
var cl:*;
var name:String = getQualifiedClassName(arg);
if( name == "Object" || name == "Array" || name == "Vector" ){
if( name == "Object" ){ cl = {}; }
else if( name == "Array" ){ cl = []; }
else if( name == "Vector" ){ cl = Vector([]); }
for( var s:String in arg ){
cl[s] = clone( arg[s] );
}
return cl;
}else if( arg is Object && arg.hasOwnProperty("clone") && arg.clone is Function ){
return arg.clone();
}
return arg;
}