~harrison-rt/+junk/jaguar_company

« back to all changes in this revision

Viewing changes to tags/2.1/jaguar_company_2.1.oxp/Scripts/jaguar_company_tracker.js

  • Committer: Richard Harrison
  • Date: 2013-01-19 22:57:39 UTC
  • Revision ID: harrison.rt@gmail.com-20130119225739-8f9knzzag8xd7zhm
Tags: 2.2
* Wrong variable in the buoy script.
* Corrected a logic check in the asteroid script.
* Optimized the cleanup code in jaguar_company_attackers.js
* Fewer boulders.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*jslint indent: 4, maxlen: 120, maxerr: 50, white: true, es5: true, undef: true, regexp: true, newcap: true */
 
2
/*jshint es5: true, undef: true, eqnull: true, noempty: true, eqeqeq: true, boss: true, loopfunc: true, laxbreak: true,
 
3
strict: true, curly: true */
 
4
/*global Timer, addFrameCallback, removeFrameCallback, isValidFrameCallback */
 
5
 
 
6
/* Jaguar Company Tracker
 
7
 *
 
8
 * Copyright © 2012 Richard Thomas Harrison (Tricky)
 
9
 *
 
10
 * This work is licensed under the Creative Commons
 
11
 * Attribution-Noncommercial-Share Alike 3.0 Unported License.
 
12
 *
 
13
 * To view a copy of this license, visit
 
14
 * http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter
 
15
 * to Creative Commons, 171 Second Street, Suite 300, San Francisco,
 
16
 * California, 94105, USA.
 
17
 *
 
18
 * Ship/Effect related functions for the patrol tracker.
 
19
 */
 
20
 
 
21
(function () {
 
22
    "use strict";
 
23
 
 
24
    /* Standard public variables for OXP scripts. */
 
25
    this.name = "jaguar_company_tracker.js";
 
26
    this.author = "Tricky";
 
27
    this.copyright = "© 2012 Richard Thomas Harrison (Tricky)";
 
28
    this.license = "CC BY-NC-SA 3.0";
 
29
    this.description = "Ship script for the Jaguar Company Tracker.";
 
30
    this.version = "1.1";
 
31
 
 
32
    /* Private variable. */
 
33
    var p_tracker = {};
 
34
 
 
35
    /* Ship event callbacks. */
 
36
 
 
37
    /* Initialise various variables on ship birth. Oolite v1.76.1 and older. */
 
38
    this.shipSpawned = function () {
 
39
        /* No longer needed after setting up. */
 
40
        delete this.shipSpawned;
 
41
 
 
42
        /* Common setup. */
 
43
        this.$setUp();
 
44
        /* Use a frame callback to keep the position constant. */
 
45
        this.$trackerFCBReference = addFrameCallback(this.$invisibleTrackerFCB.bind(this));
 
46
    };
 
47
 
 
48
    /* Initialise various variables on effect birth. Oolite v1.77 and newer. */
 
49
    this.effectSpawned = function () {
 
50
        /* No longer needed after setting up. */
 
51
        delete this.shipSpawned;
 
52
 
 
53
        /* Common setup. */
 
54
        this.$setUp();
 
55
        /* Updated by the visual effect frame callback. */
 
56
        p_tracker.material = 0;
 
57
        /* Use a frame callback to keep the position constant. */
 
58
        this.$trackerFCBReference = addFrameCallback(this.$trackerFCB.bind(this));
 
59
    };
 
60
 
 
61
    /* Patrol tracker was destroyed.
 
62
     *
 
63
     * INPUTS
 
64
     *   whom - entity that caused the death.
 
65
     *   why - cause as a string.
 
66
     *
 
67
     * Not triggered for Oolite v1.77 and newer visual effects.
 
68
     */
 
69
    this.shipDied = function (whom, why) {
 
70
        var mainScript = worldScripts["Jaguar Company"],
 
71
        destroyedBy = whom,
 
72
        tracker = this.ship;
 
73
 
 
74
        if (whom && whom.isValid) {
 
75
            destroyedBy = "ship#" + whom.entityPersonality + " (" + whom.displayName + ")";
 
76
 
 
77
            if (system.shipsWithPrimaryRole("jaguar_company_patrol").length > 0) {
 
78
                /* Patrol still around. Re-spawn. */
 
79
                mainScript.$tracker = mainScript.$jaguarCompanyBase.spawnOne("jaguar_company_tracker");
 
80
            }
 
81
        }
 
82
 
 
83
        if (p_tracker.logging && p_tracker.logExtra) {
 
84
            log(this.name, "shipDied::" +
 
85
                "ship#" + tracker.entityPersonality + " (" + tracker.displayName + ")" +
 
86
                " was destroyed by " + destroyedBy +
 
87
                ", reason: " + why);
 
88
        }
 
89
    };
 
90
 
 
91
    /* The patrol tracker has just become invalid or was removed. */
 
92
    this.shipRemoved = this.effectRemoved = this.entityDestroyed = this.$removeTrackerRefs = function () {
 
93
        /* Stop and remove the timer. */
 
94
        if (this.$trackerTimerReference) {
 
95
            if (this.$trackerTimerReference.isRunning) {
 
96
                this.$trackerTimerReference.stop();
 
97
            }
 
98
 
 
99
            delete this.$trackerTimerReference;
 
100
        }
 
101
 
 
102
        /* Stop and remove the frame callback. */
 
103
        if (this.$trackerFCBReference) {
 
104
            if (isValidFrameCallback(this.$trackerFCBReference)) {
 
105
                removeFrameCallback(this.$trackerFCBReference);
 
106
            }
 
107
 
 
108
            delete this.$trackerFCBReference;
 
109
        }
 
110
    };
 
111
 
 
112
    /* Other global functions. */
 
113
 
 
114
    /* Setup the private main variable + some public variables. */
 
115
    this.$setUp = function () {
 
116
        /* Initialise the p_tracker variable object.
 
117
         * Encapsulates all private global data.
 
118
         */
 
119
        p_tracker = {
 
120
            /* Local copies of the logging variables. */
 
121
            logging : worldScripts["Jaguar Company"].$logging,
 
122
            logExtra : worldScripts["Jaguar Company"].$logExtra,
 
123
            /* Updated by the timer. */
 
124
            closestPatrolShip : null
 
125
        };
 
126
 
 
127
        /* Track the patrol ships every 0.25 seconds. */
 
128
        this.$trackerTimerReference = new Timer(this, this.$trackerTimer, 0.25, 0.25);
 
129
    };
 
130
 
 
131
    /* Tracker timer. Updates the closest patrol ship position. */
 
132
    this.$trackerTimer = function () {
 
133
        var tracker = this.ship || this.visualEffect,
 
134
        patrolShips;
 
135
 
 
136
        if (!tracker || !tracker.isValid) {
 
137
            /* Tracker no longer valid. */
 
138
            this.$removeTrackerRefs();
 
139
 
 
140
            if (p_tracker.logging && p_tracker.logExtra) {
 
141
                log(this.name, "$trackerTimer::Tracker not valid");
 
142
            }
 
143
 
 
144
            return;
 
145
        }
 
146
 
 
147
        /* Search for the patrol ships. */
 
148
        patrolShips = system.shipsWithPrimaryRole("jaguar_company_patrol", player.ship);
 
149
 
 
150
        if (!patrolShips.length) {
 
151
            /* We are on our own. Deactivate the black box. */
 
152
            worldScripts["Jaguar Company"].$deactivateJaguarCompanyBlackbox();
 
153
 
 
154
            if (p_tracker.logging && p_tracker.logExtra) {
 
155
                log(this.name, "$trackerTimer::Tracker removed - no patrol ships");
 
156
            }
 
157
 
 
158
            return;
 
159
        }
 
160
 
 
161
        /* Update the closest patrol ship. */
 
162
        p_tracker.closestPatrolShip = patrolShips[0];
 
163
    };
 
164
 
 
165
    /* Tracker frame callback.
 
166
     *
 
167
     * Used by Oolite v1.76.1 or older.
 
168
     *
 
169
     * INPUT
 
170
     *   delta - amount of game clock time past since the last frame.
 
171
     */
 
172
    this.$invisibleTrackerFCB = function (delta) {
 
173
        var tracker = this.ship,
 
174
        cps = p_tracker.closestPatrolShip,
 
175
        distance;
 
176
 
 
177
        if (!tracker || !tracker.isValid) {
 
178
            /* Tracker can be invalid for 1 frame. */
 
179
            this.$removeTrackerRefs();
 
180
 
 
181
            return;
 
182
        }
 
183
 
 
184
        if (delta === 0.0 || !cps || !cps.isValid) {
 
185
            /* Do nothing if paused or the position of the closest patrol ship has not been setup. */
 
186
            return;
 
187
        }
 
188
 
 
189
        /* Distance above the closest patrol ship. */
 
190
        distance = 5 + cps.collisionRadius;
 
191
        /* Keep the tracker above the closest patrol ship. */
 
192
        tracker.position = cps.position.add(cps.orientation.vectorUp().multiply(distance));
 
193
    };
 
194
 
 
195
    /* Tracker frame callback.
 
196
     *
 
197
     * Used for visual effects.
 
198
     *
 
199
     * INPUT
 
200
     *   delta - amount of game clock time past since the last frame.
 
201
     */
 
202
    this.$trackerFCB = function (delta) {
 
203
        var tracker = this.visualEffect,
 
204
        cps = p_tracker.closestPatrolShip,
 
205
        PS,
 
206
        distance,
 
207
        vector,
 
208
        angle,
 
209
        cross;
 
210
 
 
211
        if (!tracker || !tracker.isValid) {
 
212
            /* Tracker can be invalid for 1 frame. */
 
213
            this.$removeTrackerRefs();
 
214
 
 
215
            return;
 
216
        }
 
217
 
 
218
        if (delta === 0.0 || !cps || !cps.isValid) {
 
219
            /* Do nothing if paused or the closest patrol ship has not been setup. */
 
220
            return;
 
221
        }
 
222
 
 
223
        /* Player ship object. */
 
224
        PS = player.ship;
 
225
        /* Vector pointing towards the target. */
 
226
        vector = cps.position.subtract(PS.position).direction();
 
227
 
 
228
        if (vector.dot(PS.heading) >= 0) {
 
229
            p_tracker.material = 0;
 
230
        } else {
 
231
            p_tracker.material = 1;
 
232
        }
 
233
 
 
234
        if (!p_tracker.material) {
 
235
            /* Change the tracker colour to be green. */
 
236
            tracker.setMaterials({
 
237
                jaguar_company_tracker : {
 
238
                    diffuse_color : ["0", "0.667", "0", "1"],
 
239
                    diffuse_map : "jaguar_company_tracker_diffuse.png",
 
240
                    emission_color : ["0", "0.05", "0", "1"],
 
241
                    shininess : "5",
 
242
                    specular_color : ["0", "0.2", "0", "1"]
 
243
                }
 
244
            });
 
245
        } else if (p_tracker.material) {
 
246
            /* Change the tracker colour to be red. */
 
247
            tracker.setMaterials({
 
248
                jaguar_company_tracker : {
 
249
                    diffuse_color : ["0.667", "0", "0", "1"],
 
250
                    diffuse_map : "jaguar_company_tracker_diffuse.png",
 
251
                    emission_color : ["0.05", "0", "0", "1"],
 
252
                    shininess : "5",
 
253
                    specular_color : ["0.2", "0", "0", "1"]
 
254
                }
 
255
            });
 
256
        }
 
257
 
 
258
        /* Distance in front of the player. */
 
259
        distance = 100 + PS.collisionRadius;
 
260
        /* Keep the tracker in front of the player. */
 
261
        tracker.position = PS.position.add(PS.heading.multiply(distance));
 
262
        /* Angle to the target from current heading. */
 
263
        angle = PS.heading.angleTo(vector);
 
264
        /* Cross vector for rotate. */
 
265
        cross = PS.heading.cross(vector).direction();
 
266
        /* Rotate the tracker by the angle. */
 
267
        tracker.orientation = PS.orientation.rotate(cross, -angle);
 
268
    };
 
269
}).call(this);