view source
JavaScript
$(function(){
$('#log').jScrollPane();
var max = 0;
$("label").each(function(){
if ($(this).width() > max)
max = $(this).width();
});
$("label").width(max);
$('#button').click(function(){
$(this).hide();
start();
});
});
function start(){
var player1 = new Player($('#aaaa'));
var player2 = new Player($('#bbbb'));
player1.enemy = player2;
player2.enemy = player1;
Player.prototype.turn = Player_turn;
Player.prototype.display = Player_display;
Player.prototype.die = Player_die;
Player.prototype.atack = Player_atack;
Player.prototype.magick = Player_magick;
Player.prototype.healing = Player_healing;
Player.prototype.calcCost = Player_calcCost;
Player.prototype.getTactics = Player_getTactics;
Player.prototype.clone = clone;
Player.prototype.copy = copy;
jQuery.data(player1.dom, "def", { hp: player1.hp, mp: "mpmpmp" });
jQuery.data(player2.dom, "def", { hp: player2.hp, mp: "mpmpmp" });
player1.dom.find('input').attr('readonly','readonly');
player2.dom.find('input').attr('readonly','readonly');
if(player1.sp > player2.sp){
init = player1;
back = player2;
} else {
init = player2;
back = player1;
}
loop(init, back);
}
function loop(player1, player2){
player1.turn(player2);
var t = parseInt($('#t').val());
if(player1.hp < 0){
player1.die();
}if(player2.hp < 0){
player2.die();
} else {
setTimeout(function(){
loop(player2,player1);
},t);
}
}
function Player(dom){
this.dom = dom;
this.name = dom.find('input.name').val();
this.hp = parseInt(dom.find('input.hp').val());
this.mp = parseInt(dom.find('input.mp').val());
this.ap = parseInt(dom.find('input.ap').val());
this.dp = parseInt(dom.find('input.dp').val());
this.bp = parseInt(dom.find('input.bp').val());//brain
this.sp = parseInt(dom.find('input.sp').val());//speed
}
function Player_turn(enemy){
var dice1 = Math.floor(Math.random() * 6) + 1;
var dice2 = Math.floor(Math.random() * 6) + 1;
var d = dice1 + dice2;
$('#dice1').val(dice1);
$('#dice2').val(dice2);
var tac = this.getTactics();
d += tac;
if(d > 10){
this.healing(enemy);
} else if (d < 4) {
this.magick(enemy);
} else {
this.atack(enemy);
}
this.display();
enemy.display();
}
function Player_die(){
this.dom.find('input.hp').val(0);
writeLog(this.name + 'は死んだ');
$('#button').show();
saveData(this,this.enemy);
}
function Player_atack(enemy){
writeLog(this.name + 'の攻撃!');
var rs = (this.sp - enemy.sp) + Math.floor(Math.random() * (20));
msgShow(rs);
if(rs < 2){
writeLog('ミス!');
return;
}
var ra = Math.floor(Math.random() * this.ap / 2);
var damage = parseInt( this.ap - enemy.dp / 2 + ra);
if(damage < 0){
damage = 0;
}
enemy.hp -= damage;
enemy.dom.find('input.hp').val(enemy.hp);
writeLog(enemy.name + 'に'+ damage + 'のダメージ!');
writeLog(enemy.name + 'の残りHP'+ enemy.hp);
}
function Player_magick(enemy){
writeLog(this.name + 'は呪文をとなえた!');
var cost = 2;
var calc_flag = this.calcCost(cost);
if(!calc_flag){
return;
}
var damage = parseInt( this.bp );
enemy.hp -= damage;
writeLog(enemy.name + 'に'+ damage + 'のダメージ!');
}
function Player_healing(enemy){
writeLog(this.name + 'は祈りをささげた!');
var cost = 3;
var calc_flag = this.calcCost(cost);
if(!calc_flag){
return;
}
var recovery = parseInt( this.bp );
this.hp += recovery;
writeLog(recovery + '回復!');
}
function Player_calcCost(cost){
if(this.mp < cost ){
writeLog('……魔力が足りない');
return 0;
}
this.mp -= cost;
return 1;
}
function Player_display(){
this.dom.find('input.hp').val(this.hp);
this.dom.find('input.mp').val(this.mp);
this.dom.find('input.ap').val(this.ap);
this.dom.find('input.dp').val(this.dp);
this.dom.find('input.bp').val(this.bp);
this.dom.find('input.sp').val(this.sp);
}
function Player_getTactics(){
return parseInt(this.dom.find('.tactics').val());
}
function writeLog(msg){
$('#log').prepend('<p>' + msg + '</p>\n');
reinitialiseScrollPane($('#log'));
}
function reinitialiseScrollPane(dom){
dom.jScrollPane();
}
function msgShow(msg){
$('#message').val(msg);
}
function clone(){
var f = function(){};
f.prototype = this;
return new f;
}
function copy(){
return this;
}
function saveData(player1,player2){
player1.hp_max = jQuery.data(player1.dom, 'def').hp;
player2.hp_max = jQuery.data(player2.dom, 'def').hp;
var data = "file=a.txt"
+ "&name=" + player1.name + ':' + player1.hp + '/' + player1.hp_max
+ "&pass=" + player2.name + ':' + player2.hp + '/' + player2.hp_max;
$.ajax({
type: "POST",
url: "/api/save",
data: data,
success: function(msg){
alert( "戦績をセーブしてました(という予定)" );
// alert( "Data Saved: " + msg );
}
});
}
CSS
.hori:after{
content:"";
display:block;
clear:both;
}
.hori .li{
float:left;
}
.li .isbn img{
width:200px;
height:200px;
}
.li{
width:200px;
margin:10px;
}
width:200px;
margin:10px;
}
.li.textarea{
width:400px;
margin:10px;
}
.li label,.li input[type="text"]{
float:left;
display:block;
height:18px;
}
.li label{
text-align:right;
width:96px;
}
.li input{
width:100px;
text-align:right;
}
input.dice{
text-align:center;
font-size:18px;
width:40px;
}
#log{
width:400px;
height:400px;
border:1px dotted #aaa;
border-right-style:none;
}
HTML
ページのソースを表示 : Ctrl+U , DevTools : F12
view-source:https://hi0a.com/demo/-js/js-battle/
ABOUT
hi0a.com 「ひまアプリ」は無料で遊べるミニゲームや便利ツールを公開しています。
プログラミング言語の動作デモやWEBデザイン、ソースコード、フロントエンド等の開発者のための技術を公開しています。
必要な機能の関数をコピペ利用したり勉強に活用できます。
プログラムの動作サンプル結果は画面上部に出力表示されています。
環境:最新のブラウザ GoogleChrome / Windows / Android / iPhone 等の端末で動作確認しています。
画像素材や音素材は半分自作でフリー素材配布サイトも利用しています。LINK参照。
動く便利なものが好きなだけで技術自体に興味はないのでコードは汚いです。
途中放置や実験状態、仕様変更、API廃止等で動かないページもあります。