-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuck.js
165 lines (149 loc) · 4.11 KB
/
puck.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { World, Bodies, Body } from 'matter-js';
import { getDistance } from './utils/geometry';
export default class Puck {
constructor(
id, position, radius, goal, goalRadius, envWidth, envHeight, scene, color, map, group
) {
this.id = id;
this.prevPosition = position;
this.velocityScale = 1;
this.groupGoal = { ...goal };
this.goal = goal;
this.radius = radius;
this.goalRadius = goalRadius;
this.color = color;
this.envWidth = envWidth;
this.envHeight = envHeight;
this.scene = scene;
this.engine = this.scene.engine;
this.world = this.scene.world;
this.map = map;
this.group = group;
// Create Matter.js body and attach it to world
this.body = Bodies.circle(position.x, position.y, this.radius);
// this.body.friction = 0;
this.body.frictionAir = 1;
// this.body.frictionStatic = 0;
// this.body.restitution = 0;
// this.body.collisionFilter = {
// group: -1,
// category: 1,
// mask: 1,
// };
World.add(this.world, this.body);
// Initialize velocity according to movement goal
this.velocity = { x: 0, y: 0 };
// Distances Configurations
// this.blockedDistance = this.radius * 1.5;
this.goalReachedDist = this.goalRadius;
this.deepInGoalDist = 2 * (this.goalRadius / 3);
}
get position() {
return {
x: this.body.position.x,
y: this.body.position.y
};
}
set position(val) {
Body.set(this.body, 'position', { x: val?.x || null, y: val?.y || null });
}
timeStep() {
this.prevPosition = this.position;
this.position = this.body?.position;
this.updateGoal();
this.limitGoal();
}
updateGoal() {
if (this.reachedGoal() || !this.map) {
this.goal = this.groupGoal;
} else {
const mapY = Math.min(this.map.length, Math.max(0, Math.floor(this.position.y / 4)));
const mapX = Math.min(this.map[0].length, Math.max(0, Math.floor(this.position.x / 4)));
const dir = this.map != null && this.map[mapY] != null && this.map[mapY][mapX] != null
? this.map[mapY][mapX]
: [1, 1];
this.goal = {
x: this.position.x + dir[0] * this.radius * 10,
y: this.position.y + dir[1] * this.radius * 10
};
}
}
reachedGoal() {
return this.reachedDist(this.groupGoal, this.goalReachedDist);
}
deepInGoal() {
return this.reachedDist(this.goal, this.deepInGoalDist);
}
reached(point) {
const ret = this.getDistanceTo(point) <= this.radius * 10;
return ret;
}
reachedDist(point, distance) {
const ret = this.getDistanceTo(point) <= distance;
return ret;
}
getDistanceTo(point) {
const ret = getDistance(this.position, point);
return ret;
}
limitGoal() {
const { radius } = this;
this.goal = {
x: Math.min(Math.max(radius, this.goal.x), this.envWidth - radius),
y: Math.min(Math.max(radius, this.goal.y), this.envHeight - radius)
};
}
generateStaticObjectDefinition() {
return {
type: 'circle',
center: this.position,
radius: this.radius,
skipOrbit: true,
fromPuck: true
};
}
}
export const PuckRenderables = [
{
type: 'Body',
svgClass: 'puck-body',
dataPoints: { sceneProp: 'pucks' },
shape: 'circle',
staticAttrs: {
r: { prop: 'radius' },
id: { prop: 'id' },
fill: { prop: 'color' }
},
dynamicAttrs: {
cx: { prop: 'position.x' },
cy: { prop: 'position.y' }
},
styles: {
stroke: 'black',
'stroke-width': 1,
'stroke-opacity': 1,
'fill-opacity': 1
},
drag: {
prop: 'position',
pause: true,
onStart: {
styles: {
stroke: 'lightgray'
},
log: [
{ prop: 'color' },
{ prop: 'groupGoal' },
{ prop: 'goal' },
{ prop: 'position' },
{ prop: 'velocity' }
]
},
onEnd: {
styles: {
stroke: 'black'
}
}
}
}
];