项目地址
https://github.com/mangenotwork/HABL-H5
基于 lufylegend 开发
lufylegend 框架官网 : http://www.lufylegend.com/
lufylegend是一个兼容性极高,功能极多,使用方便的HTML5游戏引擎。
游戏地图
var script = {
stage01:{
add:[
{chara:"player",img:"hero",x:1,y:1}
],
map:[
[ 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3],
[ 2, 0, 0, 9, 0, 0, 0,10, 0, 0, 0,10, 0, 0, 0,10, 0, 0, 0,10, 0, 0, 0, 0, 2],
[ 2, 0, 0, 9, 0, 0, 0,10, 0, 0, 0,10, 0, 0, 0,10, 0, 0, 0,10, 0, 0, 0, 0, 2],
[ 2, 0, 0, 9, 0, 0, 0,10, 0, 0, 0,10, 0, 0, 0,10, 0, 0, 0,10, 0, 0, 0, 0, 2],
[ 2, 0, 0, 1, 1, 8, 1, 1, 1, 8, 1, 1, 1, 8, 1, 1, 1, 8, 1, 1, 1, 8, 1, 1, 2],
[ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
[ 2, 7, 7, 8, 7, 7, 6, 7, 7, 6, 7, 6, 8, 7, 7, 7, 6, 6, 7, 8, 6, 7, 7, 7, 2],
[ 2, 0, 0, 0, 0, 0, 2, 0, 0, 0,10, 0, 0, 9, 0, 0, 0, 0,10, 0, 9, 0, 0, 0, 2],
[ 2, 0, 0, 0, 0, 0, 2, 0, 0, 0,10, 0, 0, 9, 0, 0, 0, 0,10, 0, 9, 0, 0, 0, 2],
[ 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0,10, 0, 9, 0, 0, 0, 2],
[ 2, 1, 1, 1, 1, 1, 2, 0, 0, 0,10, 0, 0, 0, 0, 0, 0, 0,10, 0, 0, 0, 0, 0, 2],
[ 2, 0, 0, 0, 0, 0, 2, 1, 1, 1, 1, 0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#cccccc',[0,0,
32*12+5,32*12+5],true,'#cccccc');
interface_Layer.x = 14;
interface_Layer.y = 115;
mapLayer = new LSprite();
interface_Layer.addChild(mapLayer);
mapLayer.x = 0;
mapLayer.y = 0;
mapLayer.removeAllChild();
var map_info = script.stage01;
map = map_info.map;
mapdata = map_info.mapData;
smapdata = map_info.sim_mapData
addMap(0,0);
delMap();
charaLayer = new LSprite();
interface_Layer.addChild(charaLayer);
charaLayer.removeAllChild();
addChara();
interface_Layer.addEventListener(LEvent.ENTER_FRAME,onframe);
}
function initMap(){
}
function addMap(cx,cy){
var i,j,index,indexX,indexY;
var bitmapdata,bitmap;
var mapX = mapLayer.x / STEP;
var mapY = mapLayer.y / STEP;
var mx = cx<0?-1:0,my = cy<0?-1:0;
mapLayer.removeAllChild();
for(i=my;i<20 +Math.abs(cy) && i-mapY < map.length;i++){
for(j=mx;j<20 +Math.abs(cx)&& j-mapX < map[0].length;j++){
index = map[parseInt(i-mapY)][parseInt(j-mapX)];
indexY = Math.floor(index /15);
indexX = index - indexY*15;
bitmapdata = new LBitmapData(dataList["map"],indexX*32,indexY*32,32,32);
bitmap = new LBitmap(bitmapdata);
bitmap.x = j*STEP - mapLayer.x;
bitmap.y = i*STEP - mapLayer.y;
mapLayer.addChild(bitmap);
}
}
npc_show(1,3,"npc 1",true);
boss_all_show(boss_arr);
}
function delMap(){
var bitmap,i;
for(i=0;i<mapLayer.childList.length;i++){
bitmap = mapLayer.childList[i];
if(bitmap.x + mapLayer.x < 0 || bitmap.x + mapLayer.x >= 32*15 ||
bitmap.y + mapLayer.y < 0 || bitmap.y + mapLayer.y >= 32*15){
mapLayer.removeChild(bitmap);
i--;
}
}
}
添加角色、NPC、怪物
function addChara(){
var stage = script.stage01;
var charaList = stage.add;
var chara,charaObj;
for(var i=0;i<charaList.length;i++){
charaObj = charaList[i];
if(charaObj.chara == "player"){
bitmapdata = new LBitmapData(dataList[charaObj.img]);
chara = new Character(true,i,bitmapdata,4,3);
player = chara;
}else{
bitmapdata = new LBitmapData(dataList[charaObj.img]);
chara = new Character(false,i,bitmapdata,3,4);
}
chara.x = charaObj.x * 32;
chara.y = charaObj.y * 32;
charaLayer.addChild(chara);
}
}
角色的操作实现
function onframe(){
for(var i=0;i<charaLayer.childList.length;i++){
charaLayer.childList[i].onframe();
}
}
function Character(isHero,index,data,row,col,speed){
base(this,LSprite,[]);
var self = this;
self.isHero = isHero;
self.index = index;
self.speed = speed==null?3:speed;
self.speedIndex = 0;
data.setProperties(0,0,data.image.width/col,data.image.height/row);
var list = LGlobal.divideCoordinate(data.image.width,data.image.height,row,col);
self.anime = new LAnimation(this,data,list);
self.move = false;
self.moveIndex = 0;
};
Character.prototype.onframe = function (){
var self = this;
if(self.speedIndex++ < self.speed)return;
self.speedIndex = 0;
if(self.move){self.onmove();}
self.anime.onframe();
};
Character.prototype.onmove = function (){
var self = this;
var ml_cnt = 4;
var ml = STEP;
switch (self.direction){
case UP:
if(mapmove){
mapLayer.y += ml;
charaLayer.y += ml;
}
self.y -= ml;
break;
case LEFT:
if(mapmove){
mapLayer.x += ml;
charaLayer.x += ml;
}
self.x -= ml;
break;
case RIGHT:
if(mapmove){
mapLayer.x -= ml;
charaLayer.x -= ml;
}
self.x += ml;
break;
case DOWN:
if(mapmove){
mapLayer.y -= ml;
charaLayer.y -= ml;
}
self.y += ml;
break;
}
self.moveIndex++;
if(self.moveIndex >= 0){
if(mapmove)delMap();
if(self.direction != self.direction_next){
self.direction = self.direction_next;
self.anime.setAction(self.direction);
}
if(!isKeyDown || !self.checkRoad()){
self.move = false;
return;
}
self.checkMap(self.direction);
posi = self.get_posi();
}
};
Character.prototype.get_posi = function(dir){
var self = this;
var tox,toy,myCoordinate;
if(dir==null)dir=self.direction;
myCoordinate = self.getCoordinate();
switch (dir){
case UP:
tox = myCoordinate.x;
toy = myCoordinate.y - 1;
break;
case LEFT:
tox = myCoordinate.x - 1;
toy = myCoordinate.y ;
break;
case RIGHT:
tox = myCoordinate.x + 1;
toy = myCoordinate.y;
break;
case DOWN:
tox = myCoordinate.x;
toy = myCoordinate.y + 1;
break;
}
if(mapdata[toy][tox] != 1 & mapdata[toy][tox] != 3){
posi[0] = tox;
posi[1] = toy;
}
else{
posi[0] = self.x/STEP;
posi[1] = self.y/STEP;
}
return posi;
}
Character.prototype.checkRoad = function (dir){
var self = this;
var tox,toy,myCoordinate;
if(dir==null)dir=self.direction;
myCoordinate = self.getCoordinate();
switch (dir){
case UP:
tox = myCoordinate.x;
toy = myCoordinate.y - 1;
break;
case LEFT:
tox = myCoordinate.x - 1;
toy = myCoordinate.y ;
break;
case RIGHT:
tox = myCoordinate.x + 1;
toy = myCoordinate.y;
break;
case DOWN:
tox = myCoordinate.x;
toy = myCoordinate.y + 1;
break;
}
if(toy <= 0 || tox <= 0)return false;
if(mapdata[toy][tox] == 1 || mapdata[toy][tox] == 3){
return false
};
return true;
};
Character.prototype.setCoordinate = function (sx,sy){
var self = this;
self.x = sx*STEP;
self.y = sy*STEP;
};
Character.prototype.getCoordinate = function (){
var self = this;
return {x:parseInt(self.x/STEP),y:parseInt(self.y/STEP)};
};
Character.prototype.changeDir = function (dir){
var self = this;
if(!self.move){
self.direction = dir;
self.direction_next = dir;
self.anime.setAction(dir);
if(!self.checkRoad(dir))return;
self.checkMap(dir);
self.move = true;
self.moveIndex = 0;
}else if(dir != self.direction){
self.direction_next = dir;
}
};
Character.prototype.checkMap = function (dir){
var self = this;
mapmove = false;
switch (dir){
case UP:
if(self.y + charaLayer.y> STEP*2)break;
if(mapLayer.y >= 0)break;
gundong_y -= 1;
addMap(0,-1);
get_area("up",0,-1);
mapmove = true;
break;
case LEFT:
if(self.x + charaLayer.x > STEP*2)break;
if(mapLayer.x >= 0)break;
gundong_x -= 1;
addMap(-1,0);
get_area("left",-1,0);
mapmove = true;
break;
case RIGHT:
if(self.x < STEP*15 - 2*STEP)break;
if(STEP*15 - mapLayer.x >= map[0].length*STEP)break;
gundong_x += 1;
addMap(1,0);
get_area("right",1,0);
mapmove = true;
break;
case DOWN:
if(self.y < (STEP*14+30) - 2*STEP)break;
if( (STEP*14+30) - mapLayer.y >= map.length*STEP)break;
gundong_y += 1;
addMap(0,1);
get_area("down",0,1);
mapmove = true;
break;
}
};
function get_area(type,cx,cy){
if(type == "right"){
arca_x[0] = arca_x[0]+cx;
arca_x[1] = arca_x[1]+cx;
}
if(type == "left"){
arca_x[0] = arca_x[0]+cx;
arca_x[1] = arca_x[1]+cx;
}
if(type == "up"){
arca_y[0] = arca_y[0]+cy;
arca_y[1] = arca_y[1]+cy;
}
if(type == "down"){
arca_y[0] = arca_y[0]+cy;
arca_y[1] = arca_y[1]+cy;
}
}
小地图
function map_info_Layer_show(){
map_info_Layer = new LSprite();
backLayer.addChild(map_info_Layer);
s_map(1,1);
var map_title = new LTextField();
map_title.text = game_play_info_data.user_map;
map_title.size = 10;
map_title.color = "#53FF53";
map_title.x = 330;
map_title.y = 2;
map_info_Layer.addChild(map_title);
}
function s_map(y,x){
if(map_s_L){
map_s_L.remove();
}
var smap_0,smap_bitmap_0;
var smap_1,smap_bitmap_1;
var smap_2,smap_bitmap_2;
var smap_3,smap_bitmap_3;
var s_mapdata = smapdata;
if(s_mapdata[x][y]!=1 || s_mapdata[x][y]!=3){
s_mapdata[x][y] = 2;
}
map_s_L = new LSprite();
map_info_Layer.addChild(map_s_L);
map_s_L.x = 330;
map_s_L.y = 20;
for(i=0;i < s_mapdata.length-1;i++){
for(j=0;j < s_mapdata[0].length-1;j++){
index = s_mapdata[i][j];
smap_1 = new LBitmapData(dataList["s_map_no"]);
smap_bitmap_1 = new LBitmap(smap_1);
smap_0 = new LBitmapData(dataList["s_map_yes"]);
smap_bitmap_0 = new LBitmap(smap_0);
smap_2 = new LBitmapData(dataList["s_map_my"]);
smap_bitmap_2 = new LBitmap(smap_2);
smap_3 = new LBitmapData(dataList["s_map_npc"]);
smap_bitmap_3 = new LBitmap(smap_3);
if(index == 1){
smap_bitmap_1.x = j*4 - 4;
smap_bitmap_1.y = i*4 - 4;
map_s_L.addChild(smap_bitmap_1);
}
else if(index == 0){
smap_bitmap_0.x = j*4 - 4;
smap_bitmap_0.y = i*4 - 4;
map_s_L.addChild(smap_bitmap_0);
}
else if(index == 2){
smap_bitmap_2.x = j*4 - 4;
smap_bitmap_2.y = i*4 - 4;
map_s_L.addChild(smap_bitmap_2);
}
else if(index == 3){
smap_bitmap_3.x = j*4 - 4;
smap_bitmap_3.y = i*4 - 4;
map_s_L.addChild(smap_bitmap_3);
}
}
}
s_mapdata[x][y] = 0;
}