我知道这对您来说听起来像是可怕的开销,但您应该考虑使用场景图来处理屏幕上的所有碰撞检测、绘制甚至单击事件。
场景图基本上是一个表示 1-父到 n-子关系的树形数据结构(注意:每个 HTML 页面的 DOM 也是一个场景图)
因此,为了解决这个问题,您将有一个称为“节点”或其他名称的基本接口或抽象类,代表场景图中每个节点都必须实现的接口。同样,它就像 dom 中的元素一样,它们都有 CSS 属性、事件处理方法和位置修饰符。
Node:
{
children: [],
update: function() {
for(var i = 0; i < this.children.length; i++) {
this.children[i].update();
}
},
draw: function() {
for(var i = 0; i < this.children.length; i++) {
this.children[i].draw();
}
}
}
现在,您可能知道,如果您在 DOM 中的每个元素、位置或其他元素移动一个元素,所有子元素都会自动转到其父元素的新位置,这一行为是通过转换继承实现的,为了简单起见,我'我不会显示 3D 变换矩阵,而只是一个平移(x,y 偏移)。
Node:
{
children: [],
translation: new Translation(),
update: function(worldTranslation) {
worldTranslation.addTranslation(this.translation);
for(var i = 0; i < this.children.length; i++) {
this.children[i].update(worldTranslation);
}
worldTranslation.removeTranslation(this.translation);
},
draw: function() {
for(var i = 0; i < this.children.length; i++) {
this.children[i].draw();
}
}
}
转型:
{
x: 0,
y: 0,
addTranslation: function(translation) {
this.x += translation.x;
this.y += translation.y;
},
removeTranslation: function(translation) {
this.x -= translation.x;
this.y -= translation.y;
}
}
(请注意,我们不会实例化新的翻译对象,因为在全局翻译上添加/删除值更便宜)
现在,您的 worldTranslation(或 globalTranslation)具有节点可以从其父级继承的所有偏移量。
在开始碰撞检测之前,我将展示如何使用这种技术绘制精灵。
基本上,您将用位置图像对填充绘图循环中的数组
Node:
{
children: [],
translation: new Translation(),
image: null, //assume this value is a valid htmlImage or htmlCanvas element, ready to be drawn onto a canvas
screenPosition: null, //assume this is an object with x and y values like {x: 0, y: 0}
update: function(worldTranslation) {
worldTranslation.addTranslation(this.translation);
this.screenPosition = {x: worldTranslation.x, y: worldTranslation.y};
for(var i = 0; i < this.children.length; i++) {
this.children[i].update(worldTranslation);
}
worldTranslation.removeTranslation(this.translation);
},
draw: function(spriteBatch) {
spriteBatch.push({
x: this.screenPosition.x,
y: this.screenPosition.y,
});
for(var i = 0; i < this.children.length; i++) {
this.children[i].draw(spriteBatch);
}
}
}
渲染函数:
function drawScene(rootNode, context) {
//clear context here
var spriteBatch = [];
rootNode.draw(spriteBatch);
//if you have to, do sorting according to position.x, position.y or some z-value you can set in the draw function
for(var i = 0; i < spriteBatch.length; i++) {
var sprite = spriteBatch[i];
context.drawImage(sprite.image, sprite.position.x, sprite.position.y);
}
}
现在我们已经对从场景图中绘制图像有了基本的了解,我们开始进行碰撞检测:
首先我们需要我们的节点有 BoundryBoxes:
Box:
{
x: 0,
y: 0,
width: 0,
height: 0,
collides: function(box) {
return !(
((this.y + this.height) < (box.y)) ||
(this.y > (box.y + box.height)) ||
((this.x + this.width) < box.x) ||
(this.x > (box.x + box.width))
);
}
}
在 2D 游戏中,我更喜欢让边界框不在其所在节点的坐标系中,而不是让它知道它的位置Absolute价值观。
通过CSS解释:
在CSS中,您可以设置边距和填充,这些值不依赖于页面,而是它们仅存在于设置了这些值的元素的“坐标系”中。所以它就像有position:absolute和left:10px vs margin-left:10px;
2D 的好处是我们不需要一直冒泡场景图来查找检测,而且我们不必从当前坐标系计算盒子 -> 进入世界坐标系 -> 返回每个节点进行碰撞检测。
Node:
{
children: [],
translation: new Translation(),
image: null,
screenPosition: null,
box: null, //the boundry box
update: function(worldTranslation) {
worldTranslation.addTranslation(this.translation);
this.screenPosition = {x: worldTranslation.x, y: worldTranslation.y};
this.box.x = worldTranslation.x;
this.box.y = worldTranslation.y;
this.box.width = this.image.width;
this.box.height = this.image.height;
for(var i = 0; i < this.children.length; i++) {
this.children[i].update(worldTranslation);
}
worldTranslation.removeTranslation(this.translation);
},
collide: function(box, collisions) {
if(this.box.collides(box)) {
collisions.push(this);
}
//we will optimize this later, in normal sceneGraphs a boundry box asures that it contains ALL children
//so we only will go further down the tree if this node collides with the box
for(var i = 0; i < this.children.length; i++) {
this.children[i].collide(box, collisions);
}
},
draw: function(spriteBatch) {
spriteBatch.push({
x: this.screenPosition.x,
y: this.screenPosition.y,
image: this.image,
});
for(var i = 0; i < this.children.length; i++) {
this.children[i].draw(spriteBatch);
}
}
}
碰撞功能:
function collideScene(rootNode, box) {
var hits = [];
rootNode.collide(box, hits);
for(var i = 0; i < hits.length; i++) {
var hit = hits[i];
//your code for every hit
}
}
Note:并非每个节点都必须代表一个图像,您可以创建节点以仅向其子节点添加翻译,就像创建一个没有视觉效果的 DIV 容器来排列一堆对象,而只需编辑一个对象的位置。一个角色可以包括:
CharacterRoot (Translation Only)
CharacterSprite (Image)
Weapon (Image)
Shield (Image)
现在这些只是游戏中使用的场景管理的一些基础知识,您可以在谷歌上搜索我在这里使用的许多术语,进行一些进一步的优化,并随意提出任何问题。