Skip to content

Instantly share code, notes, and snippets.

@teramotodaiki
Last active June 5, 2016 09:36
Show Gist options
  • Save teramotodaiki/2aec10925b3dfabf45d7a8d55fa494ea to your computer and use it in GitHub Desktop.
Save teramotodaiki/2aec10925b3dfabf45d7a8d55fa494ea to your computer and use it in GitHub Desktop.
// Game start
game.onload = function () {
document.oncontextmenu = function(){
return false;
};
var map = Hack.maps['map1'];
map.load(); // Load Map; Hack.defaultParentNode == map.scene
// ラベル削除
game.addEventListener('enterframe', function event() {
if (Hack.lifeLabel && Hack.scoreLabel) {
Hack.lifeLabel.remove();
//Hack.scoreLabel.remove();
game.removeEventListener('enterframe', event);
}
});
game.on('load', function () {
Hack.player.activeFlag = false;
Hack.player.setInterval(function () {
if (Hack.player.activeFlag === true) {
Hack.player.ink += 1;
}
}, 30);
});
// タイマー
var timeCounter = 0;
var second = 0;
function time(){
timeCounter++;
};
function secondTime(){
time();
if( (timeCounter % 30) === 0 ){
second = timeCounter / 30;
}
};
var every = MapObject();
every.onenterframe = function(){
secondTime();
// 30秒たつとクリア
if(second > 20){
var b1 = MapObject.collection.filter(function(node) {
return node.frame === 135 && node.type === '1';
}).length;
var b = MapObject.collection.filter(function(node) {
return node.frame === 135 && node.type === '2';
}).length;
if(b1 > b){
Hack.gameclear();
}
else{
Hack.gameover();
}
}
};
// ラベル
Hack.scoreLabel.label = 'TIME:';
Hack.scoreLabel.onenterframe = function(){
this.score = 60-second;
};
/*
var init = RPGObject.prototype.initialize;
RPGObject.prototype.initialize = function() {
init.apply(this, arguments);
this.addEventListener('becomedead', function() {
// この中身はすべてのキャラクターの体力が変わったら呼ばれる
this.destroy = null;
});
};
this.addEventListener('hpchange', function() {
if (this.hp <= 0){
this.behavior = BehaviorTypes.Dead = null;
}
});
*/
//----------// HP ゲージはここから //----------//
(function() {
RPGMap.Layer.HP = RPGMap.Layer.Over + 1;
var Surface3D = enchant.Class.create(RPGObject, {
initialize: function(width, height) {
RPGObject.call(this, width, height, 0, 0);
this.image = new enchant.Surface(width, height);
this.collisionFlag = false;
// バグ対策
Object.defineProperties(this, {
color: { value: null },
originalColor: { value: null }
});
}
});
var Gauge = enchant.Class.create(Surface3D, {
initialize: function(target, key, max) {
Surface3D.call(this, target.width, Gauge.globalStatus.defaultHeight);
this.layer = RPGMap.Layer.HP;
this.target = target;
if (max === undefined) {
max = target[key];
}
// ゲージ情報を追加
if (!target._gauges) target._gauges = {};
// 既にゲージがあったら無視
if (target._gauges[key]) return;
var self = this;
target._gauges[key] = {
width: target.width,
height: Gauge.globalStatus.defaultHeight,
index: Object.keys(target._gauges).length,
max: max,
resize: function(width, height) {
this.width = width === null ? this.width : width;
this.height = height === null ? this.height : height;
self.image = new enchant.Surface(this.width, this.height);
return this;
},
colors: {
1.0: 'green'
},
color: function(colors, background) {
this.colors = colors || this.colors;
this.background = background || this.background;
return this;
},
background: '#000',
lineWidth: 2,
strokeStyle: '#000',
context: self.image.context,
border: function(lineWidth, color) {
this.lineWidth = lineWidth;
this.strokeStyle = color;
return this;
}
};
target.on('becomedead', function() {
self.remove();
self.clearEventListener();
});
var sort = function() {
return Object.keys(target._gauges).map(function(key) {
return target._gauges[key];
}).sort(function(a, b) {
return a.index - b.index;
});
};
target.on('added', function() {
this.parentNode.addChild(self);
this.map.layerChangeFlag = true;
});
this.on('render', function() {
var gauge = target._gauges[key];
this.width = gauge.width;
this.height = gauge.height;
this.x = target.x + (target.width - this.width) / 2;
this.y = target.y - Gauge.globalStatus.margin - gauge.height;
var s = sort().slice(0, gauge.index);
if (s.length) {
var a = 0;
var y = s.forEach(function(gauge) {
a += gauge.height + Gauge.globalStatus.space;
});
this.y -= a;
}
// this.scaleX = Math.abs(target.scaleX);
// this.scaleY = target.scaleY;
var context = this.image.context;
context.clearRect(0, 0, this.image.width, this.image.height);
context.fillStyle = gauge.background;
context.fillRect(0, 0, this.width, this.height);
var colors = Object.keys(gauge.colors).sort(function(a, b) {
return parseFloat(b) - parseFloat(a);
});
var hpPer = target[key] / max;
hpPer = Math.clamp(hpPer, 0, 1);
colors.forEach(function(colorKey) {
if (hpPer <= parseFloat(colorKey)) {
context.fillStyle = gauge.colors[colorKey];
}
});
context.fillRect(0, 0, gauge.width / max * target[key], gauge.height);
context.strokeStyle = gauge.strokeStyle;
context.strokeRect(0, 0, this.width, this.height);
});
return this;
}
});
Gauge.bind = function(target, key, max) {
new Gauge(target, key, max);
return target._gauges[key];
};
Gauge.globalStatus = {
margin: 10,
space: 6,
defaultHeight: 10
};
window.Gauge = Gauge;
})();
// ( Keep this line -- ここはけさないでね ) //
// 攻撃ができるかフラグ
var attack = false;
// プレイヤー(騎士)
var player = Hack.player = new Player();
player.locate(3, 5);
player.hp = 3;
player.destroy = function() {};
player.onbecomedead = function () {
this.behavior = BehaviorTypes.Idle;
Hack.player.locate(3, 5);
player.hp = 3;
};
player.ink = 10;
player.inkGauge = Gauge.bind(player, 'ink', 10);
player.d = 0;
game.onabuttondown = function () {
if(player.d <=99){
player.d += 1;
}
if(player.d >=100){
player.d += 0;
}
if(player.ink >=1){
player.ink -= 1;
Hack.player.attack = function() {
var beam1 = new MapObject(135);
beam1.color = 'blue';
beam1.type = '1';
//beam1.scale(0.8,0.8);
var muki = player.forward;
beam1.locate(player.mapX + muki.x, player.mapY + muki.y);
beam1.collisionFlag = false;
beam1.layer = RPGMap.Layer.Under;
Hack.Attack.call(this, player.mapX + muki.x, player.mapY + muki.y, 1);
beam1.onattacked = function () {
this.destroy();
};
};
}
if(player.ink <=0){
player.ink -= 0;
Hack.player.attack = function() {};
}
};
/*
// 攻撃ができるかできないか切り替える関数
Hack.toggleAttack = function() {
// 攻撃できるなら
if (player.ink >= 1) {
// 攻撃できないようにする
}
// 攻撃できないなら
else {
// 攻撃できるようにする
}
}
// 攻撃ができるかフラグを反転する
// フラグ = !フラグ はフラグを反転するテクニック!
attack = !attack;
};
*/
var aoi = false;
player.inkGauge.visible = false;
/*
game.rootScene.addEventListener('touchstart', function(){
if (game.input.a) {
var onattacked = Hack.player.onattacked;
Hack.player.setTimeout(function () {
Hack.player.onattacked = onattacked;
Hack.player.opacity = 1;
}, 100);
Hack.player.onattacked = null;
Hack.player.opacity = 0.5;
if(player.d === 100){
Hack.player.d -= 100;
}
}
if (aoi === false) {
// 青くする
player.cvsRender = function(context) {
context.fillStyle = 'blue';
context.fillRect(-this.offset.x, -this.offset.y, 32, 32);
};
player.inkGauge.visible = true;
// 動かしたり止めたりを変更する関数
Hack.toggleFlag = function () {
// true(動かす)、false(止める)の切り替え
Hack.player.activeFlag = !Hack.player.activeFlag;
};
Hack.toggleFlag();
aoi = true;
} else {
player.inkGauge.visible = false;
player.cvsRender = enchant.Sprite.prototype.cvsRender;
Hack.toggleFlag();
aoi = false;
}
});
*/
/*game.addEventListener('contextmenu', function(){
Hack.log('aaa');*/
// プレイヤー(騎士)
var player2 = new Boy();
player2.locate(27, 5);
player2.hp = 3;
player2.destroy = function(){};
game.onbbuttondown = function () {
player2.direction = 3;
player2.walk();
};
game.oncbuttondown = function () {
player2.direction = 1;
player2.walk();
};
game.ondbuttondown = function () {
player2.direction = 0;
player2.walk();
};
game.onebuttondown = function () {
player2.direction = 2;
player2.walk();
};
game.onfbuttondown = function () {
var beam = new MapObject(135);
beam.color = 'orange';
beam.type = '2';
//beam.scale(0.8,0.8);
var muki = player2.forward;
beam.locate(player2.mapX + muki.x, player2.mapY + muki.y);
beam.collisionFlag = false;
beam.layer = RPGMap.Layer.Under;
Hack.Attack.call(this, player2.mapX + muki.x, player2.mapY + muki.y, 1);
beam.onattacked = function () {
this.destroy();
};
};
player2.onbecomedead = function () {
this.behavior = BehaviorTypes.Idle;
player2.locate(27, 5);
player2.hp = 3;
};
var orengi = false;
/*
game.rootScene.addEventListener('touchstart', function(){
if (orengi === false) {
// オレンジくする
player2.cvsRender = function(context) {
context.fillStyle = 'orange';
context.fillRect(-this.offset.x, -this.offset.y, 32, 32);
};
orengi = true;
}
else {
// ~~~
player2.cvsRender = enchant.Sprite.prototype.cvsRender;
orengi = false;
}
});
*/
var camera = new Camera();
camera.target = player2;
camera.zoom = 1;
Camera.arrange(2, 1);
// 全てのカメラにゲージを追加する
Camera.collection.forEach(function(camera) {
// 何のゲージにするか
var key = 'd';
// ゲージの色
var color = 'blue';
// その他の設定
var m = 10;
var h = 20;
if (!camera.target) camera.target = Hack.player;
var max = camera.target[key];
max = 100;
// ゲージを描画する
camera.onrendered = function(e) {
var context = e.surface.context;
context.fillStyle = '#000';
context.fillRect(m, m, this.width - m * 2, h);
context.fillStyle = color;
context.fillRect(m, m, (this.width - m * 2) / max * this.target[key], h);
context.lineWidth = 2;
context.strokeStyle = '#fff';
context.strokeRect(m, m, this.width - m * 2, h);
};
});
game._element.addEventListener('mousedown', function (event) {
if (event.button === 2) {
// 右クリック
Hack.log('right');
}
});
};
// Before game start
Hack.onload = function () {
//使えるキーを増やす
game.keybind('A'.charCodeAt(0), 'b');
game.keybind('S'.charCodeAt(0), 'c');
game.keybind('D'.charCodeAt(0), 'd');
game.keybind('F'.charCodeAt(0), 'e');
game.keybind('G'.charCodeAt(0), 'f');
game.keybind('H'.charCodeAt(0), 'g');
game.keybind('J'.charCodeAt(0), 'h');
game.keybind('K'.charCodeAt(0), 'i');
game.keybind('L'.charCodeAt(0), 'j');
game.keybind('Z'.charCodeAt(0), 'k');
game.keybind('X'.charCodeAt(0), 'l');
game.keybind('C'.charCodeAt(0), 'm');
MapObject.dictionary = {
clay: 320, clayWall: 340, clayFloor: 323,
stone: 321, stoneWall: 341, stoneFloor: 342,
warp: 324, warpRed: 325,
warpGreen: 326, warpYellow: 327,
magic: 328, usedMagic: 329,
pot: 400, rock: 401, upStair: 402,
box: 420, flower: 421, downStair: 422,
trap: 440, usedTrap: 441, step: 442,
castle: 500, village: 501, caveGate: 502,
tree: 520, table: 521, openedBox: 522,
beam: 540, diamond: 560, sapphire: 561,
ruby: 562, heart: 563, skull: 564,
coin: 565, star: 566, key: 567,
bomb: 580, coldBomb: 581, egg: 582,
poo: 583,
sandySoil: 45, claySoil: 323,
grassland: 322, waterside: 205,
flatGray: 135, squareGray: 93,
};
Hack.maps = {};
// map1
Hack.maps['map1'] = new RPGMap(32, 32, 30, 10);
Hack.maps['map1'].imagePath = 'enchantjs/x2/dotmat.gif';
Hack.maps['map1'].type = 'grassland';
// < Keep this line -- ここはけさないでね > //
};
// Score up/Score down
Hack.onscorechange = function () {
// [ Keep this line -- ここはけさないでね ] //
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment