/**
+———————————————————+
| |
| Control class |
| version 0.05 |
| 2005.9.11 |
| 作者: af |
| http://www.sound.jp/airforce/ |
| Email:sidealice@yahoo.com.cn |
| |
|=========================================================|
| |
| 全局变量: |
| _global.mapW 地图宽 |
| _global.mapH 地图高 |
| _global.ntileY Tile在Y方向个数 |
| _global.ntileX Tile在X方向个数 |
| _global.tileW Tile长 |
| _global.tileH Tile宽 |
| _global.rootPath “_root” |
| _global.mapPath “_root.map” | |
| _global.tilePath “_root.map.tile”*未用 |
| _global.charPath “_root.map.char” |
| _global.clicked 鼠标点击 |
| _global.targetx 目标Tile的X格数 |
| _global.targety 目标Tile的Y格数 |
| _global.charMoving 角色移动 |
| |
+———————————————————+
_global.currentState 当前状态
_global.ctrlPath 控制路径
_root.attachMovie(“mouse”,”mouse”,200000);
eval(_global.mapPath).attachMovie(“char”,cName,100000);
*/
class ctrl extends Object {
public var ctrlName:String;
public function ctrl(cName:String) {
this.ctrlName = cName;
eval(_global.mapPath).attachMovie(“ctrl”,cName,150000);
_global.ctrlPath = eval(_global.mapPath + “.” + this.ctrlName);
}
public function drawCtrl(nx:Number, ny:Number) {
eval(_global.ctrlPath)._x = nx;
eval(_global.ctrlPath)._y = ny;
}
}
/**
+———————————————————+
| |
| Map class |
| version 0.05 |
| 2005.9.11 |
| 作者: af |
| http://www.sound.jp/airforce/ |
| Email:sidealice@yahoo.com.cn |
| |
|=========================================================|
| |
| 全局变量: |
| _global.mapW 地图宽 |
| _global.mapH 地图高 |
| _global.ntileY Tile在Y方向个数 |
| _global.ntileX Tile在X方向个数 |
| _global.tileW Tile长 |
| _global.tileH Tile宽 |
| _global.rootPath “_root” |
| _global.mapPath “_root.map” | |
| _global.tilePath “_root.map.tile”*未用 |
| _global.charPath “_root.map.char” |
| _global.clicked 鼠标点击 |
| _global.targetx 目标Tile的X格数 |
| _global.targety 目标Tile的Y格数 |
| _global.charMoving 角色移动 |
| |
+———————————————————+
*/
class map extends MovieClip {
//
//——————–成员定义——————–
//
//
public var mapW:Number, mapH:Number;
public var tileW:Number, tileH:Number;
public var rootPath, mapPath, tilePath, charPath;
public var walkable:Boolean = true;
public var mapData = [[]];
//
//——————–定义结束——————–
//
//
//——————–构造函数——————–
//
//
public function map(sName:String) {
this.tileW=_global.tileW
this.tileH=_global.tileH
this.rootPath=_global.rootPath
this.mapPath=_global.mapPath
this.tilePath=_global.tilePath
this.charPath=_global.charPath
_root.createEmptyMovieClip(sName,_root.getNextHighestDepth());
_global.mapPath = this.mapPath = this.rootPath + “.” + sName;
}
//==================================================
//——————–loadMap()——————–
//loadMap(tempMap)
//tempMap=[[]]->2D Array
//
public function loadMap(tempMap) {
this.mapData = tempMap;
this.mapW = this.mapData[0].length * this.tileW;
this.mapH = this.mapData.length * this.tileH;
_global.mapW = this.mapW;
_global.mapH = this.mapH;
_global.ntileY = this.mapData.length;
_global.ntileX = this.mapData[0].length;
//tracepro(mapData,”mapData”);
}
//==================================================
//——————–drawMap()——————–
//attachMovie(id,newName,depth)
//
public function drawMap() {
var tempPath = eval(_global.mapPath)
for (var i = 0; i < this.mapData.length; ++i) {
for (var j = 0; j < this.mapData[0].length; ++j) {
var tilename = “t_” + i + “_” + j;
tempPath.attachMovie(“tile”,tilename,i * 100 + j * 2);
tempPath[tilename]._x = j * this.tileW;
tempPath[tilename]._y = i * this.tileH;
tempPath[tilename].gotoAndStop(this.mapData[i][j] + 1);
if (this.mapData[i][j] == 0) {
tempPath[tilename].walkable = true;
}
else {
tempPath[tilename].walkable = false;
}
}
}
}
//==================================================
//——————–TraceBack——————–
//
public function tracepath() {
trace(“———–trace path start———–”);
trace(“rootPath: ” + rootPath);
trace(“mapPath: ” + mapPath);
trace(“tilePath: ” + tilePath);
trace(“———–trace path end———–”);
}
}
/**
保留使用:
public function set frame(n:Number) {
this.gotoAndStop(n + 1);
switch (n) {
case 0 :
this.walkable = true;
trace(“Set to wall!”);
break;
case 1 :
this.walkable = false;
trace(“Set to floor!”);
break;
default :
trace(“Wraning: no frame number to set!”);
}
}
public function get frame() {
}
**/
/**
+———————————————————+
|???????????????????????????????????????????????????????? |
|?? Path class??????????????????????????????????????????? |
|?? version 0.05????????????????????????????????????????? |
|?? 2005.9.11???????????????????????????????????????????? |
|?? 作者: af????????????????????????????????????????????? |
|?? http://www.sound.jp/airforce/???????????????????????? |
|?? Email:sidealice@yahoo.com.cn????????????????????????? |
|???????????????????????????????????????????????????????? |
|=========================================================|
|???????????????????????????????????????????????????????? |
|?? 全局变量:???????????????????????????????????????????? |
|?? _global.mapW?????????????????? 地图宽???????????????? |
|?? _global.mapH?????????????????? 地图高???????????????? |
|?? _global.ntileY???????????????? Tile在Y方向个数??????? |
|?? _global.ntileX???????????????? Tile在X方向个数??????? |
|?? _global.tileW????????????????? Tile长???????????????? |
|?? _global.tileH????????????????? Tile宽???????????????? |
|?? _global.rootPath?????????????? “_root”??????????????? |
|?? _global.mapPath??????????????? “_root.map”??????????? |?????????????????????? |
|?? _global.tilePath?????????????? “_root.map.tile”*未用? |
|?? _global.charPath?????????????? “_root.map.char”?????? |
|?? _global.clicked??????????????? 鼠标点击?????????????? |
|?? _global.targetx??????????????? 目标Tile的X格数??????? |
|?? _global.targety??????????????? 目标Tile的Y格数??????? |
|?? _global.charMoving???????????? 角色移动?????????????? |
|???????????????????????????????????????????????????????? |
+———————————————————+
*/
class path extends Array {
?//
?//——————–成员定义——————–
?//
?//
?public var rootPath, mapPath, tilePath, charPath;
?public var done:Boolean = false, visited:Boolean = true;
?public var nodename:String;
?public var cost:Number;
?public var x:Number, y:Number;
?public var parentx:Number, parenty:Number;
?public var newPath:Array;
?public var pathTime;
?public var Unchecked_Neighbours;
?//
?//——————–定义结束——————–
?//
?//
?//——————–构造函数——————–
?//
?//
?public function path() {
?}
?//==================================================
?//———-Function findPath()———-
?//findPath(startx,starty,targetx,targety)
?//
?public function findPath(startx, starty, targetx, targety) {
??clearMark();
??// start timer
??// make new array for unchecked tiles
??this.Unchecked_Neighbours = [];
??// not done yet
??this.done = false;
??// initialize
??this.nodename = “node_” + starty + “_” + startx;
??// create first node
??var cost = Math.abs(startx – targetx) + Math.abs(starty – targety);
??this[this.nodename] = {x:startx, y:starty, visited:true, parentx:null, parenty:null, cost:cost};
??// add node to array
??this.Unchecked_Neighbours[this.Unchecked_Neighbours.length] = this[this.nodename];
??// loop
??while (this.Unchecked_Neighbours.length > 0) {
???// get the next node
???var N = this.Unchecked_Neighbours.shift();
???// we’ve finished
???if (N.x == targetx and N.y == targety) {
????// done
????make_path(N);
????this.done = true;
????break;
???}
???else {
????N.visited = true;
????// right neighbour
????addNode(N,N.x + 1,N.y,targetx,targety);
????// left neighbour
????addNode(N,N.x – 1,N.y,targetx,targety);
????// up neighbour
????addNode(N,N.x,N.y + 1,targetx,targety);
????// down neighbour
????addNode(N,N.x,N.y – 1,targetx,targety);
???}
??}
??// remove path object
??delete this;
??// check if path was found
??if (this.done) {
???// we reached the goal
???return true;
??}
??else {
???// we couldn’t get there;
???return false;
???trace(“we couldn’t get there;”);
??}
?}
?//==================================================
?//——————–make_path()——————–
?//make_path(ob)
?//
?public function make_path(ob) {
??// create path array
??this.newPath = new Array();
??// loop until no more parents
??while (ob.parentx != null) {
???// add the x and y
???this.newPath[this.newPath.length] = ob.x;
???this.newPath[this.newPath.length] = ob.y;
???// mark the tile
???eval(_global.mapPath)["t_" + ob.y + "_" + ob.x].mark.gotoAndStop(2);
???// make its parent new object
???ob = this["node_" + ob.parenty + "_" + ob.parentx];
??}
??// start moving again
??_global.charMoving = true;
?}
?//==================================================
?//——————–addNode()——————–
?//addNode(ob, x, y, targetx, targety)
?//
?public function addNode(ob, x, y, targetx, targety) {
??// name of the new node
??this.nodename = “node_” + y + “_” + x;
??// add to the unchecked neighbours, if its walkable
??if (eval(_global.mapPath)["t_" + y + "_" + x].walkable) {
???// find estimated cost to the target
???this.cost = Math.abs(x – targetx) + Math.abs(y – targety);
???// and if its not visited yet
???if (this[this.nodename].cost > cost or this[this.nodename].cost == undefined) {
????// make new node
????this[this.nodename] = {x:x, y:y, visited:false, parentx:ob.x, parenty:ob.y, cost:cost};
????// add to the array
????for (var i = 0; i < this.Unchecked_Neighbours.length; i++) {
?????if (cost < this.Unchecked_Neighbours[i].cost) {
??????this.Unchecked_Neighbours.splice(i,0,this[this.nodename]);
??????break;
?????}
????}
????// didnt add it yet, too much cost, add to the end
????if (i >= this.Unchecked_Neighbours.length) {
?????this.Unchecked_Neighbours[this.Unchecked_Neighbours.length] = this[this.nodename];
????}
???}
??}
?}
?//==================================================
?//——————–clearMark()——————–
?//
?public function clearMark() {
??for (var i = 0; i < _global.ntileY; ++i) {
???for (var j = 0; j < _global.ntileX; ++j) {
????eval(_global.mapPath)["t_" + i + "_" + j].mark.gotoAndStop(1);
???}
??}
?}
}
/*
*/
/**
+———————————————————+
|???????????????????????????????????????????????????????? |
|?? Char class??????????????????????????????????????????? |
|?? version 0.05????????????????????????????????????????? |
|?? 2005.9.11???????????????????????????????????????????? |
|?? 作者: af????????????????????????????????????????????? |
|?? http://www.sound.jp/airforce/???????????????????????? |
|?? Email:sidealice@yahoo.com.cn????????????????????????? |
|???????????????????????????????????????????????????????? |
|=========================================================|
|???????????????????????????????????????????????????????? |
|?? 全局变量:???????????????????????????????????????????? |
|?? _global.mapW?????????????????? 地图宽???????????????? |
|?? _global.mapH?????????????????? 地图高???????????????? |
|?? _global.ntileY???????????????? Tile在Y方向个数??????? |
|?? _global.ntileX???????????????? Tile在X方向个数??????? |
|?? _global.tileW????????????????? Tile长???????????????? |
|?? _global.tileH????????????????? Tile宽???????????????? |
|?? _global.rootPath?????????????? “_root”??????????????? |
|?? _global.mapPath??????????????? “_root.map”??????????? |?????????????????????? |
|?? _global.tilePath?????????????? “_root.map.tile”*未用? |
|?? _global.charPath?????????????? “_root.map.char”?????? |
|?? _global.clicked??????????????? 鼠标点击?????????????? |
|?? _global.targetx??????????????? 目标Tile的X格数??????? |
|?? _global.targety??????????????? 目标Tile的Y格数??????? |
|?? _global.charMoving???????????? 角色移动?????????????? |
|???????????????????????????????????????????????????????? |
+———————————————————+
*/
class char extends MovieClip {
?//
?//——————–成员定义——————–
?public var xtile:Number, ytile:Number;
?public var dirx:Number = 0, diry:Number = 0;
?public var x:Number = 0, y:Number = 0;
?public var speed:Number = 2;
?public var charName:String;
?public var charMoveCost:Number = 3;
?public var moveField:Array;
?public static var nchar:Number = 1;
?//角色总数
?public var depth:Number;
?//角色MC深度
?public var id:Number;
?//角色ID号索引
?//
?//——————–定义结束——————–
?//
?//
?//——————–构造函数——————–
?//创造角色
?//
?public function char(cName:String) {
??this.charName = cName;
??this.depth = nchar;
??this.id = nchar – 1;
??eval(_global.mapPath).attachMovie(“char”,cName,100000 + this.depth);
??_global.charPath[this.id] = eval(_global.mapPath + “.” + this.charName);
??nchar++;
?}
?//==================================================
?//——————–drawChar()——————–
?//drawChar(xtile:Number, ytile:Number)
?//
?public function drawChar(nxtile:Number, nytile:Number) {
??this.xtile = nxtile;
??this.ytile = nytile;
??_global.charPath[this.id]._x = (this.xtile * _global.tileW) + _global.tileW / 2;
??_global.charPath[this.id]._y = (this.ytile * _global.tileW) + _global.tileW / 2;
??this._x = this.x = _global.charPath[this.id]._x;
??this._y = this.y = _global.charPath[this.id]._y;
??_global.charMoving = false;
?}
?//==================================================
?//——————–moveChar()——————–
?//moveChar(pather:path)
?//
?public function move(pather) {
??if ((this.x – _global.tileW / 2) % _global.tileW == 0 and (this.y – _global.tileH / 2) % _global.tileH == 0) {
???// calculate the tile where chars center is
???this.xtile = Math.floor(this.x / _global.tileW);
???this.ytile = Math.floor(this.y / _global.tileH);
???if (_global.clicked) {
????_global.clicked = false;
????pather.findPath(this.xtile,this.ytile,_global.targetx,_global.targety);
????return;
???}
???if (pather.newPath.length > 0) {
????_global.targety = pather.newPath.pop();
????_global.targetx = pather.newPath.pop();
????// right
????if (_global.targetx > this.xtile) {
?????this.dirx = 1;
?????this.diry = 0;
?????// left
????}
????else if (_global.targetx < this.xtile) {
?????this.dirx = -1;
?????this.diry = 0;
?????// up
????}
????else if (_global.targety > this.ytile) {
?????this.dirx = 0;
?????this.diry = 1;
?????// down
????}
????else {
?????this.dirx = 0;
?????this.diry = -1;
????}
???}
???else {
????_global.charMoving = false;
????return;
???}
??}
??// move
??this.y += this.speed * this.diry;
??this.x += this.speed * this.dirx;
??// update char position
??eval(_global.charPath[this.id])._x = this.x;
??eval(_global.charPath[this.id])._y = this.y;
??// face the direction
??eval(_global.charPath[this.id]).gotoAndStop(this.dirx + this.diry * 2 + 3);
?}
?//==================================================
?//——————–isArrive()——————–
?//isCharArrive()
?//
?public function isArrive():Boolean {
??if (this.x = _global.targetx && this.y == _global.targety) {
???trace(“char Arrive”);
???return true;
??}
?}
?//==================================================
?//——————–drawField()——————–
?//drawField()
?//
?public function drawField() {
??this.moveField = new Array();
??for (var cost:Number = 0; cost <= this.charMoveCost; cost++) {
???for (var i:Number = this.charMoveCost; cost < i; i–) {
????eval(_global.mapPath)["t_" + (this.ytile + cost) + "_" + (this.xtile + (this.charMoveCost - i))].mark.gotoAndStop(2);
????eval(_global.mapPath)["t_" + (this.ytile + cost) + "_" + (this.xtile - (this.charMoveCost - i))].mark.gotoAndStop(2);
????eval(_global.mapPath)["t_" + (this.ytile - cost) + "_" + (this.xtile + (this.charMoveCost - i))].mark.gotoAndStop(2);
????eval(_global.mapPath)["t_" + (this.ytile - cost) + "_" + (this.xtile - (this.charMoveCost - i))].mark.gotoAndStop(2);
????this.moveField.push([(this.ytile + cost), (this.xtile + (this.charMoveCost - i))]);
????this.moveField.push([(this.ytile + cost), (this.xtile - (this.charMoveCost - i))]);
????this.moveField.push([(this.ytile - cost), (this.xtile + (this.charMoveCost - i))]);
????this.moveField.push([(this.ytile - cost), (this.xtile - (this.charMoveCost - i))]);
???}
??}
?}
?//==================================================
?//——————–onTarget()——————–
?//onTarget()
?//
?public function onTarget(nx:Number, ny:Number):Boolean {
??if (nx == this.xtile && ny == this.ytile) {
???return true;
??}
??return false;
?}
?//==================================================
?//——————–inField()——————–
?//inField()
?//
?public function inField(nx:Number, ny:Number):Boolean {
??for (var obj in this.moveField) {
???if (this.moveField[obj][0] == nx && this.moveField[obj][1] == ny) {
????return true;
???}
??}
??return false;
?}
?//==================================================
?//——————–getCharN()——————–
?//getCharN()
?//
?public function getCharN():Number {
??return nchar;
?}
}
/**
+———————————————————+
| |
| Game class |
| version 0.05 |
| 2005.9.11 |
| 作者: af |
| http://www.sound.jp/airforce/ |
| Email:sidealice@yahoo.com.cn |
| |
|=========================================================|
| |
| 全局变量: |
| _global.mapW 地图宽 |
| _global.mapH 地图高 |
| _global.ntileY Tile在Y方向个数 |
| _global.ntileX Tile在X方向个数 |
| _global.tileW Tile长 |
| _global.tileH Tile宽 |
| _global.rootPath “_root” |
| _global.mapPath “_root.map” | |
| _global.tilePath “_root.map.tile”*未用 |
| _global.charPath “_root.map.char” |
| _global.clicked 鼠标点击 |
| _global.targetx 目标Tile的X格数 |
| _global.targety 目标Tile的Y格数 |
| _global.charMoving 角色移动 |
| |
+———————————————————+
*/
class game extends Object {
//
//——————–成员定义——————–
public var gameMap;
public var gamePath:path;
public var gameEnemyChar:char;
public var gameMainChar:char;
public var currentChar:char;
//public var gameEnemyList;
public var xmouse:Number, ymouse:Number;
public var gameCtrl:ctrl;
//
//——————–定义结束——————–
//
//
//——————–构造函数——————–
//开始游戏
//
public function game() {
init();
var myMap1 = [[8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10], [7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5], [2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 15], [0, 0, 15, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 14, 0, 0, 0, 0, 0, 14], [0, 0, 14, 0, 0, 15, 0, 0, 0, 14, 0, 0, 0, 13, 0, 0, 0, 0, 0, 13], [0, 0, 13, 0, 0, 14, 0, 0, 0, 13, 0, 0, 0, 12, 0, 0, 0, 0, 0, 13], [0, 0, 12, 0, 0, 13, 0, 0, 0, 13, 0, 0, 0, 11, 0, 0, 0, 0, 0, 13], [0, 0, 11, 0, 0, 12, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13], [0, 0, 0, 0, 0, 11, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13], [0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11]];
this.gameMap = new map(“game”);
this.gameMap.loadMap(myMap1);
this.gameMap.drawMap();
this.gameMainChar = new char(“Af”);
this.gameMainChar.drawChar(7,7);
this.gameEnemyChar = new char(“Enemy”);
this.gameEnemyChar.drawChar(7,10);
//this.gameMainChar.inField(8,8)
_root.attachMovie(“mouse”,”mouse”,200000);
this.gameCtrl = new ctrl(“gamemenu”);
this.gameCtrl.drawCtrl(0,_global.tileH * _global.ntileY);
}
//==================================================
//——————–init()——————–
//初始化全局变量
//
public function init() {
_global.tileW = 32;
_global.tileH = 32;
_global.rootPath = “_root”;
_global.mapPath = “_root.map”;
_global.tilePath = “_root.map.tiles”;
_global.charPath = new Array();
_global.clicked = false;
_global.charSelectedName = “er”
}
//==================================================
//——————–getTarget()——————–
//获得鼠标点击的目标格数
//
public function getTarget() {
this.gamePath = new path();
var tempPath = eval(this.gameMap.mapPath);
if (tempPath["t_" + this.ymouse + "_" + this.xmouse].walkable) {
// update target tile
_global.targetx = this.xmouse;
_global.targety = this.ymouse;
// if moving, wait until center is reached
if (!_global.charMoving) {
if (this.gameMainChar.onTarget(this.xmouse, this.ymouse)) {
this.gameMainChar.drawField();
this.currentChar = this.gameMainChar;
}
if (this.gameEnemyChar.onTarget(this.xmouse, this.ymouse)) {
this.gameEnemyChar.drawField();
this.currentChar = this.gameEnemyChar;
}
this.gamePath.findPath(this.currentChar.xtile,this.currentChar.ytile,_global.targetx,_global.targety);
}
else {
_global.clicked = true;
}
}
}
//==================================================
//——————–work()——————–
//鼠标事件的检查,更新位置
//
public function work() {
//this.gameMainChar.isArrive()
this.xmouse = Math.round((_root._xmouse – _global.tileW / 2) / _global.tileW);
this.ymouse = Math.round((_root._ymouse – _global.tileH / 2) / _global.tileH);
_root.mouse._x = this.xmouse * _global.tileW;
_root.mouse._y = this.ymouse * _global.tileH;
if (!_global.charMoving) {
eval(_global.charPath[this.currentChar.id]).gotoAndStop(“wait”);
this.gamePath.clearMark();
}
else {
this.currentChar.move(this.gamePath);
this.currentChar.play();
}
if (_root._xmouse >= 0 && _root._xmouse <= _global.mapW && _root._ymouse >= 0 && _root._ymouse <= _global.mapH) {
_root.mouse._visible = true;
}
else {
_root.mouse._visible = false;
}
updateData();
}
//==================================================
//——————–updateData()——————–
//updateData()
public function updateData() {
}
//==================================================
//——————–TraceBack——————–
//
public function tracepro(obj:Object, objName:String) {
trace(“———–trace property start———–”);
trace(“|Object Name:” + objName);
for (var prop in obj) {
trace(objName + “.” + prop + ” = ” + obj[prop]);
}
trace(“———–trace property end———–”);
}
}
