Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AroriaNetwork
GitHub Repository: AroriaNetwork/3kho-backup
Path: blob/main/projects/missiles/src/missile.js
1835 views
1
MG.missile = (function () {
2
3
var ACCELERATION_TIME_CONSTANT = 1.0;
4
var DRIFT_DAMPING = 0.25;
5
6
var MAX_RADIUS = 0.8*MG.TUNNEL_RADIUS;
7
8
var MissileState = {
9
CRASHED: 'crashed',
10
AUTOPILOT: 'autopilot',
11
MANUAL: 'manual'
12
}
13
14
var mState;
15
16
var mOffset;
17
var mVelocity;
18
var mTargetVelocity;
19
20
var mX;
21
var mY;
22
23
var mTargetX;
24
var mTargetY;
25
26
var mDriftVelX;
27
var mDriftVelY;
28
var mDriftCounter;
29
30
31
return {
32
init: function () {
33
this.reset();
34
},
35
36
37
reset: function (){
38
mState = MissileState.AUTOPILOT;
39
40
mOffset = 200.0;
41
mVelocity = 0.0;
42
mTargetVelocity = 400.0;
43
44
mX = 0.0;
45
mY = 0.0;
46
47
mTargetX = 0.0;
48
mTargetY = 0.0;
49
50
mDriftVelX = 0.0;
51
mDriftVelY = 0.0;
52
mDriftCounter = 1.0;
53
54
},
55
56
57
update: function (dt) {
58
59
switch (mState) {
60
case MissileState.AUTOPILOT:
61
/* When under autopilot control, the missile will randomly
62
drift around the center of the tunnel, changing direction at
63
discrete intervals. */
64
/* The drift counter contains the time until the next direction change. */
65
mDriftCounter -= dt;
66
if (mDriftCounter < 0) {
67
mDriftCounter = 1.1 + 0.9*Math.random();
68
69
mDriftVelX = (MG.TUNNEL_RADIUS*(Math.random()-0.5) - mTargetX)/1.5;
70
mDriftVelY = (MG.TUNNEL_RADIUS*(Math.random()-0.5) - mTargetY)/1.5;
71
}
72
73
/* TODO Smooth */
74
mX += mDriftVelX * dt ;
75
mY += mDriftVelY * dt ;
76
77
break;
78
79
case MissileState.MANUAL:
80
mX += (mTargetX - mX) * dt / DRIFT_DAMPING;
81
mY += (mTargetY - mY) * dt / DRIFT_DAMPING;
82
break;
83
84
default:
85
/* leave the missile pointing in the same direction */
86
}
87
88
/* Clamp the missile's position to inside the tunnel wall */
89
var radius = Math.sqrt(mX*mX + mY*mY);
90
var newRadius = Math.min(MAX_RADIUS, radius);
91
92
mX = (radius === 0) ? 0 : mX*newRadius/radius;
93
mY = (radius === 0) ? 0 : mY*newRadius/radius;
94
95
96
97
if (mState === MissileState.CRASHED) {
98
/* If the missile has crashed, it will bounce backwards coming
99
to rest near the location of the previous barrier. */
100
mVelocity += dt*MG.BARRIER_SPACING*mVelocity/(mOffset - MG.BARRIER_SPACING);
101
} else {
102
mVelocity += dt*(mTargetVelocity - mVelocity)/ACCELERATION_TIME_CONSTANT;
103
}
104
105
mOffset -= mVelocity * dt;
106
},
107
108
getPosition: function () {
109
return {x: mX, y:mY};
110
},
111
112
getTarget: function () {
113
return {x: mTargetX, y:mTargetY};
114
},
115
116
getOffset: function () {
117
return mOffset;
118
},
119
120
getVelocity: function () {
121
return mVelocity;
122
},
123
124
isCrashed: function () {
125
return mState == MissileState.CRASHED;
126
},
127
128
setTarget: function (targetX, targetY) {
129
if (mState === MissileState.MANUAL) {
130
mTargetX = targetX;
131
mTargetY = targetY;
132
}
133
},
134
135
setVelocity: function (velocity) {
136
mTargetVelocity = velocity;
137
},
138
139
140
setManual: function () {
141
mState = MissileState.MANUAL;
142
},
143
144
setAutopilot: function () {
145
mState = MissileState.AUTOPILOT;
146
},
147
148
149
onBarrierPassed: function () {
150
mOffset += MG.BARRIER_SPACING;
151
},
152
153
onCrash: function () {
154
mVelocity = -Math.abs(mVelocity);
155
156
mState = MissileState.CRASHED;
157
}
158
};
159
}());
160
161