-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollision.js
More file actions
154 lines (139 loc) · 4.75 KB
/
collision.js
File metadata and controls
154 lines (139 loc) · 4.75 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
142
143
144
145
146
147
148
149
150
151
import {debug, Entity, Component, System, DeleteComponent, TransformComponent, Game} from './engine.js';
import {Vector, pointInPolygon} from './vector.js';
import {Quadtree} from './quadtree.js';
export class CollisionComponent extends Component {
constructor(polygon) {
super("collision");
this.vertices = [];
for(const vertex of polygon){
this.vertices.push(new Vector(vertex))
}
this.collisionCallbacks = new Map();
this.lastTransform = new TransformComponent();
this.uniqueID = false;
this.polygon = [];
}
registerCallback(componentId, callback) {
this.collisionCallbacks.set(componentId,callback);
}
handleCollision(entity, collision) {
for(const key of this.collisionCallbacks.keys()){
if(collision.hasComponent(key)){
const callback = this.collisionCallbacks.get(key);
callback(entity, collision);
}
}
}
hasCallback(entity){
for(const key of this.collisionCallbacks.keys()){
if(entity.hasComponent(key)){
return true;
}
}
return false;
}
draw(ctx, transform) {
let c = 0;
for(const polygon of this.polygon){
if(polygon){c++;
ctx.strokeStyle = `rgba(0,0,${c*2},${(c)*0.008})`;
ctx.beginPath();
ctx.moveTo(polygon[0].x, polygon[0].y);
for (let i = 1; i < polygon.length; i++) {
ctx.lineTo(polygon[i].x, polygon[i].y);
}
ctx.closePath();
ctx.stroke();
}
if(c > 254) {this.polygon.shift()};
}
ctx.strokeStyle = "black";
ctx.save();
ctx.translate(transform.x, transform.y);
ctx.rotate(transform.rotation);
ctx.scale(transform.scale, transform.scale);
ctx.beginPath();
ctx.moveTo(this.vertices[0].x, this.vertices[0].y);
for (let i = 1; i < this.vertices.length; i++) {
ctx.lineTo(this.vertices[i].x, this.vertices[i].y);
}
ctx.closePath();
ctx.stroke();
ctx.restore();
}
}
export class CollisionSystem extends System{
constructor() {
super("collision")
this.quadtree = new Quadtree({x: -500, y: -500, width: 2000, height: 2000}, 16);
this.next = false;
// Create a new worker thread and pass it the name of a JavaScript file to execute
this.worker = new Worker('./engine/collisionWorker.js',{type:'module'});
// Listen for messages sent back from the worker thread
this.worker.addEventListener('message', this.processCollisions.bind(this));
this.entities = new Map();
this.workerReady = true;
this.workerlate = 0;
this.workerET = 0;
}
update(entities, dt) {
if(this.workerReady){
const message = [];
for(const entity of entities){
if(this.entities.has(entity.uniqueID)){
this.entities.get(entity.uniqueID)
}
if(entity.hasComponent("collision")){
if(!this.entities.has(entity.uniqueID)){
this.entities.set(entity.uniqueID,entity);
this.entities.get(entity.uniqueID)
}
const collision = entity.getComponent("collision");
const transform = entity.getComponent("transform");
if(!transform.equals(collision.lastTransform)){
message.push({entityID:entity.uniqueID,transform:transform,vertices:collision.vertices,remove:false});
collision.lastTransform = new TransformComponent(transform.x,transform.y,transform.z,transform.rotation,transform.scale);
}
}
}
for(const entity of this.entities.values()){
if(!entities.has(entity)){
message.push({entityID:entity.uniqueID, remove: true});
this.entities.delete(entity.uniqueID);
}else{
entity.active = false;
}
}
this.worker.postMessage(message);
//console.log("sent Message from main thread")
this.workerReady = false;
this.workerlate = 0;
}
else
this.workerlate++;
}
processCollisions(event){
if(event.data.status == "done"){
//console.log("Main thread recieved")
const activeCollisions = event.data.collisoins;
for(const collision of activeCollisions){
const collider = this.entities.get(collision.collider);
const other = this.entities.get(collision.other);
//collider:collider,other:potentialCollider.entity
//console.log(`collider:${JSON.stringify(collider)},other:${JSON.stringify(other)}`)
if(collider.hasComponent("collision")){
const polygon = collider.getComponent("collision").polygon;
polygon.push(collision.polygon);
if(polygon.length > 120){
polygon.shift();
}
if(other){
collider.getComponent("collision").handleCollision(collider,other);
}
}
}
}
this.workerReady = true;
this.workerET = event.data.et;
}
}