Created
November 30, 2021 01:22
-
-
Save elleonard/7f16c16e062c238cea8a25fc301246e1 to your computer and use it in GitHub Desktop.
RPGツクールMV 捕食イベントのサンプル
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
(function () { | |
'use strict'; | |
const deadSelfSwitchCh = "A"; | |
const _Game_CharacterBase_updateMove = Game_CharacterBase.prototype.updateMove; | |
Game_CharacterBase.prototype.updateMove = function () { | |
_Game_CharacterBase_updateMove.call(this); | |
/** | |
* 捕食者は停止時に捕食判定(被捕食者のイベント実行)を行う | |
*/ | |
if (!this.isMoving() && this.isEater()) { | |
this.checkEventTriggerEater(); | |
} | |
}; | |
Game_CharacterBase.prototype.isEater = function () { | |
return false; | |
}; | |
/** | |
* 捕食イベント(被捕食者のイベント)の実行判定 | |
*/ | |
Game_CharacterBase.prototype.checkEventTriggerEater = function () { | |
const food = $gameMap.events().find(gameEvent => gameEvent.pos(this.x, this.y) && gameEvent.isAliveFood()); | |
if (food) { | |
food.start(); | |
} | |
}; | |
/** | |
* メモ欄に <eater> タグが記述されていれば捕食者 | |
* @return {boolean} | |
*/ | |
Game_Event.prototype.isEater = function () { | |
return this.event() && this.event().meta.eater; | |
}; | |
/** | |
* メモ欄に <food> タグが記述されていれば被捕食者 | |
* @return {boolean} | |
*/ | |
Game_Event.prototype.isFood = function () { | |
return this.event() && this.event().meta.food; | |
}; | |
/** | |
* 生存している被捕食者であるか | |
* セルフスイッチがOFFの場合、生存している | |
* @return {boolean} | |
*/ | |
Game_Event.prototype.isAliveFood = function () { | |
const key = [$gameMap.mapId(), this.eventId(), deadSelfSwitchCh]; | |
return this.isFood() && !$gameSelfSwitches.value(key); | |
}; | |
/** | |
* 最も近い生存している被捕食者を返す | |
* 誰も生存していない場合はnullを返す | |
* @return {Game_Event|null} | |
*/ | |
Game_Event.prototype.nearestAliveFood = function () { | |
return $gameMap.events().reduce((result, gameEvent) => { | |
if (!gameEvent.isAliveFood()) { | |
return result; | |
} | |
return result && $gameMap.distance(this.x, this.y, result.x, result.y) < $gameMap.distance(this.x, this.y, gameEvent.x, gameEvent.y) | |
? result | |
: gameEvent; | |
}, null); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment