/*:
* @target MZ
* @plugindesc Zetu's Point N Click Adventure
* @author Zetu/DrJosephJorgen
* @help NOTE GUIDE:
* - Database/Items/note
*
* - Start/End Notes:
* <zetu pnc>
* </zetu pnc>
* - Include Item in Point & Click Item Menu
* SHOW IN MENU {TYPE}
* TYPE = ICON (Uses the item db icon, 32x32 by default)
* TYPE = picture (Uses name of picture)
* - Common Event when inspecting item.
* SELF EVENT {commoneventid}
*/
/*
* @param InventoryItemWidth
* @text Item Width
* @type number
* @default 32
*
* @param InventoryItemHeight
* @text Item Height
* @type number
* @default 32
*
* -------------------------------------------
*
* @command SetupSubscene
* @text P&C: Setup Subscene
* @desc Setup the Subscene for examining. With default window sizes, Max should be 792 x 376.
*
* @arg Width
* @text Subscene Width
* @type number
* @default 792
*
* @arg Height
* @text Subscene Height
* @type number
* @default 376
*
* @arg Items
* @text Subscene Items
* @type struct<SubsceneItem>[]
*
*/
/*~struct~SubsceneItem:
*
* @param TileId
* @text Tile Id
* @type number
* @desc Leave 0 to disable.
* @default 0
*
* @param IconId
* @text Icon Id
* @type number
* @desc Leave 0 to disable.
* @default 0
*
* @param Picture
* @text Picture
* @type file
* @desc Image of Item (if not tile/icon)
* @dir pictures/pictures
*
* @param PictureFrames
* @text Picture Frames
* @type number
* @desc The number of available frames on image
*
* @param PictureFrameIndex
* @text Picture Frame Index Variable
* @type variable
* @desc If Picture Frames > 1, use this for Index
* @default 0
*
* @param X
* @text X Position
* @type number
* @desc Pixels from right
* @default 0
*
* @param Y
* @text Y Position
* @type number
* @desc Pixels from right
* @default 0
*
* @param Zoom
* @text Zoom Level
* @type number
* @decimals 2
* @desc How far to zoom the item
* @default 1.00
*
* @param Switch
* @text Switch Required
* @type switch
* @desc If switch is needed to be ON/OFF
*
* @param SwitchState
* @text Switch State
* @type boolean
* @on ON
* @off OFF
* @default true
*
* @param CommonEventId
* @text Common Event
* @type common_event
*
*/
console.log(0);
Zetu.setupHeader("Zetu_MZ_PointNClick", "1.0")
.setNamespace("PointNClick")
.requires("Zetu_MZ_Core", "1.0")
.optional("Zetu_MZ_Cursor", "1.0");
Zetu.command("SetupSubscene", (args) => {
Game_PointNClickObject.loadCommandObject(args);
SceneManager.push(Scene_PointNClick);
});
Zetu.note.namespace("pnc")
.item(/SHOW IN MENU (.+)/i, (item, type) => {
if (type.toUpperCase() == "ICON") {
item.pncUseIcon = true;
} else if (type.length > 0) {
item.pncUsePicture = type;
}
});
// #region DataManager
DataManager.destroyPointNClick = function () {
if (Object.keys($gamePointNClick).length == 0) return;
$gamePointNClick.Items.forEach((item) => {
if (item.bitmap) item.bitmap.destroy();
});
}
// #endregion
// #region Game_Map
const zetu_pnc_Game_Map_setupEvents = Game_Map.prototype.setupEvents;
Game_Map.prototype.setupEvents = function () {
zetu_pnc_Game_Map_setupEvents.call(this);
Game_PointNClickObject.preloadCache(this._events, this._commonEvents);
}
// #endregion
// #region Scene_PointNClick
function Scene_PointNClick() {
this.initialize(...arguments);
}
Scene_PointNClick.prototype = Object.create(Scene_Message.prototype);
Scene_PointNClick.prototype.constructor = Scene_PointNClick;
Scene_PointNClick.prototype.initialize = function (){
Scene_Message.prototype.initialize.call(this);
this._interpreter = new Game_Interpreter();
this._interpreter.setWaitMode("");
}
Scene_PointNClick.prototype.create = function() {
Scene_Message.prototype.create.call(this);
this.createBackground();
this.createWindowLayer();
this.createAllWindows();
};
Scene_PointNClick.prototype.createAllWindows = function() {
this.createInteractable();
this.createInventory();
Scene_Message.prototype.createAllWindows.call(this);
};
Scene_PointNClick.prototype.start = function() {
Scene_Message.prototype.start.call(this);
};
Scene_PointNClick.prototype.update = function() {
this.checkReturnScene();
this.updateInterpreter();
this.updateItems();
Scene_Message.prototype.update.call(this);
};
Scene_PointNClick.prototype.updateInterpreter = function() {
this._interpreter.update();
this._interactableWindow.interpreterRunning = this._interpreter.isRunning()
if (this._interpreter.isRunning()) return;
if ($gameTemp.isCommonEventReserved()) {
var ce = $gameTemp.retrieveCommonEvent();
this._interpreter.setup(ce.list);
}
};
Scene_PointNClick.prototype.updateWindows = function() {
this._interactableWindow.update();
this._inventoryWindow.update();
};
Scene_PointNClick.prototype.topMenuHeight = function() {
// TO DO: Inventory Item Height
return 32 + $gameSystem.windowPadding() * 2;
}
Scene_PointNClick.prototype.createBackground = function() {
this._backgroundFilter = new PIXI.filters.BlurFilter();
this._backgroundSprite = new Sprite();
this._backgroundSprite.bitmap = SceneManager.backgroundBitmap();
this._backgroundSprite.filters = [this._backgroundFilter];
this.addChild(this._backgroundSprite);
this.setBackgroundOpacity(192);
};
Scene_PointNClick.prototype.createInteractable = function () {
let width = $gamePointNClick.Width;
width += 2 * $gameSystem.windowPadding();
let height = $gamePointNClick.Height;
height += 2 * $gameSystem.windowPadding();
let x = (Graphics.width - width) / 2;
let y = (Graphics.height - height) / 2;
const rect = new Rectangle(x,y,width,height);
this._interactableWindow = new Window_PointNClick(rect);
this.addWindow(this._interactableWindow);
}
Scene_PointNClick.prototype.createInventory = function () {
var width = Graphics.boxWidth;
var height = Zetu.PointNClick.InventoryItemHeight;
height += 2 * $gameSystem.windowPadding();
height += Window_ItemPointNClick.prototype.rowSpacing();
const wx = (Graphics.boxWidth - width) / 2;
const wy = 0;
const rect = new Rectangle(wx,wy,width,height);
this._inventoryWindow = new Window_ItemPointNClick(rect);
this.addWindow(this._inventoryWindow);
}
Scene_PointNClick.prototype.setBackgroundOpacity = function(opacity) {
this._backgroundSprite.opacity = opacity;
};
Scene_PointNClick.prototype.checkReturnScene = function () {
if (this._interpreter.isRunning()) return;
if (Input.isRepeated("cancel")) {
//SoundManager.playCancel();
SceneManager.pop();
}
};
Scene_PointNClick.prototype.updateItems = function () {
var needsRefresh = false;
$gamePointNClick.Items.forEach((item) => {
if (item.isChanged()) needsRefresh = true;
item.update();
});
if (needsRefresh) {
this._inventoryWindow.refresh();
this._interactableWindow.refresh();
}
}
// #endregion
// #region Window_ItemPointNClick
function Window_ItemPointNClick() {
this.initialize(...arguments);
}
Window_ItemPointNClick.prototype = Object.create(Window_ItemList.prototype);
Window_ItemPointNClick.prototype.constructor = Window_ItemPointNClick;
Window_ItemPointNClick.prototype.initialize = function(rect) {
Window_ItemList.prototype.initialize.call(this, rect);
this.refresh();
};
Window_ItemPointNClick.prototype.update = function () {
if (this.requiresRefresh()) this.refresh();
Window_ItemList.prototype.update.call(this);
}
Window_ItemPointNClick.prototype.requiresRefresh = function() {
// if an Item is removed:
if (this._data.filter((item => !this.includes(item)))) return true;
// if an Item is added:
var data = $gameParty.allItems().filter(item => this.includes(item));
if (this._data.length !== data.length) return true;
return false;
}
Window_ItemPointNClick.prototype.maxPageRows = () => 1;
Window_ItemPointNClick.prototype.includes = function(item) {
if (!item) return false;
if (item.pncUseIcon) return true;
if (item.pncUsePicture) return true;
return false;
};
Window_ItemPointNClick.prototype.isEnabled = function(/*item*/) {
return true;
};
Window_ItemPointNClick.prototype.makeItemList = function() {
this._data = $gameParty.allItems().filter(item => this.includes(item));
};
Window_ItemPointNClick.prototype.drawItem = function(index) {
const item = this.itemAt(index);
if (item) {
const maxCols = this.maxCols();
const itemWidth = this.itemWidth();
const itemHeight = this.itemHeight();
const colSpacing = this.colSpacing();
const rowSpacing = this.rowSpacing();
//const rect = this.itemLineRect(index);
// var width = Zetu.PointNClick.InventoryItemWidth;
// var height = Zetu.PointNClick.InventoryItemHeight;
// var x = (index * width);
// var y = 0;
//const rect = new Rectangle(x, y, width, height);
const rect = this.itemRect(index);
if (item.pncUseIcon) {
this.drawIcon(item.iconIndex, rect.x, rect.y)
}
// this.changePaintOpacity(this.isEnabled(item));
// this.drawItemName(item, rect.x, rect.y, rect.width - numberWidth);
// this.drawItemNumber(item, rect.x, rect.y, rect.width);
// this.changePaintOpacity(1);
}
}
Window_ItemPointNClick.prototype.itemWidth = () => Zetu.PointNClick.InventoryItemWidth;
Window_ItemPointNClick.prototype.itemHeight = () => Zetu.PointNClick.InventoryItemHeight;
Window_ItemPointNClick.prototype.lineHeight = () => Zetu.PointNClick.InventoryItemHeight;
Window_ItemPointNClick.prototype.colSpacing = () => 4;
Window_ItemPointNClick.prototype.rowSpacing = () => 4;
Window_ItemPointNClick.prototype.maxCols = function () {
return Math.floor(this.innerWidth / (this.itemWidth() + this.colSpacing()));
}
// #endregion
// #region Window_PointNClick
function Window_PointNClick() {
this.initialize(...arguments);
}
Window_PointNClick.prototype = Object.create(Window_Base.prototype);
Window_PointNClick.prototype.constructor = Window_PointNClick;
Window_PointNClick.prototype.initialize = function(rect) {
this.interpreterRunning = false;
Window_Base.prototype.initialize.call(this, rect);
this.refresh();
}
Window_PointNClick.prototype.itemHeight = function() {
return this.innerHeight;
};
Window_PointNClick.prototype.update = function () {
this.updateInput();
}
Window_PointNClick.prototype.updateInput = function () {
if (this.interpreterRunning) return;
if (TouchInput.isTriggered()) {
let x = this.touchX();
let y = this.touchY();
if (!x || !y) return;
for (var i = $gamePointNClick.Items.length - 1; i >= 0; i--) {
let item = $gamePointNClick.Items[i];
if (item.commonEventId <= 0) continue;
if (item.checkCollision(x, y)) {
$gameTemp.reserveCommonEvent(item.commonEventId);
return;
}
}
}
}
Window_PointNClick.prototype.createContents = function () {
const width = this.contentsWidth();
const height = this.contentsHeight();
this.destroyContents();
this.contents = new Bitmap(width, height);
//this.contentsBack = new Bitmap(width, height);
this.resetFontSettings();
this.drawItems();
}
Window_PointNClick.prototype.drawItems = function () {
$gamePointNClick.Items.forEach((item) => {
item.refresh();
if (item.visible)
this.contents.blt(item.bitmap, 0 ,0, item.bitmap.width, item.bitmap.height, item.x, item.y);
});
}
Window_PointNClick.prototype.refresh = function() {
this.createContents();
}
// #endregion
// #region Game_PointNClickObject
function Game_PointNClickObject() {
this.initialize(...arguments);
}
Game_PointNClickObject.loadCommandObject = function (args) {
args = Zetu.parse(args);
args.Items = args.Items.map(item => new Game_PointNClickObject(item));
Zetu.PointNClick.Current = args;
return args;
}
const preloadCommand = function(command) {
var args = command.parameters[3];
var obj = Game_PointNClickObject.loadCommandObject(args);
obj.Items.forEach((item) => {
if (item.Picture) ImageManager.loadPicture(item.Picture);
});
};
// Without this, events with pictures will possibly be blank on first entry
Game_PointNClickObject.preloadCache = function (events, commonEvents) {
events
.flatMap((event) => event.event().pages)
.flatMap((page) => page.list.slice(0, 200))
.filter((command) => command.code == 357)
.forEach(preloadCommand);
commonEvents
.flatMap((commonEvent) => commonEvent.list().slice(0, 200))
.filter((command) => command.code == 357)
.forEach(preloadCommand);
}
Game_PointNClickObject.prototype.initialize = function(item) {
this.collisions = {};
this.tileId = parseInt(item.TileId) || 0;
this.iconId = parseInt(item.IconId) || 0;
this.picture = item.Picture;
this.pictureFrames = parseInt(item.PictureFrames) || 1;
this.pictureFrameIndexVariable = parseInt(item.PictureFrameIndex);
this.index = 0;
this.x = parseInt(item.X) || 0;
this.y = parseInt(item.Y) || 0;
this.zoom = parseFloat(item.Zoom);
this.switchId = parseInt(item.Switch) || 0;
this.switchState = item.SwitchState == "true";
this.isReady = false;
this.bitmap = this.newBitmap();
this.commonEventId = parseInt(item.CommonEventId) || 0;
this.refresh();
}
Game_PointNClickObject.prototype.newBitmap = function () {
if (this.tileId > 0) {
var tileset = $dataTilesets[$gameMap.tilesetId()];
var page = Math.floor(this.tileId / 256);
var tilesetName = tileset.tilesetNames[page + 5];
var index = this.tileId - (page * 256);
var N = $gameMap.tileWidth(), M = $gameMap.tileHeight();
var ox = index % 8;
var oy = Math.floor(index / 8);
if (oy > 16) {
ox += 16;
oy -= 16;
}
ox *= N;
oy *= M;
var height = Math.floor(N * this.zoom);
var width = Math.floor(M * this.zoom);
var bitmap = new Bitmap(height, width);
var image = ImageManager.loadTileset(tilesetName);
bitmap.blt(ImageManager.loadTileset(tilesetName), ox, oy, N, M, 0, 0, height, width);
return bitmap;
}
if (this.iconId > 0) {
var N = ImageManager.iconWidth, M = ImageManager.iconWidth;
var ox = this.tileId % 16;
var oy = Math.floor(this.tileId / 16);
ox *= N;
oy *= M;
var width = Math.floor(N * this.zoom);
var height = Math.floor(M * this.zoom);
var bitmap = new Bitmap(width, height);
bitmap.blt(ImageManager.loadSystem("IconSet"), ox, oy, N, M, width, height);
return bitmap;
}
if (this.picture) {
var image = ImageManager.loadPicture(this.picture);
if (this.pictureFrames <= 1) {
var bitmap = new Bitmap(Math.floor(image.width * this.zoom), Math.floor(image.height * this.zoom));
bitmap.blt(image,
0,0,image.width,image.height,
0,0,bitmap.width,bitmap.height);
return bitmap;
}
var width = Math.floor(image.width / this.pictureFrames);
var index = $gameVariables.value(this.pictureFrameIndexVariable);
var bitmap = new Bitmap(Math.floor(width * this.zoom), Math.floor(image.height * this.zoom));
bitmap.blt(image, width * index, 0, width, image.height
, 0, 0, bitmap.width, bitmap.height);
return bitmap;
}
throw new Error("Could Not Find Bitmap for an Item in the Scene");
}
Game_PointNClickObject.prototype.tilesetName = function () {
var tileset = $dataTilesets[$gameMap.tilesetId()];
var tilesetIndex = (this.tileId / 256) + 5;
return tileset[tilesetIndex];
}
Game_PointNClickObject.prototype.tileIndex = function () {
return this.tileId % 256;
}
Game_PointNClickObject.prototype.checkCollision = function(x, y) {
if (!this.visible) return false;
var cx = x - this.x;
var cy = y - this.y;
if (cx < 0) return false;
if (cy < 0) return false;
if (cx >= this.bitmap.width) return false;
if (cy >= this.bitmap.height) return false;
let alpha = this.bitmap.getAlphaPixel(cx, cy);
return alpha > 0;
}
Game_PointNClickObject.prototype.checkVisibilityState = function () {
if (this.switchId > 0 && $gameSwitches.value(this.switchId) != this.switchState) return false;
return true;
}
Game_PointNClickObject.prototype.update = function () {
this.visible = this.checkVisibilityState();
}
Game_PointNClickObject.prototype.refresh = function () {
this.bitmap.destroy();
this.bitmap = this.newBitmap();
}
Game_PointNClickObject.prototype.isChanged = function() {
if (this.visible != this.checkVisibilityState()) return true;
if (this.pictureFrames > 0
&& !!this.picture && this.tileId == 0 && this.iconId == 0
&& $gameVariables.value(this.pictureFrameIndexVariable) != this.index) return true;
return false;
}
// #endregion