In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

本気でダンジョンRPG作る step2-(主人公、敵を動かす)

LIVE CODING 2010/04/23 13:41-15:59 22:07-0:08
wonderflでがんばってダンジョンRPGを作ってます。
マップ
緑:部屋 または 通路
赤:敵
ピンク:主人公
水色:階段
通ったことのない場所の階段,敵は見えません
操作方法
移動:方向キー
足踏み: A
階段を下りる: 階段上でSpace
今のところB2階までしかありません
English
I make a RPG.

step2:  move charactors
walk: arrow keys
step in place: A
go down stairs: Space
map
green: room or passage
yellow: Enemy
pink: player
light blue: staire
PREVIOUS 	http://wonderfl.net/c/eamf
NEXT 		http://wonderfl.net/c/t3qt
前のステップの差分は,上の[diff()]をクリックするとみることができます
/**
 * Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/tFGr
 */

package{
	//LIVE CODING 2010/04/23 13:41-15:59 22:07-0:08
	
	//wonderflでがんばってダンジョンRPGを作ってます。
	
	//マップ
	//	緑:部屋 または 通路
	//	赤:敵
	//	ピンク:主人公
	//	水色:階段
	//通ったことのない場所の階段,敵は見えません
	
	
	//操作方法
	
	//移動:方向キー
	//足踏み: A
	//階段を下りる: 階段上でSpace
	
	//今のところB2階までしかありません
	
	
	
	
	
	//English
	
	// I make a RPG.
	// 
	// step2:  move charactors
	
	// walk: arrow keys
	// step in place: A
	// go down stairs: Space
	
	// map
	//	green: room or passage
	//	yellow: Enemy
	//	pink: player
	//	light blue: staire
	

	
	
	
	//PREVIOUS 	http://wonderfl.net/c/eamf
	//NEXT 		http://wonderfl.net/c/t3qt
	
	//前のステップの差分は,上の[diff()]をクリックするとみることができます
	
	

	
	
	
	
	
	
	import flash.display.*;
    public class Game extends Sprite{
        public function Game() {
        		var back:Bitmap = new Bitmap(new BitmapData( 465, 465, true, 0xFFFFFFFF ) );
			back.bitmapData.perlinNoise(465, 465,1,1,true,true,7);	
        		addChild(back);
        		
           	stage.addChild( new RPG() );
           	
        }
    }
}

import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.utils.*;
import flash.text.TextField;

class RPG extends Sprite {
	private var map:Map;
	public function RPG() {
		addEventListener( Event.ADDED_TO_STAGE, init );
	}
	public function init(e:Event):void{
		removeEventListener( Event.ADDED_TO_STAGE, init );
		
		map = new Map(35,35);
		while(this.numChildren>0){
			this.removeChildAt(0);
		}
		
		map.bitmap.scaleX = 12;
		map.bitmap.scaleY = 12;
		
		map.bitmap.x = 20;
		map.bitmap.y = 20;
		

           	

           	
		addChild(map.bitmap);
		stage.addEventListener( "keyDown", map.man.onKeyDown );
		
		
		
		
		
		addChild(map.textField);//仮
		
	}
}






//マップデータのクラス
class Map{
	public var bitmap:Bitmap;
	
	public var textField:TextField = new TextField(); 
	
	public var rank:int = 0; //階層
	public var width:int;
	public var height:int;
	
	
	
	
	
	//マップの内容 0:壁、1:部屋 2:通路 3:アイテム 4:敵 5:自分 6:階段
	public var data:Vector.<Vector.<int>>;
	//マップに関する詳細なデータ
	public var floors:Vector.<Vector.<Floor>>;
	
	
	//部屋のサイズは6×6固定になります
	public static var ROOM_SIZE:int = 6;
	
	
	
	public var rooms:Vector.<Room>;//部屋のデータ
	public var routes:Vector.<Route>;
	public var enemys:Vector.<Enemy>;
	public var items:Vector.<DropItem>;//床に落ちているアイテム
	
	public var man:Man = new Man();
	
	//wとhには21以上を指定してください。
	public function Map(w:int = 50,h:int = 50){
		if(w < ROOM_SIZE *3 +2){w = ROOM_SIZE *3 +2;}
		if(h < ROOM_SIZE *3 +2){h = ROOM_SIZE *3 +2;}
		width = w;
		height = h;
		init();
	}
	
	//現在のサイズでマップを初期化
	public function init():void{
		bitmap = new Bitmap(new BitmapData( width, height, true, 0x00000000 ) );
		rank = 0;
		makeFloor();
	}
	
	public function makeFloor():void{
		rooms = new Vector.<Room>();
		routes = new Vector.<Route>();
		enemys = new Vector.<Enemy>();
		items = new Vector.<DropItem>();
		
		data = new Vector.<Vector.<int>>(width);
		data.fixed = true;
		
		floors = new Vector.<Vector.<Floor>>(width);
		floors.fixed = true;
		
		for(var i:int=0; i<width; i++ ){
			data[i] = new Vector.<int>(height);
			data[i].fixed = true;
			
			floors[i] = new Vector.<Floor>(height);
			floors[i].fixed = true;
			
			for(var j:int=0; j<height; j++ ){
				data[i][j] = 0;
				floors[i][j] = new Floor(i,j,this);
				floors[i][j].wall(0);
				if( i==0 || j==0 || i==width-1 || j==height-1 ){
					floors[i][j].routeable = false;
					floors[i][j].roomable = false;
				}
			}	
		}
		
		if( generate() ){
			update();
		}else{
			makeFloor();
		}
	}
	
	//ランダムマップの生成をします
	public function generate():Boolean{
		var missCount:int = 0;
		while(missCount<10){
			if( requestRoom( 1+Math.random()*(width-ROOM_SIZE-2), 1+Math.random()*(height-ROOM_SIZE-2) ) == false){
				missCount++;
			}else{
				missCount = 0;
			}
		}
		if(rooms.length<2){
			if(width >= ROOM_SIZE*2+3 && height >= ROOM_SIZE*2+3){
				return false;
			}
		}else{
			var c:int = 0;
			while(connect() == false){
				var i:int = c%rooms.length;
				exit( rooms[i] );c++;
				if(c>rooms.length*100){return false;}
			}
		}
		for each(var route:Route in routes){
			cutRoute(route);
		}
		setParts();
		
		return true;
	}
	
	//指定された位置に部屋を作成を試みます
	//成功したかどうかをbooleanで返します。
	private function requestRoom(x:int,y:int):Boolean{
		if(width < ROOM_SIZE*2+3 || height < ROOM_SIZE*2+3){return false;}
		var miss:Boolean = false;
		for(var i:int = x; i< x+ROOM_SIZE; i++){
			for(var j:int = y; j< y+ROOM_SIZE; j++){
				if( floors[i][j].roomable == false ){
					miss = true;
				}
			}
		}
		if(miss){ return false; }
		else{ makeRoom(x,y); }
		return false;
	}
	//部屋を作成します。requestRoom以外から呼び出さないでください。
	private function makeRoom(x:int,y:int):void{
		rooms.push(new Room(x,y,ROOM_SIZE,ROOM_SIZE,rooms.length,this));
		for(var i:int = x-1; i< x+ROOM_SIZE+1; i++){
			for(var j:int = y-1; j< y+ROOM_SIZE+1; j++){
				if(x <= i && i< x+ROOM_SIZE && y <= j && j < y+ROOM_SIZE ){
					data[i][j] = 1;
					floors[i][j].room(rooms.length-1);
				}else if( (x+1<=i && i<x+ROOM_SIZE-1) || (y+1<=j && j<y+ROOM_SIZE-1) ){
					floors[i][j].roomable=false;
				}else{
					floors[i][j].roomable=false;
					floors[i][j].routeable=false;
				}
			}
		}
	}
	//すべての部屋がつながってるか調べます
	private function connect():Boolean{
		var ok:Boolean = true;
		var v:Vector.<int> = rooms[0].getConnect();
		for(var i:int=0; i<rooms.length; i++){
			if(v.indexOf(i)==-1){ok=false;}
		}
		return ok;
	}
	//rに出口となる部屋をまっすぐ伸ばします。
	private function exit(r:Room):Boolean{
		var a:int = Math.random()*4;
		var c:int;
		var x:int;
		var y:int;
		var dx:int=0;
		var dy:int=0;
		switch(a){
			case 0://上に道を伸ばす
				x = r.x+1 + Math.random()*(ROOM_SIZE-2);
				y = r.y-1;
				dy = -1; 
				break;
			case 1://下に道を伸ばす
				x = r.x+1 + Math.random()*(ROOM_SIZE-2);
				y = r.y+r.height;
				dy = 1; 
				break;
			case 2://左に道を伸ばす
				x = r.x-1;
				y = r.y+1 + Math.random()*(ROOM_SIZE-2);
				dx = -1; 
				break;
			case 3://右に道を伸ばす
				x = r.x+r.width;
				y = r.y+1 + Math.random()*(ROOM_SIZE-2);
				dx = 1; 
				break;
		}
		if(floors[x][y].routeable == true){
			var route:Route = new Route(routes.length);
			route.rooms.push(r);
			
			c = 0; 
			while(c<height){
				if(floors[x][y].routeable == true){
					data[x][y] = 2;
					floors[x][y].route(route.index);
					route.floors.push(floors[x][y]);
					x += dx;
					y += dy;
				}else if(data[x][y]==1){
					close(dx,dy,r);
					close(-dx,-dy,rooms[floors[x][y].index]);
					rooms[ floors[x][y].index ].exits.push( route );
					r.exits.push( route );
					route.rooms.push( rooms[floors[x][y].index] );
					routes.push(route);
					break;
				}else if(data[x][y]==2){
					close(dx,dy,r);
					r.exits.push(route);
					routes[ floors[x][y].index ].connect(route);
					break;
				}else if(c>0){
					close(dx,dy,r);
					r.exits.push(route);
					routes.push(route);
					break;
				}else{
					break;
				}
				c++;
			}
		}else{
			return false;
		}
		return true;
	}
	//rの一面を出口にできなくする。
	private function close(dx:int,dy:int,r:Room):void{
		for(var i:int = r.x+dx; i< r.x+r.width+dx; i++){
			for(var j:int = r.y+dy; j< r.y+r.height+dy; j++){
				floors[i][j].routeable=false;
			}
		}
	}
	//行き止まりを消す。
	public function cutRoute(r:Route):void{
		var loop:Boolean = true;
		while(loop){
			loop = false;
			for each(var f:Floor in r.floors){
				var c:int = 0;
				var side:Floor = floors[f.x + 1][f.y];
				if( ( side.name == "route"  && side.index == r.index ) || side.name == "room"){c++;}
				side = floors[f.x - 1][f.y];
				if( ( side.name == "route"  && side.index == r.index ) || side.name == "room"){c++;}
				side = floors[f.x][f.y - 1];
				if( ( side.name == "route"  && side.index == r.index ) || side.name == "room"){c++;}
				side = floors[f.x][f.y + 1];
				if( ( side.name == "route"  && side.index == r.index ) || side.name == "room"){c++;}
				if(c == 1){
					loop = true;
					data[f.x][f.y] = 0;
					floors[f.x][f.y].wall(0);
					r.floors.splice(r.floors.indexOf(f),1);
				}
			}
		}
	} 
	//人、敵、階段、アイテムなどを設置
	private function setParts():void{
		
		GameUtil.setItems(this,rank);
		GameUtil.setEnemys(this,rank);
		
		//主人公は0番の部屋に配置
		var x:int = rooms[0].x + rooms[0].width * Math.random(); 
		var y:int = rooms[0].y + rooms[0].height * Math.random(); 
		data[x][y] =  5;
		man.init( x, y, this);
		
		//階段は最後のの部屋に配置
		var l:int = rooms.length-1;
		x = rooms[l].x + rooms[l].width * Math.random(); 
		y = rooms[l].y + rooms[l].height * Math.random(); 
		data[x][y] =  6;
		floors[x][y].stair();
	}
	//階段を下りる
	public function down():void{
		rank++;
		if(rank > GameData.DEPTH ){rank = 0;}
		makeFloor();
	}
	
	//敵の行動などを1つ進める
	public function step():void{
		for each( var enemy:Enemy in enemys ){
			enemy.walk();
		}
		update();
	}
	
	//textFieldから現在の状態を出力
	public function output():void{
		var str:String = "";
		var f:Floor = floors[man.x][man.y];
		
		str += "現在地:  B" + (rank+1) + "階";
		str += "  x," + man.x;
		str += "  y," + man.y;
		str += "  " + f.name + f.index +"\n";
		str += "\n近くの敵:\n";
		
		for each(var e:Enemy in enemys){
			var ef:Floor = floors[e.x][e.y];
			if(ef.name == f.name && ef.index == f.index){
				str += e.data.name;
				str += "   life:" + e.life + "/" + e.maxLife;
				str += "   walkType:" + e.data.walkType +"\n";
			}
		}
		
		textField.width = 400;
		textField.text = str;
	}
	
	
	
	//bitmapを更新します。
	public function update():void{
		output();
		
		var bitmapData:BitmapData = bitmap.bitmapData;
		bitmapData.lock();
		for(var i:int=0; i<width; i++ ){
			for(var j:int=0; j<height; j++ ){
				var color:uint = 0x3300FF00;
				switch(data[i][j]){
					case 0:
						color = 0x00000000; 
						break;
					case 1:
						if(floors[i][j].visit){color = 0xFF00FF00;}
						break;
					case 2:
						if(floors[i][j].visit){color = 0xFF00FF00;}
						break;
					case 3:
						if(floors[i][j].visit){color = 0xFF0000FF;}
						break;
					case 4:
						if(floors[i][j].visit){color = 0xFFFF0000;}
						break;
					case 5:
						color = 0xFFFF00FF; 
						break;
					case 6:
						if(floors[i][j].visit){color = 0xFF00FFFF;}
						break;
				}
				bitmapData.setPixel32(i,j,color);
			}
		}
		bitmapData.unlock();
	}
}



//部屋のクラス
class Room{
	public var x:int;
	public var y:int;
	public var width:int;
	public var height:int;
	public var index:int;
	public var map:Map;
	
	
	public var exits:Vector.<Route> = new Vector.<Route>();

	
	public function Room(xx:int,yy:int,w:int,h:int,i:int,m:Map):void{
		x=xx;
		y=yy;
		width=w;
		height=h;
		index=i;
		map=m;
	}
	
	
	//この部屋とつながっている部屋を返します
	public function getConnect( v:Vector.<int> = null ):Vector.<int>{
		if(v == null){v = new Vector.<int>();}
		if(v.indexOf(index)== -1){ v.push(index);}
		for each(var route:Route in exits){
			v = route.getConnect(v);
		}
		return v;
	}
	
	//この部屋を通ったことがあるところとして設定
	public function visit():void{
		for (var i:int = x; i<x+width; i++){
			for (var j:int = y; j<y+height; j++){
				map.floors[i][j].visit = true ;
			}
		}
	}
}
//道のクラス
class Route{
	//通り道{x,y,visit}
	public var floors:Vector.<Floor> = new Vector.<Floor>();
	//つながっている部屋のリスト。
	public var rooms:Vector.<Room> = new Vector.<Room>();
	public var index:int;
	public function Route(i:int){
		index = i;
	}
	//Routeを接続します。
	public function connect(r:Route):void{
		floors.concat(r.floors);
		rooms.concat(r.rooms);
		for each(var room:Room in r.rooms){
			room.exits.push(this);
		}
		for each(var floor:Floor in r.floors){
			floor.index = index;
		} 
	}
	public function getConnect( v:Vector.<int> = null ):Vector.<int>{
		for each(var room:Room in rooms){
			if(v.indexOf(room.index) == -1){ v= room.getConnect(v) }
		}	
		return v;
	}
}
class Floor{
	public var x:int;	
	public var y:int;
	public var map:Map;
	
	public var name:String="wall";
	public var data:int=0;
	public var index:int = 0;
	public var visit:Boolean=false;
	public var routeable:Boolean=true;
	public var roomable:Boolean=true;
	public var isStair:Boolean=false;
	
	public function Floor(xx:int,yy:int,m:Map){
		x = xx;
		y = yy;
		map = m;
	}
	//この床を壁として初期化
	public function wall(i:int):void{
		name = "wall";
		index = i;
		data = 0;
		routeable = true;
		roomable = true;
	} 
	//この床を部屋として初期化
	public function room(i:int):void{
		name = "room";
		index = i;
		data = 1;
		routeable = false;
		roomable = false;
	} 
	//この床を道として初期化
	public function route(i:int):void{
		name = "route";
		index = i;
		data = 2;
		routeable = false;
		roomable = false;
	}
	
	//この床を階段にする
	public function stair():void{
		isStair = true;
		data = 6;
	} 
	
	//この床を通ったことがある場所として設定.
	public function over():void{
		if(visit == false){
			if(name == "room"){
				map.rooms[index].visit();
			}else{
				visit = true;
			}
		}
	} 
}



//床に落ちているアイテムのクラス
class DropItem{
	public var x:int = 0;
	public var y:int = 0;
	public var name:String = "item";//itemのクラス
}

//キャラクターのクラス
//キャラクターには敵や自分が含まれます。
class Charactor{
	public var x:int = 0;
	public var y:int = 0;
	public var map:Map;		//このキャラクターが配置されているマップ
	public var life:int = 100;
	public var maxLife:int = 100;
	public var name:String = "chara";
	public var motion:Motion;
}
//主人公のクラス
class Man extends Charactor{
	public function Man(){
	}
	public function init(xx:int, yy:int, m:Map):void{
		x=xx; y=yy; map=m;
		name = "man";
		motion = new Motion(name);
		map.floors[x][y].over();
	}
	public function onKeyDown(e:KeyboardEvent):void{
		if(e.keyCode == 	Keyboard.UP){ move(0,-1); }
		else if(e.keyCode == 	Keyboard.DOWN){ move(0,1); }
		else if(e.keyCode == 	Keyboard.LEFT){ move(-1,0); }
		else if(e.keyCode == 	Keyboard.RIGHT){ move(1,0); }
		else if(e.keyCode ==	65){ move(0,0) }
		else if(e.keyCode ==	Keyboard.SPACE && map.floors[x][y].isStair){ map.down(); }
	}
	public function move(dx:int,dy:int):Boolean{
		if(motion.update()){
			return false;
		}else{
			var data:int = map.data[x+dx][y+dy];
			if( data == 0 || data == 4 ){
				return false;
			}else{
				motion.walk(x,y,x+dx,y+dy);
				map.data[x][y] = map.floors[x][y].data;
				x+=dx; y+=dy;
				map.data[x][y] = 5;
				map.floors[x][y].over();
				map.step();
			}
		}
		return true;
	}
}
class Enemy extends Charactor{
	public var data:Object;
	public function Enemy(xx:int, yy:int, m:Map,n:String){
		x=xx; y=yy; map=m;
		name = n;
		data = GameData.ENEMY_DATA[n];
		life = data.life;
		maxLife = life;
		motion = new Motion(name);
	}
	public function walk():void{
		var c:int = 0;
		while(c < 10){
			switch(data.walkType){
				case "chase": 
				if(map.floors[map.man.x][map.man.y].name == "room" && map.floors[map.man.x][map.man.y].index == map.floors[x][y].index){
						if( chase() ){break;}
					}else{
						if( walkRandom() ){break;}
					}
					break;
				case "random":
					if( walkRandom() ){break;}
					break;
			}
			c++;
		}
	}
	public function walkRandom():Boolean{
		var d:int = Math.random()*2;
		var dx:int;var dy:int;
		if(d==0){
			dx = 0;
			d = Math.random()*2;
			dy = d*2-1;
		}else{
			dy = 0;
			d = Math.random()*2;
			dx = d*2-1;
		}
		return move(dx,dy);
	}
	
	public function chase():Boolean{
		var d:int = Math.random()*2;
		var dx:int;var dy:int;
		if(d==0){
			if(map.man.x == x){d=1;}
		}else{
			if(map.man.y == y){d=0;}
		}
		
		if(d==1){
			dx = 0;
			if(map.man.y > y){dy=1;}
			else{dy=-1}
		}else{
			dy = 0;
			if(map.man.x > x){dx=1;}
			else{dx=-1}
		}
		
		return move(dx,dy);
	}
	
	public function move(dx:int,dy:int):Boolean{
		if(motion.update()){
			return false;
		}else{
			var d:int = map.data[x+dx][y+dy];
			if( d == 0 || d == 2 || d == 4 || d == 5){
				return false;
			}else{
				motion.walk(x,y,x+dx,y+dy);
				map.data[x][y] = map.floors[x][y].data;
				x+=dx; y+=dy;
				map.data[x][y] = 4;
			}
		}
		return true;
	}
}

class Motion{
	//モーションが稼働しているか
	public var moving:Boolean = false;
	
	//モーションの名前
	public var name:String = "";
	public var page:int = 0;		//モーション画像の番号
	public var numPage:int = 2;	//モーション画像の数
	
	//動作主の名前
	public var parent:String;
	
	
	//モーションの始点
	public var startX:Number;
	public var startY:Number;
	
	//モーションの終点
	public var endX:Number;
	public var endY:Number;
	
	public var length:int;	//モーションの継続時間(ミリ秒)
	public var time:int;		//モーション開始からの経過時間
	
	private var startTime:int;	//モーションを開始した時刻
	
	public function Motion(p:String){parent = p;}
	
	//モーションを開始します
	public function start():void{
		moving = true;
		startTime = getTimer();
		time = 0;
	}
	//モーションを終了します
	public function stop():void{
		moving = false;
	}
	//モーションの状態を更新します
	public function update():Boolean{
		if(moving){
			time = getTimer() - startTime;
			if(time > length){
				stop();
			}
		}
		return moving;
	}
	
	public function walk( sx:Number,sy:Number,ex:Number,ey:Number ):void{
		startX = sx; startY = sy; 
		endX = ex; endY = ey; 
		
		name = "walk";
		numPage = 2;		//2枚絵
		length = 100;	//モーション時間	0.1s
		start();
	}
}









//敵、画像、アイテム、技、などのデータが収められている静的クラス
class GameData{
	public static var ROOM_WIDTH:int = 6;
	public static var ROOM_HEIGHT:int = 6;
	public static var DEPTH:int = 1;
	
	
	
	
	
	//各階のデータ=============================================
	public static var RANK_ARRAY:Array =[
		{//0階
			minEnemy:1,
			maxEnemy:2,
			enemy:["mosamosa","fusafusa"]
		},
		{//1階
			minEnemy:1,
			maxEnemy:2,
			enemy:["mosamosa","fusafusa"]
		}
	];
	//(各階のデータここまで)=============================================
	
	
	
	
	
	
	
	
	
	//敵のデータ==============================================
	public static var ENEMY_DATA:Object = {
		mosamosa:{//もさもさ
			name:"もさもさ",
			life:60,
			walkType:"chase"
		},
		fusafusa:{
			name:"ふさふさ",
			life:50,
			walkType:"random"
		}
	}
	//(敵のデータここまで)====================================
}

class GameUtil{
	//その階層に見合ったアイテムを配置
	public static function setItems(map:Map,rank:int):void{}

	public static function setEnemys(map:Map,rank:int):void{
		for (var i:int=1;i<map.rooms.length;i++){
			var room:Room = map.rooms[i];
			var max:int = GameData.RANK_ARRAY[rank].maxEnemy;
			var min:int = GameData.RANK_ARRAY[rank].minEnemy;
			var c:int = min + (max-min+1) * Math.random();
			while(c>0){
				var x:int = room.x + room.width * Math.random(); 
				var y:int = room.y + room.height * Math.random(); 
				var e:Array = GameData.RANK_ARRAY[rank].enemy;
				var l:int = e.length*Math.random();
				var name:String = e[l];
				
				map.data[x][y] = 4;
				map.enemys.push( new Enemy(x,y,map,name) )
				c--;
			}
		}
	}
}