| 赞 | 168 |
| VIP | |
| 好人卡 | |
| 积分 | 229 |
| 经验 | |
| 最后登录 | 2025-4-1 |
| 在线时间 | 8598 小时 |
- 梦石
- 0
- 星屑
- 22879
- 在线时间
- 8598 小时
- 注册时间
- 2011-12-31
- 回帖
- 3270
|
发表于 2015-11-6 11:56:32
|
显示全部楼层
本帖最后由 tseyik 于 2015-11-8 11:44 编辑
範例1.06,開新工程,把東西放到新工程柤応文件夾
demo.zip
(328.86 KB, 下载次数: 523)
增加点滅功能(Fire)
[fold=Terrax Plugins - Lighting system1.06][pre lang="ruby"]//=============================================================================
// Terrax Plugins - Lighting system
// TerraxLighting.js
// Version: 1.06
//=============================================================================
//
// This script should be compatible with most other scripts, it overwrites one
// core script (Spriteset_Map.prototype.createLowerLayer) in rpg_sprites.js to
// add a new layer.
//=============================================================================
/*:
* @plugindesc v1.06 Creates an extra layer that darkens a map and adds lightsources!
* @author Terrax
*
* @param Player radius
* @desc Adjust the light radius around the player
* Default: 300
* @default 300
*
* @param Reset Lights
* @desc Resets the light switches each map
* Default: No
* @default No
*
* @help
* To activate the script in an area, do the following:
* 1. Put an event switch into the map.
* 2. In the 'Note' field (Next to the name) put the following text :
* Light 250 #FFFFFF
* - Light activates the script
* - 250 is the lightradius of the object
* - #FFFFFF is the lightcolor (white in this case)
* 3. You're done, its that simple.
*
* To alter the player radius in game use the following plugin command :
* Light radius 200 #FFFFFF
* (to change the radius and the color)
*
* Optional: to turn on and off lightsources in the game, do the following:
* Give the lightsource the normal def : Light 250 #FFFFFF and an extra number
* so it becomes 'Light 250 #FFFFFF 1'
* To turn on this light use plugin command : 'Light on 1'.
* The plugin command will also trigger SelfSwitch 'D' on the targeted light(s).
* To turn off the light use plugin command : 'Light off 1'.
* You can reset the switches each map with the option or manualy by
* the plugin command 'Light switch reset'
*
* Replacing the 'Light' keyworld with 'Fire' will give the lights a subtle flicker
*
* Released under the MIT license,
* if used for commercial projects feel free to make a donation or
* better yet, give me a free version of what you have created.
* e-mail : fox(AT)caiw.nl / terraxz2 on steam.
*
*/
//=============================================================================
// ps.. if my code looks funky, i'm an old guy..
// object orientated programming bugs the hell out of me.
var testcounter = 0;
//var lightarray_id = [];
//var lightarray_state = [];
(function() {
var lightarray_id = [];
var lightarray_state = [];
var oldmap = 0;
var playercolor = '#FFFFFF';
var playerflicker = false;
var playerflashlight = false;
var parameters = PluginManager.parameters('TerraxLighting');
var player_radius = Number(parameters['Player radius'] || 300);
var reset_each_map = parameters['Reset Lights'] || No;
var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
_Game_Interpreter_pluginCommand.call(this, command, args);
if (command === 'Light' || command === 'light' || command === 'Fire' || command === 'fire' || command === 'Flashlight' || command === 'flashlight') {
if (command === 'Flashlight' || command === 'flashlight') {
playerflashlight = true;
} else {
playerflashlight = false;
}
if (command === 'Fire' || command === 'fire') {
playerflicker = true;
} else {
playerflicker = false;
}
var evid = this._eventId;
// Light radius 100 #FFFFFF
if (args[0] == 'radius') {
var newradius = Number(args[1]);
if (newradius >= 0) {
player_radius = newradius;
}
if (args.length > 2) {
playercolor = args[2];
var isValidPlayerColor = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(playercolor);
if (!isValidPlayerColor) {
playercolor = '#FFFFFF'
}
}
}
if (args[0] == 'on') {
var lightid = Number(args[1]);
var idfound = false;
for (var i = 0; i < lightarray_id.length; i++) {
if (lightarray_id == lightid) {
idfound = true;
lightarray_state = true;
//Graphics.printError('test','found');
}
}
if (idfound == false) {
lightarray_id.push(lightid);
lightarray_state.push(true);
}
}
if (args[0] == 'off') {
var lightid = Number(args[1]);
var idfound = false;
for (var i = 0; i < lightarray_id.length; i++) {
if (lightarray_id == lightid) {
idfound = true;
lightarray_state = false;
}
}
if (idfound == false) {
lightarray_id.push(lightid);
lightarray_state.push(false);
}
}
if (args[0] == 'switch' && args[1] == 'reset') {
// reset switches to false
for (var i = 0; i < $dataMap.events.length; i++) {
if ($dataMap.events) {
for (var j = 0; j < lightarray_id.length; j++) {
if (lightarray_id[j] == lightid) {
var mapid = $gameMap.mapId();
var eventid = $dataMap.events.id;
var key = [mapid,eventid,'D'];
$gameSelfSwitches.setValue(key, false);
}
}
}
}
Lightarray_id = [];
lightarray_state = [];
}
}
}
Spriteset_Map.prototype.createLightmask = function() {
this._lightmask = new Lightmask();
this.addChild(this._lightmask);
};
function Lightmask() {
this.initialize.apply(this, arguments);
}
Lightmask.prototype = Object.create(PIXI.DisplayObjectContainer.prototype);
Lightmask.prototype.constructor = Lightmask;
Lightmask.prototype.initialize = function() {
PIXI.DisplayObjectContainer.call(this);
this._width = Graphics.width;
this._height = Graphics.height;
this._sprites = [];
this._createBitmap();
};
//Updates the Lightmask for each frame.
Lightmask.prototype.update = function() {
this._updateMask();
};
//@method _createBitmaps
Lightmask.prototype._createBitmap = function() {
this._maskBitmap = new Bitmap(2500,1500); // one big bitmap to fill the intire screen with black
var canvas = this._maskBitmap.canvas;
};
/**
* @method _updateAllSprites
* @private
*/
Lightmask.prototype._updateMask = function() {
// ****** DETECT MAP CHANGES ********
var map_id = $gameMap.mapId();
if (map_id != oldmap) {
oldmap = map_id;
if (reset_each_map == 'Yes' || reset_each_map == 'yes') {
// reset switches to false
for (var i = 0; i < $dataMap.events.length; i++) {
if ($dataMap.events) {
for (var j = 0; j < lightarray_id.length; j++) {
if (lightarray_id[j] == lightid) {
var mapid = $gameMap.mapId();
var eventid = $dataMap.events.id;
var key = [mapid,eventid,'D'];
$gameSelfSwitches.setValue(key, false);
}
}
}
}
Lightarray_id = [];
lightarray_state = [];
}
}
// remove old sprites
for (var i = 0; i < this._sprites.length; i++) { // remove all old sprites
this._removeSprite();
}
// are there lightsources on this map?
for (var i = 0; i < $dataMap.events.length; i++) {
if ($dataMap.events) {
var note = $dataMap.events.note
var note_args = note.split(" ");
var note_command = note_args.shift();
if (note_command == "Light" || note_command == "light" || note_command == "Fire" || note_command == "fire") {
this._addSprite(0,0,this._maskBitmap); // yes.. then turn off the lights
break;
}
}
}
// ****** PLAYER LIGHTGLOBE ********
var canvas = this._maskBitmap.canvas;
var ctx = canvas.getContext("2d");
this._maskBitmap.fillRect(0, 0, 2500, 1500, 'black');
//ctx.globalCompositeOperation = 'lighten';
ctx.globalCompositeOperation = 'lighter';
var pw = $gameMap.tileWidth()
var ph = $gameMap.tileHeight();
var dx = $gameMap.displayX();
var dy = $gameMap.displayY();
var px = $gamePlayer._realX;
var py = $gamePlayer._realY;
var pd = $gamePlayer._direction;
var x1 = (pw/2)+( (px-dx)*pw);
var y1 = (ph/2)+( (py-dy)*ph);
var paralax = false;
// paralax does something weird with coordinates.. recalc needed
if (dx>$gamePlayer.x) {
var xjump = $gameMap.width() - Math.floor(dx-px);
x1 = (pw/2)+(xjump*pw);
}
if (dy>$gamePlayer.y) {
var yjump = $gameMap.height() - Math.floor(dy-py);
y1 = (ph/2)+(yjump*ph);
}
if (player_radius > 0) {
if (playerflashlight == true) {
this._maskBitmap.radialgradientFillRect2(x1,y1, 20, player_radius, playercolor, 'black', pd);
} else {
if (player_radius < 100){
this._maskBitmap.radialgradientFillRect(x1,y1, 0, player_radius, '#999999', 'black', playerflicker);
} else {
this._maskBitmap.radialgradientFillRect(x1,y1, 20, player_radius, playercolor, 'black', playerflicker);
}
}
}
// ********** OTHER LIGHTSOURCES **************
for (var i = 0; i < $dataMap.events.length; i++) {
if ($dataMap.events) {
var note = $dataMap.events.note;
//var eventid = $dataMap.events.id;
var note_args = note.split(" ");
var note_command = note_args.shift();
if (note_command == "Light" || note_command == "light" || note_command == "Fire" || note_command == "fire") {
var objectflicker = false;
if (note_command == "Fire" || note_command == "fire") {
objectflicker = true;
}
var light_radius = note_args.shift();
// light radius
if (light_radius >= 0) {
// light color
var colorvalue = note_args.shift();
var isValidColor = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(colorvalue)
if (!isValidColor) {
colorvalue = '#FFFFFF'
}
// conditional lighting
var lightid = note_args.shift();
var state = true;
if (lightid > 0) {
var state = false;
for (var j = 0; j < lightarray_id.length; j++) {
if (lightarray_id[j] == lightid) {
idfound = true;
state = lightarray_state[j];
var mapid = $gameMap.mapId();
var eventid = $dataMap.events.id;
//Graphics.printError('test',mapid+' '+eventid);
key = [mapid,eventid,'D'];
if (state == true) {
$gameSelfSwitches.setValue(key, true);
} else {
$gameSelfSwitches.setValue(key, false);
}
}
}
}
// show light
if (state == true) {
var lpx = $dataMap.events.x;
var lpy = $dataMap.events.y;
var lx1 = (pw/2)+( (lpx-dx)*pw);
var ly1 = (ph/2)+( (lpy-dy)*ph);
// paralax does something weird with coordinates.. recalc needed
if (dx-10>lpx) {
var lxjump = $gameMap.width() - (dx-lpx);
lx1 = (pw/2)+(lxjump*pw);
}
if (dy-10>lpy) {
var lyjump = $gameMap.height() -(dy-lpy);
ly1 = (ph/2)+(lyjump*ph);
}
this._maskBitmap.radialgradientFillRect(lx1,ly1, 0, light_radius , colorvalue, 'black', objectflicker);
}
}
}
}
}
// reset drawmode to normal
ctx.globalCompositeOperation = 'source-over';
};
/**
* @method _addSprite
* @private
*/
Lightmask.prototype._addSprite = function(x1,y1,selectedbitmap) {
var sprite = new Sprite(this.viewport);
sprite.bitmap = selectedbitmap;
sprite.opacity = 255;
sprite.blendMode = 2;
sprite.x = x1;
sprite.y = y1;
this._sprites.push(sprite);
this.addChild(sprite);
sprite.rotation = 0;
sprite.ax = 0
sprite.ay = 0
sprite.opacity = 255;
};
/**
* @method _removeSprite
* @private
*/
Lightmask.prototype._removeSprite = function() {
this.removeChild(this._sprites.pop());
};
// ******************* NORMAL LIGHT SHAPE ***********************************
// Fill gradient circle
Bitmap.prototype.radialgradientFillRect = function(x1, y1, r1, r2, color1, color2, flicker) {
var context = this._context;
var grad;
if (flicker == true) {
var gradrnd = Math.floor((Math.random()*7)+1);
var colorrnd = Math.floor((Math.random()*10)-5);
var r = hexToRgb(color1).r;
var g = hexToRgb(color1).g;
var b = hexToRgb(color1).b;
g = g + colorrnd;
if (g<0) { g = 0; }
if (g>255) { g = 255; }
color1 = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
r2 = r2 - gradrnd;
}
grad = context.createRadialGradient(x1, y1, r1, x1, y1, r2);
grad.addColorStop(0, color1);
grad.addColorStop(1, color2);
context.save();
context.fillStyle = grad;
context.fillRect(x1-r2, y1-r2, r2*2, r2*2);
context.restore();
this._setDirty();
};
// ********************************** FLASHLIGHT *************************************
// Fill gradient Cone
Bitmap.prototype.radialgradientFillRect2 = function(x1, y1, r1, r2, color1, color2, direction) {
var context = this._context;
var grad;
// smal dim glove around player
context.save();
r1 = 0;
r2 = 40;
grad = context.createRadialGradient(x1, y1, r1, x1, y1, r2);
grad.addColorStop(0, '#999999');
grad.addColorStop(1, color2);
context.fillStyle = grad;
context.fillRect(x1-r2, y1-r2, r2*2, r2*2);
// flashlight
//direction = 4;
for (var cone = 0; cone < 8; cone++) {
r1 = cone * 2;
r2 = cone * 12;
switch(direction) {
case 6:
x1 = x1 + cone*6;
break;
case 4:
x1 = x1 - cone*6;
break;
case 2:
y1 = y1 + cone*6;
break;
case 8:
y1 = y1 - cone*6;
break;
}
grad = context.createRadialGradient(x1, y1, r1, x1, y1, r2);
grad.addColorStop(0, color1);
grad.addColorStop(1, color2);
context.fillStyle = grad;
context.fillRect(x1-r2, y1-r2, r2*2, r2*2);
}
context.restore();
this._setDirty();
};
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
//****
// This function is overwritten from rpg_sprites.js
//****
Spriteset_Map.prototype.createLowerLayer = function() {
Spriteset_Base.prototype.createLowerLayer.call(this);
this.createParallax();
this.createTilemap();
this.createCharacters();
this.createShadow();
this.createDestination();
this.createLightmask();
this.createWeather();
};
})();[/pre][/fold] |
评分
-
查看全部评分
|