Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var slice = [].slice;
- v2.cross = function(a, b) {
- return a[0] * b[1] - b[0] * a[1];
- };
- types.Unit.prototype.fromSpec = function(spec) {
- var data, i, j, l, len, len1, len2, len3, len4, len5, n, o, p, part, partNum, q, ref, ref1, ref2, ref3, ref4, ref5, results, thrust, w, x;
- this.cost = 0;
- this.hp = 5;
- this.jumpDistance = 0;
- this.jumpCount = 0;
- this.speed = 0;
- this.turnSpeed = 1;
- this.mass = 0;
- this.energy = 0;
- this.baseGenEnergy = 2.5;
- this.genEnergy = this.baseGenEnergy;
- this.storeEnergy = 0;
- this.genShield = 0;
- this.shield = 0;
- this.cloak = 0;
- this.radius = 20;
- this.weaponArc = 0;
- this.minArc = 0;
- thrust = 0;
- data = fromShort(spec);
- this.name = data.name || "";
- this.aiRules = data.aiRules || [];
- ref = data.parts;
- for (partNum = j = 0, len = ref.length; j < len; partNum = ++j) {
- p = ref[partNum];
- if (!window.parts[p.type]) {
- continue;
- }
- part = new window.parts[p.type];
- part.unit = this;
- part.pos = v2.create(p.pos);
- part.dir = p.dir || 0;
- part.partNum = partNum;
- if (part.weapon) {
- this.weapons.push(part);
- }
- if (p.ghostCopy) {
- part.ghostCopy = true;
- }
- this.parts.push(part);
- this.cost += part.cost || 0;
- this.hp += part.hp || 0;
- thrust += part.thrust || 0;
- this.mass += part.mass;
- this.turnSpeed += part.turnSpeed || 0;
- this.genEnergy += part.genEnergy || 0;
- this.storeEnergy += part.storeEnergy || 0;
- this.genShield += part.genShield || 0;
- this.shield += part.shield || 0;
- this.jumpCount += part.jumpCount || 0;
- this.limitBonus += part.limitBonus || 0;
- if (part.arc && this.weaponArc < part.arc) {
- this.weaponArc = part.arc;
- }
- if (part.arc && (this.minArc === 0 || this.minArc > part.arc)) {
- this.minArc = part.arc;
- }
- }
- this.maxHP = this.hp;
- this.energy = this.storeEnergy;
- this.turnSpeed = this.turnSpeed / this.mass;
- this.maxSpeed = thrust / this.mass * 9;
- this.maxShield = this.shield;
- this.damageRatio = 1;
- this.jumpDistance = this.jump = Math.min(this.maxJump / 500, 41 * this.jumpCount / this.mass) * 500;
- this.computeCenter();
- ref1 = this.parts;
- for (l = 0, len1 = ref1.length; l < len1; l++) {
- part = ref1[l];
- if (typeof part.init === "function") {
- part.init();
- }
- }
- this.computeRadius();
- this.computeBoundary();
- this.weaponRange = 0;
- this.weaponDPS = 0;
- this.weaponDamage = 0;
- ref2 = this.weapons;
- for (n = 0, len2 = ref2.length; n < len2; n++) {
- w = ref2[n];
- w.applyBuffs();
- w.reloadTime = Math.ceil(w.reloadTime);
- if (w.reloadTime < 1) {
- w.reloadTime = 1;
- }
- if (w.range > this.weaponRange) {
- this.weaponRange = w.range;
- }
- w.dps = w.damage / w.reloadTime;
- this.weaponDamage += w.damage;
- this.weaponDPS += w.dps;
- }
- this.weapons.sort(function(a, b) {
- return b.dps - a.dps;
- });
- this.mainWeapon = this.weapons[0];
- ref3 = this.weapons;
- for (i = o = 0, len3 = ref3.length; o < len3; i = ++o) {
- w = ref3[i];
- w.turretNum = i;
- }
- this.moveEnergy = 0;
- ref4 = this.parts;
- for (q = 0, len4 = ref4.length; q < len4; q++) {
- part = ref4[q];
- if (part.thrust > 0) {
- this.moveEnergy += part.useEnergy;
- }
- }
- this.fireEnergy = 0;
- ref5 = this.parts;
- results = [];
- for (x = 0, len5 = ref5.length; x < len5; x++) {
- part = ref5[x];
- if (part.fireEnergy > 0) {
- results.push(this.fireEnergy += part.fireEnergy);
- } else {
- results.push(void 0);
- }
- }
- return results;
- };
- types.Unit.prototype.computeBoundary = function() {
- var diffCross, extend, findPartPoints, left, max, min, points, right, split, u, v;
- findPartPoints = (function(_this) {
- return function() {
- var fn1, j, l, len, len1, p, part, partPoints, ref, rst;
- partPoints = function(part) {
- var size;
- if (part.dir % 2 === 0) {
- size = [part.size[0], part.size[1]];
- } else {
- size = [part.size[1], part.size[0]];
- }
- v2.scale(size, 10);
- return [v2.add(v2.create([-size[0], size[1]]), part.pos), v2.add(v2.create([size[0], size[1]]), part.pos), v2.add(v2.create([size[0], -size[1]]), part.pos), v2.add(v2.create([-size[0], -size[1]]), part.pos)];
- };
- rst = [];
- ref = _this.parts;
- for (j = 0, len = ref.length; j < len; j++) {
- part = ref[j];
- if (!part.decal) {
- rst.push.apply(rst, partPoints(part));
- }
- }
- fn1 = function(p) {
- return v2.sub(p, _this.center);
- };
- for (l = 0, len1 = rst.length; l < len1; l++) {
- p = rst[l];
- fn1(p);
- }
- return rst;
- };
- })(this);
- diffCross = function(u, v, p) {
- return v2.cross(v2.sub(p, u, [0, 0]), v2.sub(v, u, [0, 0]));
- };
- min = function(points, fn) {
- return points.sort(function(a, b) {
- return fn(a) - fn(b);
- })[0];
- };
- max = function(points, fn) {
- return points.sort(function(a, b) {
- return fn(b) - fn(a);
- })[0];
- };
- split = function(u, v, points) {
- var p;
- return (function() {
- var j, len, results;
- results = [];
- for (j = 0, len = points.length; j < len; j++) {
- p = points[j];
- if (diffCross(u, v, p) < 0) {
- results.push(p);
- }
- }
- return results;
- })();
- };
- extend = function(u, v, points) {
- var p1, p2, w;
- if (!(points != null ? points.length : void 0)) {
- return [];
- }
- w = min(points, function(p) {
- return diffCross(u, v, p);
- });
- p1 = split(w, v, points);
- p2 = split(u, w, points);
- return slice.call(extend(w, v, p1)).concat([w], slice.call(extend(u, w, p2)));
- };
- points = findPartPoints();
- u = min(points, function(p) {
- return p[0];
- });
- v = max(points, function(p) {
- return p[0];
- });
- left = split(u, v, points);
- right = split(v, u, points);
- return this.boundPoints = [v].concat(slice.call(extend(u, v, left)), [u], slice.call(extend(v, u, right)), [v]);
- };
- types.Unit.prototype.draw = function() {
- var a, color, drawLine, i, j, l, len, len1, n, part, partNum, r, ref, ref1, ref2, s, t, toWorld, value;
- if (this.dead) {
- return;
- }
- ref = this.parts;
- for (j = 0, len = ref.length; j < len; j++) {
- part = ref[j];
- if (typeof part.preDraw === "function") {
- part.preDraw();
- }
- part.computeWorldPos();
- }
- if (this.maxShield > 0) {
- s = this.warpIn * 2 - 1;
- if (s > 0) {
- color = [this.color[0], this.color[1], this.color[2], 255 * this.shield / this.maxShield * s];
- r = (this.radius + 40) / 255;
- baseAtlas.drawSprite("img/point02.png", this.pos, [r, r], 0, color);
- }
- }
- if (this.warpIn < 1) {
- this.warpIn += 1 / 60;
- }
- ref1 = this.parts;
- for (partNum = l = 0, len1 = ref1.length; l < len1; partNum = ++l) {
- part = ref1[partNum];
- value = part.pos[1] / 700 - .5 + this.warpIn;
- if (value > 0) {
- part.draw();
- } else if (value > -0.2) {
- a = -Math.sin(value / Math.PI * 50);
- baseAtlas.drawSprite("img/unitBar/fire02.png", part.worldPos, [1, 1], 0, [255, 255, 255, 255 * a]);
- continue;
- }
- }
- if (this.energy / this.storeEnergy < .05) {
- t = Math.abs(Math.sin(Date.now() / 300));
- baseAtlas.drawSprite("img/unitBar/energy1.png", [this.pos[0], this.pos[1] + 100 + this.radius], [1, 1], 0, [255, 100, 100, t * 200]);
- }
- drawLine = function(from, to) {
- var d, offset, rot;
- offset = [0, 0];
- v2.sub(to, from, offset);
- rot = v2.angle(offset);
- d = v2.mag(offset) / 380;
- v2.scale(offset, .5);
- v2.add(offset, from);
- return baseAtlas.drawSprite("img/laser01.png", offset, [.2, d], rot, [0, 255, 0, 255]);
- };
- toWorld = (function(_this) {
- return function(from) {
- var p;
- p = v2.create(from);
- v2.rotate(p, _this.rot + Math.PI);
- return v2.add(p, _this.pos);
- };
- })(this);
- if(this.boundPoints) {
- for (i = n = 0, ref2 = this.boundPoints.length - 1; 0 <= ref2 ? n < ref2 : n > ref2; i = 0 <= ref2 ? ++n : --n) {
- drawLine(toWorld(this.boundPoints[i]), toWorld(this.boundPoints[i + 1]));
- }
- }
- if (control.debug && (this._pos2 != null) && this._pos) {
- a = 16;
- _offset[0] = this._pos2[0] + (this._pos[0] - this._pos2[0]) * a;
- _offset[1] = this._pos2[1] + (this._pos[1] - this._pos2[1]) * a;
- baseAtlas.drawSprite("img/pip1.png", this._pos, [1, 1], 0, [0, 255, 0, 255]);
- baseAtlas.drawSprite("img/pip1.png", this.pos, [1, 1], 0);
- return baseAtlas.drawSprite("img/pip1.png", this._pos2, [1, 1], 0, [255, 0, 0, 255]);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement