Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AroriaNetwork
GitHub Repository: AroriaNetwork/3kho-backup
Path: blob/main/projects/missiles/src/barrier.js
1835 views
1
MG.BarrierType = {
2
RANDOM: 'random',
3
4
BARRIER_1: 1,
5
BARRIER_2: 2,
6
BARRIER_3: 3,
7
BARRIER_4: 4,
8
BARRIER_5: 5,
9
BARRIER_6: 6,
10
11
BLANK: 'blank',
12
START: 'start',
13
FINISH: 'finish'
14
};
15
MG.NUM_RANDOM_BARRIERS = 6;
16
17
/* TODO find nicer way of initalising MG.BARRIER_PATH_IDS */
18
MG.BARRIER_PATH_IDS = {}
19
MG.BARRIER_PATH_IDS[MG.BarrierType.RANDOM] = '';
20
MG.BARRIER_PATH_IDS[MG.BarrierType.BARRIER_1] = 'barrier-path-1';
21
MG.BARRIER_PATH_IDS[MG.BarrierType.BARRIER_2] = 'barrier-path-2';
22
MG.BARRIER_PATH_IDS[MG.BarrierType.BARRIER_3] = 'barrier-path-3';
23
MG.BARRIER_PATH_IDS[MG.BarrierType.BARRIER_4] = 'barrier-path-4';
24
MG.BARRIER_PATH_IDS[MG.BarrierType.BARRIER_5] = 'barrier-path-5';
25
MG.BARRIER_PATH_IDS[MG.BarrierType.BARRIER_6] = 'barrier-path-6';
26
MG.BARRIER_PATH_IDS[MG.BarrierType.BLANK] = 'barrier-path-blank';
27
MG.BARRIER_PATH_IDS[MG.BarrierType.START] = 'barrier-path-blank';
28
MG.BARRIER_PATH_IDS[MG.BarrierType.FINISH] = 'barrier-path-finish';
29
30
MG.Barrier = function (type) {
31
if (type === undefined) {type = MG.BarrierType.RANDOM;}
32
33
var mIsInitialised = false;
34
35
var mTheta = 0.0;
36
var mDTheta = 300.0*(0.5 - Math.random());
37
38
var mIsRandom = (type === MG.BarrierType.RANDOM);
39
var mType = (type === MG.BarrierType.RANDOM) ? Math.ceil(MG.NUM_RANDOM_BARRIERS*Math.random()) : type;
40
41
var mRootNode;
42
var mFrontPath;
43
var mBackPath;
44
45
this.init = function () {
46
47
mRootNode = document.createElementNS(NAMESPACE_SVG, 'g');
48
49
/* The path representing the face nearest the camera */
50
mFrontPath = document.getElementById(MG.BARRIER_PATH_IDS[mType]).cloneNode(true);
51
mFrontPath.setAttribute('class', 'barrier-path-front');
52
53
/* The path partially obscured by the front path, added to give the illusion of thickness. */
54
mBackPath = document.getElementById(MG.BARRIER_PATH_IDS[mType]).cloneNode(true);
55
mBackPath.setAttribute('class', 'barrier-path-back');
56
57
mRootNode.setAttribute('class', 'barrier');
58
mRootNode.appendChild(mBackPath);
59
mRootNode.appendChild(mFrontPath);
60
61
mIsInitialised = true;
62
};
63
64
this.destroy = function () {
65
mRootNode.parentNode.removeChild(mRootNode);
66
};
67
68
/**
69
* Checks whether the point specified by x and y will collide with the barrier.
70
* Works by counting the number of intersections between the path outlining the
71
* barrier and a line between some point outside of the barrier, and the point
72
* that is being tested.
73
* Returns true if the point intersects the transformed barrier, false otherwise.
74
*/
75
this.collides = function (x, y) {
76
/* transform the provided coordinates to the coordinate system of the barrier */
77
var x_ = x * Math.cos(mTheta*Math.PI/180) + y * Math.sin(mTheta*Math.PI/180);
78
var y_ = -x * Math.sin(mTheta*Math.PI/180) + y * Math.cos(mTheta*Math.PI/180);
79
80
/* the line to be used for finding the intersections should already exist
81
but needs to be made to the point to the point that is to be tested */
82
var lineNode = document.getElementById('collision-line');
83
lineNode.setAttribute('x2', x_);
84
lineNode.setAttribute('y2', y_);
85
86
/* As the barriers path may not have been created yet, the original path is used */
87
var pathNode = document.getElementById(MG.BARRIER_PATH_IDS[mType]);
88
89
90
/* `Line` and `Path` are both part of Kevin Lindsey's svg geometry library. */
91
/* `Path` has been hacked to properly support elliptical arc segments. */
92
var line = new Line(lineNode);
93
var path = new Path(pathNode);
94
95
var intersections = new Intersection.intersectShapes(path, line);
96
97
return intersections.points.length % 2 === 1;
98
};
99
100
this.update = function (dt) {
101
mTheta += mDTheta * dt;
102
};
103
104
/**
105
* Updates the barriers representation in the DOM to reflect changes made at
106
* the last update.
107
* `x` and `y` define the position of the viewpoint.
108
* `offset` is the distance of the barrier from the viewpoint.
109
*/
110
this.updateDOM = function (x, y, offset) {
111
var frontScale = MG.PROJECTION_PLANE_DISTANCE /
112
(Math.tan(Math.PI * MG.FIELD_OF_VIEW/360.0)*(offset));
113
114
var backScale = MG.PROJECTION_PLANE_DISTANCE /
115
(Math.tan(Math.PI * MG.FIELD_OF_VIEW/360.0)*(offset + 10));
116
117
mFrontPath.setAttribute('transform',
118
'scale(' + frontScale + ') translate(' + x + ',' + y + ') rotate(' + mTheta + ')');
119
mBackPath.setAttribute('transform',
120
'scale(' + backScale + ') translate(' + x + ',' + y + ') rotate(' + mTheta + ')');
121
122
offset = Math.max(MG.LINE_OF_SIGHT - MG.BARRIER_SPACING ,Math.min(MG.LINE_OF_SIGHT,offset));
123
var fog = 100 -100*(MG.LINE_OF_SIGHT - offset)/MG.BARRIER_SPACING;
124
125
mFrontPath.setAttribute('fill', 'rgb('+(100+fog)+'%,'
126
+(100+fog)+'%,'
127
+(100+fog)+'%)');
128
mBackPath.setAttribute('fill', 'rgb(' +(60+fog)+'%,'
129
+(60+fog)+'%,'
130
+(60+fog)+'%)');
131
mFrontPath.setAttribute('stroke', 'rgb(' +(0+fog)+'%,'
132
+(0+fog)+'%,'
133
+(0+fog)+'%)');
134
mBackPath.setAttribute('stroke', 'rgb(' +(0+fog)+'%,'
135
+(0+fog)+'%,'
136
+(0+fog)+'%)');
137
};
138
139
this.getType = function () {
140
return mType;
141
};
142
143
this.isRandom = function () {
144
return mIsRandom;
145
};
146
147
this.isInitialised = function () {
148
return mIsInitialised;
149
};
150
151
this.getRootNode = function () {
152
return mRootNode;
153
};
154
155
};
156
157