Skip to content

Instantly share code, notes, and snippets.

@teramotodaiki
Last active July 17, 2016 02:43
Show Gist options
  • Save teramotodaiki/7064ba336a0b8d6544ea0202dd2422bf to your computer and use it in GitHub Desktop.
Save teramotodaiki/7064ba336a0b8d6544ea0202dd2422bf to your computer and use it in GitHub Desktop.
game.preload('enchantjs/monster5.gif');
// ====> ウロボロスクラス
// 文字からフレーム配列を生成する
var anm = function(source) {
Array.prototype._SCALE_ = function(scale) {
var result = [];
for (var i = 0; i < this.length * scale; ++i) {
result.push(this[i / scale | 0]);
}
return result;
};
var result = source.replace(/ /g, '').replace(/\*/g, ')._SCALE_(').split('+').map(function (s) {
return Function('return (' + s + ')')();
}).reduce(function (a, b) {
return a.concat(b);
});
delete Array.prototype._SCALE_;
return result;
};
RPGObject.prototype.animation = function(type, source) {
type = BehaviorTypes[type.charAt(0).toUpperCase() + type.slice(1)];
this.setFrame(type, anm(source));
};
var Ouroboros = enchant.Class(EnemyBase, {
initialize: function(){
EnemyBase.call(this, 80, 80, -24, -36);
this.image = game.assets['enchantjs/monster5.gif'];
this.animation('idle', '[0] * 60 + [1] * 20');
this.animation('attack', '[1, 5, 9, 10, 6, 1] * 3 + null');
this.animation('walk', '[2, 3, 4, 3] * 4');
this.animation('dead', '[1, 5, 7, 4, 0] * 5 + null');
},
onbecomeidle: function () {
// コウモリと9割同じ
// 咬みつくモーションをカッコよくするためにプレイヤーの1マス横を目指す
var target = Hack.player;
var signX = this.direction = Math.sign(target.mapX - this.mapX);
var moveX = 32 * Math.sign(target.mapX - this.mapX - signX);
var moveY = 32 * Math.sign(target.mapY - this.mapY);
this.tl.become('walk').moveBy(moveX, moveY, 30, enchant.Easing.QUAD_EASEINOUT).then(function () {
this.direction = signX = Math.sign(target.mapX - this.mapX);
// プレイヤーと同じ、もしくは1マス横なら攻撃する
if (this.mapX === (target.mapX - signX) && this.mapY === target.mapY) {
this.tl.become('attack').delay(20).become('idle');
Hack.Attack.call(this, this.mapX + signX, this.mapY, this.atk);
}
else this.tl.become('idle');
});
}
});
// <==== ウロボロスクラス
// 遅延評価してRPGObjectを返すジェネレータ(実体はただの関数)
// @return RPGObject like object
var mod = function () {
var ouroboros = new Ouroboros();
ouroboros.color = 'white';
return ouroboros;
};
game.onload = function () {
var map = Hack.maps['map1'];
map.load(); // Load Map; Hack.defaultParentNode == map.scene
var player = Hack.player = new Player();
player.locate(1, 5);
// シルバーウロボロス出現テスト
var test = mod();
test.locate(13, 5);
test.onbecomeidle = function () {
// コウモリと9割同じ
// 咬みつくモーションをカッコよくするためにプレイヤーの1マス横を目指す
var target = Hack.player;
var signX = this.direction = Math.sign(target.mapX - this.mapX);
var moveX = 32 * Math.sign(target.mapX - this.mapX - signX);
var moveY = 32 * Math.sign(target.mapY - this.mapY);
this.tl.become('walk').moveBy(moveX, moveY, 30, enchant.Easing.QUAD_EASEINOUT).then(function () {
this.direction = signX = Math.sign(target.mapX - this.mapX);
// プレイヤーと同じ、もしくは1マス横なら攻撃する
if (this.mapX === (target.mapX - signX) && this.mapY === target.mapY) {
this.tl.become('attack').delay(20).become('idle');
Hack.Attack.call(this, this.mapX + signX, this.mapY, this.atk);
}
else this.tl.become('idle');
});
};
test.onbecomedead = function() {
Hack.gameclear();
};
};
return mod;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment