~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_base_buoy.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 worldScripts, log, Timer, addFrameCallback, removeFrameCallback, isValidFrameCallback, Vector3D */
 
5
 
 
6
/* Jaguar Company Base Buoy
 
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 related functions for the base buoy.
 
19
 */
 
20
 
 
21
(function () {
 
22
    "use strict";
 
23
 
 
24
    /* Standard public variables for OXP scripts. */
 
25
    this.name = "jaguar_company_base_buoy.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 Base Buoy.";
 
30
    this.version = "1.1";
 
31
 
 
32
    /* Private variable. */
 
33
    var p_buoy = {};
 
34
 
 
35
    /* Ship event callbacks. */
 
36
 
 
37
    /* Initialise various variables on ship birth. */
 
38
    this.shipSpawned = function () {
 
39
        /* No longer needed after setting up. */
 
40
        delete this.shipSpawned;
 
41
 
 
42
        /* Initialise the p_buoy variable object.
 
43
         * Encapsulates all private global data.
 
44
         */
 
45
        p_buoy = {
 
46
            /* Cache the world scripts. */
 
47
            mainScript : worldScripts["Jaguar Company"],
 
48
            attackersScript : worldScripts["Jaguar Company Attackers"],
 
49
            /* Local copies of the logging variables. */
 
50
            logging : worldScripts["Jaguar Company"].$logging,
 
51
            logExtra : worldScripts["Jaguar Company"].$logExtra
 
52
        };
 
53
 
 
54
        /* Register this buoy as a friendly. */
 
55
        p_base.attackersScript.$addFriendly(this.ship);
 
56
        /* Wait 5 seconds then find the witchpoint. */
 
57
        p_buoy.nextTarget = "WITCHPOINT";
 
58
        this.$buoyTimerReference = new Timer(this, this.$buoyTimer, 5);
 
59
    };
 
60
 
 
61
    /* Base buoy was removed by script. */
 
62
    this.shipRemoved = function (suppressDeathEvent) {
 
63
        if (suppressDeathEvent) {
 
64
            return;
 
65
        }
 
66
 
 
67
        /* Reset the script check. */
 
68
        worldScripts["Jaguar Company"].$buoyOK = false;
 
69
        /* Force a launch of a new buoy. */
 
70
        worldScripts["Jaguar Company"].$buoyLaunched = false;
 
71
    };
 
72
 
 
73
    /* The base buoy has just become invalid. */
 
74
    this.entityDestroyed = function () {
 
75
        /* Reset the script check. */
 
76
        worldScripts["Jaguar Company"].$buoyOK = false;
 
77
        /* Force a launch of a new buoy. */
 
78
        worldScripts["Jaguar Company"].$buoyLaunched = false;
 
79
        /* Stop and remove the frame callback and timer. */
 
80
        this.$removeBuoyTimer();
 
81
        this.$removeBuoyFCB();
 
82
    };
 
83
 
 
84
    /* Stop and remove the timer. */
 
85
    this.$removeBuoyTimer = function () {
 
86
        if (this.$buoyTimerReference) {
 
87
            if (this.$buoyTimerReference.isRunning) {
 
88
                this.$buoyTimerReference.stop();
 
89
            }
 
90
 
 
91
            delete this.$buoyTimerReference;
 
92
        }
 
93
    };
 
94
 
 
95
    /* Stop and remove the frame callback. */
 
96
    this.$removeBuoyFCB = function () {
 
97
        /* Turn the flashers on. */
 
98
        this.ship.lightsActive = true;
 
99
 
 
100
        if (this.$buoyFCBReference) {
 
101
            if (isValidFrameCallback(this.$buoyFCBReference)) {
 
102
                removeFrameCallback(this.$buoyFCBReference);
 
103
            }
 
104
 
 
105
            delete this.$buoyFCBReference;
 
106
        }
 
107
    };
 
108
 
 
109
    /* Point the dish at Jaguar Company Patrol. */
 
110
    this.$findJaguarCompanyPatrol = function () {
 
111
        var patrolShips,
 
112
        patrolShipsLength,
 
113
        patrolShipsCounter,
 
114
        midpointPosition;
 
115
 
 
116
        /* Search for the patrol ships. */
 
117
        patrolShips = system.shipsWithPrimaryRole("jaguar_company_patrol");
 
118
 
 
119
        if (!patrolShips.length) {
 
120
            /* We are on our own. Point the dish at the witchpoint. */
 
121
            return p_buoy.mainScript.$witchpointBuoy.position;
 
122
        }
 
123
 
 
124
        /* Cache the length. */
 
125
        patrolShipsLength = patrolShips.length;
 
126
 
 
127
        /* Work out the midpoint position of all the patrol ships. */
 
128
        midpointPosition = new Vector3D(0, 0, 0);
 
129
 
 
130
        for (patrolShipsCounter = 0; patrolShipsCounter < patrolShipsLength; patrolShipsCounter += 1) {
 
131
            midpointPosition = midpointPosition.add(patrolShips[patrolShipsCounter].position);
 
132
        }
 
133
 
 
134
        midpointPosition.x /= patrolShipsLength;
 
135
        midpointPosition.y /= patrolShipsLength;
 
136
        midpointPosition.z /= patrolShipsLength;
 
137
 
 
138
        return midpointPosition;
 
139
    };
 
140
 
 
141
    this.$buoyTimer = function () {
 
142
        var buoy = this.ship,
 
143
        position,
 
144
        vector;
 
145
 
 
146
        if (p_buoy.nextTarget === "JAGUAR_COMPANY_PATROL") {
 
147
            /* Find the position of then patrol ships. */
 
148
            position = this.$findJaguarCompanyPatrol();
 
149
        } else if (p_buoy.nextTarget === "PLANET") {
 
150
            this.$removeBuoyTimer();
 
151
            /* Wait 30 seconds then track Jaguar Company Patrol every 1 minute. */
 
152
            p_buoy.nextTarget = "JAGUAR_COMPANY_PATROL";
 
153
            this.$buoyTimerReference = new Timer(this, this.$buoyTimer, 30, 60);
 
154
            /* Find the position of the main planet. */
 
155
            position = system.mainPlanet.position;
 
156
        } else {
 
157
            this.$removeBuoyTimer();
 
158
 
 
159
            if (system.isInterstellarSpace) {
 
160
                /* Wait 30 seconds then track Jaguar Company Patrol every 1 minute. */
 
161
                p_buoy.nextTarget = "JAGUAR_COMPANY_PATROL";
 
162
                this.$buoyTimerReference = new Timer(this, this.$buoyTimer, 30, 60);
 
163
            } else {
 
164
                /* Wait 30 seconds then find the planet. */
 
165
                p_buoy.nextTarget = "PLANET";
 
166
                this.$buoyTimerReference = new Timer(this, this.$buoyTimer, 30);
 
167
            }
 
168
 
 
169
            /* Find the position of the witchpoint. */
 
170
            position = p_buoy.mainScript.$witchpointBuoy.position;
 
171
        }
 
172
 
 
173
        /* Vector pointing towards the target. */
 
174
        vector = position.subtract(buoy.position).direction();
 
175
        /* Angle to the target from current heading. */
 
176
        p_buoy.finalAngle = buoy.heading.angleTo(vector);
 
177
 
 
178
        if (p_buoy.finalAngle < 0.087266462599716478846184538424431) {
 
179
            /* Already pointing in the rough direction of the target.
 
180
             * Looking for a difference of greater than 5 degrees.
 
181
             */
 
182
            return;
 
183
        }
 
184
 
 
185
        /* Cross vector for rotate. */
 
186
        p_buoy.cross = buoy.heading.cross(vector).direction();
 
187
        /* Starting angle. */
 
188
        p_buoy.angle = 0;
 
189
        /* Should take about 5 seconds (at 60 FPS). */
 
190
        p_buoy.deltaAngle = p_buoy.finalAngle / 300;
 
191
 
 
192
        if (p_buoy.logging && p_buoy.logExtra) {
 
193
            log(this.name, "$buoyTimer::Buoy tracking target...");
 
194
        }
 
195
 
 
196
        /* Use a frame callback to do this smoothly. */
 
197
        this.$buoyFCBReference = addFrameCallback(this.$buoyFCB.bind(this));
 
198
    };
 
199
 
 
200
    /* Frame callback to slowly rotate the buoy towards Jaguar Company Patrol.
 
201
     *
 
202
     * INPUT
 
203
     *   delta - amount of game clock time past since the last frame.
 
204
     */
 
205
    this.$buoyFCB = function (delta) {
 
206
        var buoy = this.ship;
 
207
 
 
208
        if (!buoy || !buoy.isValid) {
 
209
            /* Buoy can be invalid for 1 frame. */
 
210
            this.$removeBuoyTimer();
 
211
            this.$removeBuoyFCB();
 
212
 
 
213
            return;
 
214
        }
 
215
 
 
216
        if (delta === 0.0) {
 
217
            /* Do nothing if paused. */
 
218
            return;
 
219
        }
 
220
 
 
221
        if (p_buoy.angle >= p_buoy.finalAngle) {
 
222
            /* Reached the desired orientation. */
 
223
            this.$removeBuoyFCB();
 
224
 
 
225
            return;
 
226
        }
 
227
 
 
228
        /* Rotate in delta angle steps. */
 
229
        buoy.orientation = buoy.orientation.rotate(p_buoy.cross, -p_buoy.deltaAngle);
 
230
        /* Update the current angle. */
 
231
        p_buoy.angle += p_buoy.deltaAngle;
 
232
    };
 
233
}).call(this);