-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPixelPerfectCollition.js
More file actions
141 lines (133 loc) · 4.57 KB
/
PixelPerfectCollition.js
File metadata and controls
141 lines (133 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//=============================================================================
// Pixel Perfect Colition
// PixelPerfectColition.js
// Version: 1.0.1
//=============================================================================
/*
* Copyright 2015 Ramiro Rojo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*:
* @plugindesc Allows to check if two sprites are colliding.
* This is a tool for scripters.
*
* @author Ramiro Rojo
*
* @help
*
* This plugin doesn't have any parameters.
*
* Note: At the current version sprites do take account of anchor,
* but they don't account rotation or scale into consideration.
* This will made them quite fast at the cost of not using anything fancy.
*
* Ths plugins provides two simple, yet powerful functions:
*
* Sprite.prototype.collitionDataWith(rect);
* - Gives a collition rect between two sprites.
* - Parameters:
* - sprite: The sprite to check the rect.
*
* - Returns: The collition data between both sprites or null if
* no collition was made.
* The data contains a rect and the initial points of both sprites.
*
* Sprite.prototype.collidesWith(sprite[, alpha])
* - Checks if two sprites are colliding by pixels.
* - Parameters:
* - sprite: The sprite to check collition.
* - alpha: The tolerance to check collition, (by default 1)
* if you are using alpha blending on your sprites considerer
* giving it a larger value.
*
* - Returns: true if the sprites are colliding, false otherwise.
*/
//=============================================================================
var Imported = Imported || {};
Imported['PixelPerfectColition'] = '1.0.1';
Sprite.prototype.collitionDataWith = function (sprite) {
// get the rect of collition
var w1 = this.scale.x * this.width,
w2 = sprite.scale.x * sprite.width,
h1 = this.height,
h2 = sprite.height,
x1 = this.x - w1 * this.anchor.x,
x2 = sprite.x - w2 * sprite.anchor.x,
y1 = this.y - this.anchor.y * h1,
y2 = sprite.y - sprite.anchor.y * h2,
x = Math.max(x1, x2),
y = Math.max(y1, y2),
n1 = Math.min(x1 + w1, x2 + w2),
n2 = Math.min(y1 + h1, y2 + h2);
if (n1 >= x && n2 >= y) {
return {
rect: new Rectangle(x, y, n1 - x, n2 - y),
p1 : new Point(x1, y1),
p2 : new Point(x2, y2)
};
}
return null;
}
Sprite.prototype.collidesWith = function (sprite /*, alpha */) {
var data = this.collitionDataWith(sprite);
if (!data) {
return false;
}
var rect = data.rect;
var alpha = arguments.length > 1 ? arguments[1] : 1;
var p1, p2, a1, b1, a2, b2;
var ex = rect.width + rect.x;
var ey = rect.height + rect.y;
for (var i = rect.x; i < ex; ++i) {
a1 = i - data.p1.x + this._frame.x;
a2 = i - data.p2.x + sprite._frame.x;
for (var j = rect.y; j < ey; ++j) {
b1 = j - data.p1.y + this._frame.y;
p1 = this.bitmap.getAlphaPixel(a1, b1);
b2 = j - data.p2.y + sprite._frame.y;
p2 = sprite.bitmap.getAlphaPixel(a2, b2);
if (Number(p1) > 0 && Number(p2) > 0) {
return true;
}
}
}
return false;
}
/*
function Scene_PPP() {
Scene_Base.apply(this, arguments);
}
Scene_PPP.prototype = Object.create(Scene_Base.prototype);
Scene_PPP.prototype.constructor = Scene_PPP;
Scene_PPP.prototype.create = function () {
this.s1 = new Sprite(ImageManager.loadPicture('s1'));
this.s2 = new Sprite(ImageManager.loadPicture('s1'));
this.s2.x = Graphics.width / 2;
this.s2.y = Graphics.height / 2;
this.s1.anchor.x = this.s2.anchor.x = 0.5;
this.s1.anchor.y = this.s2.anchor.y =0.5;
this.txt = new Sprite(new Bitmap(Graphics.width, 32));
this.addChild(this.s1);
this.addChild(this.s2);
this.addChild(this.txt);
}
Scene_PPP.prototype.update = function () {
Scene_Base.prototype.update.apply(this, arguments);
if (TouchInput.isTriggered()) {
this.s1.x = TouchInput.x;
this.s1.y = TouchInput.y;
this.txt.bitmap.clear();
var t = this.s1.collidesWith(this.s2) ? 'YES!' : 'NO!';
this.txt.bitmap.drawText('Collide: ' + t, 0, 0, Graphics.width, 32);
}
}
*/