新增碰撞检测算法

This commit is contained in:
YHH
2020-06-09 22:32:18 +08:00
parent 0fd33be5f9
commit 831152aa26
5 changed files with 301 additions and 2 deletions

View File

@@ -1,5 +1,8 @@
class Entity {
private static _idGenerator: number;
public name: string;
public readonly id: number;
/** 当前实体所属的场景 */
public scene: Scene;
/** 封装实体的位置/旋转/缩放,并允许设置一个高层结构 */
@@ -9,6 +12,7 @@ class Entity {
private _updateOrder: number = 0;
private _enabled: boolean = true;
private _isDestoryed: boolean;
private _tag: number = 0;
public componentBits: BitSet;
@@ -116,10 +120,20 @@ class Entity {
return this;
}
public get tag(){
return this._tag;
}
public set tag(value: number){
this.setTag(value);
}
constructor(name: string){
this.name = name;
this.transform = new Transform(this);
this.components = new ComponentList(this);
this.id = Entity._idGenerator ++;
this.componentBits = new BitSet();
}
@@ -142,6 +156,20 @@ class Entity {
}
}
public setTag(tag: number): Entity{
if (this._tag != tag){
if (this.scene){
this.scene.entities.removeFromTagList(this);
}
this._tag = tag;
if (this.scene){
this.scene.entities.addToTagList(this);
}
}
return this;
}
public attachToScene(newScene: Scene){
this.scene = newScene;
newScene.entities.add(this);

View File

@@ -4,6 +4,8 @@ class EntityList{
private _entitiesToAdded: Entity[] = [];
private _tempEntityList: Entity[] = [];
private _entities: Entity[] = [];
private _entityDict: Map<number, Entity[]> = new Map<number, Entity[]>();
private _unsortedTags: number[] = [];
constructor(scene: Scene){
this.scene = scene;
@@ -40,6 +42,31 @@ class EntityList{
return this._entitiesToAdded.firstOrDefault(entity => entity.name == name);
}
public getTagList(tag: number){
let list = this._entityDict.get(tag);
if (!list){
list = [];
this._entityDict.set(tag, list);
}
return this._entityDict.get(tag);
}
public addToTagList(entity: Entity){
let list = this.getTagList(entity.tag);
if (!list.contains(entity)){
list.push(entity);
this._unsortedTags.push(entity.tag);
}
}
public removeFromTagList(entity: Entity){
let list = this._entityDict.get(entity.tag);
if (list){
list.remove(entity);
}
}
public update(){
for (let i = 0; i < this._entities.length; i++){
let entity = this._entities[i];
@@ -58,6 +85,7 @@ class EntityList{
}
this._entities.length = 0;
this._entityDict.clear();
}
public updateLists(){
@@ -89,5 +117,13 @@ class EntityList{
this._tempEntityList.forEach(entity => entity.onAddedToScene());
this._tempEntityList.length = 0;
}
if (this._unsortedTags.length > 0){
this._unsortedTags.forEach(tag => {
this._entityDict.get(tag).sort();
});
this._unsortedTags.length = 0;
}
}
}