今回は、マリオのマップスクロール処理を行います。
マップスクロールを行う位置は、画面の真ん中を基準にします。
マップスクロールができる場合にマリオが画面中央に行った場合は、右方向に
マリオがスクロールできるようにし、マップの限界点まで進んだ場合は、
マリオが限界点まで進めるような処理を書いていきます。
解説
マリオのマップスクロール処理をやります。
まず、次のように100×15のマップを作ります。
次に、const.jsを開いて今回使用する定数の定義を行います。
const.js
DRAW_MAX_MAP_Xではスクロールされることを考慮して、
画面外+1描画させるようにします。
// Chapter22 Scroll
// x座標のスクロール開始地点
var SCROLL_POINT_X = (640 / 2) - (MAP_SIZE / 2);
// マップチップの最大描画範囲(画面外+1のマップチップチップも描画させる)
var DRAW_MAX_MAP_X = MAX_MAP_X + 1;
続いて、mario.jsを開いてください。
mario.js
必要なメンバー変数を定義します。
// chapter22スクロール処理
this.maxDrawMapX = DRAW_MAX_MAP_X; // 最大の描画範囲X
this.minDrawMapX = 0; // 最小の描画範囲X
this.mapScrollX = 0; // スクロール量X
this.moveNumX = 0; // 総移動量X
this.scrollEndX = (100 - 10) * MAP_SIZE - HALF_MAP_SIZE; // スクロールの終わりとなる終点X
続いて、マップスクロールさせる関数を定義します。
ここでは、マリオが中央にきたらマリオの動きを止めてマップのみをスクロールさせるように、
マリオを中央に固定して、X軸のマップスクロール位置を管理するmapScrollXと
総移動距離を管理するmoveNumXを増加させます。
その他にマップチップの最大の描画範囲と最小の描画範囲を管理する
maxDrawMapX・minDrawMapXの値を変更させます。
スクロールの終点まで来たら、今度はマリオを移動させるコードを書き、
マップチップの最大・最小描画量をストップさせます。
/**
chapter22 スクロール処理
*/
Mario.prototype.doMapScrollX = function(){
// スクロール基準点を超えたら
if(this.moveNumX >= SCROLL_POINT_X && this.moveNumX < this.scrollEndX){
this.mapScrollX = this.moveNumX - SCROLL_POINT_X; // マップスクロール量
this.posX = SCROLL_POINT_X; // 固定
// マップを描画する範囲をずらす
this.maxDrawMapX = DRAW_MAX_MAP_X + Math.floor(this.mapScrollX / MAP_SIZE); // 最大の描画範囲X
this.minDrawMapX = this.maxDrawMapX - DRAW_MAX_MAP_X; // 最小の描画範囲X
}
else if(this.moveNumX >= this.scrollEndX){
this.mapScrollX = this.scrollEndX - SCROLL_POINT_X; // マップスクロール量X
this.maxDrawMapX = MAX_MAP_X + Math.floor((this.mapScrollX + HALF_MAP_SIZE) / MAP_SIZE); // 最大の描画範囲X
if(this.maxDrawMapX > 100) this.maxDrawMapX = 100;
this.minDrawMapX = this.maxDrawMapX - DRAW_MAX_MAP_X; // 最小の描画範囲X
this.posX = this.moveNumX - this.mapScrollX;
}
}
続いて、総移動距離となるmoveNumXに移動距離を代入します。
Mario.prototype.moveX =
...
this.posX += this.addPosX;
this.moveNumX += this.addPosX;
this.updateMapPositionX(this.posX);
...
}
マップチップの描画処理などを変更するので
main.jsを開いてください。
main.js
move関数の最後で先ほど定義した関数を呼びます。
function move(){
...
// ジャンプ処理
gMario.jumpAction(gUpPush,gMapChip);
// スクロール処理
gMario.doMapScrollX();
}
続いて、マップチップの描画関数を更新します。
x軸の描画のところで、先ほど定義した最小描画位置と最大描画位置を
forループの中に入れます。
そして、マップチップが移動するように描画位置のところにmapScrollX変数を追加します。
function drawMap(map){
// y軸
for(var y = 0;y < MAX_MAP_Y;++y){
// x軸
for(var x = gMario.minDrawMapX;x < gMario.maxDrawMapX;++x){
var indexX = 32 * ((map[y][x] + 16) % 16);
var indexY = 32 * Math.floor(map[y][x] / 16);
g_Ctx.drawImage(gMapTex,indexX,indexY,32,32,(x * 32) - gMario.mapScrollX,y * 32,32,32);
}
}
}
あとは、最大値を100としたマップチップの座標を代入します。
めんどいですが、配列を作成します。
var gMapChip = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,64,0,64,0,64,0,0,0,0,0,64,0,0,0,64,0,0,0,0,0,64,0,0,0,64,0,0,0,0,64,0,0,0,0,64,0,0,0,64,0,0,64,0,64,0,64,0,64,0,0,0,0,0,64,0,0,0,64,0,0,0,0,0,64,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,64,0,0,64,0,0,0,0,64,0,0,0,0,0,64,0,0,0,0,64,64,64,64,64,0,0,0,0,64,0,0,0,0,64,0,0,0,64,0,0,64,0,0,64,0,0,64,0,0,0,0,64,0,0,0,0,0,64,0,0,0,0,64,64,64,64,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,0,0,0,64,0,0,0,64,64,64,64,64,64,64,64,64,0,0,0,64,0,0,64,0,0,0,0,0,64,0,0,0,0,64,0,0,0,64,0,0,64,0,0,0,0,0,64,0,0,0,64,64,64,64,64,64,64,64,64,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,64,64,64,64,64,0,0,0,0,64,0,0,0,0,0,64,0,0,64,0,0,0,0,0,0,0,0,0,64,0,0,64,0,0,0,64,0,0,64,64,64,64,64,0,0,64,64,64,64,64,0,0,64,0,0,0,0,0,64,0,0,64,0,0,0,0,0,0,0,0,0,64,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[112,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,114],
[128,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,130]
];
これで、マップスクロールが完成しましたが、マップスクロール後の当たり判定を行なっていないので、
次回はマップスクロールに対応した当たり判定を行います。
今回修正したソース
mario.js
function Mario(posX,posY){
// 定数
this.NORMAL_JUMP_POWER = 10;
this.DASH_JUMP_POWER = 13;
this.addPosX = 0;
this.addPosY = 0;
this.posX = posX;
this.posY = posY;
// どのタイミングでアニメーションを切り替えるか
this.animCnt = 0;
// 切り出す始点のX座標
this.animX = 0;
this.animOffsetX = 0;
// 方向を切り替えるための変数
this.direction = RIGHT_DIR;
// ダッシュフラグ
this.isDash = false;
// ジャンプ
this.isJump = false;
this.jumpCnt = 0;
this.jumpPower = 0;
// マップチップ座標
this.rightMapX = 0;
this.leftMapX = 0;
this.upMapY = 0;
this.downMapY = 0;
// chapter22 スクロール処理
this.maxDrawMapX = DRAW_MAX_MAP_X; // 最大の描画範囲のX
this.minDrawMapX = 0; // 最小の描画範囲X
this.mapScrollX = 0; // スクロール量X
this.moveNumX = 0; // 総移動量X
this.scrollEndX = (100 - 10) * MAP_SIZE - HALF_MAP_SIZE; // スクロールの終わりとなる終点X
}
/*
描画関数
ctx:context
texture:img class
*/
Mario.prototype.draw = function(ctx,texture){
ctx.drawImage(texture, (this.animX * 32) + this.animOffsetX,this.direction * 32,32,32,this.posX,this.posY,32,32);
}
Mario.prototype.moveX = function(mapChip,moveX){
// 加算量を代入する
this.addPosX = moveX;
// 移動後の加算量を渡すマップチップの状況に応じてx方向の加算量を決める
this.collisionX(mapChip,this.posX + this.addPosX);
// 移動方向変える
if(moveX > 0){
this.direction = RIGHT_DIR;
}
else{
this.direction = LEFT_DIR;
}
this.posX += this.addPosX;
this.moveNumX += this.addPosX;
this.updateMapPositionX(this.posX);
// ダッシュ時のアニメーションは早くする
var cnt = this.isDash ? 2 : 1;
this.animCnt += cnt;
// animation
if(this.animCnt >= 12){
this.animCnt = 0;
// 一定以上に達したらアニメーションを更新する
if(++this.animX > 3){
this.animX = 0;
}
}
}
Mario.prototype.setIsDash = function(isDash){
this.isDash = isDash;
}
/**
ジャンプ動作ボタンが押された時にジャンプフラグを立てる
*/
Mario.prototype.setJumpSettings = function(isDash){
if(!this.isJump){
this.isJump = true;
this.animOffsetX = 128;
var jumpNum = isDash ? this.DASH_JUMP_POWER : this.NORMAL_JUMP_POWER;
this.jumpPower = jumpNum;
}
}
/**
ジャンプ動作
isPush : 対象のキーが押されているか
*/
Mario.prototype.jumpAction = function(isPush,mapChip){
this.addPosY = this.jumpPower;
this.collisionY(mapChip,this.posY - this.addPosY);
this.posY -= this.addPosY;
// 落下中はジャンプさせないようにする
if(this.addPosY < 0){
this.isJump = true;
}
// 落下量調整
if(this.jumpPower > -MAX_GRAVITY){
// 上昇中かつキーが押されている場合は下降量を減らす
if(isPush && this.jumpPower > 0){
this.jumpPower -= (GRAVITY_POWER - (GRAVITY_POWER / 2));
}else{
this.jumpPower -= GRAVITY_POWER;
}
}
console.log("jumpPower = " + this.jumpPower);
}
/**
x軸方向のマップチップ座標の更新
posX : マップチップ更新対象となるx座標
*/
Mario.prototype.updateMapPositionX = function(posX){
// x座標
this.leftMapX = Math.floor(posX / MAP_SIZE);
this.rightMapX = Math.floor((posX + MAP_SIZE - 1) / MAP_SIZE);
// 配列外チェック
if(this.leftMapX >= MAX_MAP_X){
this.leftMapX = MAX_MAP_X;
}
if(this.leftMapX < 0){
this.leftMapX = 0;
}
if(this.rightMapX >= MAX_MAP_X){
this.rightMapX = MAX_MAP_X;
}
if(this.rightMapX < 0){
this.rightMapX = 0;
}
}
/**
chapter20
Y軸方向のマップチップの更新
*/
Mario.prototype.updateMapPositionY = function(posY){
// y
this.upMapY = Math.floor(posY / MAP_SIZE);
this.downMapY = Math.floor((posY + MAP_SIZE - 1) / MAP_SIZE);
// 配列外チェック
if(this.upMapY >= MAX_MAP_Y - 1){
this.upMapY = MAX_MAP_Y - 1;
}
if(this.upMapY < 0){
this.upMapY = 0;
}
if(this.downMapY >= MAX_MAP_Y - 1){
this.downMapY = MAX_MAP_Y - 1;
}
if(this.downMapY < 0){
this.downMapY = 0;
}
}
/**
chapter17
マップチップ座標を更新する
*/
Mario.prototype.updateMapPosition = function(){
this.updateMapPositionX(this.posX);
this.updateMapPositionY(this.posY);
// log
console.log("rightMapX = " + this.rightMapX + ", leftMapX = " + this.leftMapX + ",upMapY = " + this.upMapY + ",this.downMapY = "
+ this.downMapY);
console.log("mario posX = " + this.posX + ",mario posY = " + this.posY);
}
/**
chapter19
オブジェクトとの当たり判定X
*/
Mario.prototype.collisionX = function(map,posX){
this.updateMapPositionX(posX);
// マリオの右側
if(isObjectMap(map[this.downMapY][this.rightMapX]) || isObjectMap(map[this.upMapY][this.rightMapX])){
// (加算される前の)中心点からの距離を取る
var vecX = Math.abs((this.posX + HALF_MAP_SIZE) - ((this.rightMapX * MAP_SIZE) + HALF_MAP_SIZE));
this.addPosX = Math.abs(MAP_SIZE - vecX);
}
// マリオの左側
else if(isObjectMap(map[this.downMapY][this.leftMapX]) || isObjectMap(map[this.upMapY][this.leftMapX])){
// (加算される前の)中心点からの距離を取る
var vecX = Math.abs((this.posX + HALF_MAP_SIZE) - ((this.leftMapX * MAP_SIZE) + HALF_MAP_SIZE));
this.addPosX = -Math.abs(MAP_SIZE - vecX);
}
}
/**
chapter20
オブジェクトとの当たり判定Y
*/
Mario.prototype.collisionY = function(map,posY){
this.updateMapPositionY(posY);
// マリオの上側に当たった場合
if(isObjectMap(map[this.upMapY][this.rightMapX]) || isObjectMap(map[this.upMapY][this.leftMapX])){
// (加算される前の)中心点からの距離をみる
var vecY = Math.abs((this.posY + HALF_MAP_SIZE) - ((this.upMapY * MAP_SIZE) + HALF_MAP_SIZE));
// Yの加算量調整
this.addPosY = Math.abs(MAP_SIZE - vecY);
// 落下させる
this.jumpPower = 0;
}
// マリオの下側
else if(isObjectMap(map[this.downMapY][this.rightMapX]) || isObjectMap(map[this.downMapY][this.leftMapX])){
// (加算される前の)中心点からの距離を見る
var vecY = Math.abs((this.posY + HALF_MAP_SIZE) - ((this.downMapY * MAP_SIZE) + HALF_MAP_SIZE));
// Yの加算量調整
this.addPosY = Math.abs(MAP_SIZE - vecY);
// 地面についた
this.posY += this.addPosY;
this.addPosY = 0;
this.jumpPower = 0;
this.isJump = false;
// リセットアニメーション
this.animOffsetX = 0;
}
}
/**
chapter22 スクロール処理
*/
Mario.prototype.doMapScrollX = function(){
// スクロール基準点を超えたら
if(this.moveNumX >= SCROLL_POINT_X && this.moveNumX < this.scrollEndX){
this.mapScrollX = this.moveNumX - SCROLL_POINT_X; // マップスクロール量
this.posX = SCROLL_POINT_X; // 固定
// マップを描画する範囲をずらす
this.maxDrawMapX = DRAW_MAX_MAP_X + Math.floor(this.mapScrollX / MAP_SIZE); // 最大の描画範囲X
this.minDrawMapX = this.maxDrawMapX - DRAW_MAX_MAP_X; // 最小の描画範囲X
}
else if(this.moveNumX >= this.scrollEndX){
this.mapScrollX = this.scrollEndX - SCROLL_POINT_X; // マップスクロール量X
this.maxDrawMapX = MAX_MAP_X + Math.floor((this.mapScrollX + HALF_MAP_SIZE) / MAP_SIZE); // 最大の描画範囲X
if(this.maxDrawMapX > 100) this.maxDrawMapX = 100;
this.minDrawMapX = this.maxDrawMapX - DRAW_MAX_MAP_X; // 最小の描画範囲X
this.posX = this.moveNumX - this.mapScrollX;
}
}
const.js
var RIGHT_DIR = 1;
var LEFT_DIR = 0;
var MAX_MAP_X = 20;
var MAX_MAP_Y = 15;
var DASH_SPEED = 5;
var NORMAL_SPPED = 3;
// 重力値
var GRAVITY_POWER = 1;
// 最大の重力量
var MAX_GRAVITY = 8;
var MAP_SIZE = 32;
var HALF_MAP_SIZE = 16;
// chapter22 scroll
// x座標のスクロール開始地点
var SCROLL_POINT_X = (640 / 2) - (MAP_SIZE / 2);
// マップチップの最大描画範囲(画面外+1のマップチップも描画させる)
var DRAW_MAX_MAP_X = MAX_MAP_X + 1;
main.js
var g_Canvas;
var g_Ctx;
// texture
var gMarioTex;
var gMapTex;
var gMario;
// key
var gSpacePush = false; // space
var gLeftPush = false; // left
var gRightPush = false; // right
var gUpPush = false; // up
var gDownPush = false; // down
// keyの定義
var SPACE_KEY = 32;
var LEFT_KEY = 37;
var RIGHT_KEY = 39;
var UP_KEY = 38;
var DOWN_KEY = 40;
var gMapChip = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,64,64,0,0,0,0,0,0,64,0,64,0,0,0,0,0,0,64,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,64,0,64,0,64,0,0,0,0,0,64,0,0,0,64,0,0,0,0,0,64,0,0,0,64,0,0,0,0,64,0,0,0,0,64,0,0,0,64,0,0,64,0,64,0,64,0,64,0,0,0,0,0,64,0,0,0,64,0,0,0,0,0,64,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,64,0,0,64,0,0,0,0,64,0,0,0,0,0,64,0,0,0,0,64,64,64,64,64,0,0,0,0,64,0,0,0,0,64,0,0,0,64,0,0,64,0,0,64,0,0,64,0,0,0,0,64,0,0,0,0,0,64,0,0,0,0,64,64,64,64,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,0,0,0,64,0,0,0,64,64,64,64,64,64,64,64,64,0,0,0,64,0,0,64,0,0,0,0,0,64,0,0,0,0,64,0,0,0,64,0,0,64,0,0,0,0,0,64,0,0,0,64,64,64,64,64,64,64,64,64,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,64,64,64,64,64,0,0,0,0,64,0,0,0,0,0,64,0,0,64,0,0,0,0,0,0,0,0,0,64,0,0,64,0,0,0,64,0,0,64,64,64,64,64,0,0,64,64,64,64,64,0,0,64,0,0,0,0,0,64,0,0,64,0,0,0,0,0,0,0,0,0,64,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[112,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,114],
[128,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,129,130]
];
/**
onload
最初に呼び出される関数
*/
onload = function () {
// キャンバスに代入
g_Canvas = document.getElementById('id_canvas');
// cavasに対応していない
if (!g_Canvas || !g_Canvas.getContext) {
alert("html5に対応していないので、実行できません");
return false;
}
g_Ctx = g_Canvas.getContext('2d'); // ctx
loadTexture();
// mario
gMario = new Mario(0,384);
// キーの登録
window.addEventListener('keydown', keyDown, true);
window.addEventListener('keyup', keyUp, true);
requestNextAnimationFrame(animate); // loopスタート
};
/*
テクスチャのロード
*/
function loadTexture(){
gMarioTex = new Image();
gMarioTex.src = "resource/main.png";
gMapTex = new Image();
gMapTex.src = "resource/map512.png";
}
function animate(now) {
move();
// 描画
Draw();
requestNextAnimationFrame(animate);
}
/*
60fps毎に処理を実行
*/
window.requestNextAnimationFrame =
(function () {
var originalWebkitRequestAnimationFrame = undefined,
wrapper = undefined,
callback = undefined,
geckoVersion = 0,
userAgent = navigator.userAgent,
index = 0,
self = this;
// Workaround for Chrome 10 bug where Chrome
// does not pass the time to the animation function
if (window.webkitRequestAnimationFrame) {
// Define the wrapper
wrapper = function (time) {
if (time === undefined) {
time = +new Date();
}
self.callback(time);
};
// Make the switch
originalWebkitRequestAnimationFrame = window.webkitRequestAnimationFrame;
window.webkitRequestAnimationFrame = function (callback, element) {
self.callback = callback;
// Browser calls the wrapper and wrapper calls the callback
originalWebkitRequestAnimationFrame(wrapper, element);
}
}
// Workaround for Gecko 2.0, which has a bug in
// mozRequestAnimationFrame() that restricts animations
// to 30-40 fps.
if (window.mozRequestAnimationFrame) {
// Check the Gecko version. Gecko is used by browsers
// other than Firefox. Gecko 2.0 corresponds to
// Firefox 4.0.
index = userAgent.indexOf('rv:');
if (userAgent.indexOf('Gecko') != -1) {
geckoVersion = userAgent.substr(index + 3, 3);
if (geckoVersion === '2.0') {
// Forces the return statement to fall through
// to the setTimeout() function.
window.mozRequestAnimationFrame = undefined;
}
}
}
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback, element) {
var start,
finish;
window.setTimeout( function () {
start = +new Date();
callback(start);
finish = +new Date();
self.timeout = 1000 / 60 - (finish - start);
}, self.timeout);
};
}
)
();
/*
Draw
描画
*/
function Draw(){
drawMap(gMapChip);
//g_Ctx.drawImage(gMarioTex,0,0,24,24,gMarioPosX,gMarioPosY,24,24);
gMario.draw(g_Ctx,gMarioTex);
}
/**
マップチップを描画
map:対象のマップチップ配列
*/
function drawMap(map){
// y軸
for(var y = 0;y < MAX_MAP_Y;++y){
// x軸
for(var x = gMario.minDrawMapX;x < gMario.maxDrawMapX;++x){
var indexX = 32 * ((map[y][x] + 16) % 16);
var indexY = 32 * Math.floor(map[y][x] / 16);
g_Ctx.drawImage(gMapTex,indexX,indexY,32,32,(x * 32) - gMario.mapScrollX,y * 32,32,32);
}
}
}
function move(){
// マップ座標の更新
gMario.updateMapPosition();
// 左キーが押されている状態
if(gLeftPush){
if(gSpacePush){
gMario.setIsDash(true);
gMario.moveX(gMapChip,-DASH_SPEED);
}
else{
gMario.setIsDash(false);
gMario.moveX(gMapChip,-NORMAL_SPPED);
}
}
// →キーが押されている状態
if(gRightPush){
if(gSpacePush){
gMario.setIsDash(true);
gMario.moveX(gMapChip,DASH_SPEED);
}
else{
gMario.setIsDash(false);
gMario.moveX(gMapChip,NORMAL_SPPED);
}
}
// ジャンプ動作
if(gUpPush){
// ジャンプ設定をオンにする
gMario.setJumpSettings(gSpacePush);
}
// ジャンプ処理
gMario.jumpAction(gUpPush,gMapChip);
// スクロール処理
gMario.doMapScrollX();
}
/*
キーを押した時の操作
*/
function keyDown(event) {
var code = event.keyCode; // どのキーが押されたか
switch(code) {
// スペースキー
case SPACE_KEY:
// スクロールさせないため
event.returnValue = false; // ie
event.preventDefault(); // firefox
gSpacePush = true;
break;
// ←キー
case LEFT_KEY:
gLeftPush = true;
break;
// →キー
case RIGHT_KEY:
gRightPush = true;
break;
// ↑キー
case UP_KEY:
event.returnValue = false; // ie
event.preventDefault(); // firefox
gUpPush = true;
break;
// ↓キー
case DOWN_KEY:
event.returnValue = false; // ie
event.preventDefault(); // firefox
gDownPush = true;
break;
}
}
/*
キーを離した時のイベント
*/
function keyUp(event) {
code = event.keyCode;
switch(code) {
// スペースキー
case SPACE_KEY:
gSpacePush = false;
break;
// ←キー
case LEFT_KEY:
gLeftPush = false;
break;
case RIGHT_KEY:
// →キー
gRightPush = false;
break;
case UP_KEY:
// ↑キー
gUpPush = false;
break;
case DOWN_KEY:
// ↓キー
gDownPush = false;
break;
}
}