Created
March 11, 2018 15:08
-
-
Save rinne-grid/b644bdc426304be3a5de8bd730ba8132 to your computer and use it in GitHub Desktop.
画像検索結果から、取得した画像をエネミーとして利用する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This program is released under the MIT license. | |
// http://opensource.org/licenses/mit-license.php | |
/*:ja | |
* | |
* @plugindesc 画像検索結果から、取得した画像をエネミーとして利用する | |
* @author rinne_grid | |
* @help | |
* プラグインコマンド | |
* SearchGame battle 10 1 | |
* | |
* - battle: | |
* - 常にbattleを指定する。 | |
* - 10: | |
* - 10番目のアクターをキーワード文字列として利用する(検索キーワードやエネミーの名前) | |
* - 1: | |
* - 検索結果から何枚の画像を取得するかを指定する。基本的に1を指定。 | |
* | |
* | |
* 利用例 | |
* - 名前入力の処理 (対象のアクターは10番目を選択) | |
* - プラグインコマンドで SearchGame battle 10 1 を指定 | |
* - 戦闘の処理で、適当なグループを指定する | |
* | |
* | |
* @param RequestURL | |
* @desc リクエスト送信先のURL。検索結果を保存したローカルサーバーを指定する | |
* @default http://localhost:8000/ | |
* */ | |
(function() { | |
var _Game_Interpreter_pluginCommand = | |
Game_Interpreter.prototype.pluginCommand; | |
Game_Interpreter.prototype.pluginCommand = function(command, args) { | |
_Game_Interpreter_pluginCommand.call(this, command, args); | |
if(command === 'SearchGame') { | |
switch(args[0]) { | |
case 'battle': | |
Scene_SearchGame.prototype.setParameters.call(this, args[1], args[2]); | |
SceneManager.push(Scene_SearchGame); | |
break; | |
} | |
} | |
}; | |
function Scene_SearchGame() { | |
this.initialize.apply(this, arguments); | |
} | |
Scene_SearchGame.prototype = Object.create(Scene_Base.prototype); | |
Scene_SearchGame.prototype.constructor = Scene_SearchGame; | |
Scene_SearchGame.prototype.initialize = function() { | |
Scene_Base.prototype.initialize.call(this); | |
this._searchBmpList = []; | |
this.getImageUrlList(); | |
}; | |
Scene_SearchGame.prototype.update = function() { | |
Scene_Base.prototype.update.call(this); | |
var scene_serachGameScope = this; | |
if(this.isLoad() && this._searchBmpList.length === 0) { | |
this.loadSearchBitmapData(); | |
Sprite_Enemy.prototype.loadBitmap = function(name, hue) { | |
this.bitmap = scene_serachGameScope._searchBmpList[0]; | |
}; | |
Game_Troop.prototype.setup = function(troopId) { | |
this.clear(); | |
this._troopId = troopId; | |
this._enemies = []; | |
this.troop().members.forEach(function(member) { | |
if ($dataEnemies[member.enemyId]) { | |
var enemyId = member.enemyId; | |
var x = member.x; | |
var y = member.y; | |
var enemy = new Game_Enemy(enemyId, x, y); | |
enemy.enemy().battlerName = Scene_SearchGame.keyword; | |
enemy.enemy().name = Scene_SearchGame.keyword; | |
if (member.hidden) { | |
enemy.hide(); | |
} | |
this._enemies.push(enemy); | |
} | |
}, this); | |
this.makeUniqueNames(); | |
}; | |
Spriteset_Battle.prototype.createEnemies = function() { | |
var enemies = $gameTroop.members(); | |
var sprites = []; | |
for (var i = 0; i < enemies.length; i++) { | |
sprites[i] = new Sprite_Enemy(enemies[i]); | |
sprites[i].scale.x = 0.5; | |
sprites[i].scale.y = 0.5; | |
} | |
sprites.sort(this.compareEnemySprite.bind(this)); | |
for (var j = 0; j < sprites.length; j++) { | |
this._battleField.addChild(sprites[j]); | |
} | |
this._enemySprites = sprites; | |
}; | |
SceneManager.pop(); | |
} | |
}; | |
Scene_SearchGame.prototype.setParameters = function(keyword, num) { | |
Scene_SearchGame.keyword = $gameActors.actor(keyword).name(); | |
Scene_SearchGame.num = num; | |
}; | |
Scene_SearchGame.prototype.loaded = function(urlList) { | |
this._searchImageLoaded = true; | |
this._urlList = urlList; | |
}; | |
Scene_SearchGame.prototype.isLoad = function() { | |
return this._searchImageLoaded; | |
}; | |
Scene_SearchGame.prototype.loadSearchBitmapData = function() { | |
this._searchBmpList = []; | |
for(var i = 0; i < this._urlList.length; i++) { | |
var bmp = ImageManager.loadNormalBitmap(this._urlList[i]["url"]); | |
if(bmp !== null) { | |
this._searchBmpList.push(bmp); | |
break; | |
} | |
} | |
}; | |
Scene_SearchGame.prototype.getImageUrlList = function() { | |
var xhr = new XMLHttpRequest(); | |
xhr.withCredentials = true; | |
var keyword = Scene_SearchGame.keyword || "test"; | |
var cnt = Scene_SearchGame.num || 1; | |
// Google画像検索のURLはたぶんこれ。しかし試したらだめ | |
// var url = "https://www.google.co.jp/search?q="+keyword+"&tbm=isch"; | |
var parameters = PluginManager.parameters('SearchGame'); | |
var url = String(parameters['RequestURL'] || "http://localhost:8000/"); | |
switch(keyword) { | |
case "いらすとやねこ": | |
url += "neko.html"; | |
break; | |
case "くりーぱー": | |
url += "creeper.html"; | |
break; | |
case "りんねぐりっど": | |
url += "rinne_grid.html"; | |
break; | |
case "ぴぴみ": | |
url += "pipimi.html"; | |
break; | |
case "ぽぷこ": | |
url += "popuko.html"; | |
break; | |
case "ポプテピピック": | |
url += "poputepi.html"; | |
break; | |
} | |
// console.log(url); | |
var scene_searchGameScope = this; | |
xhr.open('GET', url); | |
this._searchImageLoaded = false; | |
xhr.onload = function() { | |
if(xhr.status < 400) { | |
var response = xhr.responseText; | |
var regExpImageInfo = new RegExp('<div.* class="rg_meta.*">.*<\/div>', "g"); | |
var imageInfoList = regExpImageInfo.exec(response); | |
var rgMetaInfo = imageInfoList[0].split("rg_meta"); | |
var urlList = []; | |
var regExpUrlInfo = new RegExp('"ou":(.+),"ow"'); | |
var regExpHeight = new RegExp('"oh"*\:.[0-9]+'); | |
var regExpWidth = new RegExp('"ow"*\:.[0-9]+'); | |
// 0番目は、1番目は<div>が入ってくるため使えない | |
for(var i = 2; i < 2+Scene_SearchGame.num; i++) { | |
var urlRegResults = regExpUrlInfo.exec(rgMetaInfo[i]); | |
var width = 0; | |
var height = 0; | |
width = regExpWidth.exec(rgMetaInfo[i])[0].split(":"); | |
height = regExpHeight.exec(rgMetaInfo[i])[0].split(":"); | |
var url = urlRegResults[1].replace(/"/g, ''); | |
if(width !== 0 && height !== 0) { | |
var obj = { | |
url: url, | |
width: width, | |
height: height | |
}; | |
urlList.push(obj); | |
} | |
Scene_SearchGame.prototype.loaded.call(scene_searchGameScope, urlList); | |
} | |
} | |
}; | |
xhr.onerror = function() { | |
DataManager._errorUrl = url; | |
}; | |
xhr.send(); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment